added date support
[asciidoctor_to_rss.git] / src / main / java / net / wpitchoune / asciidoctor / Main.java
index 5e687c3..43f0a88 100644 (file)
@@ -19,17 +19,17 @@ package net.wpitchoune.asciidoctor;
  */
 
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.FileReader;
 import java.io.IOException;
-import java.io.InputStream;
 import java.io.StringWriter;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
 import java.util.ArrayList;
+import java.util.Date;
 import java.util.HashMap;
-import java.util.Properties;
 import java.util.logging.Logger;
 
 import org.asciidoctor.Asciidoctor;
@@ -88,8 +88,6 @@ public class Main {
                        LOG.info("There is no declared HTML header file.");
                        return ;
                }
-               
-               
 
                sb.append("<!DOCTYPE html>\n");
                sb.append("<html>\n");                          
@@ -99,6 +97,11 @@ public class Main {
                sb.append("</head>\n");
         }
         
+        private static void appendHTMLFooter(StringBuffer sb) {
+               sb.append("</body>\n");
+               sb.append("</html>");           
+        }
+        
         private static void appendHTMLContentHeader(StringBuffer sb, String title) {
                sb.append("<div id='header'>\n");
                sb.append("<h1>");
@@ -107,11 +110,36 @@ public class Main {
                sb.append("</div>");
         }               
         
-        private static void generateHTMLFileItem(String itemTitle, String itemContent) {
+        private static void generateHTMLFileItem(File file,
+                                                String title,
+                                                String content,
+                                                String date,
+                                                Configuration cfg) throws IOException {
+               StringBuffer buf;
+               
+               buf = new StringBuffer();
+               
+               appendHTMLHead(buf, cfg);
+               
+               buf.append("<body>\n");
+               
+               appendHTMLContentHeader(buf, title);
                
+               buf.append("<div id='content'>\n");
+               if (date != null) {
+                       buf.append("<div class='date'>");
+                       buf.append(date);
+                       buf.append("</div>");
+               }
+               buf.append(content);
+               buf.append("</div>");
+               
+               appendHTMLFooter(buf);
+               
+               Files.write(file.toPath(), buf.toString().getBytes());
         }
         
-       public static void main(String[] args) throws FileNotFoundException, IOException, FeedException {
+       public static void main(String[] args) throws FileNotFoundException, IOException, FeedException, ParseException {
                File inDir, html, outDir;
                File[] adocs;
                StringWriter desc;
@@ -122,7 +150,11 @@ public class Main {
                DocumentHeader h;
                SyndContentImpl c;
                StringBuffer news;
-               String itemTitle, itemContent;
+               String itemTitle, itemContent, itemURI, strDate;
+               Date itemDate;
+               SimpleDateFormat sdf;
+               
+               sdf = new SimpleDateFormat("yyyy-dd-MM");
                
                inDir = new File(args[0]);
                outDir = new File(args[1]);
@@ -156,7 +188,14 @@ public class Main {
                        html = toHTMLFile(outDir, adoc);
                                
                        h = asciidoctor.readDocumentHeader(adoc);
-                                                           
+
+                       if (h.getAttributes().get("date") == null)
+                               strDate = h.getAttributes().get("docdate").toString();
+                       else
+                               strDate = h.getAttributes().get("date").toString();
+                                               
+                       itemDate = sdf.parse(strDate);
+                       
                        asciidoctor.convert(new FileReader(adoc), desc, new HashMap<String,Object>());
                        
                        itemTitle = h.getDocumentTitle().getMain(); 
@@ -164,7 +203,10 @@ public class Main {
                        
                        e = new SyndEntryImpl();
                        e.setTitle(itemTitle);
-                       e.setUri(cfg.getFeedBaseURL() + "/" + html.getName());
+                       itemURI = cfg.getFeedBaseURL() + "/" + html.getName(); 
+                       e.setUri(itemURI);
+                       e.setLink(itemURI);
+                       e.setPublishedDate(itemDate);
                        
                        c = toSyndContentImpl(itemContent);
                        
@@ -172,20 +214,24 @@ public class Main {
                        
                        entries.add(e);
 
-                       news.append("\n<div>\n");
+                       news.append("\n<div>");
                        news.append("<h2>");
-                       news.append(h.getDocumentTitle().getMain());
-                       news.append("</h2>\n");
+                       news.append(itemTitle);
+                       news.append("</h2>");
+                       if (news != null) {
+                               news.append("<div class='date'>");
+                               news.append(itemDate);
+                               news.append("</div>");
+                       }
                        news.append(desc.toString());
-                       news.append("\n</div>\n");     
+                       news.append("</div>\n");     
                        
-                       generateHTMLFileItem(itemTitle, itemContent);
+                       generateHTMLFileItem(html, itemTitle, itemContent, itemDate.toString(), cfg);
                }
                
                news.append("</div>\n");
                
-               news.append("</body>\n");
-               news.append("</html>\n");
+               appendHTMLFooter(news);
                
                feed.setEntries(entries);
                
@@ -193,7 +239,6 @@ public class Main {
                SyndFeedOutput output = new SyndFeedOutput();
                output.output(feed, new File(outDir, "feed.xml"));
        
-               System.out.println(news.toString());
                Files.write(new File(outDir, "news.html").toPath(), news.toString().getBytes());
        }
 }