-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDescriptionTextFormatter.cpp
More file actions
642 lines (542 loc) · 14.5 KB
/
Copy pathDescriptionTextFormatter.cpp
File metadata and controls
642 lines (542 loc) · 14.5 KB
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
#include "DescriptionTextFormatter.h"
#include <algorithm>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <Font.h>
#include <InterfaceDefs.h>
#include <string>
#include <TextView.h>
#include <vector>
namespace {
struct StyleState {
bool bold = false;
bool italic = false;
bool underline = false;
bool link = false;
bool operator==(const StyleState& other) const
{
return bold == other.bold && italic == other.italic
&& underline == other.underline && link == other.link;
}
};
struct StyledRun {
int32 offset;
StyleState style;
};
struct OpenLink {
int32 start;
std::string url;
};
struct StyledText {
std::string text;
std::vector<StyledRun> runs;
std::vector<MediaDescriptionLink> links;
};
bool
IsSpace(char character)
{
return character == ' ' || character == '\t' || character == '\n'
|| character == '\r' || character == '\f' || character == '\v';
}
std::string
LowercaseAscii(const std::string& text)
{
std::string result;
result.reserve(text.size());
for (char character : text)
result += (char)tolower((unsigned char)character);
return result;
}
std::string
TrimAscii(const std::string& text)
{
size_t start = 0;
while (start < text.size() && IsSpace(text[start]))
start++;
size_t end = text.size();
while (end > start && IsSpace(text[end - 1]))
end--;
return text.substr(start, end - start);
}
bool
StartsWith(const std::string& text, const char* prefix)
{
size_t index = 0;
while (prefix[index] != '\0') {
if (index >= text.size() || text[index] != prefix[index])
return false;
index++;
}
return true;
}
std::string
TagName(const std::string& tag, bool& closing)
{
std::string lower = LowercaseAscii(TrimAscii(tag));
closing = !lower.empty() && lower[0] == '/';
if (closing)
lower = TrimAscii(lower.substr(1));
size_t space = lower.find(' ');
if (space != std::string::npos)
lower = lower.substr(0, space);
if (!lower.empty() && lower.back() == '/')
lower.resize(lower.size() - 1);
return lower;
}
std::string
TagAttribute(const std::string& tag, const char* attribute)
{
std::string lower = LowercaseAscii(tag);
std::string needle = std::string(attribute) + "=";
size_t pos = lower.find(needle);
if (pos == std::string::npos)
return "";
pos += needle.size();
while (pos < tag.size() && IsSpace(tag[pos]))
pos++;
if (pos >= tag.size())
return "";
char quote = tag[pos];
if (quote == '"' || quote == '\'') {
size_t end = tag.find(quote, pos + 1);
if (end == std::string::npos)
return "";
return tag.substr(pos + 1, end - pos - 1);
}
size_t end = pos;
while (end < tag.size() && !IsSpace(tag[end]) && tag[end] != '>')
end++;
return tag.substr(pos, end - pos);
}
void
AppendUtf8(std::string& output, unsigned long codepoint)
{
if (codepoint <= 0x7f) {
output += (char)codepoint;
return;
}
if (codepoint <= 0x7ff) {
output += (char)(0xc0 | ((codepoint >> 6) & 0x1f));
output += (char)(0x80 | (codepoint & 0x3f));
return;
}
if (codepoint <= 0xffff) {
output += (char)(0xe0 | ((codepoint >> 12) & 0x0f));
output += (char)(0x80 | ((codepoint >> 6) & 0x3f));
output += (char)(0x80 | (codepoint & 0x3f));
return;
}
if (codepoint <= 0x10ffff) {
output += (char)(0xf0 | ((codepoint >> 18) & 0x07));
output += (char)(0x80 | ((codepoint >> 12) & 0x3f));
output += (char)(0x80 | ((codepoint >> 6) & 0x3f));
output += (char)(0x80 | (codepoint & 0x3f));
}
}
void
AppendRunIfNeeded(StyledText& output, const StyleState& style)
{
int32 offset = (int32)output.text.size();
if (!output.runs.empty() && output.runs.back().offset == offset) {
output.runs.back().style = style;
return;
}
if (output.runs.empty() || !(output.runs.back().style == style))
output.runs.push_back({offset, style});
}
StyleState
StyleAt(const StyledText& output, int32 offset)
{
StyleState style;
for (const StyledRun& run : output.runs) {
if (run.offset > offset)
break;
style = run.style;
}
return style;
}
void
AddStyleBoundary(StyledText& output, int32 offset, const StyleState& style)
{
output.runs.push_back({offset, style});
}
bool
OverlapsExistingLink(const StyledText& output, int32 start, int32 end)
{
for (const MediaDescriptionLink& link : output.links) {
if (start < link.end && end > link.start)
return true;
}
return false;
}
bool
IsUrlTail(char character)
{
return character == '.' || character == ',' || character == ';'
|| character == ':' || character == '!' || character == '?'
|| character == ')' || character == ']';
}
void
AddPlainUrlLinks(StyledText& output)
{
for (size_t index = 0; index < output.text.size(); index++) {
bool hasScheme = output.text.compare(index, 7, "http://") == 0
|| output.text.compare(index, 8, "https://") == 0;
bool hasWww = output.text.compare(index, 4, "www.") == 0;
if (!hasScheme && !hasWww)
continue;
size_t end = index;
while (end < output.text.size() && !IsSpace(output.text[end]))
end++;
while (end > index && IsUrlTail(output.text[end - 1]))
end--;
if (end <= index)
continue;
int32 startOffset = (int32)index;
int32 endOffset = (int32)end;
if (OverlapsExistingLink(output, startOffset, endOffset))
continue;
std::string url = output.text.substr(index, end - index);
if (hasWww)
url = "https://" + url;
output.links.push_back({startOffset, endOffset, url});
index = end - 1;
}
}
void
ApplyLinkStyles(StyledText& output)
{
AddPlainUrlLinks(output);
for (const MediaDescriptionLink& link : output.links) {
StyleState normalStyle = StyleAt(output, link.start);
StyleState afterStyle = StyleAt(output, link.end);
StyleState linkStyle = normalStyle;
linkStyle.underline = true;
linkStyle.link = true;
AddStyleBoundary(output, link.start, linkStyle);
AddStyleBoundary(output, link.end, afterStyle);
}
std::stable_sort(output.runs.begin(), output.runs.end(),
[](const StyledRun& left, const StyledRun& right) {
return left.offset < right.offset;
});
std::vector<StyledRun> normalized;
for (const StyledRun& run : output.runs) {
if (!normalized.empty() && normalized.back().offset == run.offset) {
normalized.back().style = run.style;
continue;
}
if (normalized.empty() || !(normalized.back().style == run.style))
normalized.push_back(run);
}
output.runs = normalized;
}
void
AppendNewline(StyledText& output, const StyleState& style,
int desiredCount = 1)
{
while (!output.text.empty() && output.text.back() == ' ')
output.text.pop_back();
int currentCount = 0;
for (size_t index = output.text.size(); index > 0
&& output.text[index - 1] == '\n'; index--) {
currentCount++;
}
while (currentCount < desiredCount && currentCount < 2) {
AppendRunIfNeeded(output, style);
output.text += '\n';
currentCount++;
}
}
void
AppendTextChar(StyledText& output, const StyleState& style, char character)
{
if (character == '\r')
return;
if (character == '\n') {
AppendNewline(output, style);
return;
}
if (character == '\t' || character == '\f' || character == '\v')
character = ' ';
if (character == ' ' && (output.text.empty() || output.text.back() == ' '
|| output.text.back() == '\n')) {
return;
}
AppendRunIfNeeded(output, style);
output.text += character;
}
struct EntityReplacement {
const char* name;
const char* replacement;
bool appendAsTextChar;
};
bool
AppendNamedEntity(const std::string& lower, StyledText& output,
const StyleState& style)
{
static const EntityReplacement kNamedEntities[] = {
{"amp", "&", false},
{"lt", "<", false},
{"gt", ">", false},
{"quot", "\"", false},
{"apos", "'", false},
{"nbsp", " ", true},
{"ndash", "-", false},
{"mdash", "-", false},
{"hellip", "...", false},
};
for (const EntityReplacement& entity : kNamedEntities) {
if (lower != entity.name)
continue;
if (entity.appendAsTextChar)
AppendTextChar(output, style, entity.replacement[0]);
else {
AppendRunIfNeeded(output, style);
output.text += entity.replacement;
}
return true;
}
return false;
}
bool
DecodeNumericEntity(const std::string& lower, unsigned long& codepoint)
{
int base = 10;
const char* digits = nullptr;
if (StartsWith(lower, "#x")) {
base = 16;
digits = lower.c_str() + 2;
} else if (StartsWith(lower, "#"))
digits = lower.c_str() + 1;
else
return false;
char* end = nullptr;
codepoint = strtoul(digits, &end, base);
return end && *end == '\0';
}
bool
AppendDecodedEntity(const std::string& entity, StyledText& output,
const StyleState& style)
{
std::string lower = LowercaseAscii(entity);
if (AppendNamedEntity(lower, output, style))
return true;
unsigned long codepoint = 0;
if (!DecodeNumericEntity(lower, codepoint))
return false;
AppendRunIfNeeded(output, style);
AppendUtf8(output.text, codepoint);
return true;
}
bool
IsHeadingTag(const std::string& lower)
{
return lower.size() == 2 && lower[0] == 'h' && lower[1] >= '1'
&& lower[1] <= '6';
}
void
ApplyLinkTag(const std::string& tag, StyledText& output, StyleState& style,
std::vector<OpenLink>& openLinks, bool closing)
{
style.underline = !closing;
style.link = !closing;
if (!closing) {
std::string href = TagAttribute(tag, "href");
if (!href.empty())
openLinks.push_back({(int32)output.text.size(), href});
return;
}
if (openLinks.empty())
return;
OpenLink link = openLinks.back();
openLinks.pop_back();
int32 end = (int32)output.text.size();
if (end > link.start)
output.links.push_back({link.start, end, link.url});
}
void
ApplyHeadingTag(StyledText& output, StyleState& style, bool closing)
{
style.bold = !closing;
AppendNewline(output, style, 2);
}
void
ApplyListItemTag(StyledText& output, const StyleState& style, bool closing)
{
AppendNewline(output, style);
if (!closing)
output.text += "- ";
}
void
ApplyTag(const std::string& tag, StyledText& output, StyleState& style,
std::vector<OpenLink>& openLinks)
{
bool closing = false;
std::string lower = TagName(tag, closing);
if (lower == "b" || lower == "strong")
style.bold = !closing;
else if (lower == "i" || lower == "em")
style.italic = !closing;
else if (lower == "u")
style.underline = !closing;
else if (lower == "a")
ApplyLinkTag(tag, output, style, openLinks, closing);
else if (IsHeadingTag(lower))
ApplyHeadingTag(output, style, closing);
else if (lower == "br")
AppendNewline(output, style);
else if (lower == "p" || lower == "div")
AppendNewline(output, style, 2);
else if (lower == "li")
ApplyListItemTag(output, style, closing);
}
std::string
TrimFormattedText(const std::string& text)
{
size_t start = 0;
while (start < text.size() && IsSpace(text[start]))
start++;
size_t end = text.size();
while (end > start && IsSpace(text[end - 1]))
end--;
return text.substr(start, end - start);
}
bool
ConsumeTag(const std::string& description, size_t& index, StyledText& output,
StyleState& style, std::vector<OpenLink>& openLinks)
{
if (description[index] != '<')
return false;
size_t close = description.find('>', index + 1);
if (close == std::string::npos)
return false;
ApplyTag(description.substr(index + 1, close - index - 1), output, style,
openLinks);
index = close;
return true;
}
bool
ConsumeEntity(const std::string& description, size_t& index,
StyledText& output, const StyleState& style)
{
if (description[index] != '&')
return false;
size_t close = description.find(';', index + 1);
if (close == std::string::npos || close - index > 16)
return false;
std::string entity = description.substr(index + 1, close - index - 1);
if (!AppendDecodedEntity(entity, output, style))
return false;
index = close;
return true;
}
void
TrimStyledRuns(StyledText& output, size_t leading)
{
std::vector<StyledRun> trimmedRuns;
for (StyledRun& run : output.runs) {
run.offset = std::max<int32>(0, run.offset - (int32)leading);
if (run.offset <= (int32)output.text.size()
&& (trimmedRuns.empty()
|| trimmedRuns.back().offset != run.offset
|| !(trimmedRuns.back().style == run.style))) {
trimmedRuns.push_back(run);
}
}
if (trimmedRuns.empty() || trimmedRuns.front().offset != 0)
trimmedRuns.insert(trimmedRuns.begin(), {0, StyleState()});
output.runs = trimmedRuns;
}
void
TrimLinks(StyledText& output, size_t leading)
{
for (MediaDescriptionLink& link : output.links) {
link.start = std::max<int32>(0, link.start - (int32)leading);
link.end = std::max<int32>(0, link.end - (int32)leading);
}
}
void
TrimParsedDescription(StyledText& output)
{
size_t trimmedSize = TrimFormattedText(output.text).size();
size_t leading = 0;
while (leading < output.text.size() && IsSpace(output.text[leading]))
leading++;
output.text = output.text.substr(leading, trimmedSize);
TrimStyledRuns(output, leading);
TrimLinks(output, leading);
}
StyledText
ParseMediaDescription(const std::string& description)
{
StyledText output;
output.text.reserve(description.size());
StyleState style;
std::vector<OpenLink> openLinks;
output.runs.push_back({0, style});
for (size_t index = 0; index < description.size(); index++) {
if (ConsumeTag(description, index, output, style, openLinks))
continue;
if (ConsumeEntity(description, index, output, style))
continue;
AppendTextChar(output, style, description[index]);
}
TrimParsedDescription(output);
ApplyLinkStyles(output);
return output;
}
}
std::string
FormatMediaDescription(const std::string& description)
{
return ParseMediaDescription(description).text;
}
std::vector<MediaDescriptionLink>
MediaDescriptionLinks(const std::string& description)
{
return ParseMediaDescription(description).links;
}
void
ApplyParsedDescription(BTextView* view, StyledText styled)
{
if (!view)
return;
if (styled.runs.empty())
styled.runs.push_back({0, StyleState()});
size_t bytes = sizeof(text_run_array)
+ (styled.runs.size() - 1) * sizeof(text_run);
text_run_array* runs = (text_run_array*)malloc(bytes);
if (!runs) {
view->SetText(styled.text.c_str());
return;
}
runs->count = (int32)styled.runs.size();
rgb_color color = ui_color(B_PANEL_TEXT_COLOR);
for (size_t index = 0; index < styled.runs.size(); index++) {
uint16 face = 0;
if (styled.runs[index].style.bold)
face |= B_BOLD_FACE;
if (styled.runs[index].style.italic)
face |= B_ITALIC_FACE;
if (styled.runs[index].style.underline)
face |= B_UNDERSCORE_FACE;
if (face == 0)
face = B_REGULAR_FACE;
BFont font(be_plain_font);
font.SetFace(face);
runs->runs[index].offset = styled.runs[index].offset;
runs->runs[index].font = font;
runs->runs[index].color = styled.runs[index].style.link
? (rgb_color){0, 72, 176, 255} : color;
}
view->SetStylable(true);
view->SetText(styled.text.c_str(), runs);
free(runs);
}
void
ApplyMediaDescription(BTextView* view, const std::string& description)
{
ApplyParsedDescription(view, ParseMediaDescription(description));
}