1 package net.wpitchoune.asciidoctor;
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;
15 import com.rometools.rome.feed.synd.SyndEntry;
18 * Copyright (C) 2016 jeanfi@gmail.com
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.
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.
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
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;
41 public HTML(Configuration config) {
45 public static void appendHTMLHead(StringBuffer sb, String title, Configuration config)
49 f = config.getHTMLHeaderFile();
51 LOG.info("There is no declared HTML header file.");
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));
62 sb.append("</title>\n");
63 sb.append("</head>\n");
66 private void appendHTMLHead(StringBuffer sb, String title) throws IOException {
67 appendHTMLHead(sb, title, config);
70 public static void appendHTMLContentHeader(StringBuffer sb, String title) {
71 sb.append("<div id='header'>\n");
79 public static void appendHTMLFooter(StringBuffer sb) {
80 sb.append("</body>\n");
84 public String toHTML(SyndEntry entry) throws IOException {
87 buf = new StringBuffer();
89 appendHTMLHead(buf, entry.getTitle());
91 buf.append("<body>\n");
93 appendHTMLContentHeader(buf, entry.getTitle());
95 buf.append("<div id='content'>\n");
96 if (entry.getPublishedDate() != null) {
97 buf.append("<div class='date'>");
98 buf.append(DATE_FORMATTER.format(entry.getPublishedDate()));
101 buf.append(entry.getDescription().getValue());
102 buf.append("</div>");
104 appendHTMLFooter(buf);
106 return buf.toString();
109 private static void appendStartTag(StringBuffer buf, String tag, int indent, boolean newline) {
122 private static void appendEndTag(StringBuffer buf, String tag, int indent, boolean newline) {
135 public String toHTML(Collection<SyndEntry> entries) throws IOException {
137 List<SyndEntry> sortedEntries;
138 Comparator<SyndEntry> cmp;
140 buf = new StringBuffer();
142 appendHTMLHead(buf, config.getFeedTitle());
144 appendStartTag(buf, "body", 1, true);
146 appendHTMLContentHeader(buf, config.getFeedTitle());
148 buf.append("<div id='content'>\n");
150 cmp = new Comparator<SyndEntry>() {
152 public int compare(SyndEntry o1, SyndEntry o2) {
153 return o2.getPublishedDate().compareTo(o1.getPublishedDate());
157 sortedEntries = new ArrayList<SyndEntry>(entries);
158 Collections.sort(sortedEntries, cmp);
160 for(SyndEntry e: sortedEntries) {
161 appendStartTag(buf, "article", 3, true);
162 appendStartTag(buf, "header", 4, true);
163 appendStartTag(buf, "h1", 5, false);
164 buf.append("<a href='" + e.getUri() + "'>");
165 buf.append(e.getTitle());
167 appendEndTag(buf, "h1", 0, true);
169 if (e.getPublishedDate() != null) {
170 buf.append("<div class='date'>");
171 buf.append(DATE_FORMATTER.format(e.getPublishedDate()));
172 buf.append("</div>\n");
175 appendEndTag(buf, "header", 4, true);
177 buf.append(e.getDescription().getValue());
179 appendEndTag(buf, "article", 3, true);
182 buf.append("</div>\n");
184 HTML.appendHTMLFooter(buf);
186 return buf.toString();