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