X-Git-Url: https://git.wpitchoune.net/gitweb/?p=asciidoctor_to_rss.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fwpitchoune%2Fasciidoctor%2FHTML.java;fp=src%2Fmain%2Fjava%2Fnet%2Fwpitchoune%2Fasciidoctor%2FHTML.java;h=0b79da146037f62398526cb0370c291caf1cf3c6;hp=0000000000000000000000000000000000000000;hb=8c8b7a5a3a2272e3856b9e1b306637db28b6a46a;hpb=1dcb4b2f8e2eeb6b48ff80a02ac1df3dbb31ec21 diff --git a/src/main/java/net/wpitchoune/asciidoctor/HTML.java b/src/main/java/net/wpitchoune/asciidoctor/HTML.java new file mode 100644 index 0000000..0b79da1 --- /dev/null +++ b/src/main/java/net/wpitchoune/asciidoctor/HTML.java @@ -0,0 +1,153 @@ +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("\n"); + sb.append("\n"); + sb.append("\n"); + sb.append(new String(Files.readAllBytes(f.toPath()), + StandardCharsets.UTF_8)); + sb.append("\n"); + } + + private void appendHTMLHead(StringBuffer sb) throws IOException { + appendHTMLHead(sb, config); + } + + public static void appendHTMLContentHeader(StringBuffer sb, String title) { + sb.append(""); + } + + + public static void appendHTMLFooter(StringBuffer sb) { + sb.append("\n"); + sb.append(""); + } + + public String toHTML(SyndEntry entry) throws IOException { + StringBuffer buf; + + buf = new StringBuffer(); + + appendHTMLHead(buf); + + buf.append("\n"); + + appendHTMLContentHeader(buf, entry.getTitle()); + + buf.append("
\n"); + if (entry.getPublishedDate() != null) { + buf.append("
"); + buf.append(DATE_FORMATTER.format(entry.getPublishedDate())); + buf.append("
"); + } + buf.append(entry.getDescription().getValue()); + buf.append("
"); + + appendHTMLFooter(buf); + + return buf.toString(); + } + + public String toHTML(Collection entries) throws IOException { + StringBuffer buf; + List sortedEntries; + Comparator cmp; + + buf = new StringBuffer(); + + appendHTMLHead(buf); + + buf.append("\n"); + + appendHTMLContentHeader(buf, config.getFeedTitle()); + + buf.append("
\n"); + + cmp = new Comparator() { + + @Override + public int compare(SyndEntry o1, SyndEntry o2) { + return o2.getPublishedDate().compareTo(o1.getPublishedDate()); + } + + }; + + sortedEntries = new ArrayList(entries); + Collections.sort(sortedEntries, cmp); + + for(SyndEntry e: sortedEntries) { + buf.append("\n
"); + buf.append("

"); + buf.append(e.getTitle()); + buf.append("

"); + if (e.getPublishedDate() != null) { + buf.append("
"); + buf.append(DATE_FORMATTER.format(e.getPublishedDate())); + buf.append("
"); + } + buf.append(e.getDescription().getValue()); + buf.append("
\n"); + } + + buf.append("
\n"); + + HTML.appendHTMLFooter(buf); + + return buf.toString(); + } +}