forked from cloose/CuteMarkEd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtmlpreviewgenerator.cpp
More file actions
287 lines (231 loc) · 8.42 KB
/
htmlpreviewgenerator.cpp
File metadata and controls
287 lines (231 loc) · 8.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
* Copyright 2013-2015 Christian Loose <christian.loose@hamburg.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "htmlpreviewgenerator.h"
#include <QFile>
#include <converter/markdownconverter.h>
#include <converter/markdowndocument.h>
#include <converter/discountmarkdownconverter.h>
#include <converter/revealmarkdownconverter.h>
#ifdef ENABLE_HOEDOWN
#include <converter/hoedownmarkdownconverter.h>
#endif
#include <template/template.h>
#include "options.h"
#include "yamlheaderchecker.h"
HtmlPreviewGenerator::HtmlPreviewGenerator(Options *opt, QObject *parent) :
QThread(parent),
options(opt),
document(0),
converter(0)
{
connect(options, SIGNAL(markdownConverterChanged()), SLOT(markdownConverterChanged()));
markdownConverterChanged();
}
bool HtmlPreviewGenerator::isSupported(MarkdownConverter::ConverterOption option) const
{
return converter->supportedOptions().testFlag(option);
}
void HtmlPreviewGenerator::markdownTextChanged(const QString &text)
{
// cut YAML header
YamlHeaderChecker checker(text);
QString actualText = checker.hasHeader() && options->isYamlHeaderSupportEnabled() ?
checker.body()
: text;
// enqueue task to parse the markdown text and generate a new HTML document
QMutexLocker locker(&tasksMutex);
tasks.enqueue(actualText);
bufferNotEmpty.wakeOne();
}
QString HtmlPreviewGenerator::exportHtml(const QString &styleSheet, const QString &highlightingScript)
{
if (!document) return QString();
QString header;
if (!styleSheet.isEmpty()) {
header += QString("\n<style>%1</style>").arg(styleSheet);
}
if (!highlightingScript.isEmpty()) {
// FIXME: doesn't really belong here
QString highlightStyle;
QFile f(QString(":/scripts/highlight.js/styles/%1.css").arg(converter->templateRenderer()->codeHighlightingStyle()));
if (f.open(QIODevice::ReadOnly | QIODevice::Text)) {
highlightStyle = f.readAll();
}
header += QString("\n<style>%1</style>").arg(highlightStyle);
header += QString("\n<script>%1</script>").arg(highlightingScript);
header += "\n<script>hljs.initHighlightingOnLoad();</script>";
}
return converter->templateRenderer()->exportAsHtml(header, converter->renderAsHtml(document), renderOptions());
}
void HtmlPreviewGenerator::setMathSupportEnabled(bool enabled)
{
options->setMathSupportEnabled(enabled);
// regenerate a HTML document
generateHtmlFromMarkdown();
}
void HtmlPreviewGenerator::setDiagramSupportEnabled(bool enabled)
{
options->setDiagramSupportEnabled(enabled);
// regenerate a HTML document
generateHtmlFromMarkdown();
}
void HtmlPreviewGenerator::setCodeHighlightingEnabled(bool enabled)
{
options->setCodeHighlightingEnabled(enabled);
// regenerate a HTML document
generateHtmlFromMarkdown();
}
void HtmlPreviewGenerator::setCodeHighlightingStyle(const QString &style)
{
converter->templateRenderer()->setCodeHighlightingStyle(style);
// regenerate a HTML document
generateHtmlFromMarkdown();
}
void HtmlPreviewGenerator::markdownConverterChanged()
{
QString style;
if (converter) {
style = converter->templateRenderer()->codeHighlightingStyle();
delete converter;
}
switch (options->markdownConverter()) {
#ifdef ENABLE_HOEDOWN
case Options::HoedownMarkdownConverter:
converter = new HoedownMarkdownConverter();
converter->templateRenderer()->setCodeHighlightingStyle(style);
break;
#endif
case Options::RevealMarkdownConverter:
converter = new RevealMarkdownConverter();
converter->templateRenderer()->setCodeHighlightingStyle(style);
break;
case Options::DiscountMarkdownConverter:
default:
converter = new DiscountMarkdownConverter();
converter->templateRenderer()->setCodeHighlightingStyle(style);
break;
}
}
void HtmlPreviewGenerator::run()
{
forever {
QString text;
{
// wait for new task
QMutexLocker locker(&tasksMutex);
while (tasks.count() == 0) {
bufferNotEmpty.wait(&tasksMutex);
}
// get last task from queue and skip all previous tasks
while (!tasks.isEmpty())
text = tasks.dequeue();
}
// end processing?
if (text.isNull()) {
return;
}
// delay processing to see if more tasks are coming
// (e.g. because the user is typing fast)
this->msleep(calculateDelay(text));
// no more new tasks?
if (tasks.isEmpty()) {
// delete previous markdown document
delete document;
// generate HTML from markdown
document = converter->createDocument(text, converterOptions());
generateHtmlFromMarkdown();
// generate table of contents
generateTableOfContents();
}
}
}
void HtmlPreviewGenerator::generateHtmlFromMarkdown()
{
if (!document) return;
QString html = converter->templateRenderer()->render(converter->renderAsHtml(document), renderOptions());
emit htmlResultReady(html);
}
void HtmlPreviewGenerator::generateTableOfContents()
{
if (!document) return;
QString toc = converter->renderAsTableOfContents(document);
QString styledToc = QString("<html><head>\n<style type=\"text/css\">ul { list-style-type: none; padding: 0; margin-left: 1em; } a { text-decoration: none; }</style>\n</head><body>%1</body></html>").arg(toc);
emit tocResultReady(styledToc);
}
MarkdownConverter::ConverterOptions HtmlPreviewGenerator::converterOptions() const
{
MarkdownConverter::ConverterOptions parserOptionFlags(MarkdownConverter::TableOfContentsOption | MarkdownConverter::NoStyleOption);
// autolink
if (options->isAutolinkEnabled()) {
parserOptionFlags |= MarkdownConverter::AutolinkOption;
}
// strikethrough
if (!options->isStrikethroughEnabled()) {
parserOptionFlags |= MarkdownConverter::NoStrikethroughOption;
}
// alphabetic lists
if (!options->isAlphabeticListsEnabled()) {
parserOptionFlags |= MarkdownConverter::NoAlphaListOption;
}
// definition lists
if (!options->isDefinitionListsEnabled()) {
parserOptionFlags |= MarkdownConverter::NoDefinitionListOption;
}
// SmartyPants
if (!options->isSmartyPantsEnabled()) {
parserOptionFlags |= MarkdownConverter::NoSmartypantsOption;
}
// Footnotes
if (options->isFootnotesEnabled()) {
parserOptionFlags |= MarkdownConverter::ExtraFootnoteOption;
}
// Superscript
if (!options->isSuperscriptEnabled()) {
parserOptionFlags |= MarkdownConverter::NoSuperscriptOption;
}
return parserOptionFlags;
}
Template::RenderOptions HtmlPreviewGenerator::renderOptions() const
{
Template::RenderOptions renderOptionFlags;
// math support
if (options->isMathSupportEnabled()) {
renderOptionFlags |= Template::MathSupport;
}
// inline math support
if (options->isMathInlineSupportEnabled()) {
renderOptionFlags |= Template::MathInlineSupport;
}
// diagram support
if (options->isDiagramSupportEnabled()) {
renderOptionFlags |= Template::DiagramSupport;
}
// code highlighting
if (options->isCodeHighlightingEnabled()) {
renderOptionFlags |= Template::CodeHighlighting;
}
return renderOptionFlags;
}
int HtmlPreviewGenerator::calculateDelay(const QString &text)
{
const int MINIMUM_DELAY = 50;
const int MAXIMUM_DELAY = 2000;
// calculate the processing delay based on amount of characters in the
// markdown text. The delay is restricted to the interval [50, 2000] milliseconds;
int delay = qMin(qMax(text.size() / 100, MINIMUM_DELAY), MAXIMUM_DELAY);
return delay;
}