forked from newsboat/newsboat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
regexmanager.cpp
371 lines (338 loc) · 9.08 KB
/
regexmanager.cpp
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#include "regexmanager.h"
#include <cstring>
#include <iostream>
#include <stack>
#include "config.h"
#include "confighandlerexception.h"
#include "logger.h"
#include "strprintf.h"
#include "utils.h"
namespace newsboat {
RegexManager::RegexManager()
{
// this creates the entries in the map. we need them there to have the
// "all" location work.
locations["article"];
locations["articlelist"];
locations["feedlist"];
}
void RegexManager::dump_config(std::vector<std::string>& config_output) const
{
for (const auto& foo : cheat_store_for_dump_config) {
config_output.push_back(foo);
}
}
void RegexManager::handle_action(const std::string& action,
const std::vector<std::string>& params)
{
if (action == "highlight") {
handle_highlight_action(params);
} else if (action == "highlight-article") {
handle_highlight_article_action(params);
} else {
throw ConfigHandlerException(
ActionHandlerStatus::INVALID_COMMAND);
}
}
int RegexManager::article_matches(Matchable* item)
{
for (const auto& Matcher : matchers) {
if (Matcher.first->matches(item)) {
return Matcher.second;
}
}
return -1;
}
void RegexManager::remove_last_regex(const std::string& location)
{
auto& regexes = locations[location];
if (regexes.empty()) {
return;
}
regexes.pop_back();
}
std::map<size_t, std::string> RegexManager::extract_style_tags(std::string& str)
{
std::map<size_t, std::string> tags;
size_t pos = 0;
while (pos < str.size()) {
auto tag_start = str.find_first_of("<>", pos);
if (tag_start == std::string::npos) {
break;
}
if (str[tag_start] == '>') {
// Keep unmatched '>' (stfl way of encoding a literal '>')
pos = tag_start + 1;
continue;
}
auto tag_end = str.find_first_of("<>", tag_start + 1);
if (tag_end == std::string::npos) {
break;
}
if (str[tag_end] == '<') {
// First '<' bracket is unmatched, ignoring it
pos = tag_start + 1;
continue;
}
if (tag_end - tag_start == 1) {
// Convert "<>" into "<" (stfl way of encoding a literal '<')
str.erase(tag_end, 1);
pos = tag_start + 1;
continue;
}
tags[tag_start] = str.substr(tag_start, tag_end - tag_start + 1);
str.erase(tag_start, tag_end - tag_start + 1);
pos = tag_start;
}
return tags;
}
void RegexManager::insert_style_tags(std::string& str,
std::map<size_t, std::string>& tags)
{
// Expand "<" into "<>" (reverse of what happened in extract_style_tags()
size_t pos = 0;
while (pos < str.size()) {
auto bracket = str.find_first_of("<", pos);
if (bracket == std::string::npos) {
break;
}
pos = bracket + 1;
// Add to strings in the `tags` map so we don't have to shift all the positions in that map
// (would be necessary if inserting directly into `str`
tags[pos] = ">" + tags[pos];
}
for (auto it = tags.rbegin(); it != tags.rend(); ++it) {
if (it->first > str.length()) {
// Ignore tags outside of string
continue;
}
str.insert(it->first, it->second);
}
}
void RegexManager::merge_style_tag(std::map<size_t, std::string>& tags,
const std::string& tag, size_t start, size_t end)
{
if (end <= start) {
return;
}
// Find the latest tag occurring before `end`.
// It is important that looping executes in ascending order of location.
std::string latest_tag = "</>";
for (const auto& location_tag : tags) {
size_t location = location_tag.first;
if (location > end) {
break;
}
latest_tag = location_tag.second;
}
tags[start] = tag;
tags[end] = latest_tag;
// Remove any old tags between the start and end marker
for (auto it = tags.begin(); it != tags.end(); ) {
if (it->first > start && it->first < end) {
it = tags.erase(it);
} else {
++it;
}
}
}
void RegexManager::quote_and_highlight(std::string& str,
const std::string& location)
{
auto& regexes = locations[location];
auto tag_locations = extract_style_tags(str);
for (unsigned int i = 0; i < regexes.size(); ++i) {
const auto& regex = regexes[i].first;
if (regex == nullptr) {
continue;
}
unsigned int offset = 0;
int eflags = 0;
while (offset < str.length()) {
const auto matches = regex->matches(str.substr(offset), 1, eflags);
eflags |= REG_NOTBOL; // Don't match beginning-of-line operator (^) in following checks
if (matches.empty()) {
break;
}
const auto& match = matches[0];
if (match.first != match.second) {
const std::string marker = strprintf::fmt("<%u>", i);
const int match_start = offset + match.first;
const int match_end = offset + match.second;
merge_style_tag(tag_locations, marker, match_start, match_end);
offset = match_end;
} else {
offset++;
}
}
}
insert_style_tags(str, tag_locations);
}
void RegexManager::handle_highlight_action(const std::vector<std::string>&
params)
{
if (params.size() < 3) {
throw ConfigHandlerException(
ActionHandlerStatus::TOO_FEW_PARAMS);
}
std::string location = params[0];
if (location != "all" && location != "article" &&
location != "articlelist" && location != "feedlist") {
throw ConfigHandlerException(strprintf::fmt(
_("`%s' is an invalid dialog type"), location));
}
std::string errorMessage;
auto regex = Regex::compile(params[1], REG_EXTENDED | REG_ICASE, errorMessage);
if (regex == nullptr) {
throw ConfigHandlerException(strprintf::fmt(
_("`%s' is not a valid regular expression: %s"),
params[1],
errorMessage));
}
std::string colorstr;
if (params[2] != "default") {
colorstr.append("fg=");
if (!utils::is_valid_color(params[2])) {
throw ConfigHandlerException(strprintf::fmt(
_("`%s' is not a valid color"),
params[2]));
}
colorstr.append(params[2]);
}
if (params.size() > 3) {
if (params[3] != "default") {
if (colorstr.length() > 0) {
colorstr.append(",");
}
colorstr.append("bg=");
if (!utils::is_valid_color(params[3])) {
throw ConfigHandlerException(
strprintf::fmt(
_("`%s' is not a valid "
"color"),
params[3]));
}
colorstr.append(params[3]);
}
for (unsigned int i = 4; i < params.size(); ++i) {
if (params[i] != "default") {
if (!colorstr.empty()) {
colorstr.append(",");
}
colorstr.append("attr=");
if (!utils::is_valid_attribute(
params[i])) {
throw ConfigHandlerException(
strprintf::fmt(
_("`%s' is not "
"a valid "
"attribute"),
params[i]));
}
colorstr.append(params[i]);
}
}
}
if (location != "all") {
LOG(Level::DEBUG,
"RegexManager::handle_action: adding rx = %s "
"colorstr = %s to location %s",
params[1],
colorstr,
location);
locations[location].push_back({std::move(regex), colorstr});
} else {
std::shared_ptr<Regex> sharedRegex(std::move(regex));
for (auto& location : locations) {
LOG(Level::DEBUG,
"RegexManager::handle_action: adding "
"rx = "
"%s colorstr = %s to location %s",
params[1],
colorstr,
location.first);
location.second.push_back({sharedRegex, colorstr});
}
}
std::string line = "highlight";
for (const auto& param : params) {
line.append(" ");
line.append(utils::quote(param));
}
cheat_store_for_dump_config.push_back(line);
}
void RegexManager::handle_highlight_article_action(const
std::vector<std::string>& params)
{
if (params.size() < 3) {
throw ConfigHandlerException(
ActionHandlerStatus::TOO_FEW_PARAMS);
}
std::string expr = params[0];
std::string fgcolor = params[1];
std::string bgcolor = params[2];
std::string colorstr;
if (fgcolor != "default") {
colorstr.append("fg=");
if (!utils::is_valid_color(fgcolor)) {
throw ConfigHandlerException(strprintf::fmt(
_("`%s' is not a valid color"),
fgcolor));
}
colorstr.append(fgcolor);
}
if (bgcolor != "default") {
if (!colorstr.empty()) {
colorstr.append(",");
}
colorstr.append("bg=");
if (!utils::is_valid_color(bgcolor)) {
throw ConfigHandlerException(strprintf::fmt(
_("`%s' is not a valid color"),
bgcolor));
}
colorstr.append(bgcolor);
}
for (unsigned int i = 3; i < params.size(); i++) {
if (params[i] != "default") {
if (!colorstr.empty()) {
colorstr.append(",");
}
colorstr.append("attr=");
if (!utils::is_valid_attribute(params[i])) {
throw ConfigHandlerException(
strprintf::fmt(
_("`%s' is not a valid "
"attribute"),
params[i]));
}
colorstr.append(params[i]);
}
}
std::shared_ptr<Matcher> m(new Matcher());
if (!m->parse(params[0])) {
throw ConfigHandlerException(strprintf::fmt(
_("couldn't parse filter expression `%s': %s"),
params[0],
m->get_parse_error()));
}
int pos = locations["articlelist"].size();
locations["articlelist"].push_back({nullptr, colorstr});
matchers.push_back(
std::pair<std::shared_ptr<Matcher>, int>(m, pos));
}
std::string RegexManager::get_attrs_stfl_string(const std::string& location,
bool hasFocus)
{
const auto& attributes = locations[location];
std::string attrstr;
for (unsigned int i = 0; i < attributes.size(); ++i) {
const std::string& attribute = attributes[i].second;
attrstr.append(strprintf::fmt("@style_%u_normal:%s ", i, attribute));
if (hasFocus) {
attrstr.append(strprintf::fmt("@style_%u_focus:%s ", i, attribute));
}
}
return attrstr;
}
} // namespace newsboat