diff --git a/src/dialog/vexportdialog.cpp b/src/dialog/vexportdialog.cpp index 6b2d80d6..e57c97c8 100644 --- a/src/dialog/vexportdialog.cpp +++ b/src/dialog/vexportdialog.cpp @@ -289,7 +289,8 @@ void VExportDialog::startExport() m_consoleEdit->clear(); appendLogLine(tr("Export to %1.").arg(outputFolder)); - if (opt.m_format == ExportFormat::PDF) { + if (opt.m_format == ExportFormat::PDF + || opt.m_format == ExportFormat::HTML) { m_exporter->prepareExport(opt); } @@ -406,6 +407,10 @@ int VExportDialog::doExport(VFile *p_file, ret = doExportPDF(p_file, p_opt, p_outputFolder, p_errMsg); break; + case (int)ExportFormat::HTML: + ret = doExportHTML(p_file, p_opt, p_outputFolder, p_errMsg); + break; + default: break; } @@ -628,6 +633,40 @@ int VExportDialog::doExportPDF(VFile *p_file, } } +int VExportDialog::doExportHTML(VFile *p_file, + const ExportOption &p_opt, + const QString &p_outputFolder, + QString *p_errMsg) +{ + Q_UNUSED(p_opt); + + QString srcFilePath(p_file->fetchPath()); + + if (p_file->getDocType() != DocType::Markdown) { + LOGERR(tr("Skip exporting non-Markdown file %1 as HTML.").arg(srcFilePath)); + return 0; + } + + if (!VUtils::makePath(p_outputFolder)) { + LOGERR(tr("Fail to create directory %1.").arg(p_outputFolder)); + return 0; + } + + // Get output file. + QString suffix = ".html"; + QString name = VUtils::getFileNameWithSequence(p_outputFolder, + QFileInfo(p_file->getName()).completeBaseName() + suffix); + QString outputPath = QDir(p_outputFolder).filePath(name); + + if (m_exporter->exportHTML(p_file, p_opt, outputPath, p_errMsg)) { + appendLogLine(tr("Note %1 exported to %2.").arg(srcFilePath).arg(outputPath)); + return 1; + } else { + appendLogLine(tr("Fail to export note %1.").arg(srcFilePath)); + return 0; + } +} + bool VExportDialog::checkUserAction() { if (m_askedToStop) { diff --git a/src/dialog/vexportdialog.h b/src/dialog/vexportdialog.h index 1c648098..24ec5a33 100644 --- a/src/dialog/vexportdialog.h +++ b/src/dialog/vexportdialog.h @@ -133,6 +133,11 @@ private: const QString &p_outputFolder, QString *p_errMsg = NULL); + int doExportHTML(VFile *p_file, + const ExportOption &p_opt, + const QString &p_outputFolder, + QString *p_errMsg = NULL); + // Return false if we could not continue. bool checkUserAction(); diff --git a/src/resources/export_template.html b/src/resources/export_template.html new file mode 100644 index 00000000..f52d39d3 --- /dev/null +++ b/src/resources/export_template.html @@ -0,0 +1,14 @@ + + + +
+ + + + + + + + diff --git a/src/resources/markdown-it.js b/src/resources/markdown-it.js index b91a8b52..68417360 100644 --- a/src/resources/markdown-it.js +++ b/src/resources/markdown-it.js @@ -127,7 +127,7 @@ var updateText = function(text) { var highlightText = function(text, id, timeStamp) { var html = mdit.render(text); content.highlightTextCB(html, id, timeStamp); -} +}; var textToHtml = function(text) { var html = mdit.render(text); @@ -139,4 +139,4 @@ var textToHtml = function(text) { container.innerHTML = ""; content.textToHtmlCB(text, html); -} +}; diff --git a/src/resources/markdown_template.html b/src/resources/markdown_template.html index be804b3c..f7c91dbe 100644 --- a/src/resources/markdown_template.html +++ b/src/resources/markdown_template.html @@ -3,7 +3,7 @@ "; + return styles; +}; + +var htmlContent = function() { + content.htmlContentCB(headContent(), placeholder.innerHTML); +}; + new QWebChannel(qt.webChannelTransport, function(channel) { content = channel.objects.content; @@ -62,6 +82,10 @@ new QWebChannel(qt.webChannelTransport, content.requestTextToHtml.connect(textToHtml); content.noticeReadyToTextToHtml(); } + + if (typeof htmlContent == "function") { + content.requestHtmlContent.connect(htmlContent); + } }); var VHighlightedAnchorClass = 'highlighted-anchor'; @@ -447,7 +471,7 @@ var renderMermaidOne = function(code) { mermaidIdx++; try { // Do not increment mermaidIdx here. - var graph = mermaidAPI.render('mermaid-diagram-' + mermaidIdx, code.innerText, function(){}); + var graph = mermaidAPI.render('mermaid-diagram-' + mermaidIdx, code.textContent, function(){}); } catch (err) { content.setLog("err: " + err); return false; @@ -493,7 +517,7 @@ var renderFlowchartOne = function(code) { // Flowchart code block. flowchartIdx++; try { - var graph = flowchart.parse(code.innerText); + var graph = flowchart.parse(code.textContent); } catch (err) { content.setLog("err: " + err); return false; @@ -525,7 +549,7 @@ var renderFlowchartOne = function(code) { var isImageBlock = function(img) { var pn = img.parentNode; - return (pn.children.length == 1) && (pn.innerText == ''); + return (pn.children.length == 1) && (pn.textContent == ''); }; var isImageWithBr = function(img) { @@ -617,7 +641,7 @@ var insertImageCaption = function() { // Add caption. var captionDiv = document.createElement('div'); captionDiv.classList.add(VImageCaptionClass); - captionDiv.innerText = img.alt; + captionDiv.textContent = img.alt; img.insertAdjacentElement('afterend', captionDiv); } } diff --git a/src/resources/simple_template.html b/src/resources/simple_template.html index 165cde37..1f970dca 100644 --- a/src/resources/simple_template.html +++ b/src/resources/simple_template.html @@ -6,6 +6,6 @@ - +