summaryrefslogtreecommitdiff
path: root/src/creole.c
blob: 107b50baa54ef76077d6ca46dffdfe34f28b9b99 (plain)
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
#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);
int do_headers(const char *begin, const char *end, bool new_block, FILE *out);
int do_paragraph(const char *begin, const char *end, bool new_block, FILE *out);
int do_replacements(const char *begin, const char *end, bool new_block, FILE *out);
int do_link(const char *begin, const char *end, bool new_block, FILE *out);
int do_raw_url(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("&amp;", out);
		} else if (*p == '"') {
			fputs("&quot;", out);
		} else if (*p == '>') {
			fputs("&gt;", out);
		} else if (*p == '<') {
			fputs("&lt;", out);
		} else {
			fputc(*p, out);
		}
	}
}

// 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 int (* parser_t)(const char *begin, const char *end, bool new_block, FILE *out);

static parser_t parsers[] = { do_headers, do_paragraph, do_link, do_raw_url, do_replacements };

int 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);
}

int 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().
	{"<", "&lt;"},
	{">", "&gt;"},
	{"\"", "&quot;"},
	{"&", "&amp;"},
};

int 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 (end - begin < length) {
			continue;
		}
		if (strncmp(replacements[i].from, begin, length) == 0) {
			fputs(replacements[i].to, out);
			return length;
		}
	}

	return 0;
}

int do_link(const char *begin, const char *end, bool new_block, FILE *out)
{
	// Links start with "[[".
	if (begin + 2 >= end || begin[0] != '[' || begin[1] != '[') {
		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 /* [[]] */;
}

int do_raw_url(const char *begin, const char *end, bool new_block, FILE *out)
{
	// 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>
	const char *p = begin;
	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.
	// This is not technically correct, but it's a good enough heuristic.
	const char *q = p;
	while (q < end && !isspace(*q)) {
		q += 1;
	}

        // 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;
	}

	fputs("<a href=\"", out);
	hprint(out, begin, q);
	fputs("\">", out);
	hprint(out, begin, q);
	fputs("</a>", out);

	return q - begin;
}

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.
		int affected;
		for (unsigned i = 0; i < LENGTH(parsers); ++i) {
			affected = parsers[i](p, end, new_block, out);
			if (affected) {
				break;
			}
		}
		if (affected) {
			p += abs(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);
}