X-Git-Url: https://git.wpitchoune.net/gitweb/?p=asciidoctor_to_rss.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fwpitchoune%2Fasciidoctor%2FMain.java;fp=src%2Fmain%2Fjava%2Fnet%2Fwpitchoune%2Fasciidoctor%2FMain.java;h=5e687c342960b13893e7961da6444dd345543ce7;hp=0000000000000000000000000000000000000000;hb=f40278e6f437943d1d4eee937a8203dff046cb28;hpb=76238691b4a28a8d248bad0f17245ac273ff3ace 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()); + } +}