vnote/hgmarkdownhighlighter.h
Le Tan c8d9745253 highlight code block using regular expression
peg-markdown-highlight can't hanlde code blocks with "#" correctly. Use
regular expression to fix it to avoid mis-interpret the "#" line as a
heading.

Signed-off-by: Le Tan <tamlokveer@gmail.com>
2016-10-15 21:15:54 +08:00

83 lines
1.8 KiB
C++

/* PEG Markdown Highlight
* Copyright 2011-2016 Ali Rantakari -- http://hasseg.org
* Licensed under the GPL2+ and MIT licenses (see LICENSE for more info).
*
* highlighter.h
*
* Qt 4.7 example for highlighting a rich text widget.
*/
#ifndef HGMARKDOWNHIGHLIGHTER_H
#define HGMARKDOWNHIGHLIGHTER_H
#include <QTextCharFormat>
#include <QThread>
extern "C" {
#include "utils/peg-highlight/pmh_parser.h"
}
QT_BEGIN_NAMESPACE
class QTextDocument;
QT_END_NAMESPACE
class WorkerThread : public QThread
{
public:
WorkerThread();
~WorkerThread();
void prepareAndStart(const char *data);
pmh_element** retriveResult();
protected:
void run();
private:
void resizeBuffer(unsigned int newCap);
char *content;
unsigned int capacity;
pmh_element **result;
static const unsigned int initCapacity;
};
struct HighlightingStyle
{
pmh_element_type type;
QTextCharFormat format;
};
class HGMarkdownHighlighter : public QObject
{
Q_OBJECT
public:
HGMarkdownHighlighter(const QVector<HighlightingStyle> &styles,
QTextDocument *parent = 0, int aWaitInterval = 2000);
~HGMarkdownHighlighter();
void setStyles(const QVector<HighlightingStyle> &styles);
int waitInterval;
private slots:
void handleContentsChange(int position, int charsRemoved, int charsAdded);
void threadFinished();
void timerTimeout();
private:
QTimer *timer;
QTextDocument *document;
WorkerThread *workerThread;
bool parsePending;
pmh_element **cached_elements;
QVector<HighlightingStyle> highlightingStyles;
void clearFormatting();
void highlight();
void highlightOneRegion(const HighlightingStyle &style, unsigned long pos,
unsigned long end, bool clearBeforeHighlight = false);
void highlightCodeBlock();
void parse();
};
#endif