forked from pbek/qmarkdowntextedit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarkdownhighlighter.h
More file actions
137 lines (110 loc) · 3.45 KB
/
markdownhighlighter.h
File metadata and controls
137 lines (110 loc) · 3.45 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
/*
* Copyright (c) 2014-2019 Patrizio Bekerle -- http://www.bekerle.com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* QPlainTextEdit markdown highlighter
*/
#pragma once
#include <QTextCharFormat>
#include <QSyntaxHighlighter>
#include <QRegularExpression>
QT_BEGIN_NAMESPACE
class QTextDocument;
QT_END_NAMESPACE
class MarkdownHighlighter : public QSyntaxHighlighter
{
Q_OBJECT
public:
enum HighlightingOption{
None = 0,
FullyHighlightedBlockQuote = 0x01
};
Q_DECLARE_FLAGS(HighlightingOptions, HighlightingOption)
MarkdownHighlighter(QTextDocument *parent = nullptr,
HighlightingOptions highlightingOptions =
HighlightingOption::None);
// we use some predefined numbers here to be compatible with
// the peg-markdown parser
enum HighlighterState {
NoState = -1,
Link = 0,
Image = 3,
CodeBlock,
Italic = 7,
Bold,
List,
Comment = 11,
H1,
H2,
H3,
H4,
H5,
H6,
BlockQuote,
HorizontalRuler = 21,
Table,
InlineCodeBlock,
MaskedSyntax,
CurrentLineBackgroundColor,
BrokenLink,
// internal
CodeBlockEnd = 100,
HeadlineEnd
};
Q_ENUM(HighlighterState)
// enum BlockState {
// NoBlockState = 0,
// H1,
// H2,
// H3,
// Table,
// CodeBlock,
// CodeBlockEnd
// };
void setTextFormats(QHash<HighlighterState, QTextCharFormat> formats);
void setTextFormat(HighlighterState state, QTextCharFormat format);
void clearDirtyBlocks();
void setHighlightingOptions(HighlightingOptions options);
void initHighlightingRules();
signals:
void highlightingFinished();
protected slots:
void timerTick();
protected:
struct HighlightingRule {
HighlightingRule(const HighlighterState state_) : state(state_) {}
HighlightingRule() = default;
QRegularExpression pattern;
HighlighterState state = NoState;
int capturingGroup = 0;
int maskedGroup = 0;
bool useStateAsCurrentBlockState = false;
bool disableIfCurrentStateIsSet = false;
};
void highlightBlock(const QString &text) Q_DECL_OVERRIDE;
void initTextFormats(int defaultFontSize = 12);
void highlightMarkdown(QString text);
void highlightHeadline(QString text);
void highlightAdditionalRules(QVector<HighlightingRule> &rules,
QString text);
void highlightCodeBlock(QString text);
void highlightCommentBlock(QString text);
void addDirtyBlock(QTextBlock block);
void reHighlightDirtyBlocks();
QVector<HighlightingRule> _highlightingRulesPre;
QVector<HighlightingRule> _highlightingRulesAfter;
QVector<QTextBlock> _dirtyTextBlocks;
QHash<HighlighterState, QTextCharFormat> _formats;
QTimer *_timer;
bool _highlightingFinished;
HighlightingOptions _highlightingOptions;
void setCurrentBlockMargin(HighlighterState state);
};