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