1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

Added markdown preference chooser to settings which chat abides by.

Don't parse md if only one character is involved.
Prevent things like ~3~ being caught
This commit is contained in:
Andrew Morgan 2016-01-23 00:06:17 -08:00
parent 4a1da7099f
commit e15315b618
7 changed files with 130 additions and 23 deletions

View File

@ -55,6 +55,7 @@ ChatMessage::Ptr ChatMessage::createChatMessage(const QString &sender, const QSt
text = detectQuotes(detectAnchors(text), type); text = detectQuotes(detectAnchors(text), type);
//markdown //markdown
if (Settings::getInstance().getMarkdownPreference() != 0)
text = detectMarkdown(text); text = detectMarkdown(text);
switch(type) switch(type)
@ -183,13 +184,13 @@ QString ChatMessage::detectMarkdown(const QString &str)
QString out; QString out;
// Create regex for certain markdown syntax // Create regex for certain markdown syntax
QRegExp exp("(\\*\\*)([^\\*\\*]*)(\\*\\*)" // Bold **text** QRegExp exp("(\\*\\*)([^\\*\\*]{2,})(\\*\\*)" // Bold **text**
"|(\\*)([^\\*]*)(\\*)" // Italics *text* "|(\\*)([^\\*]{2,})(\\*)" // Italics *text*
"|(\\_)([^\\_]*)(\\_)" // Italics _text_ "|(\\_)([^\\_]{2,})(\\_)" // Italics _text_
"|(\\_\\_)([^\\_\\_]*)(\\_\\_)" // Italics __text__ "|(\\_\\_)([^\\_\\_]{2,})(\\_\\_)" // Italics __text__
"|(\\-)([^\\-]*)(\\-)" // Underline -text- "|(\\-)([^\\-]{2,})(\\-)" // Underline -text-
"|(\\~)([^\\~]*)(\\~)" // Strike ~text~ "|(\\~)([^\\~]{2,})(\\~)" // Strike ~text~
"|(\\~~)([^\\~\\~]*)(\\~~)" // Strike ~~text~~ "|(\\~~)([^\\~\\~]{2,})(\\~~)" // Strike ~~text~~
); );
// Support for multi-line text // Support for multi-line text
@ -205,21 +206,34 @@ QString ChatMessage::detectMarkdown(const QString &str)
QString htmledSnippet; 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())))
{
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 // Match captured string to corresponding md format
if (exp.cap(1) == "**") // Bold **text** if (exp.cap(1) == "**") // Bold **text**
htmledSnippet = QString("<b>%1</b>").arg(snippet.mid(2,snippet.length()-4)); 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(4) == "*" && snippet.length() > 2) // Italics *text*
htmledSnippet = QString("<i>%1</i>").arg(snippet.mid(1,snippet.length()-2)); htmledSnippet = QString("<i>%1</i>").arg(snippet.mid(mul/2,snippet.length()-mul));
else if (exp.cap(7) == "_" && snippet.length() > 2) // Italics _text_ else if (exp.cap(7) == "_" && snippet.length() > 2) // Italics _text_
htmledSnippet = QString("<i>%1</i>").arg(snippet.mid(1,snippet.length()-2)); htmledSnippet = QString("<i>%1</i>").arg(snippet.mid(mul/2,snippet.length()-mul));
else if (exp.cap(10) == "__"&& snippet.length() > 4) // Italics __text__ else if (exp.cap(10) == "__"&& snippet.length() > 4) // Italics __text__
htmledSnippet = QString("<i>%1</i>").arg(snippet.mid(2,snippet.length()-4)); htmledSnippet = QString("<i>%1</i>").arg(snippet.mid(mul,snippet.length()-2*mul));
else if (exp.cap(13) == "-" && snippet.length() > 2) // Underline -text- else if (exp.cap(13) == "-" && snippet.length() > 2) // Underline -text-
htmledSnippet = QString("<u>%1</u>").arg(snippet.mid(1,snippet.length()-2)); 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() > 2) // Strikethrough ~text~
htmledSnippet = QString("<s>%1</s>").arg(snippet.mid(1,snippet.length()-2)); 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(19) == "~~" && snippet.length() > 4) // Strikethrough ~~text~~
htmledSnippet = QString("<s>%1</s>").arg(snippet.mid(2,snippet.length()-4)); htmledSnippet = QString("<s>%1</s>").arg(snippet.mid(mul,snippet.length()-2*mul));
else else
htmledSnippet = snippet; htmledSnippet = snippet;
out.replace(offset, exp.cap().length(), htmledSnippet); out.replace(offset, exp.cap().length(), htmledSnippet);

View File

View File

