added footer support
[asciidoctor_to_rss.git] / src / main / java / net / wpitchoune / asciidoctor / HTML.java
1 package net.wpitchoune.asciidoctor;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.nio.charset.StandardCharsets;
6 import java.nio.file.Files;
7 import java.text.SimpleDateFormat;
8 import java.util.ArrayList;
9 import java.util.Collection;
10 import java.util.Collections;
11 import java.util.Comparator;
12 import java.util.List;
13 import java.util.logging.Logger;
14
15 import com.rometools.rome.feed.synd.SyndEntry;
16
17 /*
18  * Copyright (C) 2016 jeanfi@gmail.com
19  *
20  * This program is free software; you can redistribute it and/or
21  * modify it under the terms of the GNU General Public License as
22  * published by the Free Software Foundation; either version 2 of the
23  * License, or (at your option) any later version.
24  *
25  * This program is distributed in the hope that it will be useful, but
26  * WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
28  * General Public License for more details.
29  *
30  * You should have received a copy of the GNU General Public License
31  * along with this program; if not, write to the Free Software
32  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
33  * 02110-1301 USA
34  */
35
36 public final class HTML {
37         private static final Logger LOG = Logger.getLogger(Main.class.getSimpleName()); 
38         private static final SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat("yyyy-dd-MM");        
39         private final Configuration config; 
40         
41         public HTML(Configuration config) {
42                 this.config = config;
43         }
44         
45         public static void appendHTMLHead(StringBuffer sb, String title, Configuration config)
46                         throws IOException {
47                 File f;
48                 
49                 f = config.getHTMLHeaderFile();
50                 if (f == null) {
51                         LOG.info("There is no declared HTML header file.");
52                         return ;
53                 }
54
55                 sb.append("<!DOCTYPE html>\n");
56                 sb.append("<html>\n");                          
57                 sb.append("<head>\n");
58                 sb.append(new String(Files.readAllBytes(f.toPath()),
59                                      StandardCharsets.UTF_8));
60                 sb.append("<title>");
61                 sb.append(title);
62                 sb.append("</title>\n");
63                 sb.append("</head>\n");
64         }
65
66         private void appendHTMLHead(StringBuffer sb, String title) throws IOException {
67                 appendHTMLHead(sb, title, config);
68         }
69         
70         public static void appendHTMLContentHeader(StringBuffer sb, String title) {
71                 sb.append("<div id='header'>\n");
72                 sb.append("<h1>");
73                 sb.append(title);
74                 sb.append("</h1>\n");
75                 sb.append("</div>");
76         }   
77         
78         
79         private void appendHTMLFooter(StringBuffer sb) throws IOException {
80                 sb.append(new String(Files.readAllBytes(config.getHTMLFooterFile().toPath()),
81                                      StandardCharsets.UTF_8));
82                 appendEndTag(sb, "body", 1, true);
83                 appendEndTag(sb, "html", 0, false);
84         }                     
85         
86         public String toHTML(SyndEntry entry) throws IOException {
87                 StringBuffer buf;
88                 
89                 buf = new StringBuffer();
90                 
91                 appendHTMLHead(buf, entry.getTitle());
92                 
93                 buf.append("<body>\n");
94                 
95                 appendHTMLContentHeader(buf, entry.getTitle());
96                 
97                 buf.append("<div id='content'>\n");
98                 if (entry.getPublishedDate() != null) {
99                         buf.append("<div class='date'>");
100                         buf.append(DATE_FORMATTER.format(entry.getPublishedDate()));
101                         buf.append("</div>");
102                 }
103                 buf.append(entry.getDescription().getValue());
104                 buf.append("</div>");
105                 
106                 appendHTMLFooter(buf);
107                 
108                 return buf.toString();  
109         }
110         
111         private static void appendStartTag(StringBuffer buf, String tag, int indent, boolean newline) {
112                 while (indent > 0) {
113                         buf.append('\t');
114                         indent--;
115                 }
116                 buf.append('<');
117                 buf.append(tag);
118                 buf.append(">");
119                 
120                 if (newline)
121                         buf.append('\n');
122         }
123
124         private static void appendEndTag(StringBuffer buf, String tag, int indent, boolean newline) {           
125                 while (indent > 0) {
126                         buf.append('\t');
127                         indent--;
128                 }
129                 buf.append("</");
130                 buf.append(tag);
131                 buf.append(">");
132                 
133                 if (newline)    
134                         buf.append('\n');
135         }
136         
137         public String toHTML(Collection<SyndEntry> entries) throws IOException {
138                 StringBuffer buf;
139                 List<SyndEntry> sortedEntries;
140                 Comparator<SyndEntry> cmp;
141                 
142                 buf = new StringBuffer();
143                 
144                 appendHTMLHead(buf, config.getFeedTitle());
145                 
146                 appendStartTag(buf, "body", 1, true);
147                 
148                 appendHTMLContentHeader(buf, config.getFeedTitle());
149                 
150                 buf.append("<div id='content'>\n");
151                 
152                 cmp = new Comparator<SyndEntry>() {
153                         @Override
154                         public int compare(SyndEntry o1, SyndEntry o2) {
155                                 return o2.getPublishedDate().compareTo(o1.getPublishedDate());
156                         }                       
157                 };
158                 
159                 sortedEntries = new ArrayList<SyndEntry>(entries);
160                 Collections.sort(sortedEntries, cmp);
161                 
162                 for(SyndEntry e: sortedEntries) {
163                         appendStartTag(buf, "article", 3, true);
164                         appendStartTag(buf, "header", 4, true);
165                         appendStartTag(buf, "h1", 5, false);
166                         buf.append("<a href='" + e.getUri() + "'>");
167                         buf.append(e.getTitle());
168                         buf.append("</a>");
169                         appendEndTag(buf, "h1", 0, true);
170                         
171                         if (e.getPublishedDate() != null) {
172                                 buf.append("<div class='date'>");
173                                 buf.append(DATE_FORMATTER.format(e.getPublishedDate()));
174                                 buf.append("</div>\n");
175                         }
176                         
177                         appendEndTag(buf, "header", 4, true);
178                         
179                         buf.append(e.getDescription().getValue());
180                         
181                         appendEndTag(buf, "article", 3, true);
182                 }
183                                 
184                 buf.append("</div>\n");
185                 
186                 appendHTMLFooter(buf);
187                 
188                 return buf.toString();
189         }
190 }