summaryrefslogtreecommitdiff
path: root/src/html/html.c
blob: 96fce66ec7967eb75d5be10386488bf0accfc781 (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
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>

#include "config.h"
#include "cmark.h"
#include "node.h"
#include "buffer.h"
#include "html/houdini.h"

// Functions to convert cmark_nodes to HTML strings.

static bool
finish_node(strbuf *html, cmark_node *node, bool tight);

static void escape_html(strbuf *dest, const unsigned char *source, int length)
{
	if (length < 0)
		length = strlen((char *)source);

	houdini_escape_html0(dest, source, (size_t)length, 0);
}

static void escape_href(strbuf *dest, const unsigned char *source, int length)
{
	if (length < 0)
		length = strlen((char *)source);

	houdini_escape_href(dest, source, (size_t)length);
}

static inline void cr(strbuf *html)
{
	if (html->size && html->ptr[html->size - 1] != '\n')
		strbuf_putc(html, '\n');
}

// Convert the inline children of a node to a plain string.
static void inlines_to_plain_html(strbuf *html, cmark_node* node)
{
	cmark_node* cur = node->first_child;

	if (cur == NULL) {
		return;
	}

	while (true) {
		switch(cur->type) {
		case NODE_STRING:
		case NODE_INLINE_CODE:
		case NODE_INLINE_HTML:
			escape_html(html, cur->as.literal.data, cur->as.literal.len);
			break;

		case NODE_LINEBREAK:
		case NODE_SOFTBREAK:
			strbuf_putc(html, ' ');
			break;

		default:
			break;
		}

		if (cur->first_child) {
			cur = cur->first_child;
			continue;
		}

	next_sibling:
		if (cur->next) {
			cur = cur->next;
			continue;
		}
		cur = cur->parent;
		if (cur == node) {
			break;
		}
		goto next_sibling;
	}
}


// Convert a cmark_node to HTML.
static void node_to_html(strbuf *html, cmark_node *node)
{
	cmark_node *cur;
	char start_header[] = "<h0>";
	bool tight = false;
	bool visit_children;

	if (node == NULL) {
		return;
	}

	cur = node;
	while (true) {
		// Only NODE_IMAGE wants to skip its children.
		visit_children = true;

		switch(cur->type) {
		case NODE_DOCUMENT:
			break;

		case NODE_PARAGRAPH:
			if (!tight) {
				cr(html);
				strbuf_puts(html, "<p>");
			}
			break;

		case NODE_BQUOTE:
			cr(html);
			strbuf_puts(html, "<blockquote>\n");
			// BQUOTE doesn't use any of the 'as' structs,
			// so the 'list' member can be used to store the
			// current value of 'tight'.
			cur->as.list.tight = tight;
			tight = false;
			break;

		case NODE_LIST_ITEM:
			cr(html);
			strbuf_puts(html, "<li>");
			break;

		case NODE_LIST: {
			cmark_list *list = &cur->as.list;
			bool tmp;

			// make sure a list starts at the beginning of the line:
			cr(html);

			if (list->list_type == CMARK_BULLET_LIST) {
				strbuf_puts(html, "<ul>\n");
			}
			else if (list->start == 1) {
				strbuf_puts(html, "<ol>\n");
			}
			else {
				strbuf_printf(html, "<ol start=\"%d\">\n",
					      list->start);
			}

			// Store the current value of 'tight' by swapping.
			tmp = list->tight;
			list->tight = tight;
			tight = tmp;
			break;
		}

		case NODE_ATX_HEADER:
		case NODE_SETEXT_HEADER:
			cr(html);
			start_header[2] = '0' + cur->as.header.level;
			strbuf_puts(html, start_header);
			break;

		case NODE_INDENTED_CODE:
		case NODE_FENCED_CODE: {
			strbuf *info = &cur->as.code.info;
			cr(html);

			if (cur->type != NODE_FENCED_CODE
			    || strbuf_len(info) == 0) {
				strbuf_puts(html, "<pre><code>");
			}
			else {
				int first_tag = strbuf_strchr(info, ' ', 0);
				if (first_tag < 0)
					first_tag = strbuf_len(info);

				strbuf_puts(html,
					    "<pre><code class=\"language-");
				escape_html(html, info->ptr, first_tag);
				strbuf_puts(html, "\">");
			}

			escape_html(html, cur->string_content.ptr, cur->string_content.size);
			break;
		}

		case NODE_HTML:
			strbuf_put(html, cur->string_content.ptr, cur->string_content.size);
			break;

		case NODE_HRULE:
			strbuf_puts(html, "<hr />\n");
			break;

		case NODE_REFERENCE_DEF:
			break;

		case NODE_STRING:
			escape_html(html, cur->as.literal.data, cur->as.literal.len);
			break;

		case NODE_LINEBREAK:
			strbuf_puts(html, "<br />\n");
			break;

		case NODE_SOFTBREAK:
			strbuf_putc(html, '\n');
			break;

		case NODE_INLINE_CODE:
			strbuf_puts(html, "<code>");
			escape_html(html, cur->as.literal.data, cur->as.literal.len);
			break;

		case NODE_INLINE_HTML:
			strbuf_put(html,
				   cur->as.literal.data,
				   cur->as.literal.len);
			break;

		case NODE_LINK:
			strbuf_puts(html, "<a href=\"");
			if (cur->as.link.url)
				escape_href(html, cur->as.link.url, -1);

			if (cur->as.link.title) {
				strbuf_puts(html, "\" title=\"");
				escape_html(html, cur->as.link.title, -1);
			}

			strbuf_puts(html, "\">");
			break;

		case NODE_IMAGE:
			strbuf_puts(html, "<img src=\"");
			if (cur->as.link.url)
				escape_href(html, cur->as.link.url, -1);

			strbuf_puts(html, "\" alt=\"");
			inlines_to_plain_html(html, cur);

			if (cur->as.link.title) {
				strbuf_puts(html, "\" title=\"");
				escape_html(html, cur->as.link.title, -1);
			}

			strbuf_puts(html, "\" />");
			visit_children = false;
			break;

		case NODE_STRONG:
			strbuf_puts(html, "<strong>");
			break;

		case NODE_EMPH:
			strbuf_puts(html, "<em>");
			break;

		default:
			assert(false);
		}

		if (visit_children && cur->first_child) {
			cur = cur->first_child;
			continue;
		}

	next_sibling:
		tight = finish_node(html, cur, tight);
		if (cur == node) {
			break;
		}
		if (cur->next) {
			cur = cur->next;
			continue;
		}
		cur = cur->parent;
		goto next_sibling;
	}
}

// Returns the restored value of 'tight'.
static bool
finish_node(strbuf *html, cmark_node *node, bool tight)
{
	char end_header[] = "</h0>\n";

	switch (node->type) {
	case NODE_PARAGRAPH:
		if (!tight) {
			strbuf_puts(html, "</p>\n");
		}
		break;

	case NODE_BQUOTE: {
		cmark_list *list = &node->as.list;
		strbuf_puts(html, "</blockquote>\n");
		// Restore old 'tight' value.
		tight = list->tight;
		list->tight = false;
		break;
	}

	case NODE_LIST_ITEM:
		strbuf_puts(html, "</li>\n");
		break;

	case NODE_LIST: {
		cmark_list *list = &node->as.list;
		bool tmp;
		strbuf_puts(html,
			    list->list_type == CMARK_BULLET_LIST ?
			    "</ul>\n" : "</ol>\n");
		// Restore old 'tight' value.
		tmp = tight;
		tight = list->tight;
		list->tight = tmp;
		break;
	}

	case NODE_ATX_HEADER:
	case NODE_SETEXT_HEADER:
		end_header[3] = '0' + node->as.header.level;
		strbuf_puts(html, end_header);
		break;

	case NODE_INDENTED_CODE:
	case NODE_FENCED_CODE:
		strbuf_puts(html, "</code></pre>\n");
		break;

	case NODE_INLINE_CODE:
		strbuf_puts(html, "</code>");
		break;

	case NODE_LINK:
		strbuf_puts(html, "</a>");
		break;

	case NODE_STRONG:
		strbuf_puts(html, "</strong>");
		break;

	case NODE_EMPH:
		strbuf_puts(html, "</em>");
		break;

	default:
		break;
	}

	return tight;
}

unsigned char *cmark_render_html(cmark_node *root)
{
	unsigned char *result;
	strbuf html = GH_BUF_INIT;
	node_to_html(&html, root);
	result = strbuf_detach(&html);
	strbuf_free(&html);
	return result;
}