sorted news
[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, 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("</head>\n");
61         }
62
63         private void appendHTMLHead(StringBuffer sb) throws IOException {
64                 appendHTMLHead(sb, config);
65         }
66         
67         public static void appendHTMLContentHeader(StringBuffer sb, String title) {
68                 sb.append("<div id='header'>\n");
69                 sb.append("<h1>");
70                 sb.append(title);
71                 sb.append("</h1>\n");
72                 sb.append("</div>");
73         }   
74         
75         
76         public static void appendHTMLFooter(StringBuffer sb) {
77                 sb.append("</body>\n");
78                 sb.append("</html>");           
79         }                     
80         
81         public String toHTML(SyndEntry entry) throws IOException {
82                 StringBuffer buf;
83                 
84                 buf = new StringBuffer();
85                 
86                 appendHTMLHead(buf);
87                 
88                 buf.append("<body>\n");
89                 
90                 appendHTMLContentHeader(buf, entry.getTitle());
91                 
92                 buf.append("<div id='content'>\n");
93                 if (entry.getPublishedDate() != null) {
94                         buf.append("<div class='date'>");
95                         buf.append(DATE_FORMATTER.format(entry.getPublishedDate()));
96                         buf.append("</div>");
97                 }
98                 buf.append(entry.getDescription().getValue());
99                 buf.append("</div>");
100                 
101                 appendHTMLFooter(buf);
102                 
103                 return buf.toString();  
104         }
105         
106         public String toHTML(Collection<SyndEntry> entries) throws IOException {
107                 StringBuffer buf;
108                 List<SyndEntry> sortedEntries;
109                 Comparator<SyndEntry> cmp;
110                 
111                 buf = new StringBuffer();
112                 
113                 appendHTMLHead(buf);
114                 
115                 buf.append("<body>\n");
116                 
117                 appendHTMLContentHeader(buf, config.getFeedTitle());
118                 
119                 buf.append("<div id='content'>\n");
120                 
121                 cmp = new Comparator<SyndEntry>() {
122
123                         @Override
124                         public int compare(SyndEntry o1, SyndEntry o2) {
125                                 return o2.getPublishedDate().compareTo(o1.getPublishedDate());
126                         }
127                         
128                 };
129                 
130                 sortedEntries = new ArrayList<SyndEntry>(entries);
131                 Collections.sort(sortedEntries, cmp);
132                 
133                 for(SyndEntry e: sortedEntries) {
134                         buf.append("\n<div>");
135                         buf.append("<h2>");
136                         buf.append(e.getTitle());
137                         buf.append("</h2>");
138                         if (e.getPublishedDate() != null) {
139                                 buf.append("<div class='date'>");
140                                 buf.append(DATE_FORMATTER.format(e.getPublishedDate()));
141                                 buf.append("</div>");
142                         }
143                         buf.append(e.getDescription().getValue());
144                         buf.append("</div>\n");   
145                 }
146                                 
147                 buf.append("</div>\n");
148                 
149                 HTML.appendHTMLFooter(buf);
150                 
151                 return buf.toString();
152         }
153 }