improved structure
[asciidoctor_to_rss.git] / src / main / java / net / wpitchoune / asciidoctor / Main.java
index df5636f..d5b19a1 100644 (file)
@@ -19,18 +19,16 @@ 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;
 import org.asciidoctor.Asciidoctor.Factory;
@@ -51,10 +49,10 @@ import com.rometools.rome.io.SyndFeedOutput;
  * @author jeanfi@gmail.com
  */
 public class Main {
-       private static final Logger LOG = Logger.getLogger(Main.class.getSimpleName());
-
        private static final Asciidoctor asciidoctor = Factory.create();
        
+        private static final SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat("yyyy-dd-MM");        
+       
         private static File toHTMLFile(File dir, File adoc) {
                 int idx;
                 String name;
@@ -79,37 +77,7 @@ public class Main {
                return ret;
         }
         
-        private static void appendHTMLHead(StringBuffer sb, Configuration cfg)
-                       throws IOException {
-               File f;
-               
-               f = cfg.getHTMLHeaderFile();
-               if (f == null) {
-                       LOG.info("There is no declared HTML header file.");
-                       return ;
-               }
-
-               sb.append("<!DOCTYPE html>\n");
-               sb.append("<html>\n");                          
-               sb.append("<head>\n");
-               sb.append(new String(Files.readAllBytes(f.toPath()),
-                                    StandardCharsets.UTF_8));
-               sb.append("</head>\n");
-        }
-        
-        private static void appendHTMLContentHeader(StringBuffer sb, String title) {
-               sb.append("<div id='header'>\n");
-               sb.append("<h1>");
-               sb.append(title);
-               sb.append("</h1>\n");
-               sb.append("</div>");
-        }               
-        
-        private static void generateHTMLFileItem(String itemTitle, String itemContent) {
-               
-        }
-        
-       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;
@@ -118,9 +86,8 @@ public class Main {
                ArrayList<SyndEntry> entries;
                SyndEntryImpl e;
                DocumentHeader h;
-               SyndContentImpl c;
-               StringBuffer news;
-               String itemTitle, itemContent;
+               String itemTitle, itemContent, itemURI, strDate;
+               Date itemDate;
                
                inDir = new File(args[0]);
                outDir = new File(args[1]);
@@ -136,16 +103,6 @@ public class Main {
                
                entries = new ArrayList<SyndEntry>();
                
-               news = new StringBuffer();
-               
-               appendHTMLHead(news, cfg);
-               
-               news.append("<body>\n");
-               
-               appendHTMLContentHeader(news, cfg.getFeedTitle());
-               
-               news.append("<div id='content'>\n");
-               
                for (File adoc: adocs) {
                        if (!adoc.getName().endsWith(".adoc"))
                                continue;                       
@@ -154,7 +111,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 = DATE_FORMATTER.parse(strDate);
+                       
                        asciidoctor.convert(new FileReader(adoc), desc, new HashMap<String,Object>());
                        
                        itemTitle = h.getDocumentTitle().getMain(); 
@@ -162,36 +126,25 @@ public class Main {
                        
                        e = new SyndEntryImpl();
                        e.setTitle(itemTitle);
-                       e.setUri(cfg.getFeedBaseURL() + "/" + html.getName());
-                       
-                       c = toSyndContentImpl(itemContent);
+                       itemURI = cfg.getFeedBaseURL() + "/" + html.getName(); 
+                       e.setUri(itemURI);
+                       e.setLink(itemURI);
+                       e.setPublishedDate(itemDate);
                        
-                       e.setDescription(c);
+                       e.setDescription(toSyndContentImpl(itemContent));
                        
-                       entries.add(e);
-
-                       news.append("\n<div>\n");
-                       news.append("<h2>");
-                       news.append(h.getDocumentTitle().getMain());
-                       news.append("</h2>\n");
-                       news.append(desc.toString());
-                       news.append("\n</div>\n");     
+                       entries.add(e);                 
                        
-                       generateHTMLFileItem(itemTitle, itemContent);
+                       Files.write(html.toPath(), new HTML(cfg).toHTML(e).getBytes());
                }
                
-               news.append("</div>\n");
-               
-               news.append("</body>\n");
-               news.append("</html>\n");
-               
                feed.setEntries(entries);
                
                feed.setFeedType("rss_2.0");                    
                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());
+               Files.write(new File(outDir, "news.html").toPath(),
+                           new HTML(cfg).toHTML(feed.getEntries()).getBytes());
        }
 }