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;
12 import net.wpitchoune.pnews.Article;
13 import net.wpitchoune.pnews.Category;
14 import net.wpitchoune.pnews.Config;
15 import net.wpitchoune.pnews.EntityStat;
16 import net.wpitchoune.pnews.Language;
19 private static final String CLASS_NAME= HTML.class.getName();
20 private static final Logger LOG = Logger.getLogger(CLASS_NAME);
22 private static void appendA(StringBuffer buf, String child, String href, String cl) {
23 buf.append("<a href='");
27 buf.append(" class='");
36 private static void appendDiv(StringBuffer buf, String child) {
39 buf.append("</div>\n");
42 private static void appendP(StringBuffer buf, String child) {
48 private static void append(StringBuffer buf, Article a) throws UnsupportedEncodingException {
49 buf.append("<div class='article'>\n");
51 buf.append("<div class='article-image'>\n");
52 if (a.getThumbnail() != null) {
53 buf.append("<img class='left' src='");
54 buf.append(a.getThumbnail());
57 buf.append("</div>\n");
59 buf.append("<div class='article-content'>\n");
61 buf.append("<div class='article-title'>\n");
62 appendA(buf, a.getTitle(), "/redirect?url=" + URLEncoder.encode(a.getLink(), "UTF-8"), null);
63 buf.append("</div>\n");
65 buf.append("<div class='article-info'>" + a.getWebsite() + " - " + a.getPublicationDate() + "</div>");
67 buf.append("<div class='article-description'>\n");
68 if (a.getDescription() != null) {
70 if (a.getDescription().length() < 512) {
71 buf.append(a.getDescription());
73 buf.append(a.getDescription().substring(0, 512));
78 buf.append("</div>\n");
80 buf.append("</div>\n");
82 buf.append("</div>\n");
85 private static void appendMenu(StringBuffer buf, Category catActive, Config cfg) {
88 buf.append("<nav>\n");
91 for (Category cat: cfg.getCategories()) {
92 if (!cat.getLanguage().equals(catActive.getLanguage()))
97 if (cat.equals(catActive))
102 appendA(buf, cat.getLabel(), cat.getURL(), cl);
106 for (Language l: cfg.getLanguages())
107 buf.append("<li><a href='" + l.toURL() + "'>" + l.getLabel() + "</a></li>");
109 buf.append("</ul>\n");
111 buf.append("</nav>\n");
114 private static String toURL(Category catActive, String entity) {
116 return catActive.getURL() + "?entity=" + URLEncoder.encode(entity, "UTF-8");
117 } catch (UnsupportedEncodingException e) {
118 LOG.log(Level.SEVERE, "Failed to generate link to entity " + entity, e);
119 return catActive.getURL();
123 public static String toHTML(List<Article> articles, Category catActive, String entityActive, Config cfg, ArticleProvider provider) {
126 List<EntityStat> entities;
129 buf = new StringBuffer();
130 buf.append("<!DOCTYPE html>\n");
131 buf.append("<html lang='fr'>\n");
132 buf.append("<head>\n");
133 buf.append("<meta charset=\"UTF-8\">\n");
134 buf.append("<link rel='stylesheet' href='/style.css' />\n");
135 buf.append("<title>");
136 buf.append(catActive.getTitle());
137 buf.append(" - PNews</title>\n");
138 buf.append("</head>\n");
139 buf.append("<body>\n");
141 appendMenu(buf, catActive, cfg);
144 entities = provider.getEntityStats(catActive);
146 if (entities.size() > 0) {
150 for (EntityStat s: entities) {
152 if (entityActive != null && s.getEntity().equals(entityActive))
156 appendA(buf, s.getEntity(), toURL(catActive, s.getEntity()), cl);
157 buf.append("</li>\n");
162 buf.append("</ul>\n");
163 buf.append("</nav>\n");
165 } catch (IllegalArgumentException | FeedException | IOException e2) {
166 LOG.log(Level.SEVERE, "Failed to get entities", e2);
170 for (Article e: articles) {
173 } catch (UnsupportedEncodingException e1) {
174 LOG.log(Level.SEVERE, "Failed to convert article to HTML", e1);
182 buf.append("</body>\n");
183 buf.append("</html>\n");
185 return buf.toString();