2017-03-29 10:18:37 +00:00
|
|
|
#include <QtGui>
|
|
|
|
|
2017-10-01 19:09:42 +00:00
|
|
|
#include "utils/MdHighlighter.h"
|
2017-03-29 10:18:37 +00:00
|
|
|
|
|
|
|
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" << "\\*\\*([^\\\\]+)\\*\\*"
|
2017-03-29 10:18:37 +00:00
|
|
|
<< "\\*([^\\\\]+)\\*" << "\\_([^\\\\]+)\\_"
|
|
|
|
<< "\\_\\_([^\\\\]+)\\_\\_";
|
|
|
|
|
2017-04-09 19:55:06 +00:00
|
|
|
foreach (const QString &pattern, keywordPatterns)
|
|
|
|
{
|
2017-03-29 10:18:37 +00:00
|
|
|
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)
|
|
|
|
{
|
2017-04-09 19:55:06 +00:00
|
|
|
foreach (const HighlightingRule &rule, highlightingRules)
|
|
|
|
{
|
2017-03-29 10:18:37 +00:00
|
|
|
QRegExp expression(rule.pattern);
|
|
|
|
int index = expression.indexIn(text);
|
2017-04-09 19:55:06 +00:00
|
|
|
while (index >= 0)
|
|
|
|
{
|
2017-03-29 10:18:37 +00:00
|
|
|
int length = expression.matchedLength();
|
|
|
|
setFormat(index, length, rule.format);
|
|
|
|
index = expression.indexIn(text, index + length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
setCurrentBlockState(0);
|
|
|
|
}
|