feb103a45ce7ce7fe6664df60f199ed7707e054b
[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         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                                                  Date 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_FORMATTER.format(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, ParseException {
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, strDate;
156                 Date itemDate;
157                 
158                 inDir = new File(args[0]);
159                 outDir = new File(args[1]);
160                 
161                 cfg = Configuration.load(new File(args[2]));
162                 
163                 adocs = inDir.listFiles();
164
165                 feed = new SyndFeedImpl();
166                 feed.setTitle(cfg.getFeedTitle());
167                 feed.setDescription(cfg.getFeedDescription());
168                 feed.setLink(cfg.getFeedLink());
169                 
170                 entries = new ArrayList<SyndEntry>();
171                 
172                 news = new StringBuffer();
173                 
174                 appendHTMLHead(news, cfg);
175                 
176                 news.append("<body>\n");
177                 
178                 appendHTMLContentHeader(news, cfg.getFeedTitle());
179                 
180                 news.append("<div id='content'>\n");
181                 
182                 for (File adoc: adocs) {
183                         if (!adoc.getName().endsWith(".adoc"))
184                                 continue;                       
185                         desc = new StringWriter();
186                                                 
187                         html = toHTMLFile(outDir, adoc);
188                                 
189                         h = asciidoctor.readDocumentHeader(adoc);
190
191                         if (h.getAttributes().get("date") == null)
192                                 strDate = h.getAttributes().get("docdate").toString();
193                         else
194                                 strDate = h.getAttributes().get("date").toString();
195                                                 
196                         itemDate = DATE_FORMATTER.parse(strDate);
197                         
198                         asciidoctor.convert(new FileReader(adoc), desc, new HashMap<String,Object>());
199                         
200                         itemTitle = h.getDocumentTitle().getMain(); 
201                         itemContent = desc.toString();
202                         
203                         e = new SyndEntryImpl();
204                         e.setTitle(itemTitle);
205                         itemURI = cfg.getFeedBaseURL() + "/" + html.getName(); 
206                         e.setUri(itemURI);
207                         e.setLink(itemURI);
208                         e.setPublishedDate(itemDate);
209                         
210                         c = toSyndContentImpl(itemContent);
211                         
212                         e.setDescription(c);
213                         
214                         entries.add(e);
215
216                         news.append("\n<div>");
217                         news.append("<h2>");
218                         news.append(itemTitle);
219                         news.append("</h2>");
220                         if (news != null) {
221                                 news.append("<div class='date'>");
222                                 news.append(DATE_FORMATTER.format(itemDate));
223                                 news.append("</div>");
224                         }
225                         news.append(desc.toString());
226                         news.append("</div>\n");     
227                         
228                         generateHTMLFileItem(html, itemTitle, itemContent, itemDate, cfg);
229                 }
230                 
231                 news.append("</div>\n");
232                 
233                 appendHTMLFooter(news);
234                 
235                 feed.setEntries(entries);
236                 
237                 feed.setFeedType("rss_2.0");                    
238                 SyndFeedOutput output = new SyndFeedOutput();
239                 output.output(feed, new File(outDir, "feed.xml"));
240         
241                 Files.write(new File(outDir, "news.html").toPath(), news.toString().getBytes());
242         }
243 }