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.io.FeedException;
46 import com.rometools.rome.io.SyndFeedOutput;
49 * Command line program which generates a feed (RSS v2) and HTML files
50 * from a set of asciidoctor documents.
52 * @author jeanfi@gmail.com
55 private static final Logger LOG = Logger.getLogger(Main.class.getSimpleName());
57 private static final Asciidoctor asciidoctor = Factory.create();
59 private static File toHTMLFile(File dir, File adoc) {
63 name = adoc.getName();
65 idx = name.lastIndexOf('.');
68 name = name.substring(0, idx);
70 return new File(dir, name + ".html");
73 private static SyndContentImpl toSyndContentImpl(String description) {
76 ret = new SyndContentImpl();
77 ret.setType("text/html");
78 ret.setValue(description);
83 private static void appendHTMLHead(StringBuffer sb, Configuration cfg)
87 f = cfg.getHTMLHeaderFile();
89 LOG.info("There is no declared HTML header file.");
93 sb.append("<!DOCTYPE html>\n");
94 sb.append("<html>\n");
95 sb.append("<head>\n");
96 sb.append(new String(Files.readAllBytes(f.toPath()),
97 StandardCharsets.UTF_8));
98 sb.append("</head>\n");
101 private static void appendHTMLFooter(StringBuffer sb) {
102 sb.append("</body>\n");
103 sb.append("</html>");
106 private static void appendHTMLContentHeader(StringBuffer sb, String title) {
107 sb.append("<div id='header'>\n");
110 sb.append("</h1>\n");
114 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>");
132 appendHTMLFooter(buf);
134 Files.write(file.toPath(), buf.toString().getBytes());
137 public static void main(String[] args) throws FileNotFoundException, IOException, FeedException {
138 File inDir, html, outDir;
143 ArrayList<SyndEntry> entries;
148 String itemTitle, itemContent, itemURI;
150 inDir = new File(args[0]);
151 outDir = new File(args[1]);
153 cfg = Configuration.load(new File(args[2]));
155 adocs = inDir.listFiles();
157 feed = new SyndFeedImpl();
158 feed.setTitle(cfg.getFeedTitle());
159 feed.setDescription(cfg.getFeedDescription());
160 feed.setLink(cfg.getFeedLink());
162 entries = new ArrayList<SyndEntry>();
164 news = new StringBuffer();
166 appendHTMLHead(news, cfg);
168 news.append("<body>\n");
170 appendHTMLContentHeader(news, cfg.getFeedTitle());
172 news.append("<div id='content'>\n");
174 for (File adoc: adocs) {
175 if (!adoc.getName().endsWith(".adoc"))
177 desc = new StringWriter();
179 html = toHTMLFile(outDir, adoc);
181 h = asciidoctor.readDocumentHeader(adoc);
183 asciidoctor.convert(new FileReader(adoc), desc, new HashMap<String,Object>());
185 itemTitle = h.getDocumentTitle().getMain();
186 itemContent = desc.toString();
188 e = new SyndEntryImpl();
189 e.setTitle(itemTitle);
190 itemURI = cfg.getFeedBaseURL() + "/" + html.getName();
195 c = toSyndContentImpl(itemContent);
201 news.append("\n<div>");
203 news.append(itemTitle);
204 news.append("</h2>");
205 news.append(desc.toString());
206 news.append("</div>\n");
208 generateHTMLFileItem(html, itemTitle, itemContent, cfg);
211 news.append("</div>\n");
213 appendHTMLFooter(news);
215 feed.setEntries(entries);
217 feed.setFeedType("rss_2.0");
218 SyndFeedOutput output = new SyndFeedOutput();
219 output.output(feed, new File(outDir, "feed.xml"));
221 System.out.println(news.toString());
222 Files.write(new File(outDir, "news.html").toPath(), news.toString().getBytes());