/
markdown.cpp
166 lines (133 loc) · 5.36 KB
/
markdown.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "markdown.h"
#include "bugmanager.h"
#include <QTextDocument>
#include <QImage>
QString Markdown::hint()
{
return QString(QStringLiteral("**<b>bold</b>** "
"*<i>italic </i>* "
"`<code>inline-code</code>` "
"![title](image-or-file) "
"![](image-with-preview) "
"#issue-number"));
}
QString Markdown::process(const QString& s)
{
// Markdown in Qt accepts html tags, but we want to show them as they are
QString res = QString(s).replace("<", "<").replace(">", ">");
bool ok;
int offset;
do { res = processImage(res, ok); } while (ok);
do { res = processImage1(res, ok); } while (ok);
do { res = processFile(res, ok); } while (ok);
do { res = processBug(res, ok); } while (ok);
do { res = processMarkdownResource(res, ok); } while (ok);
offset = 0; do { res = processIssueNum(res, offset, ok); } while (ok);
QTextDocument doc;
doc.setMarkdown(res);
return doc.toHtml();
}
QString Markdown::processImage(const QString& s, bool& ok)
{
return processResource(s, QStringLiteral("Image"), BrowserCommands::showImage(), ok);
}
QString Markdown::processImage1(const QString& s, bool& ok)
{
return processResource(s, QStringLiteral("Img"), BrowserCommands::showImage(), ok);
}
QString Markdown::processFile(const QString& s, bool& ok)
{
return processResource(s, QStringLiteral("File"), BrowserCommands::getFile(), ok);
}
QString Markdown::processBug(const QString& s, bool& ok)
{
return processResource(s, QStringLiteral("Bug"), BrowserCommands::showRelated(), ok);
}
QString Markdown::processIssueNum(const QString& s, int& offset, bool& ok)
{
ok = false;
int startPos = s.indexOf('#', offset, Qt::CaseInsensitive);
if (startPos < 0) return s;
ok = true;
startPos++;
int endPos = startPos;
while (s[endPos].isDigit() && endPos < s.size())
endPos++;
if (endPos > startPos && (endPos == s.size() || s[endPos].isSpace() || s[endPos].isPunct()))
{
QString numStr = s.sliced(startPos, endPos-startPos);
QString link = BrowserCommands::showRelated().format(numStr, '#'+numStr);
offset = startPos-1 + link.size();
auto strBegin = QStringView(s).first(startPos-1);
auto strEnd = QStringView(s).sliced(endPos);
return strBegin % link % strEnd;
}
offset = endPos;
return s;
}
static QString makeImgLinkTag(const QString& link)
{
QFileInfo fi = BugManager::fileInDatabaseFiles(link);
QString fn = fi.absoluteFilePath();
QImage img(fn);
const int minSz = 250;
const int maxSz = 300;
int sz = qMax(img.width(), img.height());
if (sz < maxSz)
return QStringLiteral("<img src='%1'/>").arg(fn);
QString side = img.width() > img.height() ? QStringLiteral("width") : QStringLiteral("height");
QString tag = QStringLiteral("<img src='%1' %2=%3/>").arg(fn, side).arg(minSz);
return BrowserCommands::showImage().format(link, tag);
}
QString Markdown::processResource(const QString& s, const QString& tag, const BrowserCommands::Command &cmd, bool& ok)
{
ok = false;
static QLatin1String tagStart("[[");
static QLatin1String tagEnd("]]");
int startPos = s.indexOf(tagStart % tag % ':', 0, Qt::CaseInsensitive);
if (startPos < 0) return s;
int resourceStartPos = startPos + tag.size() + 3;
int endPos = s.indexOf(tagEnd, resourceStartPos+1);
if (endPos < 0) return s;
auto strBegin = QStringView(s).first(startPos);
auto strMid = QStringView(s).sliced(resourceStartPos, endPos - resourceStartPos);
auto strEnd = QStringView(s).sliced(endPos+2, s.length() - (endPos+2));
QString resource = strMid.trimmed().toString();
if (resource.isEmpty())
return s;
ok = true;
QString linkTag;
if (tag == QStringLiteral("Img") || tag == QStringLiteral("Image"))
linkTag = makeImgLinkTag(resource);
else linkTag = cmd.format(resource, resource);
return strBegin % linkTag % strEnd;
}
QString Markdown::processMarkdownResource(const QString& s, bool& ok)
{
ok = false;
static QLatin1String tagStart("![");
static QLatin1String tagMid("](");
static QLatin1String tagEnd(")");
int startPos = s.indexOf(tagStart, 0, Qt::CaseInsensitive);
if (startPos < 0) return s;
int altStartPos = startPos + 2;
int midPos = s.indexOf(tagMid, altStartPos);
if (midPos < 0) return s;
int linkStartPos = midPos + 2;
int endPos = s.indexOf(tagEnd, linkStartPos);
if (endPos < 0) return s;
auto strBegin = QStringView(s).first(startPos);
auto strAlt = QStringView(s).sliced(altStartPos, midPos - altStartPos);
auto strLink = QStringView(s).sliced(linkStartPos, endPos - linkStartPos);
auto strEnd = QStringView(s).sliced(endPos+1, s.length() - (endPos+1));
QString link = strLink.trimmed().toString();
if (link.isEmpty())
return s;
QString alt = strAlt.trimmed().toString();
ok = true;
QString linkTag;
if (BrowserCommands::isSupportedImg(link))
linkTag = alt.isEmpty() ? makeImgLinkTag(link) : BrowserCommands::showImage().format(link, alt);
else linkTag = BrowserCommands::getFile().format(link, alt.isEmpty() ? link : alt);
return strBegin % linkTag % strEnd;
}