code block highlight via Pygment

This commit is contained in:
Le Tan 2019-07-23 20:40:13 +08:00
parent d8e7583179
commit b020629b9f
12 changed files with 469 additions and 200 deletions

View File

@ -1019,11 +1019,12 @@ VNoteManagementTab::VNoteManagementTab(QWidget *p_parent)
m_singleClickOpen = new QCheckBox(tr("Single click to open a note in current tab"), this); m_singleClickOpen = new QCheckBox(tr("Single click to open a note in current tab"), this);
m_singleClickOpen->setToolTip(tr("Single click a note in the notes list to open it in current tab, " m_singleClickOpen->setToolTip(tr("Single click a note in the notes list to open it in current tab, "
"double click to open it in a new tab")); "double click to open it in a new tab"));
m_singleClickOpen->setVisible(false);
QFormLayout *noteLayout = new QFormLayout(); QFormLayout *noteLayout = new QFormLayout();
noteLayout->addRow(imageFolderLayout); noteLayout->addRow(imageFolderLayout);
noteLayout->addRow(attachmentFolderLayout); noteLayout->addRow(attachmentFolderLayout);
noteLayout->addRow(m_singleClickOpen); // noteLayout->addRow(m_singleClickOpen);
m_noteBox->setLayout(noteLayout); m_noteBox->setLayout(noteLayout);
// External File. // External File.

View File

@ -146,39 +146,83 @@ font-family: YaHei Consolas Hybrid, Consolas, Monaco, Andale Mono, Monospace, Co
# [VNote] Codeblock sylte from HighlightJS (bold, italic, underlined, strikeout, color) # [VNote] Codeblock sylte from HighlightJS (bold, italic, underlined, strikeout, color)
# The last occurence of the same attribute takes effect # The last occurence of the same attribute takes effect
# Could specify multiple attribute in one line # Could specify multiple attribute in one line
hljs-comment: af8787 hll: 404040
hljs-quote: af8787 c: 999999
hljs-doctag: ccb24c err: a61717
hljs-keyword: ccb24c esc: d0d0d0
hljs-formula: ccb24c g: d0d0d0
hljs-section: e37c84 k: bold, 6ab825
hljs-name: e37c84 l: d0d0d0
hljs-selector-tag: e37c84 n: d0d0d0
hljs-deletion: e37c84 o: d0d0d0
hljs-subst: e37c84 x: d0d0d0
hljs-literal: 56b6c2 p: d0d0d0
hljs-string: f06292 ch: italic, 999999
hljs-regexp: f06292 cm: italic, 999999
hljs-addition: f06292 cp: bold, cd2828
hljs-attribute: f06292 cpf: italic, 999999
hljs-meta-string: f06292 c1: italic, 999999
hljs-built_in: 80cbc4 cs: bold, e50808
hljs-attr: ce93db gd: d22323
hljs-variable: ce93db ge: italic, d0d0d0
hljs-template-variable: ce93db gr: d22323
hljs-type: ce93db gh: bold, ffffff
hljs-selector-class: ce93db gi: 589819
hljs-selector-attr: ce93db go: cccccc
hljs-selector-pseudo: ce93db gp: aaaaaa
hljs-number: ce93db gs: bold, d0d0d0
hljs-symbol: 84c0f2 gu: underlined, ffffff
hljs-link: underlined, 84c0f2 gt: d22323
hljs-bullet: 84c0f2 kc: bold, 6ab825
hljs-meta: 84c0f2 kd: bold, 6ab825
hljs-selector-id: 84c0f2 kn: bold, 6ab825
hljs-title: bold, 84c0f2 kp: 6ab825
hljs-emphasis: italic kr: bold, 6ab825
hljs-strong: bold kt: bold, 6ab825
ld: d0d0d0
m: 3677a9
s: ed9d13
na: bbbbbb
nb: 24909d
nc: underlined, 447fcf
no: 40ffff
nd: ffa500
ni: d0d0d0
ne: bbbbbb
nf: 447fcf
nl: d0d0d0
nn: underlined, 447fcf
nx: d0d0d0
py: d0d0d0
nt: bold, 6ab825
nv: 40ffff
ow: bold, 6ab825
w: 666666
mb: 3677a9
mf: 3677a9
mh: 3677a9
mi: 3677a9
mo: 3677a9
sa: ed9d13
sb: ed9d13
sc: ed9d13
dl: ed9d13
sd: ed9d13
s2: ed9d13
se: ed9d13
sh: ed9d13
si: ed9d13
sx: ffa500
sr: ed9d13
s1: ed9d13
ss: ed9d13
bp: 24909d
fm: 447fcf
vc: 40ffff
vg: 40ffff
vi: 40ffff
vm: 40ffff
il: 3677a9
BLOCKQUOTE BLOCKQUOTE
foreground: ccb24c foreground: ccb24c

