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
|
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <assert.h>
#include "stmd.h"
#include "debug.h"
#include "html/houdini.h"
// Functions to convert block and inline lists to HTML strings.
static void escape_html(gh_buf *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(gh_buf *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(gh_buf *html)
{
if (html->size && html->ptr[html->size - 1] != '\n')
gh_buf_putc(html, '\n');
}
// Convert a block list to HTML. Returns 0 on success, and sets result.
void blocks_to_html(gh_buf *html, block *b, bool tight)
{
struct ListData *data;
while(b != NULL) {
switch(b->tag) {
case document:
blocks_to_html(html, b->children, false);
break;
case paragraph:
if (tight) {
inlines_to_html(html, b->inline_content);
} else {
cr(html);
gh_buf_puts(html, "<p>");
inlines_to_html(html, b->inline_content);
gh_buf_puts(html, "</p>");
cr(html);
}
break;
case block_quote:
cr(html);
gh_buf_puts(html, "<blockquote>");
blocks_to_html(html, b->children, false);
gh_buf_puts(html, "</blockquote>");
cr(html);
break;
case list_item:
cr(html);
gh_buf_puts(html, "<li>");
blocks_to_html(html, b->children, tight);
gh_buf_trim(html); /* TODO: rtrim */
gh_buf_puts(html, "</li>");
cr(html);
break;
case list:
// make sure a list starts at the beginning of the line:
cr(html);
data = &(b->attributes.list_data);
if (data->start > 1) {
gh_buf_printf(html, "<%s start=\"%d\">\n",
data->list_type == bullet ? "ul" : "ol",
data->start);
} else {
gh_buf_puts(html, data->list_type == bullet ? "<ul>\n" : "<ol>\n");
}
blocks_to_html(html, b->children, data->tight);
gh_buf_puts(html, data->list_type == bullet ? "</ul>" : "</ol>");
cr(html);
break;
case atx_header:
case setext_header:
cr(html);
gh_buf_printf(html, "<h%d>", b->attributes.header_level);
inlines_to_html(html, b->inline_content);
gh_buf_printf(html, "</h%d>", b->attributes.header_level);
cr(html);
break;
case indented_code:
case fenced_code:
cr(html);
gh_buf_puts(html, "<pre");
if (b->tag == fenced_code) {
gh_buf *info = &b->attributes.fenced_code_data.info;
if (gh_buf_len(info) > 0) {
int first_tag = gh_buf_strchr(info, ' ', 0);
if (first_tag < 0)
first_tag = gh_buf_len(info);
gh_buf_puts(html, " class=\"");
escape_html(html, info->ptr, first_tag);
gh_buf_putc(html, '"');
}
}
gh_buf_puts(html, "><code>");
escape_html(html, b->string_content.ptr, b->string_content.size);
gh_buf_puts(html, "</code></pre>");
cr(html);
break;
case html_block:
gh_buf_put(html, b->string_content.ptr, b->string_content.size);
break;
case hrule:
gh_buf_puts(html, "<hr />");
cr(html);
break;
case reference_def:
break;
default:
assert(false);
}
b = b->next;
}
}
// Convert an inline list to HTML. Returns 0 on success, and sets result.
void inlines_to_html(gh_buf *html, inl* ils)
{
gh_buf scrap = GH_BUF_INIT;
while(ils != NULL) {
switch(ils->tag) {
case INL_STRING:
escape_html(html, ils->content.literal.data, ils->content.literal.len);
break;
case INL_LINEBREAK:
gh_buf_puts(html, "<br />\n");
break;
case INL_SOFTBREAK:
gh_buf_putc(html, '\n');
break;
case INL_CODE:
gh_buf_puts(html, "<code>");
escape_html(html, ils->content.literal.data, ils->content.literal.len);
gh_buf_puts(html, "</code>");
break;
case INL_RAW_HTML:
case INL_ENTITY:
gh_buf_put(html,
ils->content.literal.data,
ils->content.literal.len);
break;
case INL_LINK:
gh_buf_puts(html, "<a href=\"");
escape_href(html, ils->content.linkable.url, -1);
if (ils->content.linkable.title) {
gh_buf_puts(html, "\" title=\"");
escape_html(html, ils->content.linkable.title, -1);
}
gh_buf_puts(html, "\">");
inlines_to_html(html, ils->content.inlines);
gh_buf_puts(html, "</a>");
break;
case INL_IMAGE:
gh_buf_puts(html, "<img src=\"");
escape_href(html, ils->content.linkable.url, -1);
inlines_to_html(&scrap, ils->content.inlines);
if (scrap.size) {
gh_buf_puts(html, "\" alt=\"");
escape_html(html, scrap.ptr, scrap.size);
}
gh_buf_clear(&scrap);
if (ils->content.linkable.title) {
gh_buf_puts(html, "\" title=\"");
escape_html(html, ils->content.linkable.title, -1);
}
gh_buf_puts(html, "\"/>");
break;
case INL_STRONG:
gh_buf_puts(html, "<strong>");
inlines_to_html(html, ils->content.inlines);
gh_buf_puts(html, "</strong>");
break;
case INL_EMPH:
gh_buf_puts(html, "<em>");
inlines_to_html(html, ils->content.inlines);
gh_buf_puts(html, "</em>");
break;
}
ils = ils->next;
}
}
|