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 void appendHTMLHeader(StringBuffer sb, Properties props)
60 fileName = props.getProperty(KEY_HTML_HEADER_FILE);
62 if (fileName == null) {
63 LOG.info(KEY_HTML_HEADER_FILE + " is not set");
67 f = new File(fileName);
68 sb.append(new String(Files.readAllBytes(f.toPath()),
69 StandardCharsets.UTF_8));
72 public static void main(String[] args) throws FileNotFoundException, IOException, FeedException {
73 File inDir, html, outDir;
76 Asciidoctor asciidoctor;
79 ArrayList<SyndEntry> entries;
86 inDir = new File(args[0]);
87 outDir = new File(args[1]);
89 props = new Properties();
90 in = new FileInputStream(args[2]);
94 adocs = inDir.listFiles();
96 asciidoctor = Factory.create();
98 feed = new SyndFeedImpl();
99 feed.setTitle(props.getProperty("feed.title"));
100 feed.setDescription(props.getProperty("feed.description"));
101 feed.setLink(props.getProperty("feed.link"));
103 entries = new ArrayList<SyndEntry>();
105 news = new StringBuffer();
106 news.append("<html>\n");
108 appendHTMLHeader(news, props);
110 news.append("<body>\n");
111 for (File adoc: adocs) {
112 if (!adoc.getName().endsWith(".adoc"))
114 desc = new StringWriter();
116 html = toHTMLFile(outDir, adoc);
118 h = asciidoctor.readDocumentHeader(adoc);
120 asciidoctor.convert(new FileReader(adoc), desc, new HashMap<String,Object>());
122 e = new SyndEntryImpl();
123 e.setTitle(h.getDocumentTitle().getMain());
124 e.setUri(props.getProperty("feed.baseurl") + "/" + html.getName());
126 c = toSyndContentImpl(desc.toString());
132 news.append("\n<div>\n");
134 news.append(h.getDocumentTitle().getMain());
135 news.append("</h2>\n");
136 news.append(desc.toString());
137 news.append("\n</div>\n");
139 news.append("</body>\n");
140 news.append("</html>\n");
142 feed.setEntries(entries);
144 feed.setFeedType("rss_2.0");
145 SyndFeedOutput output = new SyndFeedOutput();
146 output.output(feed, new File(outDir, "feed.xml"));
148 System.out.println(news.toString());
149 Files.write(new File(outDir, "news.html").toPath(), news.toString().getBytes());