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 File toHTMLFile(File dir, File adoc) {
62 name = adoc.getName();
64 idx = name.lastIndexOf('.');
67 name = name.substring(0, idx);
69 return new File(dir, name + ".html");
72 private static SyndContentImpl toSyndContentImpl(String description) {
75 ret = new SyndContentImpl();
76 ret.setType("text/html");
77 ret.setValue(description);
82 private static void appendHTMLHead(StringBuffer sb, Configuration cfg)
86 f = cfg.getHTMLHeaderFile();
88 LOG.info("There is no declared HTML header file.");
92 sb.append("<!DOCTYPE html>\n");
93 sb.append("<html>\n");
94 sb.append("<head>\n");
95 sb.append(new String(Files.readAllBytes(f.toPath()),
96 StandardCharsets.UTF_8));
97 sb.append("</head>\n");
100 private static void appendHTMLFooter(StringBuffer sb) {
101 sb.append("</body>\n");
102 sb.append("</html>");
105 private static void appendHTMLContentHeader(StringBuffer sb, String title) {
106 sb.append("<div id='header'>\n");
109 sb.append("</h1>\n");
113 private static void generateHTMLFileItem(File file,
117 Configuration cfg) throws IOException {
120 buf = new StringBuffer();
122 appendHTMLHead(buf, cfg);
124 buf.append("<body>\n");
126 appendHTMLContentHeader(buf, title);
128 buf.append("<div id='content'>\n");
130 buf.append("<div class='date'>");
132 buf.append("</div>");
135 buf.append("</div>");
137 appendHTMLFooter(buf);
139 Files.write(file.toPath(), buf.toString().getBytes());
142 public static void main(String[] args) throws FileNotFoundException, IOException, FeedException, ParseException {
143 File inDir, html, outDir;
148 ArrayList<SyndEntry> entries;
153 String itemTitle, itemContent, itemURI, strDate;
155 SimpleDateFormat sdf;
157 sdf = new SimpleDateFormat("yyyy-dd-MM");
159 inDir = new File(args[0]);
160 outDir = new File(args[1]);
162 cfg = Configuration.load(new File(args[2]));
164 adocs = inDir.listFiles();
166 feed = new SyndFeedImpl();
167 feed.setTitle(cfg.getFeedTitle());
168 feed.setDescription(cfg.getFeedDescription());
169 feed.setLink(cfg.getFeedLink());
171 entries = new ArrayList<SyndEntry>();
173 news = new StringBuffer();
175 appendHTMLHead(news, cfg);
177 news.append("<body>\n");
179 appendHTMLContentHeader(news, cfg.getFeedTitle());
181 news.append("<div id='content'>\n");
183 for (File adoc: adocs) {
184 if (!adoc.getName().endsWith(".adoc"))
186 desc = new StringWriter();
188 html = toHTMLFile(outDir, adoc);
190 h = asciidoctor.readDocumentHeader(adoc);
192 if (h.getAttributes().get("date") == null)
193 strDate = h.getAttributes().get("docdate").toString();
195 strDate = h.getAttributes().get("date").toString();
197 itemDate = sdf.parse(strDate);
199 asciidoctor.convert(new FileReader(adoc), desc, new HashMap<String,Object>());
201 itemTitle = h.getDocumentTitle().getMain();
202 itemContent = desc.toString();
204 e = new SyndEntryImpl();
205 e.setTitle(itemTitle);
206 itemURI = cfg.getFeedBaseURL() + "/" + html.getName();
209 e.setPublishedDate(itemDate);
211 c = toSyndContentImpl(itemContent);
217 news.append("\n<div>");
219 news.append(itemTitle);
220 news.append("</h2>");
222 news.append("<div class='date'>");
223 news.append(itemDate);
224 news.append("</div>");
226 news.append(desc.toString());
227 news.append("</div>\n");
229 generateHTMLFileItem(html, itemTitle, itemContent, itemDate.toString(), cfg);
232 news.append("</div>\n");
234 appendHTMLFooter(news);
236 feed.setEntries(entries);
238 feed.setFeedType("rss_2.0");
239 SyndFeedOutput output = new SyndFeedOutput();
240 output.output(feed, new File(outDir, "feed.xml"));
242 Files.write(new File(outDir, "news.html").toPath(), news.toString().getBytes());