2 import java.io.FileInputStream;
3 import java.io.FileNotFoundException;
4 import java.io.FileReader;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.io.StringWriter;
8 import java.nio.charset.StandardCharsets;
9 import java.nio.file.Files;
10 import java.util.ArrayList;
11 import java.util.HashMap;
12 import java.util.Properties;
13 import java.util.logging.Logger;
15 import org.asciidoctor.Asciidoctor;
16 import org.asciidoctor.Asciidoctor.Factory;
17 import org.asciidoctor.ast.DocumentHeader;
19 import com.rometools.rome.feed.synd.SyndContentImpl;
20 import com.rometools.rome.feed.synd.SyndEntry;
21 import com.rometools.rome.feed.synd.SyndEntryImpl;
22 import com.rometools.rome.feed.synd.SyndFeed;
23 import com.rometools.rome.feed.synd.SyndFeedImpl;
24 import com.rometools.rome.io.FeedException;
25 import com.rometools.rome.io.SyndFeedOutput;
28 private static Logger LOG = Logger.getLogger(Main.class.getSimpleName());
29 private static String KEY_HTML_HEADER_FILE = "html.header.file";
31 private static File toHTMLFile(File dir, File adoc) {
35 name = adoc.getName();
37 idx = name.lastIndexOf('.');
40 name = name.substring(0, idx);
42 return new File(dir, name + ".html");
45 private static SyndContentImpl toSyndContentImpl(String description) {
48 ret = new SyndContentImpl();
49 ret.setType("text/html");
50 ret.setValue(description);
55 private static String getHTMLHeader(Properties props) throws IOException {
59 fileName = props.getProperty(KEY_HTML_HEADER_FILE);
61 if (fileName == null) {
62 LOG.info(KEY_HTML_HEADER_FILE + " is not set");
66 f = new File(fileName);
67 return new String(Files.readAllBytes(f.toPath()),
68 StandardCharsets.UTF_8);
71 public static void main(String[] args) throws FileNotFoundException, IOException, FeedException {
72 File inDir, html, outDir;
75 Asciidoctor asciidoctor;
78 ArrayList<SyndEntry> entries;
85 inDir = new File(args[0]);
86 outDir = new File(args[1]);
88 props = new Properties();
89 in = new FileInputStream(args[2]);
93 adocs = inDir.listFiles();
95 asciidoctor = Factory.create();
97 feed = new SyndFeedImpl();
98 feed.setTitle(props.getProperty("feed.title"));
99 feed.setDescription(props.getProperty("feed.description"));
100 feed.setLink(props.getProperty("feed.link"));
102 entries = new ArrayList<SyndEntry>();
104 news = new StringBuffer();
105 news.append("<html>\n");
107 getHTMLHeader(props);
109 news.append("<body>\n");
110 for (File adoc: adocs) {
111 if (!adoc.getName().endsWith(".adoc"))
113 desc = new StringWriter();
115 html = toHTMLFile(outDir, adoc);
117 h = asciidoctor.readDocumentHeader(adoc);
119 asciidoctor.convert(new FileReader(adoc), desc, new HashMap<String,Object>());
121 e = new SyndEntryImpl();
122 e.setTitle(h.getDocumentTitle().getMain());
123 e.setUri(props.getProperty("feed.baseurl") + "/" + html.getName());
125 c = toSyndContentImpl(desc.toString());
131 news.append("\n<div>\n");
133 news.append(h.getDocumentTitle().getMain());
134 news.append("</h2>\n");
135 news.append(desc.toString());
136 news.append("\n</div>\n");
138 news.append("</body>\n");
139 news.append("</html>\n");
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 System.out.println(news.toString());
148 Files.write(new File(outDir, "news.html").toPath(), news.toString().getBytes());