updated...
[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.util.ArrayList;
9 import java.util.HashMap;
10 import java.util.Properties;
11
12 import org.asciidoctor.Asciidoctor;
13 import org.asciidoctor.Asciidoctor.Factory;
14 import org.asciidoctor.ast.DocumentHeader;
15
16 import com.rometools.rome.feed.synd.SyndContentImpl;
17 import com.rometools.rome.feed.synd.SyndEntry;
18 import com.rometools.rome.feed.synd.SyndEntryImpl;
19 import com.rometools.rome.feed.synd.SyndFeed;
20 import com.rometools.rome.feed.synd.SyndFeedImpl;
21 import com.rometools.rome.io.FeedException;
22 import com.rometools.rome.io.SyndFeedOutput;
23
24 public class Main {
25         private static String KEY_HTML_HEADER = "html.header";
26         
27         private static File toHTMLFile(File dir, File adoc) {
28                 int idx;
29                 String name;
30                 
31                 name = adoc.getName(); 
32                
33                 idx = name.lastIndexOf('.');
34                 
35                 if (idx >= 0)
36                         name = name.substring(0, idx);
37                 
38                 return new File(dir, name + ".html");
39         }
40         
41         private static SyndContentImpl toSyndContentImpl(String description) {
42                 SyndContentImpl ret;
43                 
44                 ret = new SyndContentImpl();
45                 ret.setType("text/html");
46                 ret.setValue(description);
47             
48                 return ret;
49         }
50         
51         private static String getHTMLHeader(Properties props) {
52                 return null;
53         }
54         
55         public static void main(String[] args) throws FileNotFoundException, IOException, FeedException {
56                 File inDir, html, outDir;
57                 File[] adocs;
58                 StringWriter desc;
59                 Asciidoctor asciidoctor;
60                 SyndFeed feed;
61                 Properties props;
62                 ArrayList<SyndEntry> entries;
63                 SyndEntryImpl e;
64                 InputStream in;
65                 DocumentHeader h;
66                 SyndContentImpl c;
67                 StringBuffer news;
68                 
69                 inDir = new File(args[0]);
70                 outDir = new File(args[1]);
71                 
72                 props = new Properties();
73                 in = new FileInputStream(args[2]);
74                 props.load(in);
75                 in.close();
76                 
77                 adocs = inDir.listFiles();
78
79                 asciidoctor = Factory.create();
80
81                 feed = new SyndFeedImpl();
82                 feed.setTitle(props.getProperty("feed.title"));
83                 feed.setDescription(props.getProperty("feed.description"));
84                 feed.setLink(props.getProperty("feed.link"));
85                 
86                 entries = new ArrayList<SyndEntry>();
87                 
88                 news = new StringBuffer();
89                 news.append("<html>\n");
90                 news.append("<body>\n");
91                 for (File adoc: adocs) {
92                         if (!adoc.getName().endsWith(".adoc"))
93                                 continue;                       
94                         desc = new StringWriter();
95                                                 
96                         html = toHTMLFile(outDir, adoc);
97                                 
98                         h = asciidoctor.readDocumentHeader(adoc);
99                                                             
100                         asciidoctor.convert(new FileReader(adoc), desc, new HashMap<String,Object>());
101                         
102                         e = new SyndEntryImpl();
103                         e.setTitle(h.getDocumentTitle().getMain());
104                         e.setUri(props.getProperty("feed.baseurl") + "/" + html.getName());
105                         
106                         c = toSyndContentImpl(desc.toString());
107                         
108                         e.setDescription(c);
109                         
110                         entries.add(e);
111
112                         news.append("\n<div>\n");
113                         news.append("<h2>");
114                         news.append(h.getDocumentTitle().getMain());
115                         news.append("</h2>\n");
116                         news.append(desc.toString());
117                         news.append("\n</div>\n");          
118                 }
119                 news.append("</body>\n");
120                 news.append("</html>\n");
121                 
122                 feed.setEntries(entries);
123                 
124                 feed.setFeedType("rss_2.0");                    
125                 SyndFeedOutput output = new SyndFeedOutput();
126                 output.output(feed, new File(outDir, "feed.xml"));
127         
128                 System.out.println(news.toString());
129         }
130 }