improved structure
[asciidoctor_to_rss.git] / src / main / java / net / wpitchoune / asciidoctor / HTML.java
index 0b79da1..8c3bc0b 100644 (file)
@@ -42,7 +42,7 @@ public final class HTML {
                this.config = config;
        }
        
-       public static void appendHTMLHead(StringBuffer sb, Configuration config)
+       public static void appendHTMLHead(StringBuffer sb, String title, Configuration config)
                        throws IOException {
                File f;
                
@@ -57,11 +57,14 @@ public final class HTML {
                sb.append("<head>\n");
                sb.append(new String(Files.readAllBytes(f.toPath()),
                                     StandardCharsets.UTF_8));
+               sb.append("<title>");
+               sb.append(title);
+               sb.append("</title>\n");
                sb.append("</head>\n");
         }
 
-       private void appendHTMLHead(StringBuffer sb) throws IOException {
-               appendHTMLHead(sb, config);
+       private void appendHTMLHead(StringBuffer sb, String title) throws IOException {
+               appendHTMLHead(sb, title, config);
        }
        
        public static void appendHTMLContentHeader(StringBuffer sb, String title) {
@@ -83,7 +86,7 @@ public final class HTML {
                
                buf = new StringBuffer();
                
-               appendHTMLHead(buf);
+               appendHTMLHead(buf, entry.getTitle());
                
                buf.append("<body>\n");
                
@@ -103,6 +106,32 @@ public final class HTML {
                return buf.toString();  
        }
        
+       private static void appendStartTag(StringBuffer buf, String tag, int indent, boolean newline) {
+               while (indent > 0) {
+                       buf.append('\t');
+                       indent--;
+               }
+               buf.append('<');
+               buf.append(tag);
+               buf.append(">");
+               
+               if (newline)
+                       buf.append('\n');
+       }
+
+       private static void appendEndTag(StringBuffer buf, String tag, int indent, boolean newline) {           
+               while (indent > 0) {
+                       buf.append('\t');
+                       indent--;
+               }
+               buf.append("</");
+               buf.append(tag);
+               buf.append(">");
+               
+               if (newline)    
+                       buf.append('\n');
+       }
+       
        public String toHTML(Collection<SyndEntry> entries) throws IOException {
                StringBuffer buf;
                List<SyndEntry> sortedEntries;
@@ -110,38 +139,44 @@ public final class HTML {
                
                buf = new StringBuffer();
                
-               appendHTMLHead(buf);
+               appendHTMLHead(buf, config.getFeedTitle());
                
-               buf.append("<body>\n");
+               appendStartTag(buf, "body", 1, true);
                
                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>");
+                       appendStartTag(buf, "article", 3, true);
+                       appendStartTag(buf, "header", 4, true);
+                       appendStartTag(buf, "h1", 5, false);
+                       buf.append("<a href='" + e.getUri() + "'>");
                        buf.append(e.getTitle());
-                       buf.append("</h2>");
+                       buf.append("</a>");
+                       appendEndTag(buf, "h1", 0, true);
+                       
                        if (e.getPublishedDate() != null) {
-                               buf.append("<div class='date'>");
-                               buf.append(DATE_FORMATTER.format(e.getPublishedDate()));
-                               buf.append("</div>");
+                               buf.append("<div class='date'>");
+                               buf.append(DATE_FORMATTER.format(e.getPublishedDate()));
+                               buf.append("</div>\n");
                        }
+                       
+                       appendEndTag(buf, "header", 4, true);
+                       
                        buf.append(e.getDescription().getValue());
-                       buf.append("</div>\n");   
+                       
+                       appendEndTag(buf, "article", 3, true);
                }
                                
                buf.append("</div>\n");