View File

@ -144,40 +144,83 @@ font-family: YaHei Consolas Hybrid, Consolas, Monaco, Andale Mono, Monospace, Co
# [VNote] Codeblock sylte from HighlightJS (bold, italic, underlined, strikeout, color) # [VNote] Codeblock sylte from HighlightJS (bold, italic, underlined, strikeout, color)
# The last occurence of the same attribute takes effect # The last occurence of the same attribute takes effect
# Could specify multiple attribute in one line # Could specify multiple attribute in one line
hljs-comment: 6e7686 hll: 404040
hljs-quote: 6e7686 c: 999999
hljs-doctag: c678dd err: a61717
hljs-keyword: c678dd esc: d0d0d0
hljs-formula: c678dd g: d0d0d0
hljs-section: e06c75 k: bold, 6ab825
hljs-name: e06c75 l: d0d0d0
hljs-selector-tag: e06c75 n: d0d0d0
hljs-deletion: e06c75 o: d0d0d0
hljs-subst: e06c75 x: d0d0d0
hljs-literal: 56b6c2 p: d0d0d0
hljs-string: 98c379 ch: italic, 999999
hljs-regexp: 98c379 cm: italic, 999999
hljs-addition: 98c379 cp: bold, cd2828
hljs-attribute: 98c379 cpf: italic, 999999
hljs-meta-string: 98c379 c1: italic, 999999
hljs-built_in: e6c07b cs: bold, e50808
hljs-class: e6c07b gd: d22323
hljs-attr: d19a66 ge: italic, d0d0d0
hljs-variable: d19a66 gr: d22323
hljs-template-variable: d19a66 gh: bold, ffffff
hljs-type: d19a66 gi: 589819
hljs-selector-class: d19a66 go: cccccc
hljs-selector-attr: d19a66 gp: aaaaaa
hljs-selector-pseudo: d19a66 gs: bold, d0d0d0
hljs-number: d19a66 gu: underlined, ffffff
hljs-symbol: 61aeee gt: d22323
hljs-link: underlined, 61aeee kc: bold, 6ab825
hljs-bullet: 61aeee kd: bold, 6ab825
hljs-meta: 61aeee kn: bold, 6ab825
hljs-selector-id: 61aeee kp: 6ab825
hljs-title: bold, 61aeee kr: bold, 6ab825
hljs-emphasis: italic kt: bold, 6ab825
hljs-strong: bold ld: d0d0d0
m: 3677a9
s: ed9d13
na: bbbbbb
nb: 24909d
nc: underlined, 447fcf
no: 40ffff
nd: ffa500
ni: d0d0d0
ne: bbbbbb
nf: 447fcf
nl: d0d0d0
nn: underlined, 447fcf
nx: d0d0d0
py: d0d0d0
nt: bold, 6ab825
nv: 40ffff
ow: bold, 6ab825
w: 666666
mb: 3677a9
mf: 3677a9
mh: 3677a9
mi: 3677a9
mo: 3677a9
sa: ed9d13
sb: ed9d13
sc: ed9d13
dl: ed9d13
sd: ed9d13
s2: ed9d13
se: ed9d13
sh: ed9d13
si: ed9d13
sx: ffa500
sr: ed9d13
s1: ed9d13
ss: ed9d13
bp: 24909d
fm: 447fcf
vc: 40ffff
vg: 40ffff
vi: 40ffff
vm: 40ffff
il: 3677a9
BLOCKQUOTE BLOCKQUOTE
foreground: 6e7686 foreground: 6e7686

