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.FileInputStream;
23 import java.io.FileNotFoundException;
24 import java.io.FileReader;
25 import java.io.FileWriter;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.StringWriter;
29 import java.nio.charset.StandardCharsets;
30 import java.nio.file.Files;
31 import java.util.ArrayList;
32 import java.util.HashMap;
33 import java.util.Properties;
34 import java.util.logging.Logger;
36 import org.asciidoctor.Asciidoctor;
37 import org.asciidoctor.Asciidoctor.Factory;
38 import org.asciidoctor.ast.DocumentHeader;
40 import com.rometools.rome.feed.synd.SyndContentImpl;
41 import com.rometools.rome.feed.synd.SyndEntry;
42 import com.rometools.rome.feed.synd.SyndEntryImpl;
43 import com.rometools.rome.feed.synd.SyndFeed;
44 import com.rometools.rome.feed.synd.SyndFeedImpl;
45 import com.rometools.rome.feed.synd.SyndLinkImpl;
46 import com.rometools.rome.io.FeedException;
47 import com.rometools.rome.io.SyndFeedOutput;
50 * Command line program which generates a feed (RSS v2) and HTML files
51 * from a set of asciidoctor documents.
53 * @author jeanfi@gmail.com
56 private static final Logger LOG = Logger.getLogger(Main.class.getSimpleName());
58 private static final Asciidoctor asciidoctor = Factory.create();
60 private static File toHTMLFile(File dir, File adoc) {
64 name = adoc.getName();
66 idx = name.lastIndexOf('.');
69 name = name.substring(0, idx);
71 return new File(dir, name + ".html");
74 private static SyndContentImpl toSyndContentImpl(String description) {
77 ret = new SyndContentImpl();
78 ret.setType("text/html");
79 ret.setValue(description);
84 private static void appendHTMLHead(StringBuffer sb, Configuration cfg)
88 f = cfg.getHTMLHeaderFile();
90 LOG.info("There is no declared HTML header file.");
94 sb.append("<!DOCTYPE html>\n");
95 sb.append("<html>\n");
96 sb.append("<head>\n");
97 sb.append(new String(Files.readAllBytes(f.toPath()),
98 StandardCharsets.UTF_8));
99 sb.append("</head>\n");
102 private static void appendHTMLFooter(StringBuffer sb) {
103 sb.append("</body>\n");
104 sb.append("</html>");
107 private static void appendHTMLContentHeader(StringBuffer sb, String title) {
108 sb.append("<div id='header'>\n");
111 sb.append("</h1>\n");
115 private static void generateHTMLFileItem(File file,
119 Configuration cfg) throws IOException {
122 buf = new StringBuffer();
124 appendHTMLHead(buf, cfg);
126 buf.append("<body>\n");
128 appendHTMLContentHeader(buf, title);
130 buf.append("<div id='content'>\n");
132 buf.append("<div class='date'>");
134 buf.append("</div>");
137 buf.append("</div>");
139 appendHTMLFooter(buf);
141 Files.write(file.toPath(), buf.toString().getBytes());
144 public static void main(String[] args) throws FileNotFoundException, IOException, FeedException {
145 File inDir, html, outDir;
150 ArrayList<SyndEntry> entries;
155 String itemTitle, itemContent, itemURI, itemDate;
157 inDir = new File(args[0]);
158 outDir = new File(args[1]);
160 cfg = Configuration.load(new File(args[2]));
162 adocs = inDir.listFiles();
164 feed = new SyndFeedImpl();
165 feed.setTitle(cfg.getFeedTitle());
166 feed.setDescription(cfg.getFeedDescription());
167 feed.setLink(cfg.getFeedLink());
169 entries = new ArrayList<SyndEntry>();
171 news = new StringBuffer();
173 appendHTMLHead(news, cfg);
175 news.append("<body>\n");
177 appendHTMLContentHeader(news, cfg.getFeedTitle());
179 news.append("<div id='content'>\n");
181 for (File adoc: adocs) {
182 if (!adoc.getName().endsWith(".adoc"))
184 desc = new StringWriter();
186 html = toHTMLFile(outDir, adoc);
188 h = asciidoctor.readDocumentHeader(adoc);
190 if (h.getAttributes().get("date") == null)
191 itemDate = h.getAttributes().get("docdate").toString();
193 itemDate = h.getAttributes().get("date").toString();
195 asciidoctor.convert(new FileReader(adoc), desc, new HashMap<String,Object>());
197 itemTitle = h.getDocumentTitle().getMain();
198 itemContent = desc.toString();
200 e = new SyndEntryImpl();
201 e.setTitle(itemTitle);
202 itemURI = cfg.getFeedBaseURL() + "/" + html.getName();
206 c = toSyndContentImpl(itemContent);
212 news.append("\n<div>");
214 news.append(itemTitle);
215 news.append("</h2>");
216 news.append(desc.toString());
217 news.append("</div>\n");
219 generateHTMLFileItem(html, itemTitle, itemContent, itemDate, cfg);
222 news.append("</div>\n");
224 appendHTMLFooter(news);
226 feed.setEntries(entries);
228 feed.setFeedType("rss_2.0");
229 SyndFeedOutput output = new SyndFeedOutput();
230 output.output(feed, new File(outDir, "feed.xml"));
232 Files.write(new File(outDir, "news.html").toPath(), news.toString().getBytes());