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 final Logger LOG = Logger.getLogger(Main.class.getSimpleName());
29 private static final String KEY_HTML_HEADER_FILE = "html.header.file";
30 private static final String KEY_FEED_TITLE = "feed.title";
32 private static File toHTMLFile(File dir, File adoc) {
36 name = adoc.getName();
38 idx = name.lastIndexOf('.');
41 name = name.substring(0, idx);
43 return new File(dir, name + ".html");
46 private static SyndContentImpl toSyndContentImpl(String description) {
49 ret = new SyndContentImpl();
50 ret.setType("text/html");
51 ret.setValue(description);
56 private static String getFeedTitle(Properties props) {
57 return props.getProperty(KEY_FEED_TITLE);
60 private static void appendHTMLHeader(StringBuffer sb, Properties props)
65 fileName = props.getProperty(KEY_HTML_HEADER_FILE);
67 if (fileName == null) {
68 LOG.info(KEY_HTML_HEADER_FILE + " is not set");
72 f = new File(fileName);
74 sb.append("<head>\n");
75 sb.append(new String(Files.readAllBytes(f.toPath()),
76 StandardCharsets.UTF_8));
77 sb.append("</head>\n");
80 public static void main(String[] args) throws FileNotFoundException, IOException, FeedException {
81 File inDir, html, outDir;
84 Asciidoctor asciidoctor;
87 ArrayList<SyndEntry> entries;
94 inDir = new File(args[0]);
95 outDir = new File(args[1]);
97 props = new Properties();
98 in = new FileInputStream(args[2]);
102 adocs = inDir.listFiles();
104 asciidoctor = Factory.create();
106 feed = new SyndFeedImpl();
107 feed.setTitle(getFeedTitle(props));
108 feed.setDescription(props.getProperty("feed.description"));
109 feed.setLink(props.getProperty("feed.link"));
111 entries = new ArrayList<SyndEntry>();
113 news = new StringBuffer();
114 news.append("<!DOCTYPE html>\n");
115 news.append("<html>\n");
117 appendHTMLHeader(news, props);
119 news.append("<body>\n");
121 news.append("<div id='header'>\n");
123 news.append(getFeedTitle(props));
124 news.append("</h1>\n");
125 news.append("</div>");
127 news.append("<div id='content'>\n");
129 for (File adoc: adocs) {
130 if (!adoc.getName().endsWith(".adoc"))
132 desc = new StringWriter();
134 html = toHTMLFile(outDir, adoc);
136 h = asciidoctor.readDocumentHeader(adoc);
138 asciidoctor.convert(new FileReader(adoc), desc, new HashMap<String,Object>());
140 e = new SyndEntryImpl();
141 e.setTitle(h.getDocumentTitle().getMain());
142 e.setUri(props.getProperty("feed.baseurl") + "/" + html.getName());
144 c = toSyndContentImpl(desc.toString());
150 news.append("\n<div>\n");
152 news.append(h.getDocumentTitle().getMain());
153 news.append("</h2>\n");
154 news.append(desc.toString());
155 news.append("\n</div>\n");
158 news.append("</div>\n");
159 news.append("</body>\n");
160 news.append("</html>\n");
162 feed.setEntries(entries);
164 feed.setFeedType("rss_2.0");
165 SyndFeedOutput output = new SyndFeedOutput();
166 output.output(feed, new File(outDir, "feed.xml"));
168 System.out.println(news.toString());
169 Files.write(new File(outDir, "news.html").toPath(), news.toString().getBytes());