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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
|
#include "creole.h"
#include <assert.h>
#include <ctype.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LENGTH(x) (sizeof(x)/sizeof((x)[0]))
#define DEBUG(...) (fprintf(stderr, __VA_ARGS__), fflush(stderr))
void process(const char *begin, const char *end, bool new_block, FILE *out);
long do_headers(const char *begin, const char *end, bool new_block, FILE *out);
long do_paragraph(const char *begin, const char *end, bool new_block, FILE *out);
long do_replacements(const char *begin, const char *end, bool new_block, FILE *out);
long do_link(const char *begin, const char *end, bool new_block, FILE *out);
long do_raw_url(const char *begin, const char *end, bool new_block, FILE *out);
long do_emphasis(const char *begin, const char *end, bool new_block, FILE *out);
long do_bold(const char *begin, const char *end, bool new_block, FILE *out);
long do_nowiki_inline(const char *begin, const char *end, bool new_block, FILE *out);
// Prints string escaped.
void hprint(FILE *out, const char *begin, const char *end) {
for (const char *p = begin; p != end; p++) {
if (*p == '&') {
fputs("&", out);
} else if (*p == '"') {
fputs(""", out);
} else if (*p == '>') {
fputs(">", out);
} else if (*p == '<') {
fputs("<", out);
} else {
fputc(*p, out);
}
}
}
bool starts_with(const char *haystack_begin, const char *haystack_end, const char *needle) {
size_t needle_len = strlen(needle);
size_t haystack_len = haystack_end - haystack_begin;
if (needle_len > haystack_len) {
return false;
} else {
return memcmp(haystack_begin, needle, needle_len) == 0;
}
}
// A parser takes a (sub)string and returns the number of characters consumed, if any.
//
// The parameter `new_block` determines whether `begin` points to the beginning of a new block.
// The sign of the return value determines whether a new block should begin, after the consumed text.
typedef long (* parser_t)(const char *begin, const char *end, bool new_block, FILE *out);
static parser_t parsers[] = {
// Block-level elements
do_headers,
do_paragraph, // <p> should be last as it eats anything
// Inline-level elements
do_emphasis,
do_bold,
do_link,
do_raw_url,
do_nowiki_inline,
do_replacements
};
long do_headers(const char *begin, const char *end, bool new_block, FILE *out) {
if (!new_block) { // Headers are block-level elements.
return 0;
}
if (*begin != '=') {
return 0;
}
unsigned level = 0;
const char *start = begin;
while (*start == '=') {
level += 1;
start += 1;
}
if (level > 6) {
return 0;
}
while (isspace(*start)) {
start += 1;
}
const char *eol = start;
while (eol != end && *eol != '\n') {
eol += 1;
}
const char *stop = eol;
assert(stop > begin);
while (stop[-1] == '=' || isspace(stop[-1])) {
stop -= 1;
}
fprintf(out, "<h%u>", level);
process(start, stop, false, out);
fprintf(out, "</h%u>", level);
return -(eol - begin);
}
long do_paragraph(const char *begin, const char *end, bool new_block, FILE *out) {
if (!new_block) { // Paragraphs are block-level elements.
return 0;
}
const char *stop = begin + 1;
while (stop + 1 < end) {
if (stop[0] == '\n' && stop[1] == '\n') {
goto found_double_newline;
} else {
stop += 1;
}
}
stop = end;
found_double_newline:
fputs("<p>", out);
process(begin, stop, false, out);
fputs("</p>", out);
return -(stop - begin);
}
static struct {
const char *from, *to;
} replacements[] = {
// Escaped special characters
{"~[[", "[["},
{"~]]", "]]"}, // NOTE: This pattern is duplicated in do_link().
{"~//", "//"},
{"~**", "**"},
{"~{{{", "{{{"},
// Characters that have special meaning in HTML
// NOTE: These rules are duplicated in hprint().
{"<", "<"},
{">", ">"},
{"\"", """},
{"&", "&"},
};
long do_replacements(const char *begin, const char *end, bool new_block, FILE *out)
{
for (unsigned i = 0; i < LENGTH(replacements); ++i) {
size_t length = strlen(replacements[i].from);
if ((size_t)(end - begin) < length) {
continue;
}
if (strncmp(replacements[i].from, begin, length) == 0) {
fputs(replacements[i].to, out);
return length;
}
}
return 0;
}
long do_link(const char *begin, const char *end, bool new_block, FILE *out)
{
// Links start with "[[".
if (!starts_with(begin, end, "[[")) {
return 0;
}
const char *start = begin + 2;
// Find the matching, unescaped "]]".
// Poor man's for...else
const char *stop = start - 1;
do {
stop = strnstr(stop + 1, "]]", end - (stop + 1));
} while (stop != NULL && stop[-1] == '~');
if (stop == NULL) {
return 0;
}
// FIXME: How do we handle WikiWord style links? Should we just append ".html" if is_wikiword()?
const char *pipe = strnstr(start, "|", stop - start);
if (pipe != NULL) {
const char *link_address_start = start;
const char *link_address_stop = pipe;
fprintf(out, "<a href=\"");
hprint(out, link_address_start, link_address_stop);
fprintf(out, "\">");
const char *link_text_start = pipe + 1;
const char *link_text_stop = stop;
process(link_text_start, link_text_stop, false, out);
fprintf(out, "</a>");
} else {
fprintf(out, "<a href=\"");
hprint(out, start, stop);
fprintf(out, "\">");
hprint(out, start, stop); // Don't parse markup when we know it's a link.
fprintf(out, "</a>");
}
return stop - start + 4 /* [[]] */;
}
long do_raw_url(const char *begin, const char *end, bool new_block, FILE *out)
{
const char *p = begin;
// This piece of spaghetti is necessary to handle escaped urls.
// These should not actually be turned into anchor tags.
// See: <http://www.wikicreole.org/wiki/Creole1.0#section-Creole1.0-EscapeCharacter>
bool escaped = false;
if (*begin == '~') {
escaped = true;
p += 1;
}
// Eat a scheme followed by a ":". Here are the relevant rules from RFC 3986.
// - URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
// - scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
// See: <https://www.rfc-editor.org/rfc/rfc3986#section-3.1>
if (!isalpha(*p)) {
return 0;
}
while (p < end && (isalnum(*p) || *p == '+' || *p == '-' || *p == '.')) {
p += 1;
}
if (p >= end || p[0] != ':') {
return 0;
}
p += 1;
// Eat the remainder of the URI, purely going by what "legal" URI
// characters it contains.
// See: <https://stackoverflow.com/a/7109208>
const char *q = p;
while (q < end) {
switch (*q) {
case '0' ... '9':
case 'a' ... 'z':
case 'A' ... 'Z':
case '-': case '.': case '_': case '~':
case ':': case '/': case '?': case '#':
case '[': case ']': case '@': case '!':
case '$': case '&': case '\'': case '(':
case ')': case '*': case '+': case ',':
case ';': case '%': case '=':
q += 1;
break;
default:
goto end_url;
}
}
end_url:
// If there is nothing following the colon, don't accept it as a raw
// url. Otherwise we'd incorrectly find a link with the "said" protocol
// here: "And he said: blah blah".
if (q == p) {
return 0;
}
// Special case: If we end on a ".", assume it's a full stop at the end
// of a sentence. Here's an example:
// My favorite webside is https://cohost.org/.
if (q[-1] == '.') {
q -= 1;
}
if (escaped) {
hprint(out, begin + 1 /* ~ */, q);
} else {
fputs("<a href=\"", out);
hprint(out, begin, q);
fputs("\">", out);
hprint(out, begin, q);
fputs("</a>", out);
}
return q - begin;
}
long do_emphasis(const char *begin, const char *end, bool new_block, FILE *out) {
if (!starts_with(begin, end, "//")) {
return 0;
}
const char *start = begin + 2; /* // */
const char *stop = start;
do {
stop = strnstr(stop + 1, "//", end - (stop + 1));
} while (stop != NULL && (stop[-1] == '~' || stop[-1] == ':'));
if (stop == NULL) {
return 0;
}
fputs("<em>", out);
process(start, stop, false, out);
fputs("</em>", out);
return stop - start + 4; /* //...// */
}
// FIXME: This is //almost// just a copy/paste of do_emphasis. Not very DRY...
// The one difficult part is that : should only be treated as an escape character for //.
long do_bold(const char *begin, const char *end, bool new_block, FILE *out) {
if (!starts_with(begin, end, "**")) {
return 0;
}
const char *start = begin + 2; /* // */
const char *stop = start;
do {
stop = strnstr(stop + 1, "**", end - (stop + 1));
} while (stop != NULL && stop[-1] == '~');
if (stop == NULL) {
return 0;
}
fputs("<strong>", out);
process(start, stop, false, out);
fputs("</strong>", out);
return stop - start + 4; /* **...** */
}
// The inline-level nowiki element.
// This is specified together with the block-level nowiki element in the spec, but for this parser it makes more sense to treat them as separate.
// See: <http://www.wikicreole.org/wiki/Creole1.0#section-Creole1.0-NowikiPreformatted>
long do_nowiki_inline(const char *begin, const char *end, bool new_block, FILE *out) {
if (!starts_with(begin, end, "{{{")) {
return 0;
}
const char *start = begin + 3;
const char *stop = strnstr(start, "}}}", end - start);
if (stop == NULL) {
return 0;
}
// Include trailing closing braces in the span.
while (stop + 3 < end && stop[3] == '}') {
stop += 1;
}
const char *trim_start = start;
while (isspace(*trim_start)) {
trim_start += 1;
}
const char *trim_stop = stop;
while (isspace(trim_stop[-1])) {
trim_stop -= 1;
}
fputs("<tt>", out);
hprint(out, trim_start, trim_stop);
fputs("</tt>", out);
return 3 + (stop - start) + 3; /* {{{...}}} */
}
void process(const char *begin, const char *end, bool new_block, FILE *out) {
const char *p = begin;
while (p < end) {
// Eat all newlines if we're starting a block.
if (new_block) {
while (*p == '\n') {
p += 1;
if (p == end) {
return;
}
}
}
// Greedily try all parsers.
long affected;
for (unsigned i = 0; i < LENGTH(parsers); ++i) {
affected = parsers[i](p, end, new_block, out);
if (affected) {
break;
}
}
if (affected) {
p += labs(affected);
} else {
fputc(*p, out);
p += 1;
}
if (p + 1 == end) {
// Don't print single newline at end.
if (*p == '\n') {
return;
}
} else {
// Determine whether we've reached a new block.
if (p[0] == '\n' && p[1] == '\n') {
// Double newline characters separate blocks;
// if we've found them, we're starting a new block
new_block = true;
} else {
// ...otherwise the parser gets to decide.
new_block = affected < 0;
}
}
}
}
void render_creole(FILE *out, const char *source, size_t source_length)
{
process(source, source + source_length, true, out);
}
|