+package net.wpitchoune.asciidoctor;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+import java.util.logging.Logger;
+
+import com.rometools.rome.feed.synd.SyndEntry;
+
+/*
+ * Copyright (C) 2016 jeanfi@gmail.com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ */
+
+public final class HTML {
+ private static final Logger LOG = Logger.getLogger(Main.class.getSimpleName());
+ private static final SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat("yyyy-dd-MM");
+ private final Configuration config;
+
+ public HTML(Configuration config) {
+ this.config = config;
+ }
+
+ public static void appendHTMLHead(StringBuffer sb, Configuration config)
+ throws IOException {
+ File f;
+
+ f = config.getHTMLHeaderFile();
+ if (f == null) {
+ LOG.info("There is no declared HTML header file.");
+ return ;
+ }
+
+ sb.append("<!DOCTYPE html>\n");
+ sb.append("<html>\n");
+ sb.append("<head>\n");
+ sb.append(new String(Files.readAllBytes(f.toPath()),
+ StandardCharsets.UTF_8));
+ sb.append("</head>\n");
+ }
+
+ private void appendHTMLHead(StringBuffer sb) throws IOException {
+ appendHTMLHead(sb, config);
+ }
+
+ public static void appendHTMLContentHeader(StringBuffer sb, String title) {
+ sb.append("<div id='header'>\n");
+ sb.append("<h1>");
+ sb.append(title);
+ sb.append("</h1>\n");
+ sb.append("</div>");
+ }
+
+
+ public static void appendHTMLFooter(StringBuffer sb) {
+ sb.append("</body>\n");
+ sb.append("</html>");
+ }
+
+ public String toHTML(SyndEntry entry) throws IOException {
+ StringBuffer buf;
+
+ buf = new StringBuffer();
+
+ appendHTMLHead(buf);
+
+ buf.append("<body>\n");
+
+ appendHTMLContentHeader(buf, entry.getTitle());
+
+ buf.append("<div id='content'>\n");
+ if (entry.getPublishedDate() != null) {
+ buf.append("<div class='date'>");
+ buf.append(DATE_FORMATTER.format(entry.getPublishedDate()));
+ buf.append("</div>");
+ }
+ buf.append(entry.getDescription().getValue());
+ buf.append("</div>");
+
+ appendHTMLFooter(buf);
+
+ return buf.toString();
+ }
+
+ public String toHTML(Collection<SyndEntry> entries) throws IOException {
+ StringBuffer buf;
+ List<SyndEntry> sortedEntries;
+ Comparator<SyndEntry> cmp;
+
+ buf = new StringBuffer();
+
+ appendHTMLHead(buf);
+
+ buf.append("<body>\n");
+
+ appendHTMLContentHeader(buf, config.getFeedTitle());
+
+ buf.append("<div id='content'>\n");
+
+ cmp = new Comparator<SyndEntry>() {
+
+ @Override
+ public int compare(SyndEntry o1, SyndEntry o2) {
+ return o2.getPublishedDate().compareTo(o1.getPublishedDate());
+ }
+
+ };
+
+ sortedEntries = new ArrayList<SyndEntry>(entries);
+ Collections.sort(sortedEntries, cmp);
+
+ for(SyndEntry e: sortedEntries) {
+ buf.append("\n<div>");
+ buf.append("<h2>");
+ buf.append(e.getTitle());
+ buf.append("</h2>");
+ if (e.getPublishedDate() != null) {
+ buf.append("<div class='date'>");
+ buf.append(DATE_FORMATTER.format(e.getPublishedDate()));
+ buf.append("</div>");
+ }
+ buf.append(e.getDescription().getValue());
+ buf.append("</div>\n");
+ }
+
+ buf.append("</div>\n");
+
+ HTML.appendHTMLFooter(buf);
+
+ return buf.toString();
+ }
+}