Merge branch 'master' of ssh://wpitchoune.net/srv/git/asciidoctor_to_rss
[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 String getHTMLHeader(Properties props) throws IOException {
56                 String fileName;
57                 File f;
58                 
59                 fileName = props.getProperty(KEY_HTML_HEADER_FILE);
60                 
61                 if (fileName == null) {
62                         LOG.info(KEY_HTML_HEADER_FILE + " is not set");
63                         return null;
64                 }
65                 
66                 f = new File(fileName);
67                 return new String(Files.readAllBytes(f.toPath()),
68                                   StandardCharsets.UTF_8);
69         }
70         
71         public static void main(String[] args) throws FileNotFoundException, IOException, FeedException {
72                 File inDir, html, outDir;
73                 File[] adocs;
74                 StringWriter desc;
75                 Asciidoctor asciidoctor;
76                 SyndFeed feed;
77                 Properties props;
78                 ArrayList<SyndEntry> entries;
79                 SyndEntryImpl e;
80                 InputStream in;
81                 DocumentHeader h;
82                 SyndContentImpl c;
83                 StringBuffer news;
84                 
85                 inDir = new File(args[0]);
86                 outDir = new File(args[1]);
87                 
88                 props = new Properties();
89                 in = new FileInputStream(args[2]);
90                 props.load(in);
91                 in.close();
92                 
93                 adocs = inDir.listFiles();
94
95                 asciidoctor = Factory.create();
96
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"));
101                 
102                 entries = new ArrayList<SyndEntry>();
103                 
104                 news = new StringBuffer();
105                 news.append("<html>\n");
106                 
107                 getHTMLHeader(props);
108                 
109                 news.append("<body>\n");
110                 for (File adoc: adocs) {
111                         if (!adoc.getName().endsWith(".adoc"))
112                                 continue;                       
113                         desc = new StringWriter();
114                                                 
115                         html = toHTMLFile(outDir, adoc);
116                                 
117                         h = asciidoctor.readDocumentHeader(adoc);
118                                                             
119                         asciidoctor.convert(new FileReader(adoc), desc, new HashMap<String,Object>());
120                         
121                         e = new SyndEntryImpl();
122                         e.setTitle(h.getDocumentTitle().getMain());
123                         e.setUri(props.getProperty("feed.baseurl") + "/" + html.getName());
124                         
125                         c = toSyndContentImpl(desc.toString());
126                         
127                         e.setDescription(c);
128                         
129                         entries.add(e);
130
131                         news.append("\n<div>\n");
132                         news.append("<h2>");
133                         news.append(h.getDocumentTitle().getMain());
134                         news.append("</h2>\n");
135                         news.append(desc.toString());
136                         news.append("\n</div>\n");          
137                 }
138                 news.append("</body>\n");
139                 news.append("</html>\n");
140                 
141                 feed.setEntries(entries);
142                 
143                 feed.setFeedType("rss_2.0");                    
144                 SyndFeedOutput output = new SyndFeedOutput();
145                 output.output(feed, new File(outDir, "feed.xml"));
146         
147                 System.out.println(news.toString());
148                 Files.write(new File(outDir, "news.html").toPath(), news.toString().getBytes());
149         }
150 }