forked from pbek/qmarkdowntextedit
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmarkdownhighlighter.h
307 lines (246 loc) · 8.9 KB
/
markdownhighlighter.h
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
/*
* Copyright (c) 2014-2022 Patrizio Bekerle -- <[email protected]>
*
* 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 <QRegularExpression>
#include <QSyntaxHighlighter>
#include <QTextCharFormat>
QT_BEGIN_NAMESPACE
class QTextDocument;
QT_END_NAMESPACE
class MarkdownHighlighter : public QSyntaxHighlighter {
Q_OBJECT
public:
enum HighlightingOption {
None = 0,
FullyHighlightedBlockQuote = 0x01,
Underline = 0x02
};
Q_DECLARE_FLAGS(HighlightingOptions, HighlightingOption)
MarkdownHighlighter(
QTextDocument *parent = nullptr,
HighlightingOptions highlightingOptions = HighlightingOption::None);
static inline QColor codeBlockBackgroundColor() {
const QBrush brush = _formats[CodeBlock].background();
if (!brush.isOpaque()) {
return QColor(Qt::transparent);
}
return brush.color();
}
static constexpr inline bool isOctal(const char c) {
return (c >= '0' && c <= '7');
}
static constexpr inline bool isHex(const char c) {
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') ||
(c >= 'A' && c <= 'F');
}
static constexpr inline bool isCodeBlock(const int state) {
return state == MarkdownHighlighter::CodeBlock ||
state == MarkdownHighlighter::CodeBlockTilde ||
state == MarkdownHighlighter::CodeBlockComment ||
state == MarkdownHighlighter::CodeBlockTildeComment ||
state >= MarkdownHighlighter::CodeCpp;
}
static constexpr inline bool isCodeBlockEnd(const int state) {
return state == MarkdownHighlighter::CodeBlockEnd ||
state == MarkdownHighlighter::CodeBlockTildeEnd;
}
enum class RangeType {
CodeSpan,
Emphasis
};
QPair<int, int> findPositionInRanges(MarkdownHighlighter::RangeType type, int blockNum, int pos) const;
bool isPosInACodeSpan(int blockNumber, int position) const;
QPair<int, int> getSpanRange(RangeType rangeType, int blockNumber, int position) const;
// we used some predefined numbers here to be compatible with
// the peg-markdown parser
enum HighlighterState {
NoState = -1,
Link = 0,
Image = 3,
CodeBlock,
CodeBlockComment,
Italic = 7,
Bold,
List,
Comment = 11,
H1,
H2,
H3,
H4,
H5,
H6,
BlockQuote,
HorizontalRuler = 21,
Table,
InlineCodeBlock,
MaskedSyntax,
CurrentLineBackgroundColor,
BrokenLink,
FrontmatterBlock,
TrailingSpace,
CheckBoxUnChecked,
CheckBoxChecked,
StUnderline,
// code highlighting
CodeKeyWord = 1000,
CodeString = 1001,
CodeComment = 1002,
CodeType = 1003,
CodeOther = 1004,
CodeNumLiteral = 1005,
CodeBuiltIn = 1006,
// internal
CodeBlockIndented = 96,
CodeBlockTildeEnd = 97,
CodeBlockTilde = 98,
CodeBlockTildeComment,
CodeBlockEnd = 100,
HeadlineEnd,
FrontmatterBlockEnd,
// languages
/*********
* When adding a language make sure that its value is a multiple of 2
* This is because we use the next number as comment for that language
* In case the language doesn't support multiline comments in the
* traditional C++ sense, leave the next value empty. Otherwise mark the
* next value as comment for that language. e.g CodeCpp = 200
* CodeCppComment = 201
*/
CodeCpp = 200,
CodeCppComment = 201,
CodeJs = 202,
CodeJsComment = 203,
CodeC = 204,
CodeCComment = 205,
CodeBash = 206,
CodePHP = 208,
CodePHPComment = 209,
CodeQML = 210,
CodeQMLComment = 211,
CodePython = 212,
CodeRust = 214,
CodeRustComment = 215,
CodeJava = 216,
CodeJavaComment = 217,
CodeCSharp = 218,
CodeCSharpComment = 219,
CodeGo = 220,
CodeGoComment = 221,
CodeV = 222,
CodeVComment = 223,
CodeSQL = 224,
CodeJSON = 226,
CodeXML = 228,
CodeCSS = 230,
CodeCSSComment = 231,
CodeTypeScript = 232,
CodeTypeScriptComment = 233,
CodeYAML = 234,
CodeINI = 236,
CodeTaggerScript = 238,
CodeVex = 240,
CodeVexComment = 241,
CodeCMake = 242,
CodeMake = 244
};
Q_ENUM(HighlighterState)
static void setTextFormats(
QHash<HighlighterState, QTextCharFormat> formats);
static void setTextFormat(HighlighterState state, QTextCharFormat format);
void clearDirtyBlocks();
void setHighlightingOptions(const HighlightingOptions options);
void initHighlightingRules();
Q_SIGNALS:
void highlightingFinished();
protected Q_SLOTS:
void timerTick();
protected:
struct HighlightingRule {
explicit HighlightingRule(const HighlighterState state_)
: state(state_) {}
HighlightingRule() = default;
QRegularExpression pattern;
QString shouldContain;
HighlighterState state = NoState;
uint8_t capturingGroup = 0;
uint8_t maskedGroup = 0;
};
struct InlineRange {
int begin;
int end;
RangeType type;
InlineRange() = default;
InlineRange(int begin_, int end_, RangeType type_) :
begin{begin_}, end{end_}, type{type_}
{}
};
void highlightBlock(const QString &text) override;
static void initTextFormats(int defaultFontSize = 12);
static void initCodeLangs();
void highlightMarkdown(const QString &text);
/******************************
* BLOCK LEVEL FUNCTIONS
******************************/
void highlightHeadline(const QString &text);
void highlightSubHeadline(const QString &text, HighlighterState state);
void highlightAdditionalRules(const QVector<HighlightingRule> &rules,
const QString &text);
void highlightFrontmatterBlock(const QString &text);
void highlightCommentBlock(const QString &text);
void highlightThematicBreak(const QString &text);
void highlightLists(const QString &text);
void highlightCheckbox(const QString &text, int curPos);
/******************************
* INLINE FUNCTIONS
******************************/
void highlightInlineRules(const QString &text);
int highlightInlineSpans(const QString &text,
int currentPos, const QChar c);
void highlightEmAndStrong(const QString &text, const int pos);
Q_REQUIRED_RESULT int highlightInlineComment(const QString &text, int pos);
void setHeadingStyles(MarkdownHighlighter::HighlighterState rule,
const QRegularExpressionMatch &match,
const int capturedGroup);
/******************************
* CODE HIGHLIGHTING FUNCTIONS
******************************/
void highlightIndentedCodeBlock(const QString &text);
void highlightCodeFence(const QString &text);
void highlightCodeBlock(const QString &text,
const QString &opener = QStringLiteral("```"));
void highlightSyntax(const QString &text);
Q_REQUIRED_RESULT int highlightNumericLiterals(const QString &text, int i);
Q_REQUIRED_RESULT int highlightStringLiterals(QChar strType,
const QString &text, int i);
void ymlHighlighter(const QString &text);
void iniHighlighter(const QString &text);
void cssHighlighter(const QString &text);
void xmlHighlighter(const QString &text);
void makeHighlighter(const QString &text);
void taggerScriptHighlighter(const QString &text);
void addDirtyBlock(const QTextBlock &block);
void reHighlightDirtyBlocks();
void clearRangesForBlock(int blockNumber, RangeType type);
bool _highlightingFinished;
HighlightingOptions _highlightingOptions;
QTimer *_timer;
QVector<QTextBlock> _dirtyTextBlocks;
QVector<QPair<int,int>> _linkRanges;
QHash<int, QVector<InlineRange>> _ranges;
static QVector<HighlightingRule> _highlightingRules;
static QHash<HighlighterState, QTextCharFormat> _formats;
static QHash<QString, HighlighterState> _langStringToEnum;
static constexpr int tildeOffset = 300;
};