1 package net.wpitchoune.asciidoctor;
3 * Copyright (C) 2016 jeanfi@gmail.com
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 import java.io.FileNotFoundException;
23 import java.io.FileReader;
24 import java.io.IOException;
25 import java.io.StringWriter;
26 import java.nio.file.Files;
27 import java.text.ParseException;
28 import java.text.SimpleDateFormat;
29 import java.util.ArrayList;
30 import java.util.Date;
31 import java.util.HashMap;
33 import org.asciidoctor.Asciidoctor;
34 import org.asciidoctor.Asciidoctor.Factory;
35 import org.asciidoctor.ast.DocumentHeader;
37 import com.rometools.rome.feed.synd.SyndContentImpl;
38 import com.rometools.rome.feed.synd.SyndEntry;
39 import com.rometools.rome.feed.synd.SyndEntryImpl;
40 import com.rometools.rome.feed.synd.SyndFeed;
41 import com.rometools.rome.feed.synd.SyndFeedImpl;
42 import com.rometools.rome.io.FeedException;
43 import com.rometools.rome.io.SyndFeedOutput;
46 * Command line program which generates a feed (RSS v2) and HTML files
47 * from a set of asciidoctor documents.
49 * @author jeanfi@gmail.com
52 private static final Asciidoctor asciidoctor = Factory.create();
54 private static final SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat("yyyy-dd-MM");
56 private static File toHTMLFile(File dir, File adoc) {
60 name = adoc.getName();
62 idx = name.lastIndexOf('.');
65 name = name.substring(0, idx);
67 return new File(dir, name + ".html");
70 private static SyndContentImpl toSyndContentImpl(String description) {
73 ret = new SyndContentImpl();
74 ret.setType("text/html");
75 ret.setValue(description);
80 public static void main(String[] args) throws FileNotFoundException, IOException, FeedException, ParseException {
81 File inDir, html, outDir;
86 ArrayList<SyndEntry> entries;
89 String itemTitle, itemContent, itemURI, strDate;
92 inDir = new File(args[0]);
93 outDir = new File(args[1]);
95 cfg = Configuration.load(new File(args[2]));
97 adocs = inDir.listFiles();
99 feed = new SyndFeedImpl();
100 feed.setTitle(cfg.getFeedTitle());
101 feed.setDescription(cfg.getFeedDescription());
102 feed.setLink(cfg.getFeedLink());
104 entries = new ArrayList<SyndEntry>();
106 for (File adoc: adocs) {
107 if (!adoc.getName().endsWith(".adoc"))
109 desc = new StringWriter();
111 html = toHTMLFile(outDir, adoc);
113 h = asciidoctor.readDocumentHeader(adoc);
115 if (h.getAttributes().get("date") == null)
116 strDate = h.getAttributes().get("docdate").toString();
118 strDate = h.getAttributes().get("date").toString();
120 itemDate = DATE_FORMATTER.parse(strDate);
122 asciidoctor.convert(new FileReader(adoc), desc, new HashMap<String,Object>());
124 itemTitle = h.getDocumentTitle().getMain();
125 itemContent = desc.toString();
127 e = new SyndEntryImpl();
128 e.setTitle(itemTitle);
129 itemURI = cfg.getFeedBaseURL() + "/" + html.getName();
132 e.setPublishedDate(itemDate);
134 e.setDescription(toSyndContentImpl(itemContent));
138 Files.write(html.toPath(), new HTML(cfg).toHTML(e).getBytes());
141 feed.setEntries(entries);
143 feed.setFeedType("rss_2.0");
144 SyndFeedOutput output = new SyndFeedOutput();
145 output.output(feed, new File(outDir, "feed.xml"));
147 Files.write(new File(outDir, "news.html").toPath(),
148 new HTML(cfg).toHTML(feed.getEntries()).getBytes());