refactoring
[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.IOException;
26 import java.io.InputStream;
27 import java.io.StringWriter;
28 import java.nio.charset.StandardCharsets;
29 import java.nio.file.Files;
30 import java.util.ArrayList;
31 import java.util.HashMap;
32 import java.util.Properties;
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                 
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 appendHTMLContentHeader(StringBuffer sb, String title) {
103                 sb.append("<div id='header'>\n");
104                 sb.append("<h1>");
105                 sb.append(title);
106                 sb.append("</h1>\n");
107                 sb.append("</div>");
108         }               
109         
110         private static void generateHTMLFileItem(String itemTitle, String itemContent) {
111                 
112         }
113         
114         public static void main(String[] args) throws FileNotFoundException, IOException, FeedException {
115                 File inDir, html, outDir;
116                 File[] adocs;
117                 StringWriter desc;
118                 SyndFeed feed;
119                 Configuration cfg;
120                 ArrayList<SyndEntry> entries;
121                 SyndEntryImpl e;
122                 DocumentHeader h;
123                 SyndContentImpl c;
124                 StringBuffer news;
125                 String itemTitle, itemContent;
126                 
127                 inDir = new File(args[0]);
128                 outDir = new File(args[1]);
129                 
130                 cfg = Configuration.load(new File(args[2]));
131                 
132                 adocs = inDir.listFiles();
133
134                 feed = new SyndFeedImpl();
135                 feed.setTitle(cfg.getFeedTitle());
136                 feed.setDescription(cfg.getFeedDescription());
137                 feed.setLink(cfg.getFeedLink());
138                 
139                 entries = new ArrayList<SyndEntry>();
140                 
141                 news = new StringBuffer();
142                 
143                 appendHTMLHead(news, cfg);
144                 
145                 news.append("<body>\n");
146                 
147                 appendHTMLContentHeader(news, cfg.getFeedTitle());
148                 
149                 news.append("<div id='content'>\n");
150                 
151                 for (File adoc: adocs) {
152                         if (!adoc.getName().endsWith(".adoc"))
153                                 continue;                       
154                         desc = new StringWriter();
155                                                 
156                         html = toHTMLFile(outDir, adoc);
157                                 
158                         h = asciidoctor.readDocumentHeader(adoc);
159                                                             
160                         asciidoctor.convert(new FileReader(adoc), desc, new HashMap<String,Object>());
161                         
162                         itemTitle = h.getDocumentTitle().getMain(); 
163                         itemContent = desc.toString();
164                         
165                         e = new SyndEntryImpl();
166                         e.setTitle(itemTitle);
167                         e.setUri(cfg.getFeedBaseURL() + "/" + html.getName());
168                         
169                         c = toSyndContentImpl(itemContent);
170                         
171                         e.setDescription(c);
172                         
173                         entries.add(e);
174
175                         news.append("\n<div>\n");
176                         news.append("<h2>");
177                         news.append(h.getDocumentTitle().getMain());
178                         news.append("</h2>\n");
179                         news.append(desc.toString());
180                         news.append("\n</div>\n");     
181                         
182                         generateHTMLFileItem(itemTitle, itemContent);
183                 }
184                 
185                 news.append("</div>\n");
186                 
187                 news.append("</body>\n");
188                 news.append("</html>\n");
189                 
190                 feed.setEntries(entries);
191                 
192                 feed.setFeedType("rss_2.0");                    
193                 SyndFeedOutput output = new SyndFeedOutput();
194                 output.output(feed, new File(outDir, "feed.xml"));
195         
196                 System.out.println(news.toString());
197                 Files.write(new File(outDir, "news.html").toPath(), news.toString().getBytes());
198         }
199 }