-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathmarkdownrenderer.cpp
More file actions
1300 lines (1142 loc) · 44.4 KB
/
Copy pathmarkdownrenderer.cpp
File metadata and controls
1300 lines (1142 loc) · 44.4 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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "markdownrenderer.h"
#include <QAbstractTextDocumentLayout>
#include <QClipboard>
#include <QColor>
#include <QFrame>
#include <QGuiApplication>
#include <QHBoxLayout>
#include <QIcon>
#include <QMouseEvent>
#include <QPaintEvent>
#include <QPainter>
#include <QPalette>
#include <QResizeEvent>
#include <QScrollBar>
#include <QToolButton>
#include <QToolTip>
#include "llamasyntaxhighlighter.h"
#include "llamatr.h"
using namespace LlamaCpp;
static QString colorToRgba(const QColor &c)
{
return QString("rgba(%1,%2,%3,%4)").arg(c.red()).arg(c.green()).arg(c.blue()).arg(c.alpha());
}
static QString fromStdString(const std::pmr::string &s)
{
return QString::fromUtf8(s.data(), s.size());
}
MarkdownRenderer::MarkdownRenderer(QWidget *parent)
: QTextBrowser(parent)
{
setReadOnly(true);
// default palette
m_colorMap[TextForeground] = QColor(0x1F2328);
m_colorMap[BlockquoteLine] = QColor(0xd0d7de);
m_colorMap[BlockquoteText] = QColor(0x656d76);
m_colorMap[HorizontalRuler] = QColor(0xd0d7de);
m_colorMap[TableBorder] = QColor(0xd0d7de);
m_colorMap[TableOddRow] = QColor(0xf6f8fa);
m_colorMap[TableEvenRow] = QColor(0xffffff);
m_colorMap[CodeBlockBackground] = QColor(0xf6f8fa);
m_colorMap[CodeBlockBorder] = m_colorMap[TableBorder];
m_colorMap[InlineCodeBackground] = QColor(0xf6f8fa);
m_colorMap[Link] = QColor(0x0969da);
m_colorMap[OverlayBackground] = QColor(255, 255, 255, 0);
m_colorMap[OverlayButtonBackground] = QColor(240, 240, 240, 200);
m_colorMap[OverlayButtonBackgroundHover] = QColor(220, 220, 220, 200);
m_colorMap[OverlayButtonBorder] = m_colorMap[TableBorder];
m_baseFont = QFont("SF Pro", 14);
setFont(m_baseFont);
m_monoFont = QFont("SF Mono");
QPalette p = palette();
p.setColor(QPalette::Text, color(TextForeground));
setPalette(p);
m_doc = new QTextDocument(this);
m_doc->setIndentWidth(30);
setDocument(m_doc);
setupDocumentSettings();
m_cursor = QTextCursor(m_doc);
// Markus streaming parser setup (GitHub-flavored Markdown)
m_options.enable_tables = true;
m_options.enable_autolink = true;
m_options.enable_strikethrough = true;
m_options.enable_tasklist = true;
m_streamParser.SetOptions(m_options);
m_streamParser.setBlockCallback(
[this](const markus::Document &doc, size_t first, size_t last) {
renderBlocks(doc, first, last);
});
connect(verticalScrollBar(),
&QScrollBar::valueChanged,
this,
&MarkdownRenderer::updateAllOverlaysGeometry);
connect(horizontalScrollBar(),
&QScrollBar::valueChanged,
this,
&MarkdownRenderer::updateAllOverlaysGeometry);
}
MarkdownRenderer::~MarkdownRenderer()
{
qDeleteAll(m_codeOverlays);
}
void MarkdownRenderer::feed(const QByteArray &buffer)
{
QByteArray delta = buffer;
if (buffer.startsWith(m_buffer)) {
delta = buffer.mid(m_buffer.size());
} else {
// The buffer diverged (e.g. a thinking section was rewritten):
// start over and re-feed everything.
reset();
delta = buffer;
}
m_buffer = buffer;
m_cursor.beginEditBlock();
m_streamParser.Feed(std::string_view(delta.constData(), delta.size()));
renderPendingTail();
m_cursor.endEditBlock();
updateAllOverlaysGeometry();
}
void MarkdownRenderer::finish()
{
m_cursor.beginEditBlock();
m_streamParser.Flush();
renderPendingTail();
m_cursor.endEditBlock();
updateAllOverlaysGeometry();
}
void MarkdownRenderer::reset()
{
m_streamParser.Reset();
m_buffer.clear();
qDeleteAll(m_codeOverlays);
m_codeOverlays.clear();
m_toggleDetails.clear();
m_listStack.clear();
m_tableStack.clear();
m_textCharFormatStack.clear();
m_blockQuoteDepth = 0;
m_headingLevel = 0;
m_codeBlock = false;
m_codeBlockLanguage.clear();
m_codeFenceChar = QChar::Null;
m_skipNextParagraphBlock = false;
m_tailStart = -1;
if (m_doc) {
m_doc->clear();
m_cursor = QTextCursor(m_doc);
}
}
// ---------------------------------------------------------------------------
// AST rendering
// ---------------------------------------------------------------------------
void MarkdownRenderer::renderBlocks(const markus::Document &doc, size_t first, size_t last)
{
clearTailRegion();
for (size_t i = first; i < last; ++i)
renderBlock(doc, doc.children[i]);
// The blocks rendered above are now stable. Move the tail boundary to the
// end so the renderPendingTail() that runs right after this does not treat
// them as the (to-be-replaced) in-progress tail and wipe them out.
m_tailStart = documentEndPosition();
}
void MarkdownRenderer::renderBlock(const markus::Document &doc, const markus::BlockNode &node)
{
std::visit(
[&](const auto &n) {
using T = std::decay_t<decltype(n)>;
if constexpr (std::is_same_v<T, markus::Paragraph>) {
handleParagraph();
renderInlines(doc, n.children);
} else if constexpr (std::is_same_v<T, markus::Heading>) {
handleHeading(n.level);
renderInlines(doc, n.children);
leaveHeading();
} else if constexpr (std::is_same_v<T, markus::ThematicBreak>) {
handleThematicBreak();
} else if constexpr (std::is_same_v<T, markus::CodeBlock>) {
handleCodeBlock(n);
} else if constexpr (std::is_same_v<T, markus::DetailsBlock>) {
renderDetails(doc, n);
} else if constexpr (std::is_same_v<T, markus::HtmlBlock>) {
renderGenericHtmlBlock(n);
} else if constexpr (std::is_same_v<T, markus::BlockQuote>) {
handleBlockQuote();
renderBlockIds(doc, n.children);
leaveBlockQuote();
} else if constexpr (std::is_same_v<T, markus::List>) {
handleList(n);
for (const auto &item : n.items) {
handleItem(item);
renderBlockIds(doc, item.children);
}
leaveList();
} else if constexpr (std::is_same_v<T, markus::ListItem>) {
// Items are rendered as part of their List.
} else if constexpr (std::is_same_v<T, markus::Table>) {
renderTable(doc, n);
}
},
node);
}
void MarkdownRenderer::renderBlockIds(const markus::Document &doc,
const std::pmr::vector<markus::BlockNodeId> &ids)
{
for (markus::BlockNodeId id : ids)
renderBlock(doc, doc.block_nodes[id]);
}
void MarkdownRenderer::renderInlines(const markus::Document &doc,
const std::pmr::vector<markus::InlineNodeId> &ids)
{
for (markus::InlineNodeId id : ids)
renderInline(doc, id);
}
void MarkdownRenderer::renderInline(const markus::Document &doc, markus::InlineNodeId id)
{
const markus::InlineNode &node = doc.inline_nodes[id];
std::visit(
[&](const auto &n) {
using T = std::decay_t<decltype(n)>;
if constexpr (std::is_same_v<T, markus::Text>) {
m_cursor.insertText(QString::fromUtf8(n.content.data(), n.content.size()));
} else if constexpr (std::is_same_v<T, markus::SoftBreak>) {
m_cursor.insertText(QStringLiteral(" "));
} else if constexpr (std::is_same_v<T, markus::HardBreak>) {
m_cursor.insertText(QStringLiteral("\n"));
} else if constexpr (std::is_same_v<T, markus::Code>) {
handleInlineCode();
m_cursor.insertText(fromStdString(n.content));
popCharFormat();
} else if constexpr (std::is_same_v<T, markus::Emphasis>) {
handleEmph();
renderInlines(doc, n.children);
popCharFormat();
} else if constexpr (std::is_same_v<T, markus::Strong>) {
handleStrong();
renderInlines(doc, n.children);
popCharFormat();
} else if constexpr (std::is_same_v<T, markus::Strikethrough>) {
handleStrikethrough();
renderInlines(doc, n.children);
popCharFormat();
} else if constexpr (std::is_same_v<T, markus::Link>) {
handleLink(n);
renderInlines(doc, n.children);
popCharFormat();
} else if constexpr (std::is_same_v<T, markus::Image>) {
renderImage(n);
} else if constexpr (std::is_same_v<T, markus::HtmlInline>) {
m_cursor.insertText(QString::fromUtf8(n.content.data(), n.content.size()));
}
},
node);
}
// ---------------------------------------------------------------------------
// Held-back tail (live preview of the block that may still grow)
// ---------------------------------------------------------------------------
int MarkdownRenderer::documentEndPosition() const
{
if (!m_doc)
return -1;
QTextCursor cursor(m_doc);
cursor.movePosition(QTextCursor::End);
return cursor.position();
}
void MarkdownRenderer::renderPendingTail()
{
clearTailRegion();
// Mark where the in-progress tail starts. This is a plain document
// position (not a QTextCursor), because a cursor left at this spot would be
// pushed forward to the end by the insertions that follow, and the tail
// would then never be cleared.
m_tailStart = documentEndPosition();
const std::string &pending = m_streamParser.pending();
if (pending.empty())
return;
markus::Document tailDoc = markus::Parse(pending, m_options);
for (const markus::BlockNode &block : tailDoc.children)
renderBlock(tailDoc, block);
}
void MarkdownRenderer::clearTailRegion()
{
if (!m_doc || m_tailStart < 0)
return;
QTextCursor cursor(m_doc);
cursor.setPosition(qBound(0, m_tailStart, documentEndPosition()));
cursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
if (cursor.hasSelection())
cursor.removeSelectedText();
pruneStaleCodeBlocks();
}
void MarkdownRenderer::pruneStaleCodeBlocks()
{
for (auto it = m_codeOverlays.begin(); it != m_codeOverlays.end();) {
if (blockForCodeId(it.key()).isValid()) {
++it;
continue;
}
delete it.value();
it = m_codeOverlays.erase(it);
}
}
// ---------------------------------------------------------------------------
// Block-level handlers
// ---------------------------------------------------------------------------
void MarkdownRenderer::beginBlock()
{
QTextCharFormat charFmt;
if (!m_textCharFormatStack.isEmpty())
charFmt = m_textCharFormatStack.top();
QTextBlockFormat blkFmt;
if (!m_listStack.isEmpty())
blkFmt.setIndent(m_listStack.size());
if (m_detailsSecId)
blkFmt.setProperty(DetailsSectionIdProp, m_detailsSecId);
if (m_blockQuoteDepth) {
blkFmt.setProperty(QTextFormat::BlockQuoteLevel, m_blockQuoteDepth);
blkFmt.setLeftMargin(getBlockQuoteMargin(m_blockQuoteDepth, m_paragraphMargin));
}
if (m_codeBlock) {
blkFmt.setProperty(QTextFormat::BlockCodeLanguage, m_codeBlockLanguage);
if (!m_codeFenceChar.isNull()) {
blkFmt.setNonBreakableLines(true);
blkFmt.setProperty(QTextFormat::BlockCodeFence, QString(m_codeFenceChar));
}
charFmt.setFont(m_monoFont);
} else {
blkFmt.setTopMargin(m_paragraphMargin);
blkFmt.setBottomMargin(m_paragraphMargin);
}
if (m_cursor.document()->isEmpty()) {
m_cursor.setBlockFormat(blkFmt);
m_cursor.setCharFormat(charFmt);
} else {
m_cursor.insertBlock(blkFmt, charFmt);
}
}
void MarkdownRenderer::handleHeading(int level)
{
m_headingLevel = level;
beginBlock();
QTextBlockFormat blkFmt = m_cursor.blockFormat();
blkFmt.setHeadingLevel(level);
blkFmt.setTopMargin(24);
blkFmt.setBottomMargin(16);
m_cursor.setBlockFormat(blkFmt);
QTextCharFormat chFmt = m_cursor.charFormat();
static const double mult[6] = {2.0, 1.5, 1.25, 1.0, 0.875, 0.85};
chFmt.setFontPointSize(m_baseFontSize * mult[level - 1]);
chFmt.setFontWeight(QFont::Bold);
// Push the heading's char format so inline markup (e.g. `code`) inherits
// the bold, scaled font instead of restarting from the default format.
m_textCharFormatStack.push(chFmt);
m_cursor.setCharFormat(chFmt);
}
void MarkdownRenderer::leaveHeading()
{
if (!m_textCharFormatStack.isEmpty())
m_textCharFormatStack.pop();
if (!m_textCharFormatStack.isEmpty())
m_cursor.setCharFormat(m_textCharFormatStack.top());
else
m_cursor.setCharFormat(QTextCharFormat());
m_headingLevel = 0;
}
void MarkdownRenderer::handleParagraph()
{
if (m_skipNextParagraphBlock) {
m_skipNextParagraphBlock = false;
return;
}
beginBlock();
QTextBlockFormat blkFmt = m_cursor.blockFormat();
blkFmt.setTopMargin(0);
blkFmt.setBottomMargin(10);
m_cursor.setBlockFormat(blkFmt);
}
void MarkdownRenderer::handleBlockQuote()
{
++m_blockQuoteDepth;
QTextCharFormat fmt = m_textCharFormatStack.isEmpty() ? QTextCharFormat()
: m_textCharFormatStack.top();
fmt.setForeground(color(BlockquoteText));
m_textCharFormatStack.push(fmt);
m_cursor.setCharFormat(fmt);
beginBlock();
QTextBlockFormat blkFmt = m_cursor.blockFormat();
blkFmt.setLeftMargin(m_baseFontSize);
m_cursor.setBlockFormat(blkFmt);
m_skipNextParagraphBlock = true;
}
void MarkdownRenderer::leaveBlockQuote()
{
--m_blockQuoteDepth;
if (!m_textCharFormatStack.isEmpty())
m_textCharFormatStack.pop();
if (!m_textCharFormatStack.isEmpty())
m_cursor.setCharFormat(m_textCharFormatStack.top());
else
m_cursor.setCharFormat(QTextCharFormat());
}
void MarkdownRenderer::handleCodeBlock(const markus::CodeBlock &code)
{
m_codeBlock = true;
m_codeBlockLanguage = languageFromInfoString(code);
m_codeFenceChar = code.fence_char ? QChar::fromLatin1(code.fence_char) : QChar::Null;
beginBlock();
int blockId = ++m_nextCodeBlockId;
QTextBlock first = m_cursor.block();
QTextBlockFormat fmt = first.blockFormat();
fmt.setProperty(BlockCodeIdProp, blockId);
fmt.setLineHeight(70, QTextBlockFormat::ProportionalHeight);
fmt.setAlignment(Qt::AlignVCenter);
// Qt creates one block per code line and inherits this format, so the
// per-line top/bottom margins produce the vertical spacing between lines.
fmt.setTopMargin(m_paragraphMargin);
fmt.setBottomMargin(m_paragraphMargin);
fmt.setProperty(QTextFormat::BlockCodeLanguage, m_codeBlockLanguage);
if (m_blockQuoteDepth > 0) {
fmt.setProperty(QTextFormat::BlockQuoteLevel, m_blockQuoteDepth);
fmt.setLeftMargin(getBlockQuoteMargin(m_blockQuoteDepth, m_paragraphMargin)
+ m_paragraphMargin);
} else {
fmt.setLeftMargin(m_paragraphMargin);
}
if (!m_listStack.isEmpty())
fmt.setIndent(m_listStack.size());
if (!m_codeFenceChar.isNull()) {
fmt.setNonBreakableLines(true);
fmt.setProperty(QTextFormat::BlockCodeFence, QString(m_codeFenceChar));
}
m_cursor.setBlockFormat(fmt);
QTextCharFormat baseCharFmt = m_cursor.charFormat();
baseCharFmt.setFont(m_monoFont, QTextCharFormat::FontPropertiesSpecifiedOnly);
baseCharFmt.setFontFixedPitch(true);
baseCharFmt.setFontPointSize(m_baseFontSize * 0.90);
m_cursor.setCharFormat(baseCharFmt);
const QString content = fromStdString(code.content);
// Invisible 1pt zero-width spacer lines at the top and bottom of the
// block keep the code away from the edges of the rounded background.
// They are part of the code block (same block id) and are stripped from
// copies by collectCodeById().
QTextCharFormat spacerFmt = baseCharFmt;
spacerFmt.setFontPointSize(1);
m_cursor.insertText(ZeroWidthSpace + QLatin1String("\n"), spacerFmt);
QVector<HighlightFragment> fragments;
SyntaxHighlighter highlighter;
highlighter.setDefinition(syntaxDefinitionForName(m_codeBlockLanguage));
highlighter.highlight(content, baseCharFmt, fragments);
if (fragments.isEmpty()) {
m_cursor.insertText(content);
} else {
for (const HighlightFragment &fragment : fragments)
m_cursor.insertText(fragment.text, fragment.format);
}
// Same zero-width space for the bottom.
m_cursor.insertText(ZeroWidthSpace + QLatin1String("\n"), spacerFmt);
m_codeBlock = false;
m_codeBlockLanguage.clear();
m_codeFenceChar = QChar::Null;
createOverlayForCodeBlock(blockId);
}
void MarkdownRenderer::handleThematicBreak()
{
beginBlock();
QTextBlockFormat blkFmt = m_cursor.blockFormat();
blkFmt.setProperty(HorizontalRulerIdProp, 1);
m_cursor.setBlockFormat(blkFmt);
// Insert a zero space character so the block keeps its height.
m_cursor.insertText(ZeroWidthSpace);
}
void MarkdownRenderer::handleList(const markus::List &list)
{
ListState ls;
ls.list = nullptr;
ls.fmt.setIndent(m_listStack.size());
if (!list.is_ordered) {
ls.fmt.setStyle(list.bullet_char == '*' ? QTextListFormat::ListCircle
: (list.bullet_char == '+'
? QTextListFormat::ListSquare
: QTextListFormat::ListDisc));
} else {
ls.fmt.setStyle(m_listStack.isEmpty() ? QTextListFormat::ListDecimal
: QTextListFormat::ListLowerRoman);
ls.fmt.setStart(list.start);
}
m_listStack.append(ls);
}
void MarkdownRenderer::leaveList()
{
if (!m_listStack.isEmpty())
m_listStack.removeLast();
m_skipNextParagraphBlock = false;
}
void MarkdownRenderer::handleItem(const markus::ListItem &item)
{
beginBlock();
m_skipNextParagraphBlock = true;
QTextBlockFormat blkFmt = m_cursor.blockFormat();
blkFmt.setTopMargin(static_cast<int>(m_baseFontSize * 0.25));
blkFmt.setBottomMargin(0);
if (item.is_tasklist)
blkFmt.setMarker(item.tasklist_checked ? QTextBlockFormat::MarkerType::Checked
: QTextBlockFormat::MarkerType::Unchecked);
m_cursor.setBlockFormat(blkFmt);
if (!m_listStack.isEmpty()) {
ListState &ls = m_listStack.last();
if (!ls.list) {
ls.list = m_cursor.createList(ls.fmt);
} else {
if (!m_cursor.document()->isEmpty())
ls.list->add(m_cursor.block());
}
}
}
void MarkdownRenderer::renderDetails(const markus::Document &doc,
const markus::DetailsBlock &details)
{
int secId = ++m_nextDetailsId;
if (!m_toggleDetails.contains(secId))
m_toggleDetails.insert(secId, m_expandDetailsByDefault);
const bool visible = m_toggleDetails.value(secId);
const QString summary = fromStdString(details.summary);
// Summary (toggle) block.
// Note: insertHtml() on an empty block clobbers the block format, so the
// format with the details properties must be applied *after* the insert.
beginBlock();
m_cursor.insertHtml(detailsHtmlLabel(summary, secId, visible));
QTextBlockFormat sumFmt = m_cursor.blockFormat();
sumFmt.setProperty(DetailsSectionIdProp, secId);
sumFmt.setProperty(DetailsToggleBlockProp, true);
sumFmt.setProperty(DetailsSummaryTextProp, summary);
m_cursor.setBlockFormat(sumFmt);
QTextBlock toggleBlock = m_cursor.block();
// Content: markdown blocks that markus parsed from the section body.
// While rendering them, m_detailsSecId makes beginBlock() tag every inner
// block with the section id and the extra quote level indents the content
// (and makes paintEvent() draw the quote line).
const int prevSecId = m_detailsSecId;
m_detailsSecId = secId;
const int prevQuoteDepth = m_blockQuoteDepth;
m_blockQuoteDepth += 1;
QTextCharFormat quoteCharFmt;
quoteCharFmt.setForeground(color(BlockquoteText));
m_textCharFormatStack.push(quoteCharFmt);
renderBlockIds(doc, details.children);
m_textCharFormatStack.pop();
m_blockQuoteDepth = prevQuoteDepth;
m_detailsSecId = prevSecId;
for (QTextBlock blk = toggleBlock.next(); blk.isValid(); blk = blk.next()) {
// Blocks of a nested section carry their own id and were already
// shown/hidden by that section's renderDetails().
if (blk.blockFormat().property(DetailsSectionIdProp).toInt() != secId)
continue;
if (!blk.blockFormat().property(DetailsToggleBlockProp).toBool())
blk.setVisible(visible);
}
}
void MarkdownRenderer::renderGenericHtmlBlock(const markus::HtmlBlock &block)
{
QString text = fromStdString(block.content);
if (text.endsWith('\n'))
text.chop(1);
if (text.trimmed().isEmpty())
return;
beginBlock();
m_cursor.insertText(text);
}
void MarkdownRenderer::renderTable(const markus::Document &doc, const markus::Table &table)
{
const int rows = static_cast<int>(table.rows.size());
const int cols = static_cast<int>(table.alignments.size());
QTextTableFormat tblFmt;
tblFmt.setBorder(1);
tblFmt.setBorderStyle(QTextFrameFormat::BorderStyle_Solid);
tblFmt.setBorderBrush(QBrush(color(TableBorder)));
tblFmt.setCellPadding(6);
tblFmt.setCellSpacing(0);
tblFmt.setTopMargin(10);
tblFmt.setBottomMargin(10);
QTextTable *qtTable = m_cursor.insertTable(rows, cols, tblFmt);
TableState ts;
ts.qtTable = qtTable;
ts.columns = cols;
ts.colAlign.resize(cols);
for (int i = 0; i < cols; ++i) {
switch (table.alignments[i]) {
case markus::TableAlign::kCenter:
ts.colAlign[i] = Qt::AlignHCenter;
break;
case markus::TableAlign::kRight:
ts.colAlign[i] = Qt::AlignRight;
break;
default:
ts.colAlign[i] = Qt::AlignLeft;
break;
}
}
ts.curRow = -1;
ts.curCol = -1;
m_tableStack.append(ts);
for (const auto &row : table.rows) {
TableState &s = m_tableStack.last();
++s.curRow;
s.header = row.is_header;
s.curCol = -1;
for (const auto &cell : row.cells) {
++s.curCol;
QTextTableCell qtCell = s.qtTable->cellAt(s.curRow, s.curCol);
if (!qtCell.isValid())
continue;
QTextCursor cellCursor = qtCell.firstCursorPosition();
QTextBlockFormat blkFmt = cellCursor.blockFormat();
blkFmt.setAlignment(s.colAlign.value(s.curCol, Qt::AlignLeft));
cellCursor.setBlockFormat(blkFmt);
if (s.header) {
QTextCharFormat fmt = cellCursor.charFormat();
fmt.setFontWeight(QFont::Bold);
cellCursor.setCharFormat(fmt);
} else {
QTextCharFormat cellFmt = qtCell.format();
cellFmt.setBackground(s.curRow % 2 == 1 ? color(TableOddRow)
: color(TableEvenRow));
qtCell.setFormat(cellFmt);
}
m_cursor = cellCursor;
renderInlines(doc, cell.children);
}
}
TableState done = m_tableStack.takeLast();
m_cursor = done.qtTable->lastCursorPosition();
m_cursor.movePosition(QTextCursor::End);
}
void MarkdownRenderer::renderImage(const markus::Image &img)
{
QTextImageFormat imgFmt;
imgFmt.setName(fromStdString(img.destination));
if (!img.title.empty())
imgFmt.setToolTip(fromStdString(img.title));
m_cursor.insertImage(imgFmt);
}
// ---------------------------------------------------------------------------
// Inline handlers
// ---------------------------------------------------------------------------
void MarkdownRenderer::handleEmph()
{
QTextCharFormat fmt = m_textCharFormatStack.isEmpty() ? QTextCharFormat()
: m_textCharFormatStack.top();
fmt.setFontItalic(true);
m_textCharFormatStack.push(fmt);
m_cursor.setCharFormat(fmt);
}
void MarkdownRenderer::handleStrong()
{
QTextCharFormat fmt = m_textCharFormatStack.isEmpty() ? QTextCharFormat()
: m_textCharFormatStack.top();
fmt.setFontWeight(QFont::Bold);
m_textCharFormatStack.push(fmt);
m_cursor.setCharFormat(fmt);
}
void MarkdownRenderer::handleStrikethrough()
{
QTextCharFormat fmt = m_textCharFormatStack.isEmpty() ? QTextCharFormat()
: m_textCharFormatStack.top();
fmt.setFontStrikeOut(true);
m_textCharFormatStack.push(fmt);
m_cursor.setCharFormat(fmt);
}
void MarkdownRenderer::handleInlineCode()
{
QTextCharFormat fmt = m_textCharFormatStack.isEmpty() ? QTextCharFormat()
: m_textCharFormatStack.top();
fmt.setFont(m_monoFont, QTextCharFormat::FontPropertiesSpecifiedOnly);
fmt.setFontFixedPitch(true);
// Inside a heading, keep the heading's font size and skip the inline-code
// background so the code stays bold and scales with the heading.
if (m_headingLevel == 0) {
fmt.setBackground(color(InlineCodeBackground));
fmt.setFontPointSize(m_baseFontSize * 0.90);
}
m_textCharFormatStack.push(fmt);
m_cursor.setCharFormat(fmt);
}
void MarkdownRenderer::handleLink(const markus::Link &link)
{
QTextCharFormat fmt = m_textCharFormatStack.isEmpty() ? QTextCharFormat()
: m_textCharFormatStack.top();
fmt.setAnchor(true);
fmt.setAnchorHref(fromStdString(link.destination));
fmt.setForeground(color(Link));
fmt.setFontUnderline(false);
if (!link.title.empty())
fmt.setToolTip(fromStdString(link.title));
m_textCharFormatStack.push(fmt);
m_cursor.setCharFormat(fmt);
}
void MarkdownRenderer::popCharFormat()
{
if (!m_textCharFormatStack.isEmpty())
m_textCharFormatStack.pop();
if (!m_textCharFormatStack.isEmpty())
m_cursor.setCharFormat(m_textCharFormatStack.top());
else
m_cursor.setCharFormat(QTextCharFormat());
}
QString MarkdownRenderer::languageFromInfoString(const markus::CodeBlock &code)
{
const auto &info = code.info_string;
size_t end = info.find_first_of(" \t");
std::string_view lang = (end == std::string_view::npos) ? std::string_view(info)
: std::string_view(info).substr(0, end);
return QString::fromUtf8(lang.data(), lang.size());
}
int MarkdownRenderer::getBlockQuoteMargin(int depth, int paragraphMargin)
{
if (depth <= 0)
return 0;
const int extraPerLevel = 8;
return paragraphMargin + extraPerLevel * (depth - 1);
}
// ---------------------------------------------------------------------------
// Details sections
// ---------------------------------------------------------------------------
void MarkdownRenderer::toggleSection(int secId)
{
bool makeVisible = !m_toggleDetails.value(secId);
for (QTextBlock blk = document()->firstBlock(); blk.isValid(); blk = blk.next()) {
if (blk.blockFormat().property(DetailsSectionIdProp).toInt() == secId) {
if (blk.blockFormat().property(DetailsToggleBlockProp).toBool())
continue;
blk.setVisible(makeVisible);
}
}
for (QTextBlock blk = document()->firstBlock(); blk.isValid(); blk = blk.next()) {
// Find the block that acts as the clickable header for this ID
if (blk.blockFormat().property(DetailsSectionIdProp).toInt() == secId
&& blk.blockFormat().property(DetailsToggleBlockProp).toBool()) {
// Retrieve the original summary text we stored earlier
QString summary = blk.blockFormat().property(DetailsSummaryTextProp).toString();
if (summary.isEmpty())
summary = Tr::tr("Details");
// insertHtml() clobbers the block format, so re-apply it (it
// carries the details section id / toggle properties).
const QTextBlockFormat blkFmt = blk.blockFormat();
QTextCursor cursor(blk);
cursor.movePosition(QTextCursor::StartOfBlock);
cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
cursor.insertHtml(detailsHtmlLabel(summary, secId, makeVisible));
cursor.setBlockFormat(blkFmt);
break;
}
}
m_toggleDetails[secId] = makeVisible;
setupDocumentSettings();
updateAllOverlaysGeometry();
viewport()->update();
document()->setTextWidth(viewport()->width());
updateGeometry();
}
QString MarkdownRenderer::detailsHtmlLabel(const QString &summary, int secId, bool isVisible) const
{
QString icon = isVisible ? "M" : "N";
QString label = QString(
"<a href=\"details-toggle:%1\" style=\"text-decoration:none; color: %2\">"
"%3 <span style=\"font-family: heroicons_outline\">%4</span></a>")
.arg(secId)
.arg(colorToRgba(color(TextForeground)))
.arg(summary)
.arg(icon);
return label;
}
// ---------------------------------------------------------------------------
// Code block overlays
// ---------------------------------------------------------------------------
QPointF MarkdownRenderer::contentOffset() const
{
return QPointF(-horizontalScrollBar()->value(), -verticalScrollBar()->value());
}
QRectF MarkdownRenderer::blockBoundingRect(const QTextBlock &block) const
{
QRectF blockRect = document()->documentLayout()->blockBoundingRect(block);
// When having codeblock in lists, ajust the indent
QVariant idVar = block.blockFormat().property(BlockCodeIdProp);
if (idVar.isValid()) {
qreal dx = block.blockFormat().indent() * document()->indentWidth();
if (block.blockFormat().hasProperty(QTextFormat::BlockQuoteLevel))
dx += m_paragraphMargin;
blockRect.adjust(dx, 0, dx, 0);
}
return blockRect;
}
QTextBlock MarkdownRenderer::blockForCodeId(int id) const
{
for (QTextBlock blk = document()->firstBlock(); blk.isValid(); blk = blk.next()) {
if (blk.blockFormat().property(BlockCodeIdProp).toInt() == id)
return blk;
}
return QTextBlock();
}
void MarkdownRenderer::setupDocumentSettings()
{
if (!m_doc)
return;
m_doc->setDefaultFont(m_baseFont);
m_baseFontSize = m_baseFont.pointSizeF();
m_paragraphMargin = m_baseFontSize * 2 / 3;
if (viewport()->width() > 0)
document()->setTextWidth(viewport()->width());
updateGeometry();
}
void MarkdownRenderer::updateAllOverlaysGeometry()
{
if (m_codeOverlays.isEmpty())
return;
const int margin = m_paragraphMargin / 2;
const int topAdjust = 0;
const QPointF offset = contentOffset();
for (auto it = m_codeOverlays.constBegin(); it != m_codeOverlays.constEnd(); ++it) {
int blockId = it.key();
QFrame *overlay = it.value();
if (!overlay)
continue;
QTextBlock blk = blockForCodeId(blockId);
if (!blk.isValid() || !blk.isVisible()) {
overlay->hide();
continue;
}
QRectF rect = blockBoundingRect(blk);
QTextBlock last = blk;
while (true) {
QTextBlock nxt = last.next();
if (!nxt.isValid())
break;
if (nxt.blockFormat().property(BlockCodeIdProp).toInt() != blockId)
break;
last = nxt;
}
if (last != blk) {
QRectF lastRect = blockBoundingRect(last);
rect.setBottom(lastRect.bottom());
}
QRectF viewRect = rect.translated(offset);
int x = static_cast<int>(viewRect.right() - overlay->width() - margin);
int y = static_cast<int>(viewRect.top() + margin + topAdjust);
overlay->move(x, y);
overlay->show();
}
}
void MarkdownRenderer::createOverlayForCodeBlock(int blockId)
{
if (m_codeOverlays.contains(blockId))
return;
QFrame *overlay = new QFrame(viewport());
overlay->setObjectName(QStringLiteral("CodeOverlay"));
overlay->setAttribute(Qt::WA_TransparentForMouseEvents, false);
overlay->setStyleSheet(QString("QWidget { background: %1; }"
"QToolButton { "
" background: %2; "
" border: 1px solid %3; "
" border-radius: 6px; "
" padding: 4px -2px; "
" font-family: heroicons_outline; "
" font-size: 14px; "
" color: %4; "
"} "
"QToolButton:hover { "
" background-color: %5; "
"}")
.arg(colorToRgba(color(OverlayBackground)))
.arg(colorToRgba(color(OverlayButtonBackground)))
.arg(color(OverlayButtonBorder).name())
.arg(colorToRgba(color(TextForeground)))
.arg(color(OverlayButtonBackgroundHover).name()));
QHBoxLayout *hl = new QHBoxLayout(overlay);
hl->setContentsMargins(0, 0, 5, 0);
hl->setSpacing(10);
QToolButton *copyBtn = new QToolButton(overlay);
copyBtn->setText("E"); // Heroicon character for copy
copyBtn->setToolTip(Tr::tr("Copy the code below to Clipboard"));
hl->addWidget(copyBtn);
QToolButton *saveBtn = new QToolButton(overlay);
saveBtn->setText("F"); // Heroicon character for save
saveBtn->setToolTip(Tr::tr("Save the code below into a file on disk"));
hl->addWidget(saveBtn);
overlay->setLayout(hl);
overlay->hide();
connect(copyBtn, &QToolButton::clicked, this, [this, blockId, copyBtn] {
auto [code, formattedCode] = collectCodeById(blockId);
if (code.isEmpty())
return;
emit copyClicked(code, formattedCode);
const QString tip = Tr::tr("Copied to clipboard");
QPoint globalPos = copyBtn->mapToGlobal(QPoint(0, copyBtn->height()));
QToolTip::showText(globalPos, tip, copyBtn);
});
connect(saveBtn, &QToolButton::clicked, this, [this, blockId] {
auto [code, formattedCode] = collectCodeById(blockId);
if (code.isEmpty())
return;
emit saveClicked(code);
});