-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathwebviewexporter.cpp
More file actions
806 lines (659 loc) · 26.8 KB
/
Copy pathwebviewexporter.cpp
File metadata and controls
806 lines (659 loc) · 26.8 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
#include "webviewexporter.h"
#include <QDir>
#include <QElapsedTimer>
#include <QFileInfo>
#include <QProcess>
#include <QRegularExpression>
#include <QTemporaryDir>
#include <QWebEnginePage>
#include <QWidget>
#include <core/configmgr2.h>
#include <core/editorconfig.h>
#include <core/exception.h>
#include <core/htmltemplatehelper.h>
#include <core/markdowneditorconfig.h>
#include <gui/services/themeservice.h>
#include <utils/fileutils.h>
#include <utils/htmlutils.h>
#include <utils/pathutils.h>
#include <utils/processutils.h>
#include <utils/utils.h>
#include <utils/webutils.h>
#include <widgets/editors/markdownviewer.h>
#include <widgets/editors/markdownvieweradapter.h>
using namespace vnotex;
static const QString c_imgRegExp = "<img ([^>]*)src=\"(?!data:)([^\"]+)\"([^>]*)>";
namespace {
QString resolveConfigFile(ConfigMgr2 &p_configMgr, const QString &p_filePath) {
return p_configMgr.getFileFromConfigFolder(p_filePath);
}
QString readTemplateFile(const QString &p_filePath, const QString &p_logPrefix) {
try {
return FileUtils::readTextFile(p_filePath);
} catch (Exception &p_e) {
qWarning() << p_logPrefix << p_filePath << p_e.what();
return QString();
}
}
QString errorPage() {
return QStringLiteral("Failed to load HTML template. Check the logs for details. "
"Try deleting the user configuration file and the default configuration "
"file.");
}
QString fillStyleTag(const QString &p_styleFile) {
if (p_styleFile.isEmpty()) {
return QString();
}
const auto url = PathUtils::pathToUrl(p_styleFile);
return QStringLiteral("<link rel=\"stylesheet\" type=\"text/css\" href=\"%1\">\n")
.arg(url.toString());
}
QString fillScriptTag(const QString &p_scriptFile) {
if (p_scriptFile.isEmpty()) {
return QString();
}
const auto url = PathUtils::pathToUrl(p_scriptFile);
return QStringLiteral("<script type=\"text/javascript\" src=\"%1\"></script>\n")
.arg(url.toString());
}
void fillGlobalOptions(QString &p_template, const MarkdownWebGlobalOptions &p_opts) {
p_template.replace(QStringLiteral("/* VX_GLOBAL_OPTIONS_PLACEHOLDER */"),
p_opts.toJavascriptObject());
}
void fillGlobalStyles(QString &p_template, const WebResource &p_resource, ConfigMgr2 &p_configMgr,
const QString &p_additionalStyles) {
QString styles;
for (const auto &ele : p_resource.m_resources) {
if (ele.isGlobal()) {
if (ele.m_enabled) {
for (const auto &style : ele.m_styles) {
const auto styleFile = resolveConfigFile(p_configMgr, style);
const auto content = readTemplateFile(styleFile, "failed to read global styles");
if (!content.isEmpty()) {
styles += content;
}
}
}
break;
}
}
styles += p_additionalStyles;
if (!styles.isEmpty()) {
p_template.replace(QStringLiteral("/* VX_GLOBAL_STYLES_PLACEHOLDER */"), styles);
}
}
void fillThemeStyles(QString &p_template, const QString &p_webStyleSheetFile,
const QString &p_highlightStyleSheetFile) {
QString styles;
styles += fillStyleTag(p_webStyleSheetFile);
styles += fillStyleTag(p_highlightStyleSheetFile);
if (!styles.isEmpty()) {
p_template.replace(QStringLiteral("<!-- VX_THEME_STYLES_PLACEHOLDER -->"), styles);
}
}
void fillResources(QString &p_template, const WebResource &p_resource, ConfigMgr2 &p_configMgr) {
QString styles;
QString scripts;
for (const auto &ele : p_resource.m_resources) {
if (ele.m_enabled && !ele.isGlobal()) {
for (const auto &style : ele.m_styles) {
styles += fillStyleTag(resolveConfigFile(p_configMgr, style));
}
for (const auto &script : ele.m_scripts) {
scripts += fillScriptTag(resolveConfigFile(p_configMgr, script));
}
}
}
if (!styles.isEmpty()) {
p_template.replace(QStringLiteral("<!-- VX_STYLES_PLACEHOLDER -->"), styles);
}
if (!scripts.isEmpty()) {
p_template.replace(QStringLiteral("<!-- VX_SCRIPTS_PLACEHOLDER -->"), scripts);
}
}
void fillResourcesByContent(QString &p_template, const WebResource &p_resource,
ConfigMgr2 &p_configMgr) {
QString styles;
QString scripts;
for (const auto &ele : p_resource.m_resources) {
if (ele.m_enabled && !ele.isGlobal()) {
for (const auto &style : ele.m_styles) {
const auto styleFile = resolveConfigFile(p_configMgr, style);
const auto content = readTemplateFile(styleFile, "failed to read resource");
if (!content.isEmpty()) {
styles += content;
}
}
for (const auto &script : ele.m_scripts) {
const auto scriptFile = resolveConfigFile(p_configMgr, script);
const auto content = readTemplateFile(scriptFile, "failed to read resource");
if (!content.isEmpty()) {
scripts += content;
}
}
}
}
if (!styles.isEmpty()) {
p_template.replace(QStringLiteral("/* VX_STYLES_PLACEHOLDER */"), styles);
}
if (!scripts.isEmpty()) {
p_template.replace(QStringLiteral("/* VX_SCRIPTS_PLACEHOLDER */"), scripts);
}
}
QString generateMarkdownViewerTemplate(ConfigMgr2 &p_configMgr,
const MarkdownEditorConfig &p_config,
const HtmlTemplateHelper::MarkdownParas &p_paras) {
const auto &viewerResource = p_config.getViewerResource();
const auto templateFile = resolveConfigFile(p_configMgr, viewerResource.m_template);
auto htmlTemplate = readTemplateFile(templateFile, "failed to read HTML template");
if (htmlTemplate.isEmpty()) {
return errorPage();
}
fillGlobalStyles(htmlTemplate, viewerResource, p_configMgr, QString());
fillThemeStyles(htmlTemplate, p_paras.m_webStyleSheetFile, p_paras.m_highlightStyleSheetFile);
MarkdownWebGlobalOptions opts;
opts.m_webPlantUml = p_config.getWebPlantUml();
opts.m_plantUmlWebService = p_config.getPlantUmlWebService();
opts.m_webGraphviz = p_config.getWebGraphviz();
opts.m_mathJaxScript = p_config.getMathJaxScript();
opts.m_sectionNumberEnabled =
p_config.getSectionNumberMode() == MarkdownEditorConfig::SectionNumberMode::Read;
opts.m_sectionNumberBaseLevel = p_config.getSectionNumberBaseLevel();
opts.m_constrainImageWidthEnabled = p_config.getConstrainImageWidthEnabled();
opts.m_imageAlignCenterEnabled = p_config.getImageAlignCenterEnabled();
opts.m_protectFromXss = p_config.getProtectFromXss();
opts.m_htmlTagEnabled = p_config.getHtmlTagEnabled();
opts.m_autoBreakEnabled = p_config.getAutoBreakEnabled();
opts.m_linkifyEnabled = p_config.getLinkifyEnabled();
opts.m_indentFirstLineEnabled = p_config.getIndentFirstLineEnabled();
opts.m_codeBlockLineNumberEnabled = p_config.getCodeBlockLineNumberEnabled();
opts.m_transparentBackgroundEnabled = p_paras.m_transparentBackgroundEnabled;
opts.m_scrollable = p_paras.m_scrollable;
opts.m_bodyWidth = p_paras.m_bodyWidth;
opts.m_bodyHeight = p_paras.m_bodyHeight;
opts.m_transformSvgToPngEnabled = p_paras.m_transformSvgToPngEnabled;
opts.m_mathJaxScale = p_paras.m_mathJaxScale;
opts.m_removeCodeToolBarEnabled = p_paras.m_removeCodeToolBarEnabled;
fillGlobalOptions(htmlTemplate, opts);
fillResources(htmlTemplate, viewerResource, p_configMgr);
return htmlTemplate;
}
QString generateMarkdownExportTemplate(ConfigMgr2 &p_configMgr,
const MarkdownEditorConfig &p_config,
bool p_addOutlinePanel) {
auto exportResource = p_config.getExportResource();
const auto templateFile = resolveConfigFile(p_configMgr, exportResource.m_template);
auto htmlTemplate =
readTemplateFile(templateFile, "failed to read Markdown export HTML template");
if (htmlTemplate.isEmpty()) {
return errorPage();
}
fillGlobalStyles(htmlTemplate, exportResource, p_configMgr, QString());
HtmlTemplateHelper::fillOutlinePanel(htmlTemplate, exportResource, p_addOutlinePanel);
fillResourcesByContent(htmlTemplate, exportResource, p_configMgr);
return htmlTemplate;
}
} // namespace
vnotex::WebViewExporter::WebViewExporter(ServiceLocator &p_services, QWidget *p_parent)
: QObject(p_parent), m_services(p_services) {}
WebViewExporter::~WebViewExporter() { clear(); }
void WebViewExporter::clear() {
m_askedToStop = false;
delete m_viewer;
m_viewer = nullptr;
m_htmlTemplate.clear();
m_exportHtmlTemplate.clear();
m_exportOngoing = false;
}
bool vnotex::WebViewExporter::doExport(const ExportOption &p_option, const QString &p_content,
const QString &p_filePath, const QString &p_fileName,
const QString &p_resourcePath, const QString &p_destPath) {
bool ret = false;
m_askedToStop = false;
Q_ASSERT(!m_exportOngoing);
m_exportOngoing = true;
m_webViewStates = WebViewState::Started;
const auto contentPath = p_filePath.isEmpty() ? p_resourcePath : p_filePath;
auto baseUrl = PathUtils::pathToUrl(contentPath);
m_viewer->adapter()->reset();
m_viewer->setHtml(m_htmlTemplate, baseUrl);
auto textContent = p_content;
if (p_option.m_targetFormat == ExportFormat::PDF && p_option.m_pdfOption.m_addTableOfContents &&
!p_option.m_pdfOption.m_useWkhtmltopdf) {
// Add `[TOC]` at the beginning.
m_viewer->adapter()->setText("[TOC]\n\n" + textContent);
} else {
m_viewer->adapter()->setText(textContent);
}
while (!isWebViewReady()) {
Utils::sleepWait(100);
if (m_askedToStop) {
goto exit_export;
}
if (isWebViewFailed()) {
qWarning() << "WebView failed when exporting"
<< (p_filePath.isEmpty() ? p_fileName : p_filePath);
goto exit_export;
}
}
qDebug() << "WebView is ready";
// Add extra wait to make sure Web side is really ready.
Utils::sleepWait(200);
switch (p_option.m_targetFormat) {
case ExportFormat::HTML:
// TODO: MIME HTML format is not supported yet.
Q_ASSERT(!p_option.m_htmlOption.m_useMimeHtmlFormat);
ret = doExportHtml(p_option.m_htmlOption, p_destPath, baseUrl);
break;
case ExportFormat::PDF:
if (p_option.m_pdfOption.m_useWkhtmltopdf) {
ret = doExportWkhtmltopdf(p_option.m_pdfOption, p_destPath, baseUrl);
} else {
ret = doExportPdf(p_option.m_pdfOption, p_destPath);
}
break;
default:
break;
}
exit_export:
m_exportOngoing = false;
return ret;
}
void WebViewExporter::stop() { m_askedToStop = true; }
bool WebViewExporter::isWebViewReady() const {
return m_webViewStates == (WebViewState::LoadFinished | WebViewState::WorkFinished);
}
bool WebViewExporter::isWebViewFailed() const { return m_webViewStates & WebViewState::Failed; }
bool WebViewExporter::doExportHtml(const ExportHtmlOption &p_htmlOption,
const QString &p_outputFile, const QUrl &p_baseUrl) {
ExportState state = ExportState::Busy;
connect(m_viewer->adapter(), &MarkdownViewerAdapter::contentReady, this,
[&, this](const QString &p_headContent, const QString &p_styleContent,
const QString &p_content, const QString &p_bodyClassList) {
qDebug() << "doExportHtml contentReady";
// Maybe unnecessary. Just to avoid duplicated signal connections.
disconnect(m_viewer->adapter(), &MarkdownViewerAdapter::contentReady, this, 0);
if (p_content.isEmpty() || m_askedToStop) {
state = ExportState::Failed;
return;
}
if (!writeHtmlFile(p_outputFile, p_baseUrl, p_headContent, p_styleContent, p_content,
p_bodyClassList, p_htmlOption.m_embedStyles,
p_htmlOption.m_completePage, p_htmlOption.m_embedImages)) {
state = ExportState::Failed;
return;
}
state = ExportState::Finished;
});
m_viewer->adapter()->saveContent();
while (state == ExportState::Busy) {
Utils::sleepWait(100);
if (m_askedToStop) {
break;
}
}
return state == ExportState::Finished;
}
bool WebViewExporter::writeHtmlFile(const QString &p_file, const QUrl &p_baseUrl,
const QString &p_headContent, QString p_styleContent,
const QString &p_content, const QString &p_bodyClassList,
bool p_embedStyles, bool p_completePage, bool p_embedImages) {
const auto baseName = QFileInfo(p_file).completeBaseName();
const QString resourceFolderName = baseName + "_files";
auto resourceFolder =
PathUtils::concatenateFilePath(PathUtils::parentDirPath(p_file), resourceFolderName);
qDebug() << "HTML files folder" << resourceFolder;
auto htmlContent = m_exportHtmlTemplate;
const auto title = QStringLiteral("%1").arg(baseName);
HtmlTemplateHelper::fillTitle(htmlContent, title);
if (!p_styleContent.isEmpty() && p_embedStyles) {
embedStyleResources(p_styleContent);
HtmlTemplateHelper::fillStyleContent(htmlContent, p_styleContent);
}
if (!p_headContent.isEmpty()) {
HtmlTemplateHelper::fillHeadContent(htmlContent, p_headContent);
}
if (p_completePage) {
QString content(p_content);
if (p_embedImages) {
embedBodyResources(p_baseUrl, content);
} else {
fixBodyResources(p_baseUrl, resourceFolder, content);
}
HtmlTemplateHelper::fillContent(htmlContent, content);
} else {
HtmlTemplateHelper::fillContent(htmlContent, p_content);
}
if (!p_bodyClassList.isEmpty()) {
HtmlTemplateHelper::fillBodyClassList(htmlContent, p_bodyClassList);
}
FileUtils::writeFile(p_file, htmlContent);
// Delete empty resource folder.
QDir dir(resourceFolder);
if (dir.exists() && dir.isEmpty()) {
dir.cdUp();
dir.rmdir(resourceFolderName);
}
return true;
}
QSize WebViewExporter::pageLayoutSize(const QPageLayout &p_layout) const {
Q_ASSERT(m_viewer);
auto rect = p_layout.paintRect(QPageLayout::Inch);
return QSize(rect.width() * m_viewer->logicalDpiX(), rect.height() * m_viewer->logicalDpiY());
}
void WebViewExporter::prepare(const ExportOption &p_option) {
Q_ASSERT(!m_viewer && !m_exportOngoing);
Q_ASSERT(p_option.m_targetFormat == ExportFormat::PDF ||
p_option.m_targetFormat == ExportFormat::HTML);
auto *themeService = m_services.get<ThemeService>();
auto *configMgr = m_services.get<ConfigMgr2>();
Q_ASSERT(themeService);
Q_ASSERT(configMgr);
auto *adapter = new MarkdownViewerAdapter(m_services, this);
m_viewer = new MarkdownViewer(adapter, m_services, themeService->getBaseBackground(), 1,
static_cast<QWidget *>(parent()));
m_viewer->hide();
connect(m_viewer->page(), &QWebEnginePage::loadFinished, this,
[this]() { m_webViewStates |= WebViewState::LoadFinished; });
connect(adapter, &MarkdownViewerAdapter::workFinished, this,
[this]() { m_webViewStates |= WebViewState::WorkFinished; });
bool scrollable = true;
if (p_option.m_targetFormat == ExportFormat::PDF ||
(p_option.m_targetFormat == ExportFormat::HTML && !p_option.m_htmlOption.m_scrollable) ||
(p_option.m_targetFormat == ExportFormat::Custom &&
!p_option.m_customOption->m_targetPageScrollable)) {
scrollable = false;
}
const auto &config = configMgr->getEditorConfig().getMarkdownEditorConfig();
bool useWkhtmltopdf = false;
QSize pageBodySize(1024, 768);
if (p_option.m_targetFormat == ExportFormat::PDF) {
useWkhtmltopdf = p_option.m_pdfOption.m_useWkhtmltopdf;
pageBodySize = pageLayoutSize(*(p_option.m_pdfOption.m_layout));
}
qDebug() << "export page body size" << pageBodySize;
HtmlTemplateHelper::MarkdownParas paras;
auto webStyleFile = p_option.m_renderingStyleFile;
if (webStyleFile.isEmpty()) {
const auto webStyles = themeService->getWebStyles();
if (!webStyles.isEmpty()) {
webStyleFile = webStyles.constFirst().second;
}
}
paras.m_webStyleSheetFile = webStyleFile;
auto highlightStyleFile = p_option.m_syntaxHighlightStyleFile;
if (highlightStyleFile.isEmpty()) {
highlightStyleFile = themeService->getFile(Theme::File::HighlightStyleSheet);
}
paras.m_highlightStyleSheetFile = highlightStyleFile;
paras.m_transparentBackgroundEnabled = p_option.m_useTransparentBg;
paras.m_scrollable = scrollable;
paras.m_bodyWidth = pageBodySize.width();
paras.m_bodyHeight = pageBodySize.height();
paras.m_transformSvgToPngEnabled = p_option.m_transformSvgToPngEnabled;
paras.m_mathJaxScale = useWkhtmltopdf ? 2.5 : -1;
paras.m_removeCodeToolBarEnabled = p_option.m_removeCodeToolBarEnabled;
m_htmlTemplate = generateMarkdownViewerTemplate(*configMgr, config, paras);
{
const bool addOutlinePanel =
p_option.m_targetFormat == ExportFormat::HTML && p_option.m_htmlOption.m_addOutlinePanel;
m_exportHtmlTemplate = generateMarkdownExportTemplate(*configMgr, config, addOutlinePanel);
}
if (useWkhtmltopdf) {
prepareWkhtmltopdfArguments(p_option.m_pdfOption);
}
}
static QString marginToStrMM(qreal p_margin) { return QStringLiteral("%1mm").arg(p_margin); }
void WebViewExporter::prepareWkhtmltopdfArguments(const ExportPdfOption &p_pdfOption) {
m_wkhtmltopdfArgs.clear();
// Page layout.
{
const auto &layout = p_pdfOption.m_layout;
m_wkhtmltopdfArgs << "--page-size" << layout->pageSize().key();
m_wkhtmltopdfArgs << "--orientation"
<< (layout->orientation() == QPageLayout::Portrait ? "Portrait"
: "Landscape");
const auto marginsMM = layout->margins(QPageLayout::Millimeter);
m_wkhtmltopdfArgs << "--margin-bottom" << marginToStrMM(marginsMM.bottom());
m_wkhtmltopdfArgs << "--margin-left" << marginToStrMM(marginsMM.left());
m_wkhtmltopdfArgs << "--margin-right" << marginToStrMM(marginsMM.right());
m_wkhtmltopdfArgs << "--margin-top" << marginToStrMM(marginsMM.top());
// Footer.
m_wkhtmltopdfArgs << "--footer-right" << "[page]"
<< "--footer-spacing" << QString::number(marginsMM.bottom() / 3, 'f', 2);
}
m_wkhtmltopdfArgs << "--encoding" << "utf-8";
// Delay 10 seconds for MathJax.
m_wkhtmltopdfArgs << "--javascript-delay" << "5000";
m_wkhtmltopdfArgs << "--enable-local-file-access";
// Append additional global option.
if (!p_pdfOption.m_wkhtmltopdfArgs.isEmpty()) {
m_wkhtmltopdfArgs.append(ProcessUtils::parseCombinedArgString(p_pdfOption.m_wkhtmltopdfArgs));
}
// Must be put after the global object options.
if (p_pdfOption.m_addTableOfContents) {
m_wkhtmltopdfArgs << "toc";
m_wkhtmltopdfArgs << "--toc-text-size-shrink" << "1.0";
m_wkhtmltopdfArgs << "--toc-header-text" << HtmlUtils::unicodeEncode(tr("Table of Contents"));
}
}
bool WebViewExporter::embedStyleResources(QString &p_html) const {
bool altered = false;
QRegularExpression reg("\\burl\\(\"((file|qrc):[^\"\\)]+)\"\\);");
int pos = 0;
while (pos < p_html.size()) {
QRegularExpressionMatch match;
int idx = p_html.indexOf(reg, pos, &match);
if (idx == -1) {
break;
}
QString dataURI = WebUtils::toDataUri(QUrl(match.captured(1)), false);
if (dataURI.isEmpty()) {
pos = idx + match.capturedLength();
} else {
// Replace the url string in html.
QString newUrl = QStringLiteral("url('%1');").arg(dataURI);
p_html.replace(idx, match.capturedLength(), newUrl);
pos = idx + newUrl.size();
altered = true;
}
}
return altered;
}
bool WebViewExporter::embedBodyResources(const QUrl &p_baseUrl, QString &p_html) {
bool altered = false;
if (p_baseUrl.isEmpty()) {
return altered;
}
QRegularExpression reg(c_imgRegExp);
int pos = 0;
while (pos < p_html.size()) {
QRegularExpressionMatch match;
int idx = p_html.indexOf(reg, pos, &match);
if (idx == -1) {
break;
}
if (match.captured(2).isEmpty()) {
pos = idx + match.capturedLength();
continue;
}
QUrl srcUrl(p_baseUrl.resolved(match.captured(2)));
const auto dataURI = WebUtils::toDataUri(srcUrl, true);
if (dataURI.isEmpty()) {
pos = idx + match.capturedLength();
} else {
// Replace the url string in html.
QString newUrl =
QStringLiteral("<img %1src='%2'%3>").arg(match.captured(1), dataURI, match.captured(3));
p_html.replace(idx, match.capturedLength(), newUrl);
pos = idx + newUrl.size();
altered = true;
}
}
return altered;
}
static QString getResourceRelativePath(const QString &p_file) {
int idx = p_file.lastIndexOf('/');
int idx2 = p_file.lastIndexOf('/', idx - 1);
Q_ASSERT(idx > 0 && idx2 < idx);
return "." + p_file.mid(idx2);
}
bool WebViewExporter::fixBodyResources(const QUrl &p_baseUrl, const QString &p_folder,
QString &p_html) {
bool altered = false;
if (p_baseUrl.isEmpty()) {
return altered;
}
QRegularExpression reg(c_imgRegExp);
int pos = 0;
while (pos < p_html.size()) {
QRegularExpressionMatch match;
int idx = p_html.indexOf(reg, pos, &match);
if (idx == -1) {
break;
}
if (match.captured(2).isEmpty()) {
pos = idx + match.capturedLength();
continue;
}
QUrl srcUrl(p_baseUrl.resolved(match.captured(2)));
QString targetFile = WebUtils::copyResource(srcUrl, p_folder);
if (targetFile.isEmpty()) {
pos = idx + match.capturedLength();
} else {
// Replace the url string in html.
QString newUrl =
QStringLiteral("<img %1src=\"%2\"%3>")
.arg(match.captured(1), getResourceRelativePath(targetFile), match.captured(3));
p_html.replace(idx, match.capturedLength(), newUrl);
pos = idx + newUrl.size();
altered = true;
}
}
return altered;
}
bool WebViewExporter::doExportPdf(const ExportPdfOption &p_pdfOption, const QString &p_outputFile) {
ExportState state = ExportState::Busy;
bool pdfRenderReady = false;
QMetaObject::Connection conn =
connect(m_viewer->adapter(), &MarkdownViewerAdapter::pdfRenderReady, this,
[&pdfRenderReady]() { pdfRenderReady = true; });
m_viewer->page()->runJavaScript(
"if (typeof vxcore !== 'undefined' && vxcore.getWorker('mathjax')) {"
" vxcore.getWorker('mathjax').convertAllSvgToPng();"
"} else { window.vxMarkdownAdapter.onPdfRenderReady(); }");
// Bounded wait: if the render-ready signal never arrives (JS bridge not
// ready, an uncaught error in the page, ...), fall through to printToPdf
// instead of freezing the export. Worst case the math stays as SVG.
const qint64 c_pdfRenderReadyTimeoutMs = 30000;
QElapsedTimer renderTimer;
renderTimer.start();
while (!pdfRenderReady) {
Utils::sleepWait(100);
if (m_askedToStop) {
disconnect(conn);
return false;
}
if (renderTimer.hasExpired(c_pdfRenderReadyTimeoutMs)) {
qWarning() << "timed out waiting for PDF math rasterization; proceeding";
break;
}
}
disconnect(conn);
m_viewer->page()->printToPdf(
[&, this](const QByteArray &p_result) {
qDebug() << "doExportPdf printToPdf ready";
if (p_result.isEmpty() || m_askedToStop) {
state = ExportState::Failed;
return;
}
Q_ASSERT(!p_outputFile.isEmpty());
FileUtils::writeFile(p_outputFile, p_result);
state = ExportState::Finished;
},
*p_pdfOption.m_layout);
while (state == ExportState::Busy) {
Utils::sleepWait(100);
if (m_askedToStop) {
break;
}
}
return state == ExportState::Finished;
}
bool WebViewExporter::doExportWkhtmltopdf(const ExportPdfOption &p_pdfOption,
const QString &p_outputFile, const QUrl &p_baseUrl) {
if (p_pdfOption.m_wkhtmltopdfExePath.isEmpty()) {
qWarning() << "invalid wkhtmltopdf executable path";
return false;
}
ExportState state = ExportState::Busy;
connect(m_viewer->adapter(), &MarkdownViewerAdapter::contentReady, this,
[&, this](const QString &p_headContent, const QString &p_styleContent,
const QString &p_content, const QString &p_bodyClassList) {
qDebug() << "doExportWkhtmltopdf contentReady";
// Maybe unnecessary. Just to avoid duplicated signal connections.
disconnect(m_viewer->adapter(), &MarkdownViewerAdapter::contentReady, this, 0);
if (p_content.isEmpty() || m_askedToStop) {
state = ExportState::Failed;
return;
}
// Save HTML to a temp dir.
QTemporaryDir tmpDir;
if (!tmpDir.isValid()) {
state = ExportState::Failed;
return;
}
auto tmpHtmlFile = tmpDir.filePath("vnote_export_tmp.html");
if (!writeHtmlFile(tmpHtmlFile, p_baseUrl, p_headContent, p_styleContent, p_content,
p_bodyClassList, true, true, false)) {
state = ExportState::Failed;
return;
}
// Convert HTML to PDF via wkhtmltopdf.
if (htmlToPdfViaWkhtmltopdf(p_pdfOption, QStringList() << tmpHtmlFile, p_outputFile)) {
state = ExportState::Finished;
} else {
state = ExportState::Failed;
}
});
m_viewer->adapter()->saveContent();
while (state == ExportState::Busy) {
Utils::sleepWait(100);
if (m_askedToStop) {
break;
}
}
return state == ExportState::Finished;
}
bool WebViewExporter::htmlToPdfViaWkhtmltopdf(const ExportPdfOption &p_pdfOption,
const QStringList &p_htmlFiles,
const QString &p_outputFile) {
QStringList args(m_wkhtmltopdfArgs);
// Prepare the args.
for (auto const &file : p_htmlFiles) {
// Note: system's locale settings (Language for non-Unicode programs) is important to
// wkhtmltopdf. Input file could be encoded via
// QUrl::fromLocalFile(p_htmlFile).toString(QUrl::EncodeUnicode) to handle non-ASCII path. But
// for the output file, it is useless.
args << QUrl::fromLocalFile(QDir::toNativeSeparators(file)).toString(QUrl::EncodeUnicode);
}
// To handle non-ASCII path, export it to a temp file and then copy it.
QTemporaryDir tmpDir;
if (!tmpDir.isValid()) {
return false;
}
const auto tmpFile = tmpDir.filePath("vx_tmp_output.pdf");
args << QDir::toNativeSeparators(tmpFile);
bool ret = startProcess(QDir::toNativeSeparators(p_pdfOption.m_wkhtmltopdfExePath), args);
if (ret && QFileInfo::exists(tmpFile)) {
emit logRequested(tr("Copy output file (%1) to (%2).").arg(tmpFile, p_outputFile));
FileUtils::copyFile(tmpFile, p_outputFile);
}
return ret;
}
bool WebViewExporter::startProcess(const QString &p_program, const QStringList &p_args) {
emit logRequested(p_program + " " + ProcessUtils::combineArgString(p_args));
auto ret = ProcessUtils::start(
p_program, p_args, [this](const QString &p_log) { emit logRequested(p_log); }, m_askedToStop);
return ret == ProcessUtils::State::Succeeded;
}