@ -177,6 +177,7 @@ void Settings::loadGlobal()
separateWindow = s.value("separateWindow", false).toBool(); separateWindow = s.value("separateWindow", false).toBool();
dontGroupWindows = s.value("dontGroupWindows", true).toBool(); dontGroupWindows = s.value("dontGroupWindows", true).toBool();
groupchatPosition = s.value("groupchatPosition", true).toBool(); groupchatPosition = s.value("groupchatPosition", true).toBool();
markdownPreference = s.value("markdownPreference", 1).toInt();
s.endGroup(); s.endGroup();
s.beginGroup("Advanced"); s.beginGroup("Advanced");
@ -242,7 +243,7 @@ void Settings::loadGlobal()
camVideoFPS = s.value("camVideoFPS", 0).toUInt(); camVideoFPS = s.value("camVideoFPS", 0).toUInt();
s.endGroup(); s.endGroup();
// Read the embedded DHT bootsrap nodes list if needed // Read the embedded DHT bootstrap nodes list if needed
if (dhtServerList.isEmpty()) if (dhtServerList.isEmpty())
{ {
QSettings rcs(":/conf/settings.ini", QSettings::IniFormat); QSettings rcs(":/conf/settings.ini", QSettings::IniFormat);
@ -395,6 +396,7 @@ void Settings::saveGlobal()
s.setValue("groupchatPosition", groupchatPosition); s.setValue("groupchatPosition", groupchatPosition);
s.setValue("autoSaveEnabled", autoSaveEnabled); s.setValue("autoSaveEnabled", autoSaveEnabled);
s.setValue("globalAutoAcceptDir", globalAutoAcceptDir); s.setValue("globalAutoAcceptDir", globalAutoAcceptDir);
s.setValue("markdownPreference", markdownPreference);
s.endGroup(); s.endGroup();
s.beginGroup("Advanced"); s.beginGroup("Advanced");
@ -1028,6 +1030,23 @@ void Settings::setDateFormat(const QString &format)
dateFormat = format; dateFormat = format;
} }
int Settings::getMarkdownPreference() const
{
QMutexLocker locker{&bigLock};
return markdownPreference;
}
void Settings::setMarkdownPreference(int newValue)
{
QMutexLocker locker{&bigLock};
if (newValue < 0)
newValue = 1;
else if (newValue > 2)
newValue = 2;
markdownPreference = newValue;
}
QByteArray Settings::getWindowGeometry() const QByteArray Settings::getWindowGeometry() const
{ {
QMutexLocker locker{&bigLock}; QMutexLocker locker{&bigLock};

View File

@ -175,6 +175,9 @@ public:
int getThemeColor() const; int getThemeColor() const;
void setThemeColor(const int& value); void setThemeColor(const int& value);
int getMarkdownPreference() const;
void setMarkdownPreference(int newValue);
bool isCurstomEmojiFont() const; bool isCurstomEmojiFont() const;
void setCurstomEmojiFont(bool value); void setCurstomEmojiFont(bool value);
@ -367,6 +370,7 @@ private:
// ChatView // ChatView
int firstColumnHandlePos; int firstColumnHandlePos;
int secondColumnHandlePosFromRight; int secondColumnHandlePosFromRight;
int markdownPreference;
QString timestampFormat; QString timestampFormat;
QString dateFormat; QString dateFormat;
bool statusChangeNotificationEnabled; bool statusChangeNotificationEnabled;

View File

@ -82,6 +82,9 @@ static QStringList langs = {"Български",
"Türkçe", "Türkçe",
"Українська", "Українська",
"简体中文"}; "简体中文"};
static QStringList mdPrefs = {"None",
"Show Characters",
"No Characters"};
static QStringList timeFormats = {"hh:mm AP", "hh:mm", "hh:mm:ss AP", "hh:mm:ss"}; static QStringList timeFormats = {"hh:mm AP", "hh:mm", "hh:mm:ss AP", "hh:mm:ss"};
// http://doc.qt.io/qt-4.8/qdate.html#fromString // http://doc.qt.io/qt-4.8/qdate.html#fromString
@ -103,6 +106,10 @@ GeneralForm::GeneralForm(SettingsWidget *myParent) :
bodyUI->transComboBox->insertItem(i, langs[i]); bodyUI->transComboBox->insertItem(i, langs[i]);
bodyUI->transComboBox->setCurrentIndex(locales.indexOf(Settings::getInstance().getTranslation())); bodyUI->transComboBox->setCurrentIndex(locales.indexOf(Settings::getInstance().getTranslation()));
for (int i = 0; i < mdPrefs.size(); i++)
bodyUI->markdownComboBox->insertItem(i, mdPrefs[i]);
bodyUI->markdownComboBox->setCurrentIndex(Settings::getInstance().getMarkdownPreference());
bodyUI->cbAutorun->setChecked(Settings::getInstance().getAutorun()); bodyUI->cbAutorun->setChecked(Settings::getInstance().getAutorun());
bool showSystemTray = Settings::getInstance().getShowSystemTray(); bool showSystemTray = Settings::getInstance().getShowSystemTray();
@ -201,6 +208,7 @@ GeneralForm::GeneralForm(SettingsWidget *myParent) :
connect(bodyUI->showWindow, &QCheckBox::stateChanged, this, &GeneralForm::onShowWindowChanged); connect(bodyUI->showWindow, &QCheckBox::stateChanged, this, &GeneralForm::onShowWindowChanged);
connect(bodyUI->showInFront, &QCheckBox::stateChanged, this, &GeneralForm::onSetShowInFront); connect(bodyUI->showInFront, &QCheckBox::stateChanged, this, &GeneralForm::onSetShowInFront);
connect(bodyUI->notifySound, &QCheckBox::stateChanged, this, &GeneralForm::onSetNotifySound); connect(bodyUI->notifySound, &QCheckBox::stateChanged, this, &GeneralForm::onSetNotifySound);
connect(bodyUI->markdownComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(onMarkdownUpdated()));
connect(bodyUI->groupAlwaysNotify, &QCheckBox::stateChanged, this, &GeneralForm::onSetGroupAlwaysNotify); connect(bodyUI->groupAlwaysNotify, &QCheckBox::stateChanged, this, &GeneralForm::onSetGroupAlwaysNotify);
connect(bodyUI->autoacceptFiles, &QCheckBox::stateChanged, this, &GeneralForm::onAutoAcceptFileChange); connect(bodyUI->autoacceptFiles, &QCheckBox::stateChanged, this, &GeneralForm::onAutoAcceptFileChange);
connect(bodyUI->autoSaveFilesDir, SIGNAL(clicked()), this, SLOT(onAutoSaveDirChange())); connect(bodyUI->autoSaveFilesDir, SIGNAL(clicked()), this, SLOT(onAutoSaveDirChange()));
@ -361,6 +369,11 @@ void GeneralForm::onUseEmoticonsChange()
bodyUI->smileyPackBrowser->setEnabled(bodyUI->useEmoticons->isChecked()); bodyUI->smileyPackBrowser->setEnabled(bodyUI->useEmoticons->isChecked());
} }
void GeneralForm::onMarkdownUpdated()
{
Settings::getInstance().setMarkdownPreference(bodyUI->markdownComboBox->currentIndex());
}
void GeneralForm::onSetStatusChange() void GeneralForm::onSetStatusChange()
{ {
Settings::getInstance().setStatusChangeNotificationEnabled(bodyUI->statusChanges->isChecked()); Settings::getInstance().setStatusChangeNotificationEnabled(bodyUI->statusChanges->isChecked());

View File

@ -53,6 +53,7 @@ private slots:
void onStyleSelected(QString style); void onStyleSelected(QString style);
void onTimestampSelected(int index); void onTimestampSelected(int index);
void onDateFormatSelected(int index); void onDateFormatSelected(int index);
void onMarkdownUpdated();
void onSetStatusChange(); void onSetStatusChange();
void onAutoAwayChanged(); void onAutoAwayChanged();
void onUseEmoticonsChange(); void onUseEmoticonsChange();

View File

@ -6,7 +6,7 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>671</width> <width>1312</width>
<height>1098</height> <height>1098</height>
</rect> </rect>
</property> </property>
@ -14,7 +14,16 @@
<string notr="true">Form</string> <string notr="true">Form</string>
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout_2"> <layout class="QVBoxLayout" name="verticalLayout_2">
<property name="margin"> <property name="leftMargin">
<number>9</number>
</property>
<property name="topMargin">
<number>9</number>
</property>
<property name="rightMargin">
<number>9</number>
</property>
<property name="bottomMargin">
<number>9</number> <number>9</number>
</property> </property>
<item> <item>
@ -30,8 +39,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>631</width> <width>1280</width>
<height>1141</height> <height>2394</height>
</rect> </rect>
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout_4" stretch="0,0,0,1"> <layout class="QVBoxLayout" name="verticalLayout_4" stretch="0,0,0,1">
@ -227,6 +236,9 @@ instead of closing itself.</string>
<property name="toolTip"> <property name="toolTip">
<string>Set to 0 to disable</string> <string>Set to 0 to disable</string>
</property> </property>
<property name="showGroupSeparator" stdset="0">
<bool>true</bool>
</property>
<property name="suffix"> <property name="suffix">
<string notr="true"> min</string> <string notr="true"> min</string>
</property> </property>
@ -236,9 +248,6 @@ instead of closing itself.</string>
<property name="maximum"> <property name="maximum">
<number>2147483647</number> <number>2147483647</number>
</property> </property>
<property name="showGroupSeparator" stdset="0">
<bool>true</bool>
</property>
</widget> </widget>
</item> </item>
</layout> </layout>
@ -350,6 +359,46 @@ instead of closing itself.</string>
</item> </item>
</layout> </layout>
</item> </item>
<item>
<layout class="QHBoxLayout" name="generalLayout_2">
<item>
<widget class="QLabel" name="transLabel_2">
<property name="toolTip">
<string>The translation may not load until qTox restarts.</string>
</property>
<property name="text">
<string>Markdown:</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="markdownComboBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="toolTip">
<string>Select Markdown preference</string>
</property>
</widget>
</item>
<item>
<spacer name="generalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item> <item>
<widget class="QCheckBox" name="statusChanges"> <widget class="QCheckBox" name="statusChanges">
<property name="text"> <property name="text">
@ -432,6 +481,13 @@ will be sent to them when they appear online to you.</string>
</item> </item>
</layout> </layout>
</item> </item>
<item>
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string/>
</property>
</widget>
</item>
</layout> </layout>
</widget> </widget>
</item> </item>