added date support
[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.charset.StandardCharsets;
27 import java.nio.file.Files;
28 import java.text.ParseException;
29 import java.text.SimpleDateFormat;
30 import java.util.ArrayList;
31 import java.util.Date;
32 import java.util.HashMap;
33 import java.util.logging.Logger;
34
35 import org.asciidoctor.Asciidoctor;
36 import org.asciidoctor.Asciidoctor.Factory;
37 import org.asciidoctor.ast.DocumentHeader;
38
39 import com.rometools.rome.feed.synd.SyndContentImpl;
40 import com.rometools.rome.feed.synd.SyndEntry;
41 import com.rometools.rome.feed.synd.SyndEntryImpl;
42 import com.rometools.rome.feed.synd.SyndFeed;
43 import com.rometools.rome.feed.synd.SyndFeedImpl;
44 import com.rometools.rome.io.FeedException;
45 import com.rometools.rome.io.SyndFeedOutput;
46
47 /**
48  * Command line program which generates a feed (RSS v2) and HTML files 
49  * from a set of asciidoctor documents.
50  * 
51  * @author jeanfi@gmail.com
52  */
53 public class Main {
54         private static final Logger LOG = Logger.getLogger(Main.class.getSimpleName());
55
56         private static final Asciidoctor asciidoctor = Factory.create();
57         
58         private static File toHTMLFile(File dir, File adoc) {
59                 int idx;
60                 String name;
61                 
62                 name = adoc.getName(); 
63                
64                 idx = name.lastIndexOf('.');
65                 
66                 if (idx >= 0)
67                         name = name.substring(0, idx);
68                 
69                 return new File(dir, name + ".html");
70         }
71         
72         private static SyndContentImpl toSyndContentImpl(String description) {
73                 SyndContentImpl ret;
74                 
75                 ret = new SyndContentImpl();
76                 ret.setType("text/html");
77                 ret.setValue(description);
78             
79                 return ret;
80         }
81         
82         private static void appendHTMLHead(StringBuffer sb, Configuration cfg)
83                         throws IOException {
84                 File f;
85                 
86                 f = cfg.getHTMLHeaderFile();
87                 if (f == null) {
88                         LOG.info("There is no declared HTML header file.");
89                         return ;
90                 }
91
92                 sb.append("<!DOCTYPE html>\n");
93                 sb.append("<html>\n");                          
94                 sb.append("<head>\n");
95                 sb.append(new String(Files.readAllBytes(f.toPath()),
96                                      StandardCharsets.UTF_8));
97                 sb.append("</head>\n");
98         }
99         
100         private static void appendHTMLFooter(StringBuffer sb) {
101                 sb.append("</body>\n");
102                 sb.append("</html>");           
103         }
104         
105         private static void appendHTMLContentHeader(StringBuffer sb, String title) {
106                 sb.append("<div id='header'>\n");
107                 sb.append("<h1>");
108                 sb.append(title);
109                 sb.append("</h1>\n");
110                 sb.append("</div>");
111         }               
112         
113         private static void generateHTMLFileItem(File file,
114                                                  String title,
115                                                  String content,
116                                                  String date,
117                                                  Configuration cfg) throws IOException {
118                 StringBuffer buf;
119                 
120                 buf = new StringBuffer();
121                 
122                 appendHTMLHead(buf, cfg);
123                 
124                 buf.append("<body>\n");
125                 
126                 appendHTMLContentHeader(buf, title);
127                 
128                 buf.append("<div id='content'>\n");
129                 if (date != null) {
130                         buf.append("<div class='date'>");
131                         buf.append(date);
132                         buf.append("</div>");
133                 }
134                 buf.append(content);
135                 buf.append("</div>");
136                 
137                 appendHTMLFooter(buf);
138                 
139                 Files.write(file.toPath(), buf.toString().getBytes());
140         }
141         
142         public static void main(String[] args) throws FileNotFoundException, IOException, FeedException, ParseException {
143                 File inDir, html, outDir;
144                 File[] adocs;
145                 StringWriter desc;
146                 SyndFeed feed;
147                 Configuration cfg;
148                 ArrayList<SyndEntry> entries;
149                 SyndEntryImpl e;
150                 DocumentHeader h;
151                 SyndContentImpl c;
152                 StringBuffer news;
153                 String itemTitle, itemContent, itemURI, strDate;
154                 Date itemDate;
155                 SimpleDateFormat sdf;
156                 
157                 sdf = new SimpleDateFormat("yyyy-dd-MM");
158                 
159                 inDir = new File(args[0]);
160                 outDir = new File(args[1]);
161                 
162                 cfg = Configuration.load(new File(args[2]));
163                 
164                 adocs = inDir.listFiles();
165
166                 feed = new SyndFeedImpl();
167                 feed.setTitle(cfg.getFeedTitle());
168                 feed.setDescription(cfg.getFeedDescription());
169                 feed.setLink(cfg.getFeedLink());
170                 
171                 entries = new ArrayList<SyndEntry>();
172                 
173                 news = new StringBuffer();
174                 
175                 appendHTMLHead(news, cfg);
176                 
177                 news.append("<body>\n");
178                 
179                 appendHTMLContentHeader(news, cfg.getFeedTitle());
180                 
181                 news.append("<div id='content'>\n");
182                 
183                 for (File adoc: adocs) {
184                         if (!adoc.getName().endsWith(".adoc"))
185                                 continue;                       
186                         desc = new StringWriter();
187                                                 
188                         html = toHTMLFile(outDir, adoc);
189                                 
190                         h = asciidoctor.readDocumentHeader(adoc);
191
192                         if (h.getAttributes().get("date") == null)
193                                 strDate = h.getAttributes().get("docdate").toString();
194                         else
195                                 strDate = h.getAttributes().get("date").toString();
196                                                 
197                         itemDate = sdf.parse(strDate);
198                         
199                         asciidoctor.convert(new FileReader(adoc), desc, new HashMap<String,Object>());
200                         
201                         itemTitle = h.getDocumentTitle().getMain(); 
202                         itemContent = desc.toString();
203                         
204                         e = new SyndEntryImpl();
205                         e.setTitle(itemTitle);
206                         itemURI = cfg.getFeedBaseURL() + "/" + html.getName(); 
207                         e.setUri(itemURI);
208                         e.setLink(itemURI);
209                         e.setPublishedDate(itemDate);
210                         
211                         c = toSyndContentImpl(itemContent);
212                         
213                         e.setDescription(c);
214                         
215                         entries.add(e);
216
217                         news.append("\n<div>");
218                         news.append("<h2>");
219                         news.append(itemTitle);
220                         news.append("</h2>");
221                         if (news != null) {
222                                 news.append("<div class='date'>");
223                                 news.append(itemDate);
224                                 news.append("</div>");
225                         }
226                         news.append(desc.toString());
227                         news.append("</div>\n");     
228                         
229                         generateHTMLFileItem(html, itemTitle, itemContent, itemDate.toString(), cfg);
230                 }
231                 
232                 news.append("</div>\n");
233                 
234                 appendHTMLFooter(news);
235                 
236                 feed.setEntries(entries);
237                 
238                 feed.setFeedType("rss_2.0");                    
239                 SyndFeedOutput output = new SyndFeedOutput();
240                 output.output(feed, new File(outDir, "feed.xml"));
241         
242                 Files.write(new File(outDir, "news.html").toPath(), news.toString().getBytes());
243         }
244 }