X-Git-Url: http://git.wpitchoune.net/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2FMain.java;h=64b4d8820b65610dd7cbb20e25e957fc9d881dc5;hb=76238691b4a28a8d248bad0f17245ac273ff3ace;hp=96b840713d8387d5d1a0af0d71359c26e2c1656b;hpb=ba4a19cb02bf72d69bc59372fefc39074fdd121a;p=asciidoctor_to_rss.git diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 96b8407..64b4d88 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -5,9 +5,12 @@ 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; @@ -22,7 +25,11 @@ import com.rometools.rome.io.FeedException; import com.rometools.rome.io.SyndFeedOutput; public class Main { - private static File getHTMLFile(File dir, File adoc) { + 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; @@ -36,10 +43,44 @@ public class Main { 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 sw; + StringWriter desc; Asciidoctor asciidoctor; SyndFeed feed; Properties props; @@ -48,6 +89,7 @@ public class Main { InputStream in; DocumentHeader h; SyndContentImpl c; + StringBuffer news; inDir = new File(args[0]); outDir = new File(args[1]); @@ -62,40 +104,68 @@ public class Main { asciidoctor = Factory.create(); feed = new SyndFeedImpl(); - feed.setTitle(props.getProperty("feed.title")); + 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; - sw = new StringWriter(); + desc = new StringWriter(); - html = getHTMLFile(outDir, adoc); + html = toHTMLFile(outDir, adoc); h = asciidoctor.readDocumentHeader(adoc); - asciidoctor.convert(new FileReader(adoc), sw, new HashMap()); + 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 = new SyndContentImpl(); - c.setType("text/html"); - c.setValue(sw.toString()); + 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()); } -} \ No newline at end of file +}