renaming
[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 File getHTMLFile(File dir, File adoc) {
26                 int idx;
27                 String name;
28                 
29                 name = adoc.getName(); 
30                
31                 idx = name.lastIndexOf('.');
32                 
33                 if (idx >= 0)
34                         name = name.substring(0, idx);
35                 
36                 return new File(dir, name + ".html");
37         }
38         
39         public static void main(String[] args) throws FileNotFoundException, IOException, FeedException {
40                 File inDir, html, outDir;
41                 File[] adocs;
42                 StringWriter sw;
43                 Asciidoctor asciidoctor;
44                 SyndFeed feed;
45                 Properties props;
46                 ArrayList<SyndEntry> entries;
47                 SyndEntryImpl e;
48                 InputStream in;
49                 DocumentHeader h;
50                 SyndContentImpl c;
51                 
52                 inDir = new File(args[0]);
53                 outDir = new File(args[1]);
54                 
55                 props = new Properties();
56                 in = new FileInputStream(args[2]);
57                 props.load(in);
58                 in.close();
59                 
60                 adocs = inDir.listFiles();
61
62                 asciidoctor = Factory.create();
63
64                 feed = new SyndFeedImpl();
65                 feed.setTitle(props.getProperty("feed.title"));
66                 feed.setDescription(props.getProperty("feed.description"));
67                 feed.setLink(props.getProperty("feed.link"));
68                 
69                 entries = new ArrayList<SyndEntry>();
70                 
71                 for (File adoc: adocs) {
72                         if (!adoc.getName().endsWith(".adoc"))
73                                 continue;                       
74                         sw = new StringWriter();
75                                                 
76                         html = getHTMLFile(outDir, adoc);
77                                 
78                         h = asciidoctor.readDocumentHeader(adoc);
79                                                             
80                         asciidoctor.convert(new FileReader(adoc), sw, new HashMap<String,Object>());
81                         
82                         e = new SyndEntryImpl();
83                         e.setTitle(h.getDocumentTitle().getMain());
84                         e.setUri(props.getProperty("feed.baseurl") + "/" + html.getName());
85                         
86                         c = new SyndContentImpl();
87                         c.setType("text/html");
88                         c.setValue(sw.toString());
89                         
90                         e.setDescription(c);
91                         
92                         entries.add(e);
93                 }
94                 
95                 feed.setEntries(entries);
96                 
97                 feed.setFeedType("rss_2.0");                    
98                 SyndFeedOutput output = new SyndFeedOutput();
99                 output.output(feed, new File(outDir, "feed.xml"));
100         }
101 }