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