sorted by pub date
[pnews.git] / src / main / java / pnews / HTML.java
1 package pnews;
2
3 import java.util.List;
4
5 public class HTML {
6         private static void appendA(StringBuffer buf, String child, String href) {
7                 buf.append("<a href='");
8                 buf.append(href);
9                 buf.append("'>");
10                 buf.append(child);
11                 buf.append("</a>");
12         }
13         
14         private static void appendDiv(StringBuffer buf, String child) {
15                 buf.append("<div>");
16                 buf.append(child);
17                 buf.append("</div>\n");
18         }
19         
20         private static void appendP(StringBuffer buf, String child) {
21                 buf.append("<p>");
22                 buf.append(child);
23                 buf.append("</p>\n");
24         }
25         
26         private static void append(StringBuffer buf, Article a) {               
27                 buf.append("<section>\n");
28                 buf.append("<h2>");
29                 appendA(buf, a.title, a.link);
30                 buf.append("</h2>\n");
31                                 
32                 buf.append("<img class='left' src='");
33                 buf.append(a.thumbnail);
34                 buf.append("'/>\n");
35                 
36                 buf.append("<p>" + a.website + " - " + a.publicationDate + "</p>");
37                 
38                 buf.append("<p>");
39                 buf.append(a.description);
40                 buf.append("</p>");
41                 
42                 buf.append("</section>\n");             
43         }
44         
45         private static void appendMenu(StringBuffer buf) {
46                 buf.append("<nav>\n");
47                 buf.append("<ul>\n");
48
49                 for (Category cat: Category.values()) {
50                         buf.append("<li>");
51                         appendA(buf, cat.getId(), cat.getId() + ".html");
52                         buf.append("</li>");
53                 }
54                 
55                 buf.append("</ul>\n");
56                 buf.append("</nav>\n");
57         }
58         
59         public static String toHTML(List<Article> articles) {
60                 StringBuffer buf;
61                 
62                 buf = new StringBuffer();
63                 buf.append("<!DOCTYPE html>\n");
64                 buf.append("<html lang='fr'>\n");
65                 buf.append("<head>\n");
66                 buf.append("<meta charset=\"UTF-8\">\n");
67                 buf.append("<link rel='stylesheet' href='style.css' />\n");
68                 buf.append("<title>PNews</title>\n");
69                 buf.append("</head>\n");
70                 buf.append("<body>\n");
71                 
72                 appendMenu(buf);
73                 
74                 for (Article e: articles)
75                         append(buf, e);
76                 
77                 buf.append("</body>\n");
78                 buf.append("</html>\n");
79         
80                 return buf.toString();
81         }
82 }