sorted news
authorJean-Philippe Orsini <jeanfi@gmail.com>
Mon, 11 Jul 2016 12:54:42 +0000 (14:54 +0200)
committerJean-Philippe Orsini <jeanfi@gmail.com>
Mon, 11 Jul 2016 12:54:42 +0000 (14:54 +0200)
src/main/java/net/wpitchoune/asciidoctor/HTML.java [new file with mode: 0644]
src/main/java/net/wpitchoune/asciidoctor/Main.java

diff --git a/src/main/java/net/wpitchoune/asciidoctor/HTML.java b/src/main/java/net/wpitchoune/asciidoctor/HTML.java
new file mode 100644 (file)
index 0000000..0b79da1
--- /dev/null
@@ -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("<!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();
+       }
+}
index feb103a..d669885 100644 (file)
@@ -23,14 +23,12 @@ import java.io.FileNotFoundException;
 import java.io.FileReader;
 import java.io.IOException;
 import java.io.StringWriter;
-import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.HashMap;
-import java.util.logging.Logger;
 
 import org.asciidoctor.Asciidoctor;
 import org.asciidoctor.Asciidoctor.Factory;
@@ -51,8 +49,6 @@ import com.rometools.rome.io.SyndFeedOutput;
  * @author jeanfi@gmail.com
  */
 public class Main {
-       private static final Logger LOG = Logger.getLogger(Main.class.getSimpleName());
-
        private static final Asciidoctor asciidoctor = Factory.create();
        
         private static final SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat("yyyy-dd-MM");        
@@ -81,66 +77,6 @@ public class Main {
                return ret;
         }
         
-        private static void appendHTMLHead(StringBuffer sb, Configuration cfg)
-                       throws IOException {
-               File f;
-               
-               f = cfg.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 static void appendHTMLFooter(StringBuffer sb) {
-               sb.append("</body>\n");
-               sb.append("</html>");           
-        }
-        
-        private 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>");
-        }               
-        
-        private static void generateHTMLFileItem(File file,
-                                                String title,
-                                                String content,
-                                                Date date,
-                                                Configuration cfg) throws IOException {
-               StringBuffer buf;
-               
-               buf = new StringBuffer();
-               
-               appendHTMLHead(buf, cfg);
-               
-               buf.append("<body>\n");
-               
-               appendHTMLContentHeader(buf, title);
-               
-               buf.append("<div id='content'>\n");
-               if (date != null) {
-                       buf.append("<div class='date'>");
-                       buf.append(DATE_FORMATTER.format(date));
-                       buf.append("</div>");
-               }
-               buf.append(content);
-               buf.append("</div>");
-               
-               appendHTMLFooter(buf);
-               
-               Files.write(file.toPath(), buf.toString().getBytes());
-        }
-        
        public static void main(String[] args) throws FileNotFoundException, IOException, FeedException, ParseException {
                File inDir, html, outDir;
                File[] adocs;
@@ -150,8 +86,6 @@ public class Main {
                ArrayList<SyndEntry> entries;
                SyndEntryImpl e;
                DocumentHeader h;
-               SyndContentImpl c;
-               StringBuffer news;
                String itemTitle, itemContent, itemURI, strDate;
                Date itemDate;
                
@@ -169,16 +103,6 @@ public class Main {
                
                entries = new ArrayList<SyndEntry>();
                
-               news = new StringBuffer();
-               
-               appendHTMLHead(news, cfg);
-               
-               news.append("<body>\n");
-               
-               appendHTMLContentHeader(news, cfg.getFeedTitle());
-               
-               news.append("<div id='content'>\n");
-               
                for (File adoc: adocs) {
                        if (!adoc.getName().endsWith(".adoc"))
                                continue;                       
@@ -207,37 +131,20 @@ public class Main {
                        e.setLink(itemURI);
                        e.setPublishedDate(itemDate);
                        
-                       c = toSyndContentImpl(itemContent);
-                       
-                       e.setDescription(c);
+                       e.setDescription(toSyndContentImpl(itemContent));
                        
                        entries.add(e);
-
-                       news.append("\n<div>");
-                       news.append("<h2>");
-                       news.append(itemTitle);
-                       news.append("</h2>");
-                       if (news != null) {
-                               news.append("<div class='date'>");
-                               news.append(DATE_FORMATTER.format(itemDate));
-                               news.append("</div>");
-                       }
-                       news.append(desc.toString());
-                       news.append("</div>\n");     
                        
-                       generateHTMLFileItem(html, itemTitle, itemContent, itemDate, cfg);
+                       Files.write(html.toPath(), new HTML(cfg).toHTML(e).getBytes());
                }
                
-               news.append("</div>\n");
-               
-               appendHTMLFooter(news);
-               
                feed.setEntries(entries);
                
                feed.setFeedType("rss_2.0");                    
                SyndFeedOutput output = new SyndFeedOutput();
                output.output(feed, new File(outDir, "feed.xml"));
        
-               Files.write(new File(outDir, "news.html").toPath(), news.toString().getBytes());
+               Files.write(new File(outDir, "news.html").toPath(),
+                           new HTML(cfg).toHTML(feed.getEntries()).getBytes());
        }
 }