b919cf194ab074210800ff318cb5671307f66dc2
[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 entry) {
27                 
28                 buf.append("<section>\n");
29                 buf.append("<h2>");
30                 appendA(buf, entry.title, entry.link);
31                 buf.append("</h2>\n");
32                                 
33                 buf.append("<img class='left' src='");
34                 buf.append(entry.thumbnail);
35                 buf.append("'/>\n");
36                 
37                 buf.append("<p>");
38                 buf.append(entry.description);
39                 buf.append("</p>");
40                 
41                 buf.append("</section>\n");
42                 
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 }