mirror of
https://gitee.com/vnotex/vnote.git
synced 2025-07-05 22:09:52 +08:00

It is said that URL should not contain spaces. Anyway, we use regular expression syntax highlighting to complement PEG Markdown Highlight. Signed-off-by: Le Tan <tamlokveer@gmail.com>
98 lines
2.3 KiB
C++
98 lines
2.3 KiB
C++
#ifndef HGMARKDOWNHIGHLIGHTER_H
|
|
#define HGMARKDOWNHIGHLIGHTER_H
|
|
|
|
#include <QTextCharFormat>
|
|
#include <QSyntaxHighlighter>
|
|
#include <QAtomicInt>
|
|
#include <QSet>
|
|
|
|
extern "C" {
|
|
#include <pmh_parser.h>
|
|
}
|
|
|
|
QT_BEGIN_NAMESPACE
|
|
class QTextDocument;
|
|
QT_END_NAMESPACE
|
|
|
|
struct HighlightingStyle
|
|
{
|
|
pmh_element_type type;
|
|
QTextCharFormat format;
|
|
};
|
|
|
|
enum HighlightBlockState
|
|
{
|
|
Normal = 0,
|
|
CodeBlock = 1,
|
|
};
|
|
|
|
// One continuous region for a certain markdown highlight style
|
|
// within a QTextBlock.
|
|
// Pay attention to the change of HighlightingStyles[]
|
|
struct HLUnit
|
|
{
|
|
// Highlight offset @start and @length with style HighlightingStyles[styleIndex]
|
|
// within a QTextBlock
|
|
unsigned long start;
|
|
unsigned long length;
|
|
unsigned int styleIndex;
|
|
};
|
|
|
|
class HGMarkdownHighlighter : public QSyntaxHighlighter
|
|
{
|
|
Q_OBJECT
|
|
|
|
public:
|
|
HGMarkdownHighlighter(const QVector<HighlightingStyle> &styles, int waitInterval,
|
|
QTextDocument *parent = 0);
|
|
~HGMarkdownHighlighter();
|
|
void setStyles(const QVector<HighlightingStyle> &styles);
|
|
// Request to update highlihgt (re-parse and re-highlight)
|
|
void updateHighlight();
|
|
|
|
signals:
|
|
void highlightCompleted();
|
|
void imageBlocksUpdated(QSet<int> p_blocks);
|
|
|
|
protected:
|
|
void highlightBlock(const QString &text) Q_DECL_OVERRIDE;
|
|
|
|
private slots:
|
|
void handleContentChange(int position, int charsRemoved, int charsAdded);
|
|
void timerTimeout();
|
|
|
|
private:
|
|
QRegExp codeBlockStartExp;
|
|
QRegExp codeBlockEndExp;
|
|
QTextCharFormat codeBlockFormat;
|
|
QTextCharFormat m_linkFormat;
|
|
QTextCharFormat m_imageFormat;
|
|
|
|
QTextDocument *document;
|
|
QVector<HighlightingStyle> highlightingStyles;
|
|
QVector<QVector<HLUnit> > blockHighlights;
|
|
// Block numbers containing image link(s).
|
|
QSet<int> imageBlocks;
|
|
QAtomicInt parsing;
|
|
QTimer *timer;
|
|
int waitInterval;
|
|
|
|
char *content;
|
|
int capacity;
|
|
pmh_element **result;
|
|
|
|
static const int initCapacity;
|
|
|
|
void resizeBuffer(int newCap);
|
|
void highlightCodeBlock(const QString &text);
|
|
void highlightLinkWithSpacesInURL(const QString &p_text);
|
|
void parse();
|
|
void parseInternal();
|
|
void initBlockHighlightFromResult(int nrBlocks);
|
|
void initBlockHighlihgtOne(unsigned long pos, unsigned long end,
|
|
int styleIndex);
|
|
void updateImageBlocks();
|
|
};
|
|
|
|
#endif
|