View File

@ -138,42 +138,69 @@ font-family: YaHei Consolas Hybrid, Consolas, Monaco, Andale Mono, Monospace, Co
FENCEDCODEBLOCK FENCEDCODEBLOCK
foreground: 673ab7 foreground: 673ab7
font-family: YaHei Consolas Hybrid, Consolas, Monaco, Andale Mono, Monospace, Courier New font-family: YaHei Consolas Hybrid, Consolas, Monaco, Andale Mono, Monospace, Courier New
# [VNote] Codeblock sylte from HighlightJS (bold, italic, underlined, strikeout, color) # [VNote] Codeblock sylte from Pygment (bold, italic, underlined, strikeout, color)
# The last occurence of the same attribute takes effect # The last occurence of the same attribute takes effect
hljs-comment: 6c6c6c hll: ffffcc
hljs-keyword: 0000ee c: 888888
hljs-attribute: 0000ee err: FF0000
hljs-selector-tag: 0000ee k: 008800
hljs-meta-keyword: 0000ee o: 333333
hljs-doctag: 0000ee ch: 888888
hljs-name: 0000ee cm: 888888
hljs-type: 880000 cp: 557799
hljs-string: 880000 cpf: 4d99bf
hljs-number: 880000 c1: 888888
hljs-selector-id: 880000 cs: cc0000
hljs-selector-class: 880000 gd: A00000
hljs-quote: 880000 ge: italic
hljs-template-tag: 880000 gr: FF0000
hljs-deletion: 880000 gh: 000080
# Could specify multiple attribute in one line gi: 00A000
hljs-title: bold, 880000 go: 888888
hljs-section: bold, 880000 gp: c65d09
hljs-regexp: bc6060 gs: bold
hljs-symbol: bc6060 gu: bold, 800080
hljs-variable: bc6060 gt: bold, 0044DD
hljs-template-variable: bc6060 kc: bold, 008800
hljs-link: bc6060 kd: bold, 008800
hljs-selector-attr: bc6060 kn: bold, 008800
hljs-selector-pseudo: bc6060 kp: bold, 003388
hljs-literal: af00d7 kr: bold, 008800
hljs-built_in: 008700 kt: bold, 333399
hljs-bullet: 008700 m: bold, 6600EE
hljs-code: 008700 s: af00d7
hljs-addition: 008700 na: 0000CC
hljs-meta: 1f7199 nb: 007020
hljs-meta-string: 4d99bf nc: bold, BB0066
hljs-emphasis: italic no: bold, 003366
hljs-strong: bold nd: bold, 555555
ni: bold, 880000
ne: bold, FF0000
nf: bold, 0066BB
nl: bold, 997700
nn: bold, 0e84b5
nt: 007700
nv: 996633
ow: bold, 000000
w: bbbbbb
mb: bold, 6600EE
mf: bold, 6600EE
mh: bold, 005588
mi: bold, 0000DD
mo: bold, 4400EE
sc: 0044DD
sd: DD4422
se: 666666
sx: DD2200
sr: 000000
ss: AA6600
bp: 007020
fm: bold, 0066BB
vc: 336699
vg: bold, dd7700
vi: 3333BB
vm: 996633
il: bold, 0000DD
BLOCKQUOTE BLOCKQUOTE
foreground: 00af00 foreground: 00af00

View File

