-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathmarkdown.c
356 lines (300 loc) · 9.35 KB
/
markdown.c
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
* Markdown library for libpurple
* Copyright (C) 2018 Alyssa Rosenzweig
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <string.h>
#include "markdown.h"
/* Markdown test string:
*
* "<--- \o/ **¯\_(ツ)_/¯** _italics_ __underline__ *correction right* *italics2* ~~strikethrough~~ ~notstriked~ ~me https://pidgin.im <style>body{background-color:red}</script> <style>body{background-color: red}</style> <b>notbold</b> <notatag>"
*
* Checks for:
* - Correct escaping of lt/gt signs
* - Aesthetically correct (but non-comformant) handling of escape sequences as found in backslash-containing emoticons
* - Exhaustive test of syntaxes for italics, underline, strikethrough
* - Correct escaping for XSS rsisk
*
* Does not check for (open issues):
* - Italicised shruggie
*
*/
#define HTML_TOGGLE_OUT(f, a, b) \
out = g_string_append(out, f ? b : a); \
f = !f;
/* workaround errata in Discord's (users') markdown implementation */
static gboolean
markdown_underscore_match(const gchar *html, int i)
{
while (html[i] != ' ' && html[i]) {
if (html[i++] == '_') {
return !html[i] || html[i] == ' ';
}
}
return FALSE;
}
static gboolean
markdown_char_later_unspaced(const gchar *html, unsigned i, guint len, char c)
{
for (i = i + 1; i < len; ++i)
if (html[i] == c)
if (html[i - 1] != ' ')
return TRUE;
return FALSE;
}
/* Is a character escapable, that is, does it have a special meaning in
* Markdown? */
static gboolean
markdown_is_escapable(char c)
{
switch (c) {
case '\\':
case '*':
case '~':
case '_':
case '`':
return TRUE;
default:
return FALSE;
}
}
/* Should we interpret a _ as italics? */
static gboolean
markdown_should_underscore_italics(const gchar *html, unsigned i, gboolean s_italics)
{
return s_italics || markdown_underscore_match(html, i + 1);
}
/* Should we interpret _ as special at all? */
static gboolean
markdown_should_underscore(const gchar *html, unsigned i, gboolean s_italics)
{
return html[i + 1] == '_' || markdown_should_underscore_italics(html, i, s_italics);
}
static gchar *
markdown_helper_replace(gchar *html, const gchar *tag, const gchar *replacement)
{
gchar *replace_regex;
gchar *replace_with;
if (tag[0] == '<' && tag[1] == '/') {
//closing tag
replace_regex = g_strconcat("(\\s*)", tag, NULL);
replace_with = g_strconcat(replacement, "\\1", NULL);
} else {
replace_regex = g_strconcat(tag, "(\\s*)", NULL);
replace_with = g_strconcat("\\1", replacement, NULL);
}
GRegex *markdown_replace = g_regex_new(replace_regex, 0, 0, NULL);
gchar *temp = g_regex_replace(markdown_replace, html, -1, 0, replace_with, 0, NULL);
g_free(replace_regex);
g_free(replace_with);
g_regex_unref(markdown_replace);
if (temp != NULL) {
g_free(html);
html = temp;
}
return html;
}
gchar *
markdown_convert_markdown(const gchar *html, gboolean escape_html, gboolean discord_hacks)
{
g_return_val_if_fail(html != NULL, NULL);
guint html_len = strlen(html);
GString *out = g_string_sized_new(html_len * 2);
gboolean s_bold = FALSE;
gboolean s_italics = FALSE;
gboolean s_underline = FALSE;
gboolean s_strikethrough = FALSE;
gboolean s_codeblock = FALSE;
gboolean s_codebit = FALSE;
gboolean s_spoiler = FALSE;
for (guint i = 0; i < html_len; ++i) {
char c = html[i];
if ((s_codeblock || s_codebit) && c != '`') {
out = g_string_append_c(out, html[i]);
continue;
}
if (c == '\\') {
char next_char = html[++i];
/* If this is an escape-able character, don't print the
* backslash. Otherwise, do because the \ wasn't an
* escape anyway */
gboolean escapable = markdown_is_escapable(next_char);
/* Also, if this is an escapable character that would
* not actually -matter-, print it too. Fixes shruggie
* */
if (next_char == '_' && !markdown_should_underscore(html, i + 1, s_italics) && (escape_html || discord_hacks))
escapable = FALSE;
if (!escapable) {
out = g_string_append_c(out, '\\');
}
/* Append the next char regardless */
out = g_string_append_c(out, next_char);
} else if ((c == '<' || c == '>' || c == '&') && escape_html) {
/* These characters lack any particular meaning in
* Markdown, but need to be escaped to prevent getting
* mixed up with HTML. Failing to do so may result in
* valid parts of the message being stripped by
* overzealous sanitizers */
if (c == '<')
out = g_string_append(out, "<");
else if (c == '>')
out = g_string_append(out, ">");
else /*if (c == '&')*/
out = g_string_append(out, "&");
} else if (c == '*') {
if (html[i + 1] == '*') {
HTML_TOGGLE_OUT(s_bold, "<b>", "</b>");
i += 1;
} else {
/* Workaround some corner cases regarding italics placement. */
/* Don't match a*b */
gboolean unspaced_end = s_italics && html[i - 1] != ' ';
/* Don't match a* b */
gboolean unspaced_begin = !s_italics && html[i + 1] != ' ';
/* Don't match "*correction" or even "*correction *" */
gboolean balanced = s_italics || markdown_char_later_unspaced(html, i, html_len, '*');
if ((unspaced_begin || unspaced_end) && balanced) {
HTML_TOGGLE_OUT(s_italics, "<i>", "</i>");
} else {
out = g_string_append_c(out, html[i]);
}
}
} else if (c == '~' && html[i + 1] == '~') {
HTML_TOGGLE_OUT(s_strikethrough, "<s>", "</s>");
++i;
} else if (c == '_') {
if (html[i + 1] == '_') {
HTML_TOGGLE_OUT(s_underline, "<u>", "</u>");
++i;
} else {
if (markdown_should_underscore_italics(html, i, s_italics)) {
HTML_TOGGLE_OUT(s_italics, "<i>", "</i>");
} else {
out = g_string_append_c(out, html[i]);
}
}
} else if (c == '`') {
if (html[i + 1] == '`' && html[i + 2] == '`') {
if (!s_codeblock) {
#ifdef MARKDOWN_PIDGIN
out = g_string_append(out, "<br/><span style='font-family: monospace; white-space: pre'>");
#else
out = g_string_append(out, "<br/><pre>");
#endif
} else {
#ifdef MARKDOWN_PIDGIN
out = g_string_append(out, "</span>");
#else
out = g_string_append(out, "</pre>");
#endif
i += 2;
}
s_codeblock = !s_codeblock;
} else {
#ifdef MARKDOWN_PIDGIN
HTML_TOGGLE_OUT(s_codebit, "<span style='font-family: monospace; white-space: pre'>", "</span>");
#else
HTML_TOGGLE_OUT(s_codebit, "<code>", "</code>");
#endif
}
} else if (c == '|') {
if (html[i + 1] == '|') {
#ifdef MARKDOWN_PIDGIN
HTML_TOGGLE_OUT(s_spoiler, "<span style='foreground: black; background: black'>", "</span>");
#else
HTML_TOGGLE_OUT(s_spoiler, "<details><summary>Spoiler</summary>", "</details>");
#endif
i++;
}
} else {
out = g_string_append_c(out, c);
}
}
gchar *new_out = purple_strreplace(out->str, "\n", "<br>");
g_string_free(out, TRUE);
return new_out;
}
#define REPLACE_TAG(name, repl) \
html = markdown_helper_replace(html, "<" name ">", repl); \
html = markdown_helper_replace(html, "</" name ">", repl);
gchar *
markdown_html_to_markdown(gchar *html)
{
REPLACE_TAG("b", "**");
REPLACE_TAG("strong", "**");
REPLACE_TAG("i", "*");
REPLACE_TAG("em", "*");
REPLACE_TAG("u", "__");
REPLACE_TAG("s", "~~");
REPLACE_TAG("pre", "```");
REPLACE_TAG("code", "`");
/* Let newlines get passed through as HTML */
/* Workaround XHTML-IM stuff. TODO: XXX */
html = markdown_helper_replace(html, "<span style='font-weight: bold;'>", "**");
html = markdown_helper_replace(html, "</span>", "**");
return html;
}
gchar *
markdown_escape_md(const gchar *markdown, gboolean discord_hacks)
{
size_t markdown_len = strlen(markdown);
/* Worst case allocation */
GString *s = g_string_sized_new(markdown_len * 2);
gboolean verbatim = FALSE;
gboolean code_block = FALSE;
gboolean link = FALSE;
for (guint i = 0; i < markdown_len; ++i) {
char c = markdown[i];
if (c == '`') {
if (code_block) {
code_block = verbatim = FALSE;
} else if (!verbatim) {
code_block = verbatim = TRUE;
}
g_string_append_c(s, markdown[i]);
if (markdown[i + 1] == '`' && markdown[i + 2] == '`') {
i += 2;
g_string_append_c(s, markdown[i]);
g_string_append_c(s, markdown[i]);
continue;
}
}
if (!verbatim) {
if (strncmp(markdown + i, "http://", sizeof("http://") - 1) == 0 ||
strncmp(markdown + i, "https://", sizeof("https://") - 1) == 0)
{
link = verbatim = TRUE;
}
}
if (link && c == ' ') {
link = verbatim = FALSE;
}
if (!verbatim) {
if (
(c == '_' && (markdown[i + 1] == ' ' ||
markdown[i + 1] == '\0' ||
i == 0 ||
markdown[i - 1] == ' ' ||
markdown[i - 1] == '\0')) ||
(c == '*') ||
(c == '\\' && !(markdown[i + 1] == '_' && (i == 0 || markdown[i - 1] == ' ')) && !discord_hacks) ||
(c == '~' && (markdown[i + 1] == '~'))) {
g_string_append_c(s, '\\');
}
}
g_string_append_c(s, c);
}
return g_string_free(s, FALSE);
}