c83bad8a2ee00e4c7ee8a6f60f22f146fe73d7a9
[www.git] / asciidoctor / src / main / java / Main.java
1 import java.io.*;
2 import java.util.*;
3
4 import org.asciidoctor.Asciidoctor;
5 import org.asciidoctor.Asciidoctor.Factory;
6 import org.asciidoctor.Options;
7 import org.asciidoctor.ast.Document;
8 import org.asciidoctor.ast.DocumentHeader;
9
10 import com.rometools.rome.feed.synd.*;
11 import com.rometools.rome.io.FeedException;
12 import com.rometools.rome.io.SyndFeedOutput;
13
14 public class Main {
15         private static File toHTMLFile(File dir, File adoc) {
16                 int idx;
17                 String name;
18                 
19                 name = adoc.getName(); 
20                
21                 idx = name.lastIndexOf('.');
22                 
23                 if (idx >= 0)
24                         name = name.substring(0, idx);
25                 
26                 return new File(dir, name + ".html");
27         }
28         
29         public static void main(String[] args) throws FileNotFoundException, IOException, FeedException {
30                 File inDir, html, outDir;
31                 File[] adocs;
32                 StringWriter sw;
33                 Asciidoctor asciidoctor;
34                 Options opts;
35                 SyndFeed feed;
36                 Properties props;
37                 ArrayList<SyndEntry> entries;
38                 SyndEntryImpl e;
39                 InputStream in;
40                 DocumentHeader h;
41                 SyndContentImpl c;
42                 
43                 inDir = new File(args[0]);
44                 outDir = new File(args[1]);
45                 
46                 props = new Properties();
47                 in = new FileInputStream(args[2]);
48                 props.load(in);
49                 in.close();
50                 
51                 adocs = inDir.listFiles();
52
53                 asciidoctor = Factory.create();
54
55                 feed = new SyndFeedImpl();
56                 feed.setTitle(props.getProperty("feed.title"));
57                 feed.setDescription(props.getProperty("feed.description"));
58                 feed.setLink(props.getProperty("feed.link"));
59                 
60                 entries = new ArrayList<SyndEntry>();
61                 
62                 for (File adoc: adocs) {
63                         if (!adoc.getName().endsWith(".adoc"))
64                                 continue;                       
65                         sw = new StringWriter();
66                                                 
67                         html = toHTMLFile(outDir, adoc);
68                                 
69                         h = asciidoctor.readDocumentHeader(adoc);
70                                                             
71                         asciidoctor.convert(new FileReader(adoc), sw, new HashMap<String,Object>());
72                         
73                         e = new SyndEntryImpl();
74                         e.setTitle(h.getDocumentTitle().getMain());
75                         e.setUri(props.getProperty("feed.baseurl") + "/" + html.getName());
76                         
77                         c = new SyndContentImpl();
78                         c.setType("text/html");
79                         c.setValue(sw.toString());
80                         
81                         e.setDescription(c);
82                         
83                         entries.add(e);
84                 }
85                 
86                 feed.setEntries(entries);
87                 
88                 feed.setFeedType("rss_2.0");                    
89                 SyndFeedOutput output = new SyndFeedOutput();
90                 output.output(feed, new File(outDir, "feed.xml"));
91         }
92 }