1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
#include "itemrenderer.h"
#include <set>
#include <sstream>
#include "configcontainer.h"
#include "htmlrenderer.h"
#include "logger.h"
#include "rssfeed.h"
#include "strprintf.h"
#include "textformatter.h"
#include "utils.h"
namespace newsboat {
bool should_render_as_html(const std::string mime_type)
{
static const std::set<std::string> html_mime_types = {
"", // Render as HTML when no mime-type is provided
"html",
"xhtml",
"text/html",
"application/xhtml+xml"
};
return (html_mime_types.count(mime_type) >= 1);
}
std::string item_renderer::get_feedtitle(std::shared_ptr<RssItem> item)
{
const std::shared_ptr<RssFeed> feedptr = item->get_feedptr();
if (!feedptr) {
return {};
}
std::string feedtitle;
if (!feedptr->title().empty()) {
feedtitle = feedptr->title();
utils::remove_soft_hyphens(feedtitle);
} else if (!feedptr->link().empty()) {
feedtitle = feedptr->link();
} else if (!feedptr->rssurl().empty()) {
feedtitle = feedptr->rssurl();
}
return feedtitle;
}
void prepare_header(
std::shared_ptr<RssItem> item,
std::vector<std::pair<LineType, std::string>>& lines,
Links& /*links*/, bool raw = false)
{
const auto add_line =
[&lines]
(const std::string& value, const std::string& name, LineType lineType) {
if (!value.empty()) {
const auto line = strprintf::fmt("%s%s", name, value);
lines.push_back(std::make_pair(lineType, line));
}
};
const auto stfl_quote_if_needed =
[raw]
(const std::string& str) {
if (raw) {
return str;
} else {
return utils::quote_for_stfl(str);
};
};
const std::string feedtitle = item_renderer::get_feedtitle(item);
add_line(stfl_quote_if_needed(feedtitle), _("Feed: "), LineType::wrappable);
add_line(stfl_quote_if_needed(utils::utf8_to_locale(item->title())),
_("Title: "), LineType::wrappable);
add_line(stfl_quote_if_needed(utils::utf8_to_locale(item->author())),
_("Author: "), LineType::wrappable);
add_line(item->pubDate(), _("Date: "), LineType::wrappable);
add_line(item->link(), _("Link: "), LineType::softwrappable);
add_line(item->flags(), _("Flags: "), LineType::wrappable);
const bool could_be_podcast = item->enclosure_type().empty()
|| utils::is_valid_podcast_type(item->enclosure_type());
if (!item->enclosure_url().empty() && could_be_podcast) {
auto dlurl = strprintf::fmt(
"%s%s",
_("Podcast Download URL: "),
utils::censor_url(item->enclosure_url()));
if (!item->enclosure_type().empty()) {
dlurl.append(
strprintf::fmt(" (%s%s)",
_("type: "),
item->enclosure_type()));
}
lines.push_back(std::make_pair(LineType::softwrappable, dlurl));
}
lines.push_back(std::make_pair(LineType::wrappable, std::string("")));
}
std::string get_item_base_link(const RssItem& item)
{
if (!item.get_base().empty()) {
return item.get_base();
} else if (!item.link().empty()) {
return item.link();
} else {
return item.feedurl();
}
}
void prepare_enclosure(RssItem& item,
std::vector<std::pair<LineType, std::string>>& lines,
Links& links,
bool raw)
{
const auto enclosure_url = item.enclosure_url();
const auto enclosure_type = item.enclosure_type();
const auto enclosure_description = item.enclosure_description();
if (!enclosure_url.empty()
&& !enclosure_type.empty()
&& !utils::is_valid_podcast_type(enclosure_type)) {
const bool is_image = (enclosure_type.find("image/") == 0);
const auto link_type = is_image ? LinkType::IMG : LinkType::HREF;
const std::string fallback_description = is_image ? _("Image") : _("Link");
const std::string description = (enclosure_description.empty() ? fallback_description :
enclosure_description);
const auto link = utils::censor_url(
utils::absolute_url(get_item_base_link(item), enclosure_url));
const auto link_number = links.add_link(link, link_type);
std::string text = "";
if (raw) {
text.append(strprintf::fmt("%s", description));
} else {
text.append(strprintf::fmt("<u>%s</>", utils::quote_for_stfl(description)));
}
text.append(strprintf::fmt("[%d]", link_number));
lines.push_back({LineType::wrappable, text});
lines.push_back({LineType::wrappable, ""});
}
}
void render_html(
ConfigContainer& cfg,
const std::string& source,
std::vector<std::pair<LineType, std::string>>& lines,
Links& thelinks,
const std::string& url,
bool raw)
{
const std::string renderer = cfg.get_configvalue("html-renderer");
if (renderer == "internal") {
HtmlRenderer rnd(raw);
rnd.render(source, lines, thelinks, url);
} else {
const char* argv[4];
argv[0] = "/bin/sh";
argv[1] = "-c";
argv[2] = renderer.c_str();
argv[3] = nullptr;
LOG(Level::DEBUG,
"item_renderer::render_html: source = %s",
source);
LOG(Level::DEBUG,
"item_renderer::render_html: html-renderer = %s",
argv[2]);
const std::string output = utils::run_program(argv, source);
std::istringstream is(output);
std::string line;
while (!is.eof()) {
getline(is, line);
if (!raw) {
line = utils::quote_for_stfl(line);
}
lines.push_back(std::make_pair(LineType::softwrappable, line));
}
}
}
void item_renderer::render_plaintext(
const std::string& source,
std::vector<std::pair<LineType, std::string>>& lines, OutputFormat format)
{
std::string normalized = utils::replace_all(source, "\r\n", "\n");
normalized = utils::replace_all(normalized, "\r", "\n");
std::string::size_type pos = 0;
while (pos < normalized.size()) {
const auto end_of_line = normalized.find_first_of("\n", pos);
const std::string line = normalized.substr(pos, end_of_line - pos);
switch (format) {
case OutputFormat::PlainText:
lines.push_back(std::make_pair(LineType::wrappable, line));
break;
case OutputFormat::StflRichText:
const std::string stfl_quoted_line = utils::quote_for_stfl(line);
lines.push_back(std::make_pair(LineType::wrappable, stfl_quoted_line));
break;
}
if (end_of_line == std::string::npos) {
break;
}
pos = end_of_line + 1;
}
}
void render_links_summary(std::vector<std::pair<LineType, std::string>>& lines,
const Links& links)
{
if (links.size() > 0) {
lines.push_back(std::make_pair(LineType::wrappable, ""));
lines.push_back(std::make_pair(LineType::wrappable, _("Links: ")));
for (unsigned int i = 0; i < links.size(); ++i) {
auto link_text = strprintf::fmt("[%u]: %s (%s)",
i + 1,
links[i].url,
Links::type2str(links[i].type));
lines.push_back(std::make_pair(LineType::softwrappable, link_text));
}
}
}
std::string item_renderer::to_plain_text(
ConfigContainer& cfg,
std::shared_ptr<RssItem> item)
{
std::vector<std::pair<LineType, std::string>> lines;
Links links;
const auto item_description = item->description();
prepare_header(item, lines, links, true);
prepare_enclosure(*item, lines, links, true);
const auto base = get_item_base_link(*item);
const auto body = utils::utf8_to_locale(item_description.text);
if (should_render_as_html(item_description.mime)) {
render_html(cfg, body, lines, links, base, true);
} else {
render_plaintext(body, lines, OutputFormat::PlainText);
}
render_links_summary(lines, links);
TextFormatter txtfmt;
txtfmt.add_lines(lines);
unsigned int width = cfg.get_configvalue_as_int("text-width");
if (width == 0) {
width = 80;
}
return txtfmt.format_text_plain(width);
}
std::pair<std::string, size_t> item_renderer::to_stfl_list(
ConfigContainer& cfg,
std::shared_ptr<RssItem> item,
unsigned int text_width,
unsigned int window_width,
RegexManager* rxman,
const std::string& location,
Links& links)
{
std::vector<std::pair<LineType, std::string>> lines;
const auto item_description = item->description();
prepare_header(item, lines, links);
prepare_enclosure(*item, lines, links, false);
const std::string baseurl = get_item_base_link(*item);
const auto body = utils::utf8_to_locale(item_description.text);
if (should_render_as_html(item_description.mime)) {
render_html(cfg, body, lines, links, baseurl, false);
} else {
render_plaintext(body, lines, OutputFormat::StflRichText);
}
render_links_summary(lines, links);
TextFormatter txtfmt;
txtfmt.add_lines(lines);
return txtfmt.format_text_to_list(rxman, location, text_width, window_width);
}
void render_source(
std::vector<std::pair<LineType, std::string>>& lines,
std::string source)
{
/*
* This function is called instead of HtmlRenderer::render() when the
* user requests to have the source displayed instead of seeing the
* rendered HTML.
*/
std::string line;
do {
std::string::size_type pos = source.find_first_of("\r\n");
line = source.substr(0, pos);
if (pos == std::string::npos) {
source.erase();
} else {
source.erase(0, pos + 1);
}
lines.push_back(std::make_pair(LineType::softwrappable, line));
} while (source.length() > 0);
}
std::pair<std::string, size_t> item_renderer::source_to_stfl_list(
std::shared_ptr<RssItem> item,
unsigned int text_width,
unsigned int window_width,
RegexManager* rxman,
const std::string& location)
{
std::vector<std::pair<LineType, std::string>> lines;
Links links;
prepare_header(item, lines, links);
render_source(lines, utils::quote_for_stfl(utils::utf8_to_locale(
item->description().text)));
TextFormatter txtfmt;
txtfmt.add_lines(lines);
return txtfmt.format_text_to_list(rxman, location, text_width, window_width);
}
}
|