From 203544632eec7067a0e920aa8be537d0fbcfa8cc Mon Sep 17 00:00:00 2001 From: Le Tan Date: Wed, 17 Oct 2018 20:01:51 +0800 Subject: [PATCH] MdEditor: turn equation from zhihu.com to MathJax when fetching images --- src/utils/vutils.cpp | 15 +++++++++++++++ src/vmdeditor.cpp | 23 +++++++++++++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/utils/vutils.cpp b/src/utils/vutils.cpp index 36734397..42defa32 100644 --- a/src/utils/vutils.cpp +++ b/src/utils/vutils.cpp @@ -792,6 +792,11 @@ QString VUtils::generateHtmlTemplate(const QString &p_template, "processClass: \"tex2jax_process|language-mathjax|lang-mathjax\"},\n" " showProcessingMessages: false,\n" " skipStartupTypeset: " + QString("%1,\n").arg(mathjaxTypeSetOnLoad ? "false" : "true") + + " TeX: {\n" + " Macros: {\n" + " bm: [\"\\\\boldsymbol{#1}\", 1]\n" + " }\n" + " },\n" " messageStyle: \"none\"});\n" "MathJax.Hub.Register.StartupHook(\"End\", function() { handleMathjaxReady(); });\n" "\n" @@ -894,6 +899,11 @@ with 2em, if there are Chinese characters in it, the font will be a mess. "}\n" #endif "}\n" + "},\n" + "TeX: {\n" + " Macros: {\n" + " bm: [\"\\\\boldsymbol{#1}\", 1]\n" + " }\n" "}\n" "});\n" "\n"; @@ -955,6 +965,11 @@ QString VUtils::generateMathJaxPreviewTemplate() " scale: " + mathjaxScale + "\n" " },\n" " showProcessingMessages: false,\n" + " TeX: {\n" + " Macros: {\n" + " bm: [\"\\\\boldsymbol{#1}\", 1]\n" + " }\n" + " },\n" " messageStyle: \"none\"});\n" "\n"; diff --git a/src/vmdeditor.cpp b/src/vmdeditor.cpp index 5dde8f46..24a55750 100644 --- a/src/vmdeditor.cpp +++ b/src/vmdeditor.cpp @@ -2042,6 +2042,8 @@ void VMdEditor::replaceTextWithLocalImages(QString &p_text) proDlg.setWindowModality(Qt::WindowModal); proDlg.setWindowTitle(tr("Fetching Images To Local Folder")); + QRegExp zhihuRegExp("^https?://www\\.zhihu\\.com/equation\\?tex=(.+)$"); + QRegExp regExp(VUtils::c_imageLinkRegExp); for (int i = regs.size() - 1; i >= 0; --i) { proDlg.setValue(regs.size() - 1 - i); @@ -2065,6 +2067,25 @@ void VMdEditor::replaceTextWithLocalImages(QString &p_text) } proDlg.setLabelText(tr("Fetching image: %1").arg(urlToDisplay)); + // Handle equation from zhihu.com like http://www.zhihu.com/equation?tex=P. + if (zhihuRegExp.indexIn(imageUrl) != -1) { + QString tex = zhihuRegExp.cap(1).trimmed(); + + // Remove the +. + tex.replace(QChar('+'), " "); + + tex = QUrl::fromPercentEncoding(tex.toUtf8()); + if (tex.isEmpty()) { + continue; + } + + tex = "$" + tex + "$"; + p_text.replace(reg.m_startPos, + reg.m_endPos - reg.m_startPos, + tex); + continue; + } + QString destImagePath, urlInLink; // Only handle absolute file path or network path. @@ -2119,8 +2140,6 @@ void VMdEditor::replaceTextWithLocalImages(QString &p_text) p_text.replace(reg.m_startPos, reg.m_endPos - reg.m_startPos, newLink); - - qDebug() << "replace link" << linkText << "to" << newLink; } proDlg.setValue(regs.size());