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
|
#include "itemrenderer.h"
#include <set>
#include <sstream>
#include "configcontainer.h"
#include "htmlrenderer.h"
#include "rssfeed.h"
#include "textformatter.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,
std::vector<LinkPair>& /*links*/, bool raw = false)
{
const auto add_line =
[&lines]
(const std::string& value,
const std::string& name,
LineType lineType = LineType::wrappable) {
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: "));
add_line(stfl_quote_if_needed(utils::utf8_to_locale(item->title())),
_("Title: "));
add_line(stfl_quote_if_needed(utils::utf8_to_locale(item->author())),
_("Author: "));
add_line(item->pubDate(), _("Date: "));
add_line(item->link(), _("Link: "), LineType::softwrappable);
add_line(item->flags(), _("Flags: "));
if (!item->enclosure_url().empty()) {
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 std::shared_ptr<RssItem>& item)
{
if (!item->get_base().empty()) {
return item->get_base();
} else if (!item->link().empty()) {
return item->link();
} else {
return item->feedurl();
}
}
void render_html(
ConfigContainer& cfg,
const std::string& source,
std::vector<std::pair<LineType, std::string>>& lines,
std::vector<LinkPair>& 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;
}
}
std::string item_renderer::to_plain_text(
ConfigContainer& cfg,
std::shared_ptr<RssItem> item)
{
std::vector<std::pair<LineType, std::string>> lines;
std::vector<LinkPair> links;
const auto item_description = item->description();
prepare_header(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);
}
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,
std::vector<LinkPair>& links)
{
std::vector<std::pair<LineType, std::string>> lines;
const auto item_description = item->description();
prepare_header(item, lines, links);
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);
}
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;
std::vector<LinkPair> 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);
}
}
|