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

#include "cmark.h"
#include "debug.h"
#include "html/houdini.h"

typedef struct RenderStack {
    struct RenderStack *previous;
    chunk literal;
    union {
	node_inl *inl;
	node_block *block;
    } next_sibling;
    bool tight;
} render_stack;

static void free_render_stack(render_stack * rstack)
{
    render_stack * tempstack;
    while (rstack) {
	tempstack = rstack;
	rstack = rstack->previous;
	chunk_free(&(tempstack->literal));
	free(tempstack);
    }
}

static render_stack* push_inline(render_stack* rstack,
				 node_inl* inl,
				 char* literal)
{
    render_stack* newstack;
    newstack = (render_stack*)malloc(sizeof(render_stack));
    newstack->previous = rstack;
    newstack->next_sibling.inl = inl;
    newstack->literal = chunk_literal(literal);
    return newstack;
}

static render_stack* push_block(render_stack* rstack,
				node_block* block,
				char* literal,
				bool tight)
{
    render_stack* newstack;
    newstack = (render_stack*)malloc(sizeof(render_stack));
    newstack->previous = rstack;
    newstack->next_sibling.block = block;
    newstack->literal = chunk_literal(literal);
    newstack->tight = tight;
    return newstack;
}

static render_stack* pop_render_stack(render_stack* rstack)
{
    render_stack* top = rstack;

    if (rstack == NULL) {
	return NULL;
    }
    rstack = rstack->previous;
    top->previous = NULL;
    free_render_stack(top);
    return rstack;
}

// Functions to convert node_block and inline lists to HTML strings.

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 an inline list to HTML.  Returns 0 on success, and sets result.
static void inlines_to_html(strbuf *html, node_inl* ils)
{
	strbuf scrap = GH_BUF_INIT;
	node_inl* children;
	render_stack* rstack = NULL;

	while(ils != NULL) {
	        children = NULL;
		switch(ils->tag) {
			case INL_STRING:
				escape_html(html, ils->content.literal.data, ils->content.literal.len);
				break;

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

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

			case INL_CODE:
				strbuf_puts(html, "<code>");
				escape_html(html, ils->content.literal.data, ils->content.literal.len);
				strbuf_puts(html, "</code>");
				break;

			case INL_RAW_HTML:
				strbuf_put(html,
						ils->content.literal.data,
						ils->content.literal.len);
				break;

			case INL_LINK:
				strbuf_puts(html, "<a href=\"");
				if (ils->content.linkable.url)
					escape_href(html, ils->content.linkable.url, -1);

				if (ils->content.linkable.title) {
					strbuf_puts(html, "\" title=\"");
					escape_html(html, ils->content.linkable.title, -1);
				}

				strbuf_puts(html, "\">");
				inlines_to_html(html, ils->content.inlines);
				strbuf_puts(html, "</a>");
				break;

			case INL_IMAGE:
				strbuf_puts(html, "<img src=\"");
				if (ils->content.linkable.url)
					escape_href(html, ils->content.linkable.url, -1);

				inlines_to_html(&scrap, ils->content.inlines);
				strbuf_puts(html, "\" alt=\"");
				if (scrap.size)
					escape_html(html, scrap.ptr, scrap.size);
				strbuf_clear(&scrap);

				if (ils->content.linkable.title) {
					strbuf_puts(html, "\" title=\"");
					escape_html(html, ils->content.linkable.title, -1);
				}

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

			case INL_STRONG:
				strbuf_puts(html, "<strong>");
				children = ils->content.inlines;
				rstack = push_inline(rstack, ils->next, "</strong>");
				break;

			case INL_EMPH:
				strbuf_puts(html, "<em>");
				children = ils->content.inlines;
				rstack = push_inline(rstack, ils->next, "</em>");
				break;
		}
		if (children) {
		    ils = children;
		} else {
		    ils = ils->next;
		}
		while (ils == NULL && rstack != NULL) {
		    strbuf_puts(html, rstack->literal.data);
		    ils = rstack->next_sibling.inl;
		    rstack = pop_render_stack(rstack);
		}
	}

	strbuf_free(&scrap);
	free_render_stack(rstack);
}

// Convert a node_block list to HTML.  Returns 0 on success, and sets result.
static void blocks_to_html(strbuf *html, node_block *b, bool tight)
{
	struct ListData *data;

	while(b != NULL) {
		switch(b->tag) {
			case BLOCK_DOCUMENT:
				blocks_to_html(html, b->children, false);
				break;

			case BLOCK_PARAGRAPH:
				if (tight) {
					inlines_to_html(html, b->inline_content);
				} else {
					cr(html);
					strbuf_puts(html, "<p>");
					inlines_to_html(html, b->inline_content);
					strbuf_puts(html, "</p>\n");
				}
				break;

			case BLOCK_BQUOTE:
				cr(html);
				strbuf_puts(html, "<blockquote>\n");
				blocks_to_html(html, b->children, false);
				strbuf_puts(html, "</blockquote>\n");
				break;

			case BLOCK_LIST_ITEM:
				cr(html);
				strbuf_puts(html, "<li>");
				blocks_to_html(html, b->children, tight);
				strbuf_trim(html); /* TODO: rtrim */
				strbuf_puts(html, "</li>\n");
				break;

			case BLOCK_LIST:
				// make sure a list starts at the beginning of the line:
				cr(html);
				data = &(b->as.list);

				if (data->start > 1) {
					strbuf_printf(html, "<%s start=\"%d\">\n",
							data->list_type == bullet ? "ul" : "ol",
							data->start);
				} else {
					strbuf_puts(html, data->list_type == bullet ? "<ul>\n" : "<ol>\n");
				}

				blocks_to_html(html, b->children, data->tight);
				strbuf_puts(html, data->list_type == bullet ? "</ul>" : "</ol>");
				strbuf_putc(html, '\n');
				break;

			case BLOCK_ATX_HEADER:
			case BLOCK_SETEXT_HEADER:
				cr(html);
				strbuf_printf(html, "<h%d>", b->as.header.level);
				inlines_to_html(html, b->inline_content);
				strbuf_printf(html, "</h%d>\n", b->as.header.level);
				break;

			case BLOCK_INDENTED_CODE:
			case BLOCK_FENCED_CODE:
				cr(html);

				strbuf_puts(html, "<pre><code");

				if (b->tag == BLOCK_FENCED_CODE) {
					strbuf *info = &b->as.code.info;

					if (strbuf_len(info) > 0) {
						int first_tag = strbuf_strchr(info, ' ', 0);
						if (first_tag < 0)
							first_tag = strbuf_len(info);

						strbuf_puts(html, " class=\"language-");
						escape_html(html, info->ptr, first_tag);
						strbuf_putc(html, '"');
					}
				}

				strbuf_putc(html, '>');
				escape_html(html, b->string_content.ptr, b->string_content.size);
				strbuf_puts(html, "</code></pre>\n");
				break;

			case BLOCK_HTML:
				strbuf_put(html, b->string_content.ptr, b->string_content.size);
				break;

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

			case BLOCK_REFERENCE_DEF:
				break;

			default:
				assert(false);
		}

		b = b->next;
	}
}

void cmark_render_html(strbuf *html, node_block *root)
{
	blocks_to_html(html, root, false);
}