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