refactoring
authorJean-Philippe Orsini <jeanfi@gmail.com>
Sat, 2 Jul 2016 17:08:10 +0000 (19:08 +0200)
committerJean-Philippe Orsini <jeanfi@gmail.com>
Sat, 2 Jul 2016 17:08:10 +0000 (19:08 +0200)
asciidoctor_to_rss.sh
src/main/java/Main.java [deleted file]
src/main/java/net/wpitchoune/asciidoctor/Configuration.java [new file with mode: 0644]
src/main/java/net/wpitchoune/asciidoctor/Main.java [new file with mode: 0644]

index e624d1e..7696d3c 100755 (executable)
@@ -4,4 +4,4 @@ BDIR=`dirname $0`
 
 CP=$BDIR/target/classes:$BDIR/lib/asciidoctorj-1.5.4.jar:$BDIR/lib/jruby-complete-1.7.21.jar:$BDIR/target/net.wpitchoune.asciidoctor-1.0.jar:$BDIR/lib/rome-1.6.0.jar:$BDIR/lib/slf4j-api-1.7.6.jar:$BDIR/lib/slf4j-nop-1.7.6.jar:$BDIR/lib/jdom2-2.0.6.jar:$BDIR/lib/rome-utils-1.6.0.jar
 
-java -classpath $CP Main $*
+java -classpath $CP net.wpitchoune.asciidoctor.Main $*
diff --git a/src/main/java/Main.java b/src/main/java/Main.java
deleted file mode 100644 (file)
index 64b4d88..0000000
+++ /dev/null
@@ -1,171 +0,0 @@
-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.util.ArrayList;
-import java.util.HashMap;
-import java.util.Properties;
-import java.util.logging.Logger;
-
-import org.asciidoctor.Asciidoctor;
-import org.asciidoctor.Asciidoctor.Factory;
-import org.asciidoctor.ast.DocumentHeader;
-
-import com.rometools.rome.feed.synd.SyndContentImpl;
-import com.rometools.rome.feed.synd.SyndEntry;
-import com.rometools.rome.feed.synd.SyndEntryImpl;
-import com.rometools.rome.feed.synd.SyndFeed;
-import com.rometools.rome.feed.synd.SyndFeedImpl;
-import com.rometools.rome.io.FeedException;
-import com.rometools.rome.io.SyndFeedOutput;
-
-public class Main {
-       private static final Logger LOG = Logger.getLogger(Main.class.getSimpleName());
-       private static final String KEY_HTML_HEADER_FILE = "html.header.file";
-       private static final String KEY_FEED_TITLE = "feed.title";
-       
-        private static File toHTMLFile(File dir, File adoc) {
-                int idx;
-                String name;
-                
-                name = adoc.getName(); 
-               
-                idx = name.lastIndexOf('.');
-                
-                if (idx >= 0)
-                        name = name.substring(0, idx);
-                
-                return new File(dir, name + ".html");
-        }
-        
-        private static SyndContentImpl toSyndContentImpl(String description) {
-               SyndContentImpl ret;
-               
-               ret = new SyndContentImpl();
-               ret.setType("text/html");
-               ret.setValue(description);
-            
-               return ret;
-        }
-        
-        private static String getFeedTitle(Properties props) {
-               return props.getProperty(KEY_FEED_TITLE);
-        }
-        
-        private static void appendHTMLHeader(StringBuffer sb, Properties props)
-                       throws IOException {
-               String fileName;
-               File f;
-               
-               fileName = props.getProperty(KEY_HTML_HEADER_FILE);
-               
-               if (fileName == null) {
-                       LOG.info(KEY_HTML_HEADER_FILE + " is not set");
-                       return ;
-               }
-               
-               f = new File(fileName);
-               
-               sb.append("<head>\n");
-               sb.append(new String(Files.readAllBytes(f.toPath()),
-                                    StandardCharsets.UTF_8));
-               sb.append("</head>\n");
-        }
-        
-       public static void main(String[] args) throws FileNotFoundException, IOException, FeedException {
-               File inDir, html, outDir;
-               File[] adocs;
-               StringWriter desc;
-               Asciidoctor asciidoctor;
-               SyndFeed feed;
-               Properties props;
-               ArrayList<SyndEntry> entries;
-               SyndEntryImpl e;
-               InputStream in;
-               DocumentHeader h;
-               SyndContentImpl c;
-               StringBuffer news;
-               
-               inDir = new File(args[0]);
-               outDir = new File(args[1]);
-               
-               props = new Properties();
-               in = new FileInputStream(args[2]);
-               props.load(in);
-               in.close();
-               
-               adocs = inDir.listFiles();
-
-               asciidoctor = Factory.create();
-
-               feed = new SyndFeedImpl();
-               feed.setTitle(getFeedTitle(props));
-               feed.setDescription(props.getProperty("feed.description"));
-               feed.setLink(props.getProperty("feed.link"));
-               
-               entries = new ArrayList<SyndEntry>();
-               
-               news = new StringBuffer();
-               news.append("<!DOCTYPE html>\n");
-               news.append("<html>\n");                                
-               
-               appendHTMLHeader(news, props);
-               
-               news.append("<body>\n");
-               
-               news.append("<div id='header'>\n");
-               news.append("<h1>");
-               news.append(getFeedTitle(props));
-               news.append("</h1>\n");
-               news.append("</div>");
-               
-               news.append("<div id='content'>\n");
-               
-               for (File adoc: adocs) {
-                       if (!adoc.getName().endsWith(".adoc"))
-                               continue;                       
-                       desc = new StringWriter();
-                                               
-                       html = toHTMLFile(outDir, adoc);
-                               
-                       h = asciidoctor.readDocumentHeader(adoc);
-                                                           
-                       asciidoctor.convert(new FileReader(adoc), desc, new HashMap<String,Object>());
-                       
-                       e = new SyndEntryImpl();
-                       e.setTitle(h.getDocumentTitle().getMain());
-                       e.setUri(props.getProperty("feed.baseurl") + "/" + html.getName());
-                       
-                       c = toSyndContentImpl(desc.toString());
-                       
-                       e.setDescription(c);
-                       
-                       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");          
-               }
-               
-               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());
-       }
-}
diff --git a/src/main/java/net/wpitchoune/asciidoctor/Configuration.java b/src/main/java/net/wpitchoune/asciidoctor/Configuration.java
new file mode 100644 (file)
index 0000000..64668a5
--- /dev/null
@@ -0,0 +1,63 @@
+package net.wpitchoune.asciidoctor;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Properties;
+
+public final class Configuration {
+       private static final String KEY_HTML_HEADER_FILE = "html.header.file";
+       private static final String KEY_FEED_TITLE = "feed.title";
+       private static final String KEY_FEED_LINK = "feed.link";
+       private static final String KEY_FEED_DESCRIPTION = "feed.description";
+       private static final String KEY_FEED_BASEURL = "feed.baseurl";
+       private Properties properties;
+       
+       private Configuration(Properties properties) {
+               this.properties = properties;
+       }
+       
+       public String getFeedLink() {
+               return properties.getProperty(KEY_FEED_LINK);
+        }       
+       
+        public String getFeedTitle() {
+               return properties.getProperty(KEY_FEED_TITLE);
+        }       
+        
+        public String getFeedDescription() {
+               return properties.getProperty(KEY_FEED_DESCRIPTION);
+        }
+        
+        public String getFeedBaseURL() {
+               return properties.getProperty(KEY_FEED_BASEURL);
+        }
+        
+        public File getHTMLHeaderFile() {
+               String str;
+               
+               str = properties.getProperty(KEY_HTML_HEADER_FILE);
+                               
+               if (str == null)
+                       return null;
+               
+               return new File(str);
+        }
+        
+        public static Configuration load(File f) throws IOException {
+               Properties props;
+               InputStream in;
+                               
+               props = new Properties();
+               in = new FileInputStream(f);
+                               
+               try {
+                       props.load(in);
+               } finally {
+                       in.close();
+               }
+               
+               return new Configuration(props);
+        }
+}
diff --git a/src/main/java/net/wpitchoune/asciidoctor/Main.java b/src/main/java/net/wpitchoune/asciidoctor/Main.java
new file mode 100644 (file)
index 0000000..5e687c3
--- /dev/null
@@ -0,0 +1,199 @@
+package net.wpitchoune.asciidoctor;
+/*
+ * Copyright (C) 2016 jeanfi@gmail.com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ */
+
+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.util.ArrayList;
+import java.util.HashMap;
+import java.util.Properties;
+import java.util.logging.Logger;
+
+import org.asciidoctor.Asciidoctor;
+import org.asciidoctor.Asciidoctor.Factory;
+import org.asciidoctor.ast.DocumentHeader;
+
+import com.rometools.rome.feed.synd.SyndContentImpl;
+import com.rometools.rome.feed.synd.SyndEntry;
+import com.rometools.rome.feed.synd.SyndEntryImpl;
+import com.rometools.rome.feed.synd.SyndFeed;
+import com.rometools.rome.feed.synd.SyndFeedImpl;
+import com.rometools.rome.io.FeedException;
+import com.rometools.rome.io.SyndFeedOutput;
+
+/**
+ * Command line program which generates a feed (RSS v2) and HTML files 
+ * from a set of asciidoctor documents.
+ * 
+ * @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 File toHTMLFile(File dir, File adoc) {
+                int idx;
+                String name;
+                
+                name = adoc.getName(); 
+               
+                idx = name.lastIndexOf('.');
+                
+                if (idx >= 0)
+                        name = name.substring(0, idx);
+                
+                return new File(dir, name + ".html");
+        }
+        
+        private static SyndContentImpl toSyndContentImpl(String description) {
+               SyndContentImpl ret;
+               
+               ret = new SyndContentImpl();
+               ret.setType("text/html");
+               ret.setValue(description);
+            
+               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 {
+               File inDir, html, outDir;
+               File[] adocs;
+               StringWriter desc;
+               SyndFeed feed;
+               Configuration cfg;
+               ArrayList<SyndEntry> entries;
+               SyndEntryImpl e;
+               DocumentHeader h;
+               SyndContentImpl c;
+               StringBuffer news;
+               String itemTitle, itemContent;
+               
+               inDir = new File(args[0]);
+               outDir = new File(args[1]);
+               
+               cfg = Configuration.load(new File(args[2]));
+               
+               adocs = inDir.listFiles();
+
+               feed = new SyndFeedImpl();
+               feed.setTitle(cfg.getFeedTitle());
+               feed.setDescription(cfg.getFeedDescription());
+               feed.setLink(cfg.getFeedLink());
+               
+               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;                       
+                       desc = new StringWriter();
+                                               
+                       html = toHTMLFile(outDir, adoc);
+                               
+                       h = asciidoctor.readDocumentHeader(adoc);
+                                                           
+                       asciidoctor.convert(new FileReader(adoc), desc, new HashMap<String,Object>());
+                       
+                       itemTitle = h.getDocumentTitle().getMain(); 
+                       itemContent = desc.toString();
+                       
+                       e = new SyndEntryImpl();
+                       e.setTitle(itemTitle);
+                       e.setUri(cfg.getFeedBaseURL() + "/" + html.getName());
+                       
+                       c = toSyndContentImpl(itemContent);
+                       
+                       e.setDescription(c);
+                       
+                       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");     
+                       
+                       generateHTMLFileItem(itemTitle, itemContent);
+               }
+               
+               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());
+       }
+}