3 import java.io.IOException;
4 import java.io.UnsupportedEncodingException;
5 import java.net.URLEncoder;
7 import java.util.logging.Level;
8 import java.util.logging.Logger;
10 import com.rometools.rome.io.FeedException;
13 import pnews.Category;
14 import pnews.EntityStat;
15 import pnews.Language;
18 private static final String CLASS_NAME= HTML.class.getName();
19 private static final Logger LOG = Logger.getLogger(CLASS_NAME);
21 private static void appendA(StringBuffer buf, String child, String href, String cl) {
22 buf.append("<a href='");
26 buf.append(" class='");
35 private static void appendDiv(StringBuffer buf, String child) {
38 buf.append("</div>\n");
41 private static void appendP(StringBuffer buf, String child) {
47 private static void append(StringBuffer buf, Article a) throws UnsupportedEncodingException {
48 buf.append("<div class='article'>\n");
50 buf.append("<div class='article-image'>\n");
51 if (a.thumbnail != null) {
52 buf.append("<img class='left' src='");
53 buf.append(a.thumbnail);
56 buf.append("</div>\n");
58 buf.append("<div class='article-content'>\n");
60 buf.append("<div class='article-title'>\n");
61 appendA(buf, a.title, "/redirect?url=" + URLEncoder.encode(a.link, "UTF-8"), null);
62 buf.append("</div>\n");
64 buf.append("<div class='article-info'>" + a.website + " - " + a.publicationDate + "</div>");
66 buf.append("<div class='article-description'>\n");
67 if (a.description != null) {
69 buf.append(a.description);
72 buf.append("</div>\n");
74 buf.append("</div>\n");
76 buf.append("</div>\n");
79 private static void appendMenu(StringBuffer buf, Category catActive, Config cfg) {
82 buf.append("<nav>\n");
85 for (Category cat: cfg.getCategories()) {
86 if (!cat.getLanguage().equals(catActive.getLanguage()))
91 if (cat.equals(catActive))
96 appendA(buf, cat.getLabel(), cat.getURL(), cl);
100 for (Language l: cfg.getLanguages())
101 buf.append("<li><a href='" + l.toURL() + "'>" + l.getLabel() + "</a></li>");
103 buf.append("</ul>\n");
105 buf.append("</nav>\n");
108 public static String toHTML(List<Article> articles, Category catActive, Config cfg, ArticleProvider provider) {
112 List<EntityStat> entities;
114 buf = new StringBuffer();
115 buf.append("<!DOCTYPE html>\n");
116 buf.append("<html lang='fr'>\n");
117 buf.append("<head>\n");
118 buf.append("<meta charset=\"UTF-8\">\n");
119 buf.append("<link rel='stylesheet' href='/style.css' />\n");
120 buf.append("<title>");
121 buf.append(catActive.getTitle());
122 buf.append(" - PNews</title>\n");
123 buf.append("</head>\n");
124 buf.append("<body>\n");
126 cats = cfg.getCategories();
128 appendMenu(buf, catActive, cfg);
131 entities = provider.getEntityStats(catActive);
133 if (entities.size() > 0) {
134 buf.append("Hot topics: ");
137 for (EntityStat s: entities) {
139 buf.append(s.getEntity());
147 } catch (IllegalArgumentException | FeedException | IOException e2) {
148 LOG.log(Level.SEVERE, "Failed to get entities", e2);
152 for (Article e: articles) {
155 } catch (UnsupportedEncodingException e1) {
156 LOG.log(Level.SEVERE, "Failed to convert article to HTML", e1);
164 buf.append("</body>\n");
165 buf.append("</html>\n");
167 return buf.toString();