From f40278e6f437943d1d4eee937a8203dff046cb28 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Orsini Date: Sat, 2 Jul 2016 19:08:10 +0200 Subject: [PATCH] refactoring --- asciidoctor_to_rss.sh | 2 +- src/main/java/Main.java | 171 ------------------ .../net/wpitchoune/asciidoctor/Configuration.java | 63 +++++++ src/main/java/net/wpitchoune/asciidoctor/Main.java | 199 +++++++++++++++++++++ 4 files changed, 263 insertions(+), 172 deletions(-) delete mode 100644 src/main/java/Main.java create mode 100644 src/main/java/net/wpitchoune/asciidoctor/Configuration.java create mode 100644 src/main/java/net/wpitchoune/asciidoctor/Main.java diff --git a/asciidoctor_to_rss.sh b/asciidoctor_to_rss.sh index e624d1e..7696d3c 100755 --- a/asciidoctor_to_rss.sh +++ b/asciidoctor_to_rss.sh @@ -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 index 64b4d88..0000000 --- a/src/main/java/Main.java +++ /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("\n"); - sb.append(new String(Files.readAllBytes(f.toPath()), - StandardCharsets.UTF_8)); - sb.append("\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 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(); - - news = new StringBuffer(); - news.append("\n"); - news.append("\n"); - - appendHTMLHeader(news, props); - - news.append("\n"); - - news.append(""); - - news.append("
\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()); - - 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
\n"); - news.append("

"); - news.append(h.getDocumentTitle().getMain()); - news.append("

\n"); - news.append(desc.toString()); - news.append("\n
\n"); - } - - news.append("
\n"); - news.append("\n"); - news.append("\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 index 0000000..64668a5 --- /dev/null +++ b/src/main/java/net/wpitchoune/asciidoctor/Configuration.java @@ -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 index 0000000..5e687c3 --- /dev/null +++ b/src/main/java/net/wpitchoune/asciidoctor/Main.java @@ -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("\n"); + sb.append("\n"); + sb.append("\n"); + sb.append(new String(Files.readAllBytes(f.toPath()), + StandardCharsets.UTF_8)); + sb.append("\n"); + } + + private static void appendHTMLContentHeader(StringBuffer sb, String title) { + sb.append(""); + } + + 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 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(); + + news = new StringBuffer(); + + appendHTMLHead(news, cfg); + + news.append("\n"); + + appendHTMLContentHeader(news, cfg.getFeedTitle()); + + news.append("
\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()); + + 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
\n"); + news.append("

"); + news.append(h.getDocumentTitle().getMain()); + news.append("

\n"); + news.append(desc.toString()); + news.append("\n
\n"); + + generateHTMLFileItem(itemTitle, itemContent); + } + + news.append("
\n"); + + news.append("\n"); + news.append("\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()); + } +} -- 2.7.4