@ -139,42 +139,70 @@ font-family: YaHei Consolas Hybrid, Consolas, Monaco, Andale Mono, Monospace, Co
FENCEDCODEBLOCK FENCEDCODEBLOCK
foreground: 673ab7 foreground: 673ab7
font-family: YaHei Consolas Hybrid, Consolas, Monaco, Andale Mono, Monospace, Courier New font-family: YaHei Consolas Hybrid, Consolas, Monaco, Andale Mono, Monospace, Courier New
# [VNote] Codeblock sylte from HighlightJS (bold, italic, underlined, strikeout, color) # [VNote] Codeblock sylte from Pygment (bold, italic, underlined, strikeout, color)
# The last occurence of the same attribute takes effect # The last occurence of the same attribute takes effect
# Could specify multiple attribute in one line # Could specify multiple attribute in one line
hljs-comment: 6c6c6c hll: ffffcc
hljs-keyword: 0000ee c: 888888
hljs-attribute: 0000ee err: FF0000
hljs-selector-tag: 0000ee k: 008800
hljs-meta-keyword: 0000ee o: 333333
hljs-doctag: 0000ee ch: 888888
hljs-name: 0000ee cm: 888888
hljs-type: 880000 cp: 557799
hljs-string: 880000 cpf: 4d99bf
hljs-number: 880000 c1: 888888
hljs-selector-id: 880000 cs: cc0000
hljs-selector-class: 880000 gd: A00000
hljs-quote: 880000 ge: italic
hljs-template-tag: 880000 gr: FF0000
hljs-deletion: 880000 gh: 000080
hljs-title: bold, 880000 gi: 00A000
hljs-section: bold, 880000 go: 888888
hljs-regexp: bc6060 gp: c65d09
hljs-symbol: bc6060 gs: bold
hljs-variable: bc6060 gu: bold, 800080
hljs-template-variable: bc6060 gt: bold, 0044DD
hljs-link: bc6060 kc: bold, 008800
hljs-selector-attr: bc6060 kd: bold, 008800
hljs-selector-pseudo: bc6060 kn: bold, 008800
hljs-literal: af00d7 kp: bold, 003388
hljs-built_in: 008700 kr: bold, 008800
hljs-bullet: 008700 kt: bold, 333399
hljs-code: 008700 m: bold, 6600EE
hljs-addition: 008700 s: af00d7
hljs-meta: 1f7199 na: 0000CC
hljs-meta-string: 4d99bf nb: 007020
hljs-emphasis: italic nc: bold, BB0066
hljs-strong: bold no: bold, 003366
nd: bold, 555555
ni: bold, 880000
ne: bold, FF0000
nf: bold, 0066BB
nl: bold, 997700
nn: bold, 0e84b5
nt: 007700
nv: 996633
ow: bold, 000000
w: bbbbbb
mb: bold, 6600EE
mf: bold, 6600EE
mh: bold, 005588
mi: bold, 0000DD
mo: bold, 4400EE
sc: 0044DD
sd: DD4422
se: 666666
sx: DD2200
sr: 000000
ss: AA6600
bp: 007020
fm: bold, 0066BB
vc: 336699
vg: bold, dd7700
vi: 3333BB
vm: 996633
il: bold, 0000DD
BLOCKQUOTE BLOCKQUOTE
foreground: 00af00 foreground: 00af00

View File

