0dedab9fde2b01f0f0ac9e911c069fcb0842c199
[asciidoctor_to_rss.git] / src / main / java / Main.java
1 import java.io.File;
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;
14
15 import org.asciidoctor.Asciidoctor;
16 import org.asciidoctor.Asciidoctor.Factory;
17 import org.asciidoctor.ast.DocumentHeader;
18
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;
26
27 public class Main {
28         private static Logger LOG = Logger.getLogger(Main.class.getSimpleName());
29         private static String KEY_HTML_HEADER_FILE = "html.header.file";
30         
31         private static File toHTMLFile(File dir, File adoc) {
32                 int idx;
33                 String name;
34                 
35                 name = adoc.getName(); 
36                
37                 idx = name.lastIndexOf('.');
38                 
39                 if (idx >= 0)
40                         name = name.substring(0, idx);
41                 
42                 return new File(dir, name + ".html");
43         }
44         
45         private static SyndContentImpl toSyndContentImpl(String description) {
46                 SyndContentImpl ret;
47                 
48                 ret = new SyndContentImpl();
49                 ret.setType("text/html");
50                 ret.setValue(description);
51             
52                 return ret;
53         }
54         
55         private static void appendHTMLHeader(StringBuffer sb, Properties props)
56                         throws IOException {
57                 String fileName;
58                 File f;
59                 
60                 fileName = props.getProperty(KEY_HTML_HEADER_FILE);
61                 
62                 if (fileName == null) {
63                         LOG.info(KEY_HTML_HEADER_FILE + " is not set");
64                         return ;
65                 }
66                 
67                 f = new File(fileName);
68                 sb.append(new String(Files.readAllBytes(f.toPath()),
69                                      StandardCharsets.UTF_8));
70         }
71         
72         public static void main(String[] args) throws FileNotFoundException, IOException, FeedException {
73                 File inDir, html, outDir;
74                 File[] adocs;
75                 StringWriter desc;
76                 Asciidoctor asciidoctor;
77                 SyndFeed feed;
78                 Properties props;
79                 ArrayList<SyndEntry> entries;
80                 SyndEntryImpl e;
81                 InputStream in;
82                 DocumentHeader h;
83                 SyndContentImpl c;
84                 StringBuffer news;
85                 
86                 inDir = new File(args[0]);
87                 outDir = new File(args[1]);
88                 
89                 props = new Properties();
90                 in = new FileInputStream(args[2]);
91                 props.load(in);
92                 in.close();
93                 
94                 adocs = inDir.listFiles();
95
96                 asciidoctor = Factory.create();
97
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"));
102                 
103                 entries = new ArrayList<SyndEntry>();
104                 
105                 news = new StringBuffer();
106                 news.append("<html>\n");
107                 
108                 appendHTMLHeader(news, props);
109                 
110                 news.append("<body>\n");
111                 for (File adoc: adocs) {
112                         if (!adoc.getName().endsWith(".adoc"))
113                                 continue;                       
114                         desc = new StringWriter();
115                                                 
116                         html = toHTMLFile(outDir, adoc);
117                                 
118                         h = asciidoctor.readDocumentHeader(adoc);
119                                                             
120                         asciidoctor.convert(new FileReader(adoc), desc, new HashMap<String,Object>());
121                         
122                         e = new SyndEntryImpl();
123                         e.setTitle(h.getDocumentTitle().getMain());
124                         e.setUri(props.getProperty("feed.baseurl") + "/" + html.getName());
125                         
126                         c = toSyndContentImpl(desc.toString());
127                         
128                         e.setDescription(c);
129                         
130                         entries.add(e);
131
132                         news.append("\n<div>\n");
133                         news.append("<h2>");
134                         news.append(h.getDocumentTitle().getMain());
135                         news.append("</h2>\n");
136                         news.append(desc.toString());
137                         news.append("\n</div>\n");          
138                 }
139                 news.append("</body>\n");
140                 news.append("</html>\n");
141                 
142                 feed.setEntries(entries);
143                 
144                 feed.setFeedType("rss_2.0");                    
145                 SyndFeedOutput output = new SyndFeedOutput();
146                 output.output(feed, new File(outDir, "feed.xml"));
147         
148                 System.out.println(news.toString());
149                 Files.write(new File(outDir, "news.html").toPath(), news.toString().getBytes());
150         }
151 }