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