@ -135,39 +135,67 @@ font-family: YaHei Consolas Hybrid, Consolas, Monaco, Andale Mono, Monospace, Co
# [VNote] Codeblock sylte from HighlightJS (bold, italic, underlined, strikeout, color) # [VNote] Codeblock sylte from HighlightJS (bold, italic, underlined, strikeout, color)
# The last occurence of the same attribute takes effect # The last occurence of the same attribute takes effect
# Could specify multiple attribute in one line # Could specify multiple attribute in one line
hljs-comment: 6c6c6c hll: ffffcc
hljs-keyword: 0000ee c: 888888
hljs-attribute: 0000ee err: FF0000
hljs-selector-tag: 0000ee k: 008800
hljs-meta-keyword: 0000ee o: 333333
hljs-doctag: 0000ee ch: 888888
hljs-name: 0000ee cm: 888888
hljs-type: 880000 cp: 557799
hljs-string: 880000 cpf: 4d99bf
hljs-number: 880000 c1: 888888
hljs-selector-id: 880000 cs: cc0000
hljs-selector-class: 880000 gd: A00000
hljs-quote: 880000 ge: italic
hljs-template-tag: 880000 gr: FF0000
hljs-deletion: 880000 gh: 000080
hljs-title: bold, 880000 gi: 00A000
hljs-section: bold, 880000 go: 888888
hljs-regexp: bc6060 gp: c65d09
hljs-symbol: bc6060 gs: bold
hljs-variable: bc6060 gu: bold, 800080
hljs-template-variable: bc6060 gt: bold, 0044DD
hljs-link: bc6060 kc: bold, 008800
hljs-selector-attr: bc6060 kd: bold, 008800
hljs-selector-pseudo: bc6060 kn: bold, 008800
hljs-literal: af00d7 kp: bold, 003388
hljs-built_in: 008700 kr: bold, 008800
hljs-bullet: 008700 kt: bold, 333399
hljs-code: 008700 m: bold, 6600EE
hljs-addition: 008700 s: af00d7
hljs-meta: 1f7199 na: 0000CC
hljs-meta-string: 4d99bf nb: 007020
hljs-emphasis: italic nc: bold, BB0066
hljs-strong: bold no: bold, 003366
nd: bold, 555555
ni: bold, 880000
ne: bold, FF0000
nf: bold, 0066BB
nl: bold, 997700
nn: bold, 0e84b5
nt: 007700
nv: 996633
ow: bold, 000000
w: bbbbbb
mb: bold, 6600EE
mf: bold, 6600EE
mh: bold, 005588
mi: bold, 0000DD
mo: bold, 4400EE
sc: 0044DD
sd: DD4422
se: 666666
sx: DD2200
sr: 000000
ss: AA6600
bp: 007020
fm: bold, 0066BB
vc: 336699
vg: bold, dd7700
vi: 3333BB
vm: 996633
il: bold, 0000DD
BLOCKQUOTE BLOCKQUOTE
foreground: 00af00 foreground: 00af00

View File

@ -336,6 +336,9 @@ enable_smart_table=true
; Interval (milliseconds) to format table ; Interval (milliseconds) to format table
table_format_interval=2000 table_format_interval=2000
; Pygment program to call for code block highlight.
pygment_prg=pygmentize
[export] [export]
; Path of the wkhtmltopdf tool ; Path of the wkhtmltopdf tool
wkhtmltopdf=wkhtmltopdf wkhtmltopdf=wkhtmltopdf

View File

