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