Init commonmark support.

Add dependency cmark.
This commit is contained in:
Black Hat 2019-01-18 08:46:50 +08:00
parent 2b0e45bddf
commit 635dbbff39
7 changed files with 21 additions and 97 deletions

View File

@ -13,8 +13,6 @@ import Spectral 0.1
import Spectral.Setting 0.1 import Spectral.Setting 0.1
import SortFilterProxyModel 0.2 import SortFilterProxyModel 0.2
import "qrc:/js/md.js" as Markdown
Item { Item {
property var currentRoom: null property var currentRoom: null

View File

@ -10,8 +10,6 @@ import Spectral.Setting 0.1
import Spectral 0.1 import Spectral 0.1
import "qrc:/js/md.js" as Markdown
Control { Control {
property alias isReply: replyItem.visible property alias isReply: replyItem.visible
property var replyUser property var replyUser
@ -349,8 +347,7 @@ Control {
} }
if (text.indexOf(PREFIX_MARKDOWN) === 0) { if (text.indexOf(PREFIX_MARKDOWN) === 0) {
text = text.substr(PREFIX_MARKDOWN.length) text = text.substr(PREFIX_MARKDOWN.length)
var parsedText = Markdown.markdown_parser(text) currentRoom.postMarkdownText(text)
currentRoom.postHtmlMessage(text, parsedText, RoomMessageEvent.Text)
return return
} }

View File

@ -1,90 +0,0 @@
.pragma library
var preg_replace=function(a,b,c,d){void 0===d&&(d=-1);var e=a.substr(a.lastIndexOf(a[0])+1),f=a.substr(1,a.lastIndexOf(a[0])-1),g=RegExp(f,e),i=[],j=0,k=0,l=c,m=[];if(-1===d){do m=g.exec(c),null!==m&&i.push(m);while(null!==m&&-1!==e.indexOf("g"))}else i.push(g.exec(c));for(j=i.length-1;j>-1;j--){for(m=b,k=i[j].length;k>-1;k--)m=m.replace("${"+k+"}",i[j][k]).replace("$"+k,i[j][k]).replace("\\"+k,i[j][k]);l=l.replace(i[j][0],m)}return l};
var markdown_parser = function(str){
var rules = [
// headers
['/(#+)(.*)/g', function(chars, header){
var level = chars.length;
return '<h'+level+'>'+header.trim()+'</h'+level+'>';
}],
// images
// ['/\\!\\[([^\\[]+)\\]\\(([^\\(]+)\\)/g', '<img src=\"\\2\" alt=\"\\1\" />'],
// link
['/\\[([^\\[]+)\\]\\(([^\\(]+)\\)/g', '<a href=\"\\2\">\\1</a>'],
// bold
['/(\\*\\*|__)(.*?)\\1/g', '<strong>\\2</strong>'],
// emphasis
['/(\\*|_)(.*?)\\1/g', '<i>\\2</i>'],
// strike
['/(\\~\\~)(.*?)\\1/g', '<del>\\2</del>'],
// quote
['/\\:\\"(.*?)\\"\\:/g', '<q>\\1</q>'],
// unordered list
// ['/\\n\\*(.*)/g', function(item){
// return '<ul>\n<li>'+item.trim()+'</li>\n</ul>';
// }],
// ordered list
// ['/\\n[0-9]+\\.(.*)/g', function(item){
// return '<ol>\n<li>'+item.trim()+'</li>\n</ol>';
// }],
// blockquote
['/\\n\\>(.*)/g', function(str){
return '<blockquote>'+str.trim()+'</blockquote>';
}]
// paragraphs
// ['/\\n[^\\n]+\\n/g', function(line){
// line = line.trim();
// if(line[0] === '<'){
// return line;
// }
// return '\n<p>'+line+'</p>\n';
// }]
], fixes = [
['/<\\/ul>\n<ul>/g', '\n'],
['/<\\/ol>\n<ol>/g', '\n'],
['/<\\/blockquote>\n<blockquote>/g', "\n"]
];
var parse_line = function(str){
str = "\n" + str.trim() + "\n";
for(var i = 0, j = rules.length; i < j; i++){
if(typeof rules[i][1] == 'function') {
var _flag = rules[i][0].substr(rules[i][0].lastIndexOf(rules[i][0][0])+1),
_pattern = rules[i][0].substr(1, rules[i][0].lastIndexOf(rules[i][0][0])-1),
reg = new RegExp(_pattern, _flag);
var matches = reg.exec(str);
if(matches !== null){
if(matches.length > 1){
str = preg_replace(rules[i][0], rules[i][1](matches[1], matches[2]), str);
}
else
{
str = preg_replace(rules[i][0], rules[i][1](matches[0]), str);
}
}
}
else {
str = preg_replace(rules[i][0], rules[i][1], str);
}
}
return str.trim();
};
str = str.split('\n');
var rtn = [];
for(var i = 0, j = str.length; i < j; i++){
rtn.push(parse_line(str[i]));
}
rtn = rtn.join('\n');
for(i = 0, j = fixes.length; i < j; i++){
rtn = preg_replace(fixes[i][0], fixes[i][1], rtn);
}
return rtn;
};

View File

@ -2,7 +2,6 @@
<qresource prefix="/"> <qresource prefix="/">
<file>qtquickcontrols2.conf</file> <file>qtquickcontrols2.conf</file>
<file>qml/main.qml</file> <file>qml/main.qml</file>
<file>js/md.js</file>
<file>imports/Spectral/Component/Emoji/EmojiPicker.qml</file> <file>imports/Spectral/Component/Emoji/EmojiPicker.qml</file>
<file>imports/Spectral/Component/Emoji/qmldir</file> <file>imports/Spectral/Component/Emoji/qmldir</file>
<file>imports/Spectral/Component/Timeline/DownloadableContent.qml</file> <file>imports/Spectral/Component/Timeline/DownloadableContent.qml</file>

View File

@ -20,6 +20,9 @@ isEmpty(USE_SYSTEM_SORTFILTERPROXYMODEL) {
isEmpty(USE_SYSTEM_QMATRIXCLIENT) { isEmpty(USE_SYSTEM_QMATRIXCLIENT) {
USE_SYSTEM_QMATRIXCLIENT = false USE_SYSTEM_QMATRIXCLIENT = false
} }
isEmpty(USE_SYSTEM_CMARK) {
USE_SYSTEM_CMARK = false
}
isEmpty(BUNDLE_FONT) { isEmpty(BUNDLE_FONT) {
BUNDLE_FONT = false BUNDLE_FONT = false
} }
@ -36,6 +39,12 @@ $$USE_SYSTEM_SORTFILTERPROXYMODEL {
message("Falling back to built-in SortFilterProxyModel.") message("Falling back to built-in SortFilterProxyModel.")
include(include/SortFilterProxyModel/SortFilterProxyModel.pri) include(include/SortFilterProxyModel/SortFilterProxyModel.pri)
} }
$$USE_SYSTEM_CMARK {
PKGCONFIG += libcmark
} else {
message("Falling back to built-in CMark.")
include(include/SortFilterProxyModel/SortFilterProxyModel.pri)
}
# The following define makes your compiler emit warnings if you use # The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked deprecated (the exact warnings # any feature of Qt which as been marked deprecated (the exact warnings

View File

@ -14,6 +14,8 @@
#include <QMetaObject> #include <QMetaObject>
#include <QMimeDatabase> #include <QMimeDatabase>
#include "cmark.h"
#include "utils.h" #include "utils.h"
SpectralRoom::SpectralRoom(Connection* connection, QString roomId, SpectralRoom::SpectralRoom(Connection* connection, QString roomId,
@ -233,3 +235,10 @@ QVariantList SpectralRoom::getUsers(const QString& prefix) {
return matchedList; return matchedList;
} }
QString SpectralRoom::postMarkdownText(const QString& markdown) {
QByteArray local = markdown.toLocal8Bit();
const char* data = local.data();
QString html = cmark_markdown_to_html(data, local.length(), 0);
return postHtmlText(markdown, html);
}

View File

@ -74,6 +74,8 @@ class SpectralRoom : public Room {
Q_INVOKABLE QVariantList getUsers(const QString& prefix); Q_INVOKABLE QVariantList getUsers(const QString& prefix);
Q_INVOKABLE QString postMarkdownText(const QString& markdown);
private: private:
QString m_cachedInput; QString m_cachedInput;
QSet<const QMatrixClient::RoomEvent*> highlights; QSet<const QMatrixClient::RoomEvent*> highlights;