@ -5,25 +5,30 @@
#include "vdocument.h" #include "vdocument.h"
#include "utils/vutils.h" #include "utils/vutils.h"
#include "utils/veditutils.h"
#include "pegmarkdownhighlighter.h" #include "pegmarkdownhighlighter.h"
#include "vconfigmanager.h"
#include "utils/vprocessutils.h"
extern VConfigManager *g_config;
#define TaskIdProperty "PygmentTaskId"
#define TaskTimeStampProperty "PygmentTaskTimeStamp"
VCodeBlockHighlightHelper::VCodeBlockHighlightHelper(PegMarkdownHighlighter *p_highlighter, VCodeBlockHighlightHelper::VCodeBlockHighlightHelper(PegMarkdownHighlighter *p_highlighter,
VDocument *p_vdoc,
MarkdownConverterType p_type) MarkdownConverterType p_type)
: QObject(p_highlighter), : QObject(p_highlighter),
m_highlighter(p_highlighter), m_highlighter(p_highlighter),
m_vdocument(p_vdoc),
m_type(p_type), m_type(p_type),
m_timeStamp(0) m_timeStamp(0)
{ {
m_program = g_config->getPygmentPrg();
m_args << "-f" << "html" << "-O" << "encoding=utf8";
qDebug() << "code block highlight helper" << m_program << m_args;
connect(m_highlighter, &PegMarkdownHighlighter::codeBlocksUpdated, connect(m_highlighter, &PegMarkdownHighlighter::codeBlocksUpdated,
this, &VCodeBlockHighlightHelper::handleCodeBlocksUpdated); this, &VCodeBlockHighlightHelper::handleCodeBlocksUpdated);
connect(m_vdocument, &VDocument::textHighlighted,
this, &VCodeBlockHighlightHelper::handleTextHighlightResult);
// Web side is ready for code block highlight.
connect(m_vdocument, &VDocument::readyToHighlightText,
m_highlighter, &PegMarkdownHighlighter::updateHighlight);
} }
QString VCodeBlockHighlightHelper::unindentCodeBlock(const QString &p_text) QString VCodeBlockHighlightHelper::unindentCodeBlock(const QString &p_text)
@ -62,16 +67,6 @@ QString VCodeBlockHighlightHelper::unindentCodeBlock(const QString &p_text)
void VCodeBlockHighlightHelper::handleCodeBlocksUpdated(TimeStamp p_timeStamp, void VCodeBlockHighlightHelper::handleCodeBlocksUpdated(TimeStamp p_timeStamp,
const QVector<VCodeBlock> &p_codeBlocks) const QVector<VCodeBlock> &p_codeBlocks)
{ {
if (!m_vdocument->isReadyToHighlight()) {
// Immediately return empty results.
QVector<HLUnitPos> emptyRes;
for (int i = 0; i < p_codeBlocks.size(); ++i) {
updateHighlightResults(p_timeStamp, 0, emptyRes);
}
return;
}
m_timeStamp = p_timeStamp; m_timeStamp = p_timeStamp;
m_codeBlocks = p_codeBlocks; m_codeBlocks = p_codeBlocks;
for (int i = 0; i < m_codeBlocks.size(); ++i) { for (int i = 0; i < m_codeBlocks.size(); ++i) {
@ -84,11 +79,41 @@ void VCodeBlockHighlightHelper::handleCodeBlocksUpdated(TimeStamp p_timeStamp,
updateHighlightResults(p_timeStamp, block.m_startPos, it.value().m_units); updateHighlightResults(p_timeStamp, block.m_startPos, it.value().m_units);
} else { } else {
QString unindentedText = unindentCodeBlock(block.m_text); QString unindentedText = unindentCodeBlock(block.m_text);
m_vdocument->highlightTextAsync(unindentedText, i, p_timeStamp); highlightTextAsync(VEditUtils::removeCodeBlockFence(unindentedText),
block.m_lang,
i,
p_timeStamp);
} }
} }
} }
void VCodeBlockHighlightHelper::highlightTextAsync(const QString &p_text,
const QString &p_lang,
int p_id,
unsigned long long p_timeStamp)
{
if (p_lang.isEmpty() || m_program.isEmpty()) {
updateHighlightResults(p_timeStamp, 0, QVector<HLUnitPos>());
return;
}
QProcess *process = new QProcess(this);
process->setProperty(TaskIdProperty, p_id);
process->setProperty(TaskTimeStampProperty, p_timeStamp);
connect(process, SIGNAL(finished(int, QProcess::ExitStatus)),
this, SLOT(handleProcessFinished(int, QProcess::ExitStatus)));
QStringList args(m_args);
args << "-l" << p_lang;
process->start(m_program, args);
if (process->write(p_text.toUtf8()) == -1) {
qWarning() << "fail to write to QProcess:" << process->errorString();
}
process->closeWriteChannel();
}
void VCodeBlockHighlightHelper::handleTextHighlightResult(const QString &p_html, void VCodeBlockHighlightHelper::handleTextHighlightResult(const QString &p_html,
int p_id, int p_id,
unsigned long long p_timeStamp) unsigned long long p_timeStamp)
@ -154,6 +179,11 @@ void VCodeBlockHighlightHelper::parseHighlightResult(TimeStamp p_timeStamp,
QXmlStreamReader xml(p_html); QXmlStreamReader xml(p_html);
if (p_html.isEmpty()) {
goto exit;
}
{
// Must have a fenced line at the front. // Must have a fenced line at the front.
// textIndex is the start index in the code block text to search for. // textIndex is the start index in the code block text to search for.
int textIndex = text.indexOf('\n'); int textIndex = text.indexOf('\n');
@ -163,7 +193,7 @@ void VCodeBlockHighlightHelper::parseHighlightResult(TimeStamp p_timeStamp,
++textIndex; ++textIndex;
if (xml.readNextStartElement()) { if (xml.readNextStartElement()) {
if (xml.name() != "pre") { if (xml.name() != "div") {
goto exit; goto exit;
} }
@ -171,7 +201,7 @@ void VCodeBlockHighlightHelper::parseHighlightResult(TimeStamp p_timeStamp,
goto exit; goto exit;
} }
if (xml.name() != "code") { if (xml.name() != "pre") {
goto exit; goto exit;
} }
@ -209,6 +239,7 @@ void VCodeBlockHighlightHelper::parseHighlightResult(TimeStamp p_timeStamp,
} }
} }
} }
}
exit: exit:
// Pass result back to highlighter. // Pass result back to highlighter.
@ -275,8 +306,11 @@ bool VCodeBlockHighlightHelper::parseSpanElement(QXmlStreamReader &p_xml,
} }
// Got a complete span. Use relative position here. // Got a complete span. Use relative position here.
if (p_index > unitStart) {
HLUnitPos unit(unitStart, p_index - unitStart, style); HLUnitPos unit(unitStart, p_index - unitStart, style);
p_units.append(unit); p_units.append(unit);
}
return true; return true;
} else { } else {
return false; return false;
@ -305,3 +339,43 @@ void VCodeBlockHighlightHelper::addToHighlightCache(const QString &p_text,
m_cache.insert(p_text, HLResult(p_timeStamp, p_units)); m_cache.insert(p_text, HLResult(p_timeStamp, p_units));
} }
void VCodeBlockHighlightHelper::handleProcessFinished(int p_exitCode, QProcess::ExitStatus p_exitStatus)
{
QProcess *process = static_cast<QProcess *>(sender());
int id = process->property(TaskIdProperty).toInt();
TimeStamp timeStamp = process->property(TaskTimeStampProperty).toULongLong();
qDebug() << QString("Pygment finished: id %1 timestamp %2 exitcode %3 exitstatus %4")
.arg(id)
.arg(timeStamp)
.arg(p_exitCode)
.arg(p_exitStatus);
bool failed = true;
if (p_exitStatus == QProcess::NormalExit) {
if (p_exitCode < 0) {
qWarning() << "Pygment fail" << p_exitCode;
} else {
failed = false;
QByteArray outBa = process->readAllStandardOutput();
handleTextHighlightResult(QString::fromUtf8(outBa), id, timeStamp);
}
} else {
qWarning() << "fail to start Pygment process" << p_exitCode << p_exitStatus;
}
QByteArray errBa = process->readAllStandardError();
if (!errBa.isEmpty()) {
QString errStr(QString::fromLocal8Bit(errBa));
if (failed) {
qWarning() << "Pygment stderr:" << errStr;
} else {
qDebug() << "Pygment stderr:" << errStr;
}
}
if (failed) {
handleTextHighlightResult(QString(), id, timeStamp);
}
process->deleteLater();
}

