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
|
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include "bstrlib.h"
#include "stmd.h"
#include "debug.h"
#include "scanners.h"
// Functions to convert block and inline lists to HTML strings.
// Escape special characters in HTML. More efficient than
// three calls to bfindreplace. If preserve_entities is set,
// existing entities are left alone.
static bstring escape_html(bstring inp, bool preserve_entities)
{
int pos = 0;
int match;
char c;
bstring escapable = blk2bstr("&<>\"", 4);
bstring ent;
bstring s = bstrcpy(inp);
while ((pos = binchr(s, pos, escapable)) != BSTR_ERR) {
c = bchar(s,pos);
switch (c) {
case '<':
bdelete(s, pos, 1);
ent = blk2bstr("<", 4);
binsert(s, pos, ent, ' ');
bdestroy(ent);
pos += 4;
break;
case '>':
bdelete(s, pos, 1);
ent = blk2bstr(">", 4);
binsert(s, pos, ent, ' ');
bdestroy(ent);
pos += 4;
break;
case '&':
if (preserve_entities && (match = scan_entity(s, pos))) {
pos += match;
} else {
bdelete(s, pos, 1);
ent = blk2bstr("&", 5);
binsert(s, pos, ent, ' ');
bdestroy(ent);
pos += 5;
}
break;
case '"':
bdelete(s, pos, 1);
ent = blk2bstr(""", 6);
binsert(s, pos, ent, ' ');
bdestroy(ent);
pos += 6;
break;
default:
bdelete(s, pos, 1);
log_err("unexpected character %02x", c);
}
}
bdestroy(escapable);
return s;
}
static inline void cr(bstring buffer)
{
int c = bchar(buffer, blength(buffer) - 1);
if (c != '\n' && c) {
bconchar(buffer, '\n');
}
}
// Convert a block list to HTML. Returns 0 on success, and sets result.
extern int blocks_to_html(block* b, bstring* result, bool tight)
{
bstring contents = NULL;
bstring escaped, escaped2;
struct bstrList * info_words;
struct ListData * data;
bstring mbstart;
bstring html = blk2bstr("", 0);
while(b != NULL) {
switch(b->tag) {
case document:
check(blocks_to_html(b->children, &contents, false) == 0,
"error converting blocks to html");
bformata(html, "%s", contents->data);
bdestroy(contents);
break;
case paragraph:
check(inlines_to_html(b->inline_content, &contents) == 0,
"error converting inlines to html");
if (tight) {
bformata(html, "%s", contents->data);
} else {
cr(html);
bformata(html, "<p>%s</p>", contents->data);
cr(html);
}
bdestroy(contents);
break;
case block_quote:
check(blocks_to_html(b->children, &contents, false) == 0,
"error converting blocks to html");
cr(html);
bformata(html, "<blockquote>\n%s</blockquote>", contents->data);
cr(html);
bdestroy(contents);
break;
case list_item:
check(blocks_to_html(b->children, &contents, tight) == 0,
"error converting blocks to html");
brtrimws(contents);
cr(html);
bformata(html, "<li>%s</li>", contents->data);
cr(html);
bdestroy(contents);
break;
case list:
// make sure a list starts at the beginning of the line:
cr(html);
data = &(b->attributes.list_data);
check(blocks_to_html(b->children, &contents, data->tight) == 0,
"error converting blocks to html");
mbstart = bformat(" start=\"%d\"", data->start);
bformata(html, "<%s%s>\n%s</%s>",
data->list_type == bullet ? "ul" : "ol",
data->start == 1 ? "" : (char*) mbstart->data,
contents->data,
data->list_type == bullet ? "ul" : "ol");
cr(html);
bdestroy(contents);
bdestroy(mbstart);
break;
case atx_header:
case setext_header:
check(inlines_to_html(b->inline_content, &contents) == 0,
"error converting inlines to html");
cr(html);
bformata(html, "<h%d>%s</h%d>",
b->attributes.header_level,
contents->data,
b->attributes.header_level);
cr(html);
bdestroy(contents);
break;
case indented_code:
escaped = escape_html(b->string_content, false);
cr(html);
bformata(html, "<pre><code>%s</code></pre>", escaped->data);
cr(html);
bdestroy(escaped);
break;
case fenced_code:
escaped = escape_html(b->string_content, false);
cr(html);
bformata(html, "<pre><code");
if (blength(b->attributes.fenced_code_data.info) > 0) {
escaped2 = escape_html(b->attributes.fenced_code_data.info, true);
info_words = bsplit(escaped2, ' ');
bformata(html, " class=\"language-%s\"", info_words->entry[0]->data);
bdestroy(escaped2);
bstrListDestroy(info_words);
}
bformata(html, ">%s</code></pre>", escaped->data);
cr(html);
bdestroy(escaped);
break;
case html_block:
bformata(html, "%s", b->string_content->data);
break;
case hrule:
bformata(html, "<hr />");
cr(html);
break;
case reference_def:
break;
default:
log_warn("block type %d not implemented\n", b->tag);
break;
}
b = b->next;
}
*result = html;
return 0;
error:
return -1;
}
// Convert an inline list to HTML. Returns 0 on success, and sets result.
extern int inlines_to_html(inl* ils, bstring* result)
{
bstring contents = NULL;
bstring html = blk2bstr("", 0);
bstring mbtitle, escaped, escaped2;
while(ils != NULL) {
switch(ils->tag) {
case str:
escaped = escape_html(ils->content.literal, false);
bformata(html, "%s", escaped->data);
bdestroy(escaped);
break;
case linebreak:
bformata(html, "<br />\n");
break;
case softbreak:
bformata(html, "\n");
break;
case code:
escaped = escape_html(ils->content.literal, false);
bformata(html, "<code>%s</code>", escaped->data);
bdestroy(escaped);
break;
case raw_html:
case entity:
bformata(html, "%s", ils->content.literal->data);
break;
case link:
check(inlines_to_html(ils->content.inlines, &contents) == 0,
"error converting inlines to html");
if (blength(ils->content.linkable.title) > 0) {
escaped = escape_html(ils->content.linkable.title, true);
mbtitle = bformat(" title=\"%s\"", escaped->data);
bdestroy(escaped);
} else {
mbtitle = blk2bstr("",0);
}
escaped = escape_html(ils->content.linkable.url, true);
bformata(html, "<a href=\"%s\"%s>%s</a>",
escaped->data,
mbtitle->data,
contents->data);
bdestroy(escaped);
bdestroy(mbtitle);
bdestroy(contents);
break;
case image:
check(inlines_to_html(ils->content.inlines, &contents) == 0,
"error converting inlines to html");
escaped = escape_html(ils->content.linkable.url, true);
escaped2 = escape_html(contents, false);
bdestroy(contents);
bformata(html, "<img src=\"%s\" alt=\"%s\"",
escaped->data, escaped2->data);
bdestroy(escaped);
bdestroy(escaped2);
if (blength(ils->content.linkable.title) > 0) {
escaped = escape_html(ils->content.linkable.title, true);
bformata(html, " title=\"%s\"", escaped->data);
bdestroy(escaped);
}
bformata(html, " />");
break;
case strong:
check(inlines_to_html(ils->content.inlines, &contents) == 0,
"error converting inlines to html");
bformata(html, "<strong>%s</strong>", contents->data);
bdestroy(contents);
break;
case emph:
check(inlines_to_html(ils->content.inlines, &contents) == 0,
"error converting inlines to html");
bformata(html, "<em>%s</em>", contents->data);
bdestroy(contents);
break;
}
ils = ils->next;
}
*result = html;
return 0;
error:
return -1;
}
|