mirror of
https://github.com/qTox/qTox.git
synced 2024-03-22 14:00:36 +08:00
Added markdown support. Underline, Italics, Strikethrough and Bold supported.
This commit is contained in:
parent
bfb866cfa2
commit
4a1da7099f
|
@ -54,6 +54,9 @@ ChatMessage::Ptr ChatMessage::createChatMessage(const QString &sender, const QSt
|
||||||
//quotes (green text)
|
//quotes (green text)
|
||||||
text = detectQuotes(detectAnchors(text), type);
|
text = detectQuotes(detectAnchors(text), type);
|
||||||
|
|
||||||
|
//markdown
|
||||||
|
text = detectMarkdown(text);
|
||||||
|
|
||||||
switch(type)
|
switch(type)
|
||||||
{
|
{
|
||||||
case ACTION:
|
case ACTION:
|
||||||
|
@ -175,6 +178,58 @@ void ChatMessage::hideDate()
|
||||||
c->hide();
|
c->hide();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString ChatMessage::detectMarkdown(const QString &str)
|
||||||
|
{
|
||||||
|
QString out;
|
||||||
|
|
||||||
|
// Create regex for certain markdown syntax
|
||||||
|
QRegExp exp("(\\*\\*)([^\\*\\*]*)(\\*\\*)" // Bold **text**
|
||||||
|
"|(\\*)([^\\*]*)(\\*)" // Italics *text*
|
||||||
|
"|(\\_)([^\\_]*)(\\_)" // Italics _text_
|
||||||
|
"|(\\_\\_)([^\\_\\_]*)(\\_\\_)" // Italics __text__
|
||||||
|
"|(\\-)([^\\-]*)(\\-)" // Underline -text-
|
||||||
|
"|(\\~)([^\\~]*)(\\~)" // Strike ~text~
|
||||||
|
"|(\\~~)([^\\~\\~]*)(\\~~)" // Strike ~~text~~
|
||||||
|
);
|
||||||
|
|
||||||
|
// Support for multi-line text
|
||||||
|
QStringList messageLines = str.split("\n");
|
||||||
|
QStringList outLines;
|
||||||
|
for (int i = 0; i < messageLines.size(); ++i)
|
||||||
|
{
|
||||||
|
out = messageLines.at(i);
|
||||||
|
int offset = 0;
|
||||||
|
while ((offset = exp.indexIn(out, offset)) != -1)
|
||||||
|
{
|
||||||
|
QString snippet = exp.cap();
|
||||||
|
|
||||||
|
QString htmledSnippet;
|
||||||
|
|
||||||
|
// Match captured string to corresponding md format
|
||||||
|
if (exp.cap(1) == "**") // Bold **text**
|
||||||
|
htmledSnippet = QString("<b>%1</b>").arg(snippet.mid(2,snippet.length()-4));
|
||||||
|
else if (exp.cap(4) == "*" && snippet.length() > 2) // Italics *text*
|
||||||
|
htmledSnippet = QString("<i>%1</i>").arg(snippet.mid(1,snippet.length()-2));
|
||||||
|
else if (exp.cap(7) == "_" && snippet.length() > 2) // Italics _text_
|
||||||
|
htmledSnippet = QString("<i>%1</i>").arg(snippet.mid(1,snippet.length()-2));
|
||||||
|
else if (exp.cap(10) == "__"&& snippet.length() > 4) // Italics __text__
|
||||||
|
htmledSnippet = QString("<i>%1</i>").arg(snippet.mid(2,snippet.length()-4));
|
||||||
|
else if (exp.cap(13) == "-" && snippet.length() > 2) // Underline -text-
|
||||||
|
htmledSnippet = QString("<u>%1</u>").arg(snippet.mid(1,snippet.length()-2));
|
||||||
|
else if (exp.cap(16) == "~" && snippet.length() > 2) // Strikethrough ~text~
|
||||||
|
htmledSnippet = QString("<s>%1</s>").arg(snippet.mid(1,snippet.length()-2));
|
||||||
|
else if (exp.cap(19) == "~~" && snippet.length() > 4) // Strikethrough ~~text~~
|
||||||
|
htmledSnippet = QString("<s>%1</s>").arg(snippet.mid(2,snippet.length()-4));
|
||||||
|
else
|
||||||
|
htmledSnippet = snippet;
|
||||||
|
out.replace(offset, exp.cap().length(), htmledSnippet);
|
||||||
|
offset += htmledSnippet.length();
|
||||||
|
}
|
||||||
|
outLines.push_back(out);
|
||||||
|
}
|
||||||
|
return outLines.join("\n");
|
||||||
|
}
|
||||||
|
|
||||||
QString ChatMessage::detectAnchors(const QString &str)
|
QString ChatMessage::detectAnchors(const QString &str)
|
||||||
{
|
{
|
||||||
QString out;
|
QString out;
|
||||||
|
|
|
@ -61,6 +61,7 @@ public:
|
||||||
void hideDate();
|
void hideDate();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
static QString detectMarkdown(const QString& str);
|
||||||
static QString detectAnchors(const QString& str);
|
static QString detectAnchors(const QString& str);
|
||||||
static QString detectQuotes(const QString& str, MessageType type);
|
static QString detectQuotes(const QString& str, MessageType type);
|
||||||
static QString wrapDiv(const QString& str, const QString& div);
|
static QString wrapDiv(const QString& str, const QString& div);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user