View File

@ -6,10 +6,11 @@
#include <QAtomicInteger> #include <QAtomicInteger>
#include <QXmlStreamReader> #include <QXmlStreamReader>
#include <QHash> #include <QHash>
#include <QProcess>
#include <QStringList>
#include "vconfigmanager.h" #include "vconfigmanager.h"
class VDocument;
class PegMarkdownHighlighter; class PegMarkdownHighlighter;
class VCodeBlockHighlightHelper : public QObject class VCodeBlockHighlightHelper : public QObject
@ -17,7 +18,7 @@ class VCodeBlockHighlightHelper : public QObject
Q_OBJECT Q_OBJECT
public: public:
VCodeBlockHighlightHelper(PegMarkdownHighlighter *p_highlighter, VCodeBlockHighlightHelper(PegMarkdownHighlighter *p_highlighter,
VDocument *p_vdoc, MarkdownConverterType p_type); MarkdownConverterType p_type);
// @p_text: text of fenced code block. // @p_text: text of fenced code block.
// Get the indent level of the first line (fence) and unindent the whole block // Get the indent level of the first line (fence) and unindent the whole block
@ -31,6 +32,8 @@ private slots:
void handleTextHighlightResult(const QString &p_html, int p_id, unsigned long long p_timeStamp); void handleTextHighlightResult(const QString &p_html, int p_id, unsigned long long p_timeStamp);
void handleProcessFinished(int p_exitCode, QProcess::ExitStatus p_exitStatus);
private: private:
struct HLResult struct HLResult
{ {
@ -65,8 +68,12 @@ private:
TimeStamp p_timeStamp, TimeStamp p_timeStamp,
const QVector<HLUnitPos> &p_units); const QVector<HLUnitPos> &p_units);
void highlightTextAsync(const QString &p_text,
const QString &p_lang,
int p_id,
unsigned long long p_timeStamp);
PegMarkdownHighlighter *m_highlighter; PegMarkdownHighlighter *m_highlighter;
VDocument *m_vdocument;
MarkdownConverterType m_type; MarkdownConverterType m_type;
TimeStamp m_timeStamp; TimeStamp m_timeStamp;
@ -76,6 +83,10 @@ private:
// Cache for highlight result, using the code block text as key. // Cache for highlight result, using the code block text as key.
// The HLResult has relative position only. // The HLResult has relative position only.
QHash<QString, HLResult> m_cache; QHash<QString, HLResult> m_cache;
QString m_program;
QStringList m_args;
}; };
#endif // VCODEBLOCKHIGHLIGHTHELPER_H #endif // VCODEBLOCKHIGHLIGHTHELPER_H

