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.charset.StandardCharsets;
27 import java.nio.file.Files;
28 import java.text.ParseException;
29 import java.text.SimpleDateFormat;
30 import java.util.ArrayList;
31 import java.util.Date;
32 import java.util.HashMap;
33 import java.util.logging.Logger;
35 import org.asciidoctor.Asciidoctor;
36 import org.asciidoctor.Asciidoctor.Factory;
37 import org.asciidoctor.ast.DocumentHeader;
39 import com.rometools.rome.feed.synd.SyndContentImpl;
40 import com.rometools.rome.feed.synd.SyndEntry;
41 import com.rometools.rome.feed.synd.SyndEntryImpl;
42 import com.rometools.rome.feed.synd.SyndFeed;
43 import com.rometools.rome.feed.synd.SyndFeedImpl;
44 import com.rometools.rome.io.FeedException;
45 import com.rometools.rome.io.SyndFeedOutput;
48 * Command line program which generates a feed (RSS v2) and HTML files
49 * from a set of asciidoctor documents.
51 * @author jeanfi@gmail.com
54 private static final Logger LOG = Logger.getLogger(Main.class.getSimpleName());
56 private static final Asciidoctor asciidoctor = Factory.create();
58 private static final SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat("yyyy-dd-MM");
62 private static File toHTMLFile(File dir, File adoc) {
66 name = adoc.getName();
68 idx = name.lastIndexOf('.');
71 name = name.substring(0, idx);
73 return new File(dir, name + ".html");
76 private static SyndContentImpl toSyndContentImpl(String description) {
79 ret = new SyndContentImpl();
80 ret.setType("text/html");
81 ret.setValue(description);
86 private static void appendHTMLHead(StringBuffer sb, Configuration cfg)
90 f = cfg.getHTMLHeaderFile();
92 LOG.info("There is no declared HTML header file.");
96 sb.append("<!DOCTYPE html>\n");
97 sb.append("<html>\n");
98 sb.append("<head>\n");
99 sb.append(new String(Files.readAllBytes(f.toPath()),
100 StandardCharsets.UTF_8));
101 sb.append("</head>\n");
104 private static void appendHTMLFooter(StringBuffer sb) {
105 sb.append("</body>\n");
106 sb.append("</html>");
109 private static void appendHTMLContentHeader(StringBuffer sb, String title) {
110 sb.append("<div id='header'>\n");
113 sb.append("</h1>\n");
117 private static void generateHTMLFileItem(File file,
121 Configuration cfg) throws IOException {
124 buf = new StringBuffer();
126 appendHTMLHead(buf, cfg);
128 buf.append("<body>\n");
130 appendHTMLContentHeader(buf, title);
132 buf.append("<div id='content'>\n");
134 buf.append("<div class='date'>");
136 buf.append("</div>");
139 buf.append("</div>");
141 appendHTMLFooter(buf);
143 Files.write(file.toPath(), buf.toString().getBytes());
146 public static void main(String[] args) throws FileNotFoundException, IOException, FeedException, ParseException {
147 File inDir, html, outDir;
152 ArrayList<SyndEntry> entries;
157 String itemTitle, itemContent, itemURI, strDate;
160 inDir = new File(args[0]);
161 outDir = new File(args[1]);
163 cfg = Configuration.load(new File(args[2]));
165 adocs = inDir.listFiles();
167 feed = new SyndFeedImpl();
168 feed.setTitle(cfg.getFeedTitle());
169 feed.setDescription(cfg.getFeedDescription());
170 feed.setLink(cfg.getFeedLink());
172 entries = new ArrayList<SyndEntry>();
174 news = new StringBuffer();
176 appendHTMLHead(news, cfg);
178 news.append("<body>\n");
180 appendHTMLContentHeader(news, cfg.getFeedTitle());
182 news.append("<div id='content'>\n");
184 for (File adoc: adocs) {
185 if (!adoc.getName().endsWith(".adoc"))
187 desc = new StringWriter();
189 html = toHTMLFile(outDir, adoc);
191 h = asciidoctor.readDocumentHeader(adoc);
193 if (h.getAttributes().get("date") == null)
194 strDate = h.getAttributes().get("docdate").toString();
196 strDate = h.getAttributes().get("date").toString();
198 itemDate = DATE_FORMATTER.parse(strDate);
200 asciidoctor.convert(new FileReader(adoc), desc, new HashMap<String,Object>());
202 itemTitle = h.getDocumentTitle().getMain();
203 itemContent = desc.toString();
205 e = new SyndEntryImpl();
206 e.setTitle(itemTitle);
207 itemURI = cfg.getFeedBaseURL() + "/" + html.getName();
210 e.setPublishedDate(itemDate);
212 c = toSyndContentImpl(itemContent);
218 news.append("\n<div>");
220 news.append(itemTitle);
221 news.append("</h2>");
223 news.append("<div class='date'>");
224 news.append(DATE_FORMATTER.format(itemDate));
225 news.append("</div>");
227 news.append(desc.toString());
228 news.append("</div>\n");
230 generateHTMLFileItem(html, itemTitle, itemContent, itemDate.toString(), cfg);
233 news.append("</div>\n");
235 appendHTMLFooter(news);
237 feed.setEntries(entries);
239 feed.setFeedType("rss_2.0");
240 SyndFeedOutput output = new SyndFeedOutput();
241 output.output(feed, new File(outDir, "feed.xml"));
243 Files.write(new File(outDir, "news.html").toPath(), news.toString().getBytes());