#include "WedgeHighlighter.h"
#include "WedgeHeader.h"
#include <QDateTime>
#include <QRegularExpression>
void WedgeHighlighter::highlightBlock(const QString &text) {
// next time high light
int state = previousBlockState();
if (state == -1) {
state = 0;
}
QDateTime now = QDateTime::currentDateTime();
QRegularExpression headerRegex = WedgeHeader::dateRegex(m_headerFormat, now.date());
QString trimmedText = text.trimmed();
if (headerRegex.match(text).hasMatch()) {
state = 1;
} else if (trimmedText.isEmpty()) {
state = 0;
} else if (state == 1 || state == 2) {
static QRegularExpression timeRegex("^\\s*(\\d{2}):(\\d{2})");
QRegularExpressionMatch timeMatch = timeRegex.match(text);
if (timeMatch.hasMatch()) {
int h = timeMatch.captured(1).toInt();
int m = timeMatch.captured(2).toInt();
int currentMinutes = now.time().hour() * 60 + now.time().minute();
if ((h * 60 + m) > currentMinutes) {
QTextCharFormat highlightFormat;
highlightFormat.setBackground(m_highlightColor);
setFormat(0, text.length(), highlightFormat);
state = 3;
} else {
state = 2;
}
}
} else if (state == 3) {
state = 3;
}
// bold regex
static QRegularExpression boldRegex("^.*!$");
if (boldRegex.match(text).hasMatch()) {
QTextCharFormat format;
format.setFontWeight(QFont::Bold);
format.setForeground(m_focusColor);
setFormat(0, text.length(), format);
}
// italic regex
static QRegularExpression italicRegex("^.*\\?$");
if (italicRegex.match(text).hasMatch()) {
QTextCharFormat format;
format.setFontItalic(true);
setFormat(0, text.length(), format);
}
// under line regex
static QRegularExpression underlineRegex("^\\s*([^\\s].*\\*)$");
QRegularExpressionMatch uMatch = underlineRegex.match(text);
if (uMatch.hasMatch()) {
QTextCharFormat uFormat;
uFormat.setFontUnderline(true);
setFormat(uMatch.capturedStart(1), uMatch.capturedLength(1), uFormat);
}
// strike thru regex
static QRegularExpression strikethruRegex("^\\s*([^\\s].*~)$");
QRegularExpressionMatch sMatch = strikethruRegex.match(text);
if (sMatch.hasMatch()) {
QTextCharFormat sFormat;
sFormat.setFontStrikeOut(true);
setFormat(sMatch.capturedStart(1), sMatch.capturedLength(1), sFormat);
}
// comment fade regex
static QRegularExpression commentRegex("//.*$");
QRegularExpressionMatchIterator ci = commentRegex.globalMatch(text);
while (ci.hasNext()) {
QRegularExpressionMatch cm = ci.next();
QColor faded = m_textColor;
faded.setAlphaF(0.6);
QTextCharFormat cFormat;
cFormat.setForeground(faded);
setFormat(cm.capturedStart(), cm.capturedLength(), cFormat);
}
setCurrentBlockState(state);
}