@ -55,9 +55,9 @@ ChatMessage::Ptr ChatMessage::createChatMessage(const QString &sender, const QSt
@@ -55,9 +55,9 @@ ChatMessage::Ptr ChatMessage::createChatMessage(const QString &sender, const QSt
//quotes (green text)
text = detectQuotes ( detectAnchors ( text ) , type ) ;
//markdown
if ( Settings : : getInstance ( ) . getMarkdown Preference ( ) ! = NONE )
text = detectMarkdown ( text ) ;
//text styling
if ( Settings : : getInstance ( ) . getStyle Preference ( ) ! = NONE )
text = detectStyle ( text ) ;
switch ( type )
{
@ -198,19 +198,21 @@ void ChatMessage::hideDate()
@@ -198,19 +198,21 @@ void ChatMessage::hideDate()
c - > hide ( ) ;
}
QString ChatMessage : : detectMarkdown ( const QString & str )
QString ChatMessage : : detectStyle ( const QString & str )
{
QString out = str ;
// Create regex for certain markdown syntax
QRegExp exp ( " ( \\ * \\ *) ( [ ^ \ \ * \ \ * ] { 2 , } ) ( \ \ * \ \ * ) " // Bold **text**
" |( \\ *)([^ \\ *]{2,})( \\ *) " // Italics *text*
" |( \\ _)([^ \\ _]{2,})( \\ _) " // Italics _text_
" |( \\ _ \\ _)([^ \\ _ \\ _]{2,})( \\ _ \\ _) " // Bold __text__
" |( \\ -)([^ \\ -]{2,})( \\ -) " // Underline -text-
" |( \\ ~)([^ \\ ~]{2,})( \\ ~) " // Strike ~text~
" |( \\ ~~)([^ \\ ~ \\ ~]{2,})( \\ ~~) " // Strike ~~text~~
" |( \\ `)([^ \\ `]{2,})( \\ `) " // Codeblock `text`
// Create regex for text styling syntax
QRegExp exp ( " ( \\ *) ( [ ^ \ \ * ] { 2 , } ) ( \ \ * ) " // Bold *text*
" |( \\ * \\ *)([^ \\ * \\ *]{2,})( \\ * \\ *) " // Bold **text**
" |( \\ /)([^ \\ /]{2,})( \\ /) " // Italics /text/
" |( \\ / \\ /)([^ \\ / \\ /]{2,})( \\ / \\ /) " // Italics //text//
" |( \\ _)([^ \\ _]{2,})( \\ _) " // Underline _text_
" |( \\ _ \\ _)([^ \\ _ \\ _]{2,})( \\ _ \\ _) " // Underline __text__
" |( \\ ~)([^ \\ ~]{2,})( \\ ~) " // Strike ~text~
" |( \\ ~ \\ ~)([^ \\ ~ \\ ~]{2,})( \\ ~ \\ ~) " // Strike ~~text~~
" |( \\ `)([^ \\ `]{2,})( \\ `) " // Codeblock `text`
" |( \\ ` \\ ` \\ `)([^ \\ ` \\ ` \\ `]{2,})( \\ ` \\ ` \\ `) " // Codeblock ```\ntext\n```
) ;
int offset = 0 ;
@ -224,28 +226,32 @@ QString ChatMessage::detectMarkdown(const QString &str)
@@ -224,28 +226,32 @@ QString ChatMessage::detectMarkdown(const QString &str)
if ( ( snipCheck . startsWith ( ' ' ) | | snipCheck . startsWith ( ' > ' ) | | offset = = 0 )
& & ( ( snipCheck . endsWith ( ' ' ) | | snipCheck . endsWith ( ' < ' ) ) | | offset + snippet . length ( ) = = out . length ( ) ) )
{
int mul = 0 ; // Determines how many characters to strip from markdown text
// Set mul depending on markd ownPreference
if ( Settings : : getInstance ( ) . getMarkdown Preference ( ) = = WITHOUT_CHARS )
int mul = 0 ; // Determines how many characters to strip from text
// Set mul depending on style ownPreference
if ( Settings : : getInstance ( ) . getStyle Preference ( ) = = WITHOUT_CHARS )
mul = 2 ;
// Match captured string to corresponding md format
if ( exp . cap ( 1 ) = = " ** " ) // Bold **text**
// Match captured string to corresponding style format
if ( exp . cap ( 1 ) = = " * " & & snippet . length ( ) > 2 ) // Bold *text*
htmledSnippet = QString ( " <b>%1</b> " ) . arg ( snippet . mid ( mul / 2 , snippet . length ( ) - mul ) ) ;
else if ( exp . cap ( 4 ) = = " ** " & & snippet . length ( ) > 4 ) // 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*
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 ( 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-
else if ( exp . cap ( 10 ) = = " // " & & snippet . length ( ) > 4 ) // Italics //text//
htmledSnippet = QString ( " <i>%1</i> " ) . 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~
else if ( exp . cap ( 16 ) = = " __ " & & snippet . length ( ) > 4 ) // Underline __text__
htmledSnippet = QString ( " <u>%1</u> " ) . arg ( snippet . mid ( mul , snippet . length ( ) - 2 * mul ) ) ;
else if ( exp . cap ( 19 ) = = " ~ " & & snippet . length ( ) > 2 ) // Strike ~text~
htmledSnippet = QString ( " <s>%1</s> " ) . arg ( snippet . mid ( mul / 2 , snippet . length ( ) - mul ) ) ;
else if ( exp . cap ( 19 ) = = " ~~ " & & snippet . length ( ) > 4 ) // Strikethrough ~~text~~
else if ( exp . cap ( 22 ) = = " ~~ " & & snippet . length ( ) > 4 ) // Strike ~~text~~
htmledSnippet = QString ( " <s>%1</s> " ) . arg ( snippet . mid ( mul , snippet . length ( ) - 2 * mul ) ) ;
else if ( exp . cap ( 22 ) = = " ` " & & snippet . length ( ) > 2 ) // Codeblock `text`
else if ( exp . cap ( 25 ) = = " ` " & & snippet . length ( ) > 2 ) // Codeblock `text`
htmledSnippet = QString ( " <font color=#595959><code>%1</code></font> " ) . arg ( snippet . mid ( mul / 2 , snippet . length ( ) - mul ) ) ;
else if ( exp . cap ( 28 ) = = " ``` " & & snippet . length ( ) > 6 ) // Codeblock ```text```
htmledSnippet = QString ( " <font color=#595959><code>%1</code></font> " ) . arg ( snippet . mid ( 4 * mul , snippet . length ( ) - 8 * mul ) ) ;
else
htmledSnippet = snippet ;
out . replace ( offset , exp . cap ( ) . length ( ) , htmledSnippet ) ;