d66988583ff4e686f64064d77e2668329e0b1343
[asciidoctor_to_rss.git] / src / main / java / net / wpitchoune / asciidoctor / Main.java
1 package net.wpitchoune.asciidoctor;
2 /*
3  * Copyright (C) 2016 jeanfi@gmail.com
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  * 02110-1301 USA
19  */
20
21 import java.io.File;
22 import java.io.FileNotFoundException;
23 import java.io.FileReader;
24 import java.io.IOException;
25 import java.io.StringWriter;
26 import java.nio.file.Files;
27 import java.text.ParseException;
28 import java.text.SimpleDateFormat;
29 import java.util.ArrayList;
30 import java.util.Date;
31 import java.util.HashMap;
32
33 import org.asciidoctor.Asciidoctor;
34 import org.asciidoctor.Asciidoctor.Factory;
35 import org.asciidoctor.ast.DocumentHeader;
36
37 import com.rometools.rome.feed.synd.SyndContentImpl;
38 import com.rometools.rome.feed.synd.SyndEntry;
39 import com.rometools.rome.feed.synd.SyndEntryImpl;
40 import com.rometools.rome.feed.synd.SyndFeed;
41 import com.rometools.rome.feed.synd.SyndFeedImpl;
42 import com.rometools.rome.io.FeedException;
43 import com.rometools.rome.io.SyndFeedOutput;
44
45 /**
46  * Command line program which generates a feed (RSS v2) and HTML files 
47  * from a set of asciidoctor documents.
48  * 
49  * @author jeanfi@gmail.com
50  */
51 public class Main {
52         private static final Asciidoctor asciidoctor = Factory.create();
53         
54         private static final SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat("yyyy-dd-MM");        
55         
56         private static File toHTMLFile(File dir, File adoc) {
57                 int idx;
58                 String name;
59                 
60                 name = adoc.getName(); 
61                
62                 idx = name.lastIndexOf('.');
63                 
64                 if (idx >= 0)
65                         name = name.substring(0, idx);
66                 
67                 return new File(dir, name + ".html");
68         }
69         
70         private static SyndContentImpl toSyndContentImpl(String description) {
71                 SyndContentImpl ret;
72                 
73                 ret = new SyndContentImpl();
74                 ret.setType("text/html");
75                 ret.setValue(description);
76             
77                 return ret;
78         }
79         
80         public static void main(String[] args) throws FileNotFoundException, IOException, FeedException, ParseException {
81                 File inDir, html, outDir;
82                 File[] adocs;
83                 StringWriter desc;
84                 SyndFeed feed;
85                 Configuration cfg;
86                 ArrayList<SyndEntry> entries;
87                 SyndEntryImpl e;
88                 DocumentHeader h;
89                 String itemTitle, itemContent, itemURI, strDate;
90                 Date itemDate;
91                 
92                 inDir = new File(args[0]);
93                 outDir = new File(args[1]);
94                 
95                 cfg = Configuration.load(new File(args[2]));
96                 
97                 adocs = inDir.listFiles();
98
99                 feed = new SyndFeedImpl();
100                 feed.setTitle(cfg.getFeedTitle());
101                 feed.setDescription(cfg.getFeedDescription());
102                 feed.setLink(cfg.getFeedLink());
103                 
104                 entries = new ArrayList<SyndEntry>();
105                 
106                 for (File adoc: adocs) {
107                         if (!adoc.getName().endsWith(".adoc"))
108                                 continue;                       
109                         desc = new StringWriter();
110                                                 
111                         html = toHTMLFile(outDir, adoc);
112                                 
113                         h = asciidoctor.readDocumentHeader(adoc);
114
115                         if (h.getAttributes().get("date") == null)
116                                 strDate = h.getAttributes().get("docdate").toString();
117                         else
118                                 strDate = h.getAttributes().get("date").toString();
119                                                 
120                         itemDate = DATE_FORMATTER.parse(strDate);
121                         
122                         asciidoctor.convert(new FileReader(adoc), desc, new HashMap<String,Object>());
123                         
124                         itemTitle = h.getDocumentTitle().getMain(); 
125                         itemContent = desc.toString();
126                         
127                         e = new SyndEntryImpl();
128                         e.setTitle(itemTitle);
129                         itemURI = cfg.getFeedBaseURL() + "/" + html.getName(); 
130                         e.setUri(itemURI);
131                         e.setLink(itemURI);
132                         e.setPublishedDate(itemDate);
133                         
134                         e.setDescription(toSyndContentImpl(itemContent));
135                         
136                         entries.add(e);
137                         
138                         Files.write(html.toPath(), new HTML(cfg).toHTML(e).getBytes());
139                 }
140                 
141                 feed.setEntries(entries);
142                 
143                 feed.setFeedType("rss_2.0");                    
144                 SyndFeedOutput output = new SyndFeedOutput();
145                 output.output(feed, new File(outDir, "feed.xml"));
146         
147                 Files.write(new File(outDir, "news.html").toPath(),
148                             new HTML(cfg).toHTML(feed.getEntries()).getBytes());
149         }
150 }