Browse Source

MdEditor: turn equation from zhihu.com to MathJax when fetching images

Le Tan 7 years ago
parent
commit
203544632e
2 changed files with 36 additions and 2 deletions
  1. 15 0
      src/utils/vutils.cpp
  2. 21 2
      src/vmdeditor.cpp

+ 15 - 0
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"
                      "</script>\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"
                  "</script>\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"
                  "</script>\n";
 

+ 21 - 2
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());