+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());
+ }
+}