mirror of
https://github.com/qTox/qTox.git
synced 2024-03-22 14:00:36 +08:00
Removed uneccessary for loop in markdown and url parsing.
This commit is contained in:
parent
252c9c8fae
commit
d343408749
|
@ -183,7 +183,7 @@ void ChatMessage::hideDate()
|
|||
|
||||
QString ChatMessage::detectMarkdown(const QString &str)
|
||||
{
|
||||
QString out;
|
||||
QString out = str;
|
||||
|
||||
// Create regex for certain markdown syntax
|
||||
QRegExp exp("(\\*\\*)([^\\*\\*]{2,})(\\*\\*)" // Bold **text**
|
||||
|
@ -196,60 +196,53 @@ QString ChatMessage::detectMarkdown(const QString &str)
|
|||
"|(\\`)([^\\`]{2,})(\\`)" // Codeblock `text`
|
||||
);
|
||||
|
||||
// Support for multi-line text
|
||||
QStringList messageLines = str.split("\n");
|
||||
QStringList outLines;
|
||||
for (int i = 0; i < messageLines.size(); ++i)
|
||||
int offset = 0;
|
||||
while ((offset = exp.indexIn(out, offset)) != -1)
|
||||
{
|
||||
out = messageLines.at(i);
|
||||
int offset = 0;
|
||||
while ((offset = exp.indexIn(out, offset)) != -1)
|
||||
QString snipCheck = out.mid(offset-1,exp.cap(0).length()+2);
|
||||
QString snippet = exp.cap(0).trimmed();
|
||||
|
||||
QString htmledSnippet;
|
||||
|
||||
// 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.toHtmlEscaped().length()))
|
||||
{
|
||||
QString snipCheck = out.mid(offset-1,exp.cap(0).length()+2);
|
||||
QString snippet = exp.cap(0).trimmed();
|
||||
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;
|
||||
|
||||
QString htmledSnippet;
|
||||
|
||||
// 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.toHtmlEscaped().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(" <b>%1</b> ").arg(snippet.mid(mul,snippet.length()-2*mul));
|
||||
else if (exp.cap(4) == "*" && snippet.length() > 2) // Italics *text*
|
||||
htmledSnippet = QString(" <i>%1</i> ").arg(snippet.mid(mul/2,snippet.length()-mul));
|
||||
else if (exp.cap(7) == "_" && snippet.length() > 2) // Italics _text_
|
||||
htmledSnippet = QString(" <i>%1</i> ").arg(snippet.mid(mul/2,snippet.length()-mul));
|
||||
else if (exp.cap(10) == "__"&& snippet.length() > 4) // Bold __text__
|
||||
htmledSnippet = QString(" <b>%1</b> ").arg(snippet.mid(mul,snippet.length()-2*mul));
|
||||
else if (exp.cap(13) == "-" && snippet.length() > 2) // Underline -text-
|
||||
htmledSnippet = QString(" <u>%1</u> ").arg(snippet.mid(mul/2,snippet.length()-mul));
|
||||
else if (exp.cap(16) == "~" && snippet.length() > 2) // Strikethrough ~text~
|
||||
htmledSnippet = QString(" <s>%1</s> ").arg(snippet.mid(mul/2,snippet.length()-mul));
|
||||
else if (exp.cap(19) == "~~" && snippet.length() > 4) // Strikethrough ~~text~~
|
||||
htmledSnippet = QString(" <s>%1</s> ").arg(snippet.mid(mul,snippet.length()-2*mul));
|
||||
else if (exp.cap(22) == "`" && snippet.length() > 2) // Codeblock `text`
|
||||
htmledSnippet = QString("<font color=#595959><code>%1</code></font>").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();
|
||||
}
|
||||
outLines.push_back(out);
|
||||
// Match captured string to corresponding md format
|
||||
if (exp.cap(1) == "**") // Bold **text**
|
||||
htmledSnippet = QString(" <b>%1</b> ").arg(snippet.mid(mul,snippet.length()-2*mul));
|
||||
else if (exp.cap(4) == "*" && snippet.length() > 2) // Italics *text*
|
||||
htmledSnippet = QString(" <i>%1</i> ").arg(snippet.mid(mul/2,snippet.length()-mul));
|
||||
else if (exp.cap(7) == "_" && snippet.length() > 2) // Italics _text_
|
||||
htmledSnippet = QString(" <i>%1</i> ").arg(snippet.mid(mul/2,snippet.length()-mul));
|
||||
else if (exp.cap(10) == "__"&& snippet.length() > 4) // Bold __text__
|
||||
htmledSnippet = QString(" <b>%1</b> ").arg(snippet.mid(mul,snippet.length()-2*mul));
|
||||
else if (exp.cap(13) == "-" && snippet.length() > 2) // Underline -text-
|
||||
htmledSnippet = QString(" <u>%1</u> ").arg(snippet.mid(mul/2,snippet.length()-mul));
|
||||
else if (exp.cap(16) == "~" && snippet.length() > 2) // Strikethrough ~text~
|
||||
htmledSnippet = QString(" <s>%1</s> ").arg(snippet.mid(mul/2,snippet.length()-mul));
|
||||
else if (exp.cap(19) == "~~" && snippet.length() > 4) // Strikethrough ~~text~~
|
||||
htmledSnippet = QString(" <s>%1</s> ").arg(snippet.mid(mul,snippet.length()-2*mul));
|
||||
else if (exp.cap(22) == "`" && snippet.length() > 2) // Codeblock `text`
|
||||
htmledSnippet = QString("<font color=#595959><code>%1</code></font>").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();
|
||||
}
|
||||
return outLines.join("\n");
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
QString ChatMessage::detectAnchors(const QString &str)
|
||||
{
|
||||
QString out;
|
||||
QString out = str;
|
||||
|
||||
// detect URIs
|
||||
QRegExp exp("("
|
||||
|
@ -261,34 +254,28 @@ QString ChatMessage::detectAnchors(const QString &str)
|
|||
"|(?:\\b)(mailto:\\S+@\\S+\\.\\S+)" //@mail link
|
||||
"|(?:\\b)(tox:\\S+@\\S+)"); // starts with `tox` then : and only alpha-digits till the end
|
||||
// also accepts tox:agilob@net as simplified TOX ID
|
||||
//support for multi-line text
|
||||
QStringList messageLines = str.split("\n");
|
||||
QStringList outLines;
|
||||
for (int i = 0; i < messageLines.size(); ++i)
|
||||
|
||||
int offset = 0;
|
||||
while ((offset = exp.indexIn(out, offset)) != -1)
|
||||
{
|
||||
out = messageLines.at(i);
|
||||
int offset = 0;
|
||||
while ((offset = exp.indexIn(out, offset)) != -1)
|
||||
QString url = exp.cap();
|
||||
// If there's a trailing " it's a HTML attribute, e.g. a smiley img's title=":tox:"
|
||||
if (url == "tox:\"")
|
||||
{
|
||||
QString url = exp.cap();
|
||||
// If there's a trailing " it's a HTML attribute, e.g. a smiley img's title=":tox:"
|
||||
if (url == "tox:\"")
|
||||
{
|
||||
offset += url.length();
|
||||
continue;
|
||||
}
|
||||
QString htmledUrl;
|
||||
// add scheme if not specified
|
||||
if (exp.cap(2) == "www.")
|
||||
htmledUrl = QString("<a href=\"http://%1\">%1</a>").arg(url);
|
||||
else
|
||||
htmledUrl = QString("<a href=\"%1\">%1</a>").arg(url);
|
||||
out.replace(offset, exp.cap().length(), htmledUrl);
|
||||
offset += htmledUrl.length();
|
||||
offset += url.length();
|
||||
continue;
|
||||
}
|
||||
outLines.push_back(out);
|
||||
QString htmledUrl;
|
||||
// add scheme if not specified
|
||||
if (exp.cap(2) == "www.")
|
||||
htmledUrl = QString("<a href=\"http://%1\">%1</a>").arg(url);
|
||||
else
|
||||
htmledUrl = QString("<a href=\"%1\">%1</a>").arg(url);
|
||||
out.replace(offset, exp.cap().length(), htmledUrl);
|
||||
offset += htmledUrl.length();
|
||||
}
|
||||
return outLines.join("\n");
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
QString ChatMessage::detectQuotes(const QString& str, MessageType type)
|
||||
|
|
Loading…
Reference in New Issue
Block a user