cutter/src/common/MdHighlighter.cpp

44 lines
1.3 KiB
C++
Raw Normal View History

#include <QtGui>
2018-10-17 07:55:53 +00:00
#include "common/MdHighlighter.h"
MdHighlighter::MdHighlighter(QTextDocument *parent)
: QSyntaxHighlighter(parent)
{
HighlightingRule rule;
keywordFormat.setForeground(Qt::gray);
keywordFormat.setFontWeight(QFont::Bold);
QStringList keywordPatterns;
2017-03-30 02:32:51 +00:00
keywordPatterns << "^\\#{1,6}[ A-Za-z]+\\b" << "\\*\\*([^\\\\]+)\\*\\*"
<< "\\*([^\\\\]+)\\*" << "\\_([^\\\\]+)\\_"
<< "\\_\\_([^\\\\]+)\\_\\_";
for (const QString &pattern : keywordPatterns) {
rule.pattern = QRegExp(pattern);
rule.format = keywordFormat;
highlightingRules.append(rule);
}
singleLineCommentFormat.setFontWeight(QFont::Bold);
singleLineCommentFormat.setForeground(Qt::darkGreen);
rule.pattern = QRegExp(";[^\n]*");
rule.format = singleLineCommentFormat;
highlightingRules.append(rule);
}
void MdHighlighter::highlightBlock(const QString &text)
{
for (const HighlightingRule &rule : highlightingRules) {
QRegExp expression(rule.pattern);
int index = expression.indexIn(text);
2018-03-21 20:32:32 +00:00
while (index >= 0) {
int length = expression.matchedLength();
setFormat(index, length, rule.format);
index = expression.indexIn(text, index + length);
}
}
setCurrentBlockState(0);
}