From 68cad0db3846c7772d975efc1fdf4ceece7de128 Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Sun, 13 Mar 2016 01:54:09 -0800 Subject: [PATCH 1/2] Added codeblock support with ` char. Parse markdown even if manual newline is added before or after markdown'd text. Ex: Check out my\n*skills*. Removed extraneous debugging. Fixed some characters accidently parsing markdown formatting, such as **vv**' Use enum instead of magic number to check md setting. --- src/chatlog/chatmessage.cpp | 60 ++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/src/chatlog/chatmessage.cpp b/src/chatlog/chatmessage.cpp index 512cff748..27836d791 100644 --- a/src/chatlog/chatmessage.cpp +++ b/src/chatlog/chatmessage.cpp @@ -193,6 +193,7 @@ QString ChatMessage::detectMarkdown(const QString &str) "|(\\-)([^\\-]{2,})(\\-)" // Underline -text- "|(\\~)([^\\~]{2,})(\\~)" // Strike ~text~ "|(\\~~)([^\\~\\~]{2,})(\\~~)" // Strike ~~text~~ + "|(\\`)([^\\`]{2,})(\\`)" // Codeblock `text` ); // Support for multi-line text @@ -209,38 +210,37 @@ QString ChatMessage::detectMarkdown(const QString &str) QString htmledSnippet; - // Check for surrounding spaces and/or beginning/end of line - if (!((snipCheck.startsWith(' ') || offset == 0) && (snipCheck.endsWith(' ') || offset + snipCheck.trimmed().length() == out.length()))) + // Only parse if surrounded by spaces, newline(s) and/or beginning/end of line + if ((snipCheck.startsWith(' ') || snipCheck.startsWith('>') || offset == 0) && ((snipCheck.endsWith(' ') || snipCheck.endsWith('<')) || offset + snippet.toHtmlEscaped().length() == out.length())) { + int mul = 0; // Determines how many characters to strip from markdown text + // Set mul depending on markdownPreference + if (Settings::getInstance().getMarkdownPreference() == MarkdownType::WITHOUT_CHARS) + mul = 2; + + // Match captured string to corresponding md format + if (exp.cap(1) == "**") // Bold **text** + htmledSnippet = QString(" %1 ").arg(snippet.mid(mul,snippet.length()-2*mul)); + else if (exp.cap(4) == "*" && snippet.length() > 2) // Italics *text* + htmledSnippet = QString(" %1 ").arg(snippet.mid(mul/2,snippet.length()-mul)); + else if (exp.cap(7) == "_" && snippet.length() > 2) // Italics _text_ + htmledSnippet = QString(" %1 ").arg(snippet.mid(mul/2,snippet.length()-mul)); + else if (exp.cap(10) == "__"&& snippet.length() > 4) // Bold __text__ + htmledSnippet = QString(" %1 ").arg(snippet.mid(mul,snippet.length()-2*mul)); + else if (exp.cap(13) == "-" && snippet.length() > 2) // Underline -text- + htmledSnippet = QString(" %1 ").arg(snippet.mid(mul/2,snippet.length()-mul)); + else if (exp.cap(16) == "~" && snippet.length() > 2) // Strikethrough ~text~ + htmledSnippet = QString(" %1 ").arg(snippet.mid(mul/2,snippet.length()-mul)); + else if (exp.cap(19) == "~~" && snippet.length() > 4) // Strikethrough ~~text~~ + htmledSnippet = QString(" %1 ").arg(snippet.mid(mul,snippet.length()-2*mul)); + else if (exp.cap(22) == "`" && snippet.length() > 2) // Codeblock `text` + htmledSnippet = QString("%1").arg(snippet.mid(mul/2,snippet.length()-mul)); + else + htmledSnippet = snippet; + out.replace(offset, exp.cap().length(), htmledSnippet); + offset += htmledSnippet.length(); + } else offset += snippet.length(); - continue; - } - - int mul = 0; // Determines how many characters to strip from markdown text - - // Set mul depending on markdownPreference - if (Settings::getInstance().getMarkdownPreference() == 2) - mul = 2; - - // Match captured string to corresponding md format - if (exp.cap(1) == "**") // Bold **text** - htmledSnippet = QString(" %1 ").arg(snippet.mid(mul,snippet.length()-2*mul)); - else if (exp.cap(4) == "*" && snippet.length() > 2) // Italics *text* - htmledSnippet = QString(" %1 ").arg(snippet.mid(mul/2,snippet.length()-mul)); - else if (exp.cap(7) == "_" && snippet.length() > 2) // Italics _text_ - htmledSnippet = QString(" %1 ").arg(snippet.mid(mul/2,snippet.length()-mul)); - else if (exp.cap(10) == "__"&& snippet.length() > 4) // Bold __text__ - htmledSnippet = QString(" %1 ").arg(snippet.mid(mul,snippet.length()-2*mul)); - else if (exp.cap(13) == "-" && snippet.length() > 2) // Underline -text- - htmledSnippet = QString(" %1 ").arg(snippet.mid(mul/2,snippet.length()-mul)); - else if (exp.cap(16) == "~" && snippet.length() > 2) // Strikethrough ~text~ - htmledSnippet = QString(" %1 ").arg(snippet.mid(mul/2,snippet.length()-mul)); - else if (exp.cap(19) == "~~" && snippet.length() > 4) // Strikethrough ~~text~~ - htmledSnippet = QString(" %1 ").arg(snippet.mid(mul,snippet.length()-2*mul)); - else - htmledSnippet = snippet; - out.replace(offset, exp.cap().length(), htmledSnippet); - offset += htmledSnippet.length(); } outLines.push_back(out); } From 04ae894fc2fd5fdc29968d5b5f34b31133bbef7c Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Mon, 21 Mar 2016 22:31:23 -0400 Subject: [PATCH 2/2] Fixed HTML chars breaking recognition. --- src/chatlog/chatmessage.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/chatlog/chatmessage.cpp b/src/chatlog/chatmessage.cpp index 27836d791..d66441b88 100644 --- a/src/chatlog/chatmessage.cpp +++ b/src/chatlog/chatmessage.cpp @@ -210,8 +210,10 @@ QString ChatMessage::detectMarkdown(const QString &str) QString htmledSnippet; + qDebug() << "snipCheck: " << snipCheck << " out: " << out << " snipLength: " << snippet.toHtmlEscaped().length() << " outLength: " << out.toHtmlEscaped().length(); + // Only parse if surrounded by spaces, newline(s) and/or beginning/end of line - if ((snipCheck.startsWith(' ') || snipCheck.startsWith('>') || offset == 0) && ((snipCheck.endsWith(' ') || snipCheck.endsWith('<')) || offset + snippet.toHtmlEscaped().length() == out.length())) + if ((snipCheck.startsWith(' ') || snipCheck.startsWith('>') || offset == 0) && ((snipCheck.endsWith(' ') || snipCheck.endsWith('<')) || offset + snippet.toHtmlEscaped().length() == out.toHtmlEscaped().length())) { int mul = 0; // Determines how many characters to strip from markdown text // Set mul depending on markdownPreference