Bläddra i källkod

fix font-family issue in WeChat Public Account editor

It could not recognize " in font-family.
Le Tan 8 år sedan
förälder
incheckning
b45f1d9518

+ 1 - 1
src/resources/themes/v_moonlight/v_moonlight.css

@@ -84,7 +84,7 @@ pre {
 }
 
 code {
-    font-family: Consolas, Monaco, Andale Mono, Monospace, Courier New;
+    font-family: Consolas, Monaco, Monospace, Courier;
     font-size: 16px;
     color: #98C379;
 }

+ 1 - 1
src/resources/themes/v_pure/v_pure.css

@@ -84,7 +84,7 @@ pre {
 }
 
 code {
-    font-family: Consolas, Monaco, Andale Mono, Monospace, Courier New;
+    font-family: Consolas, Monaco, Monospace, Courier;
     font-size: 16px;
     color: #8E24AA;
 }

+ 1 - 1
src/resources/themes/v_white/v_white.css

@@ -83,7 +83,7 @@ pre {
 }
 
 code {
-    font-family: Consolas, Monaco, Andale Mono, Monospace, Courier New;
+    font-family: Consolas, Monaco, Monospace, Courier;
     font-size: 16px;
     color: #8E24AA;
 }

+ 3 - 1
src/resources/vnote.ini

@@ -219,7 +219,9 @@ styles_to_inline_when_copied=all$border:color:display:font-family:font-size:font
 ; p - replace the background color of <pre> with that of its child <code>
 ; n - replace the \n in <pre> with <br>
 ; g - replace local relative/absolute <img> tag with a warning label
-copy_targets="Without Background"$s:b(mark):c:i:x,OneNote$s:b(mark):c:i:m:a:x,"Microsoft Word"$s:p:b(mark|pre):c(pre):i:m:a:x,"WeChat Public Account"$s:p:b(mark|pre):c(pre):g:m:x:n,"Web Editor"$s:p:b(mark|pre):c(pre):m:x:n,"Raw HTML"$r:x
+; d - add <span> to <code> which is not inside <pre>
+; f - replace &quot; with ' in font-family style
+copy_targets="Without Background"$s:b(mark):c:i:x,OneNote$s:b(mark):c:i:m:a:x,"Microsoft Word"$s:p:b(mark|pre):c(pre):i:m:a:x,"WeChat Public Account"$s:p:b(mark|pre):c(pre):g:m:x:n:f,"Web Editor"$s:p:b(mark|pre):c(pre):m:x:n,"Raw HTML"$r:x
 
 [shortcuts]
 ; Define shortcuts here, with each item in the form "operation=keysequence".

+ 111 - 1
src/utils/vwebutils.cpp

@@ -221,6 +221,14 @@ bool VWebUtils::alterHtmlByTargetAction(const QUrl &p_baseUrl, QString &p_html,
         altered = replaceLocalImgWithWarningLabel(p_html);
         break;
 
+    case 'd':
+        altered = addSpanInsideCode(p_html);
+        break;
+
+    case 'f':
+        altered = replaceQuoteInFontFamily(p_html);
+        break;
+
     default:
         break;
     }
@@ -228,7 +236,10 @@ bool VWebUtils::alterHtmlByTargetAction(const QUrl &p_baseUrl, QString &p_html,
     return altered;
 }
 
-static int skipToTagEnd(const QString &p_html, int p_pos, const QString &p_tag)
+static int skipToTagEnd(const QString &p_html,
+                        int p_pos,
+                        const QString &p_tag,
+                        int *p_endTagIdx = NULL)
 {
     QRegExp beginReg(QString("<%1 ").arg(p_tag));
     QRegExp endReg(QString("</%1>").arg(p_tag));
@@ -243,7 +254,13 @@ static int skipToTagEnd(const QString &p_html, int p_pos, const QString &p_tag)
     }
 
     if (nEnd > -1) {
+        if (p_endTagIdx) {
+            *p_endTagIdx = nEnd;
+        }
+
         pos = nEnd + endReg.matchedLength();
+    } else if (p_endTagIdx) {
+        *p_endTagIdx = -1;
     }
 
     return pos;
