-package pnews;
-
-import java.util.List;
-
-public class HTML {
- private static void appendA(StringBuffer buf, String child, String href, String cl) {
- buf.append("<a href='");
- buf.append(href);
- buf.append("'");
- if (cl != null) {
- buf.append(" class='");
- buf.append(cl);
- buf.append('\'');
- }
- buf.append('>');
- buf.append(child);
- buf.append("</a>");
- }
-
- private static void appendDiv(StringBuffer buf, String child) {
- buf.append("<div>");
- buf.append(child);
- buf.append("</div>\n");
- }
-
- private static void appendP(StringBuffer buf, String child) {
- buf.append("<p>");
- buf.append(child);
- buf.append("</p>\n");
- }
-
- private static void append(StringBuffer buf, Article a) {
- buf.append("<div class='article'>\n");
-
- buf.append("<h2>");
- if (a.thumbnail != null) {
- buf.append("<img class='left' src='");
- buf.append(a.thumbnail);
- buf.append("'/>\n");
- }
- appendA(buf, a.title, "redirect?url="+a.link, null);
- buf.append("</h2>\n");
-
- buf.append("<div class='article-info'>" + a.website + " - " + a.publicationDate + "</div>");
-
- if (a.description != null) {
- buf.append("<p>");
- buf.append(a.description);
- buf.append("</p>");
- }
-
- buf.append("</div>\n");
- }
-
- private static void appendMenu(StringBuffer buf, Category catActive) {
- String cl;
-
- buf.append("<nav>\n");
- buf.append("<ul>\n");
-
- for (Category cat: Category.values()) {
- buf.append("<li>");
-
- if (cat.equals(catActive))
- cl = "active";
- else
- cl = null;
-
- appendA(buf, cat.getId(), cat.getId(), cl);
- buf.append("</li>");
- }
-
- buf.append("</ul>\n");
- buf.append("</nav>\n");
- }
-
- public static String toHTML(List<Article> articles, Category catActive) {
- StringBuffer buf;
-
- buf = new StringBuffer();
- buf.append("<!DOCTYPE html>\n");
- buf.append("<html lang='fr'>\n");
- buf.append("<head>\n");
- buf.append("<meta charset=\"UTF-8\">\n");
- buf.append("<link rel='stylesheet' href='style.css' />\n");
- buf.append("<title>PNews</title>\n");
- buf.append("</head>\n");
- buf.append("<body>\n");
-
- appendMenu(buf, catActive);
-
- for (Article e: articles)
- append(buf, e);
-
- buf.append("</body>\n");
- buf.append("</html>\n");
-
- return buf.toString();
- }
-}