View File

@ -20,7 +20,7 @@ const QString VConfigManager::orgName = QString("vnote");
const QString VConfigManager::appName = QString("vnote"); const QString VConfigManager::appName = QString("vnote");
const QString VConfigManager::c_version = QString("2.7.1"); const QString VConfigManager::c_version = QString("2.7.56");
const QString VConfigManager::c_dirConfigFile = QString("_vnote.json"); const QString VConfigManager::c_dirConfigFile = QString("_vnote.json");
@ -382,6 +382,8 @@ void VConfigManager::initEditorConfigs()
m_enableSmartTable = getConfigFromSettings(section, "enable_smart_table").toBool(); m_enableSmartTable = getConfigFromSettings(section, "enable_smart_table").toBool();
m_tableFormatIntervalMS = getConfigFromSettings(section, "table_format_interval").toInt(); m_tableFormatIntervalMS = getConfigFromSettings(section, "table_format_interval").toInt();
m_pygmentPrg = getConfigFromSettings(section, "pygment_prg").toString();
} }
void VConfigManager::initMarkdownConfigs() void VConfigManager::initMarkdownConfigs()

View File

@ -648,6 +648,8 @@ public:
bool getEnableCodeBlockCopyButton() const; bool getEnableCodeBlockCopyButton() const;
const QString &getPygmentPrg() const;
private: private:
void initEditorConfigs(); void initEditorConfigs();
@ -1147,6 +1149,9 @@ private:
// Whether enable copy button in code block in read mode. // Whether enable copy button in code block in read mode.
bool m_enableCodeBlockCopyButton; bool m_enableCodeBlockCopyButton;
// Pygment program to call for code block highlight in editor.
QString m_pygmentPrg;
// The name of the config file in each directory. // The name of the config file in each directory.
static const QString c_dirConfigFile; static const QString c_dirConfigFile;
@ -2989,4 +2994,9 @@ inline bool VConfigManager::getEnableCodeBlockCopyButton() const
return m_enableCodeBlockCopyButton; return m_enableCodeBlockCopyButton;
} }
inline const QString &VConfigManager::getPygmentPrg() const
{
return m_pygmentPrg;
}
#endif // VCONFIGMANAGER_H #endif // VCONFIGMANAGER_H

View File

@ -101,9 +101,7 @@ VMdEditor::VMdEditor(VFile *p_file,
} }
}); });
m_cbHighlighter = new VCodeBlockHighlightHelper(m_pegHighlighter, m_cbHighlighter = new VCodeBlockHighlightHelper(m_pegHighlighter, p_type);
p_doc,
p_type);
m_previewMgr = new VPreviewManager(this, m_pegHighlighter); m_previewMgr = new VPreviewManager(this, m_pegHighlighter);
connect(m_pegHighlighter, &PegMarkdownHighlighter::imageLinksUpdated, connect(m_pegHighlighter, &PegMarkdownHighlighter::imageLinksUpdated,