@@ -675,3 +692,96 @@ bool VWebUtils::replaceLocalImgWithWarningLabel(QString &p_html)
 
     return altered;
 }
+
+bool VWebUtils::addSpanInsideCode(QString &p_html)
+{
+    bool altered = false;
+    int pos = 0;
+
+    while (pos < p_html.size()) {
+        int tagIdx = p_html.indexOf(m_tagReg, pos);
+        if (tagIdx == -1) {
+            break;
+        }
+
+        QString tagName = m_tagReg.cap(1);
+        QString lowerName = tagName.toLower();
+        if (lowerName == "pre") {
+            // Skip <pre>.
+            pos = skipToTagEnd(p_html, tagIdx + m_tagReg.matchedLength(), tagName);
+            continue;
+        }
+
+        if (lowerName != "code") {
+            pos = tagIdx + m_tagReg.matchedLength();
+            continue;
+        }
+
+        int idx = tagIdx + m_tagReg.matchedLength() - 1;
+        Q_ASSERT(p_html[idx] == '>');
+        QString span = QString("><span%1>").arg(m_tagReg.cap(2));
+        p_html.replace(idx, 1, span);
+
+        int codeEnd = skipToTagEnd(p_html, idx + span.size(), tagName, &idx);
+        Q_ASSERT(idx > -1);
+        Q_ASSERT(codeEnd - idx == 7);
+        Q_ASSERT(p_html[idx] == '<');
+        p_html.replace(idx, 1, "</span><");
+
+        pos = codeEnd;
+
+        altered = true;
+    }
+
+    return altered;
+}
+
+// @p_html is the style string.
+static bool replaceQuoteInFontFamilyInStyleString(QString &p_html)
+{
+    QRegExp reg("font-family:((&quot;)|[^;])+;");
+    int idx = p_html.indexOf(reg);
+    if (idx == -1) {
+        return false;
+    }
+
+    QString quote("&quot;");
+    QString family = reg.cap(0);
+    if (family.indexOf(quote) == -1) {
+        return false;
+    }
+
+    QString newFamily = family.replace(quote, "'");
+    p_html.replace(idx, reg.matchedLength(), newFamily);
+    return true;
+}
+
+bool VWebUtils::replaceQuoteInFontFamily(QString &p_html)
+{
+    bool altered = false;
+    int pos = 0;
+
+    while (pos < p_html.size()) {
+        int idx = p_html.indexOf(m_styleTagReg, pos);
+        if (idx == -1) {
+            break;
+        }
+
+        QString styleStr = m_styleTagReg.cap(3);
+        if (replaceQuoteInFontFamilyInStyleString(styleStr)) {
+            QString newTag = QString("<%1%2style=\"%3\"%4>").arg(m_styleTagReg.cap(1))
+                                                            .arg(m_styleTagReg.cap(2))
+                                                            .arg(styleStr)
+                                                            .arg(m_styleTagReg.cap(4));
+            p_html.replace(idx, m_styleTagReg.matchedLength(), newTag);
+
+            pos = idx + newTag.size();
+
+            altered = true;
+        } else {
+            pos = idx + m_styleTagReg.matchedLength();
+        }
+    }
+
+    return altered;
+}

+ 6 - 0
src/utils/vwebutils.h

@@ -97,6 +97,12 @@ private:
     // Replace local absolute/relative <img> tag with a warning label.
     bool replaceLocalImgWithWarningLabel(QString &p_html);
 
+    // Add <span> inside <code> not in <pre>.
+    bool addSpanInsideCode(QString &p_html);
+
+    // Replace &quot; in font-family with '.
+    bool replaceQuoteInFontFamily(QString &p_html);
+
     QVector<CopyTarget> m_copyTargets;
 
     // Custom styles to remove when copied.