redcarpet 1.5.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.markdown +312 -20
- data/Rakefile +31 -36
- data/bin/redcarpet +1 -1
- data/ext/redcarpet/autolink.c +260 -0
- data/ext/{xhtml.h → redcarpet/autolink.h} +17 -24
- data/ext/redcarpet/buffer.c +229 -0
- data/ext/redcarpet/buffer.h +88 -0
- data/ext/redcarpet/houdini.h +29 -0
- data/ext/redcarpet/houdini_href_e.c +108 -0
- data/ext/redcarpet/houdini_html_e.c +84 -0
- data/ext/redcarpet/html.c +603 -0
- data/ext/redcarpet/html.h +67 -0
- data/ext/redcarpet/html_blocks.h +206 -0
- data/ext/redcarpet/html_smartypants.c +385 -0
- data/ext/redcarpet/markdown.c +2454 -0
- data/ext/redcarpet/markdown.h +130 -0
- data/ext/redcarpet/rc_markdown.c +137 -0
- data/ext/redcarpet/rc_render.c +434 -0
- data/ext/redcarpet/redcarpet.h +33 -0
- data/ext/redcarpet/stack.c +81 -0
- data/ext/redcarpet/stack.h +21 -0
- data/lib/redcarpet/render_man.rb +65 -0
- data/lib/redcarpet.rb +65 -72
- data/redcarpet.gemspec +29 -20
- data/test/redcarpet_test.rb +232 -88
- metadata +45 -29
- data/ext/array.c +0 -300
- data/ext/array.h +0 -148
- data/ext/buffer.c +0 -319
- data/ext/buffer.h +0 -145
- data/ext/markdown.c +0 -2076
- data/ext/markdown.h +0 -107
- data/ext/redcarpet.c +0 -140
- data/ext/xhtml.c +0 -779
- data/lib/markdown.rb +0 -1
- data/test/benchmark.rb +0 -56
- data/test/benchmark.txt +0 -306
- data/test/markdown_test.rb +0 -186
- /data/ext/{extconf.rb → redcarpet/extconf.rb} +0 -0
|
@@ -0,0 +1,603 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2009, Natacha Porté
|
|
3
|
+
* Copyright (c) 2011, Vicent Marti
|
|
4
|
+
*
|
|
5
|
+
* Permission to use, copy, modify, and distribute this software for any
|
|
6
|
+
* purpose with or without fee is hereby granted, provided that the above
|
|
7
|
+
* copyright notice and this permission notice appear in all copies.
|
|
8
|
+
*
|
|
9
|
+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
10
|
+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
11
|
+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
12
|
+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
13
|
+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
14
|
+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
15
|
+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
#include "markdown.h"
|
|
19
|
+
#include "html.h"
|
|
20
|
+
|
|
21
|
+
#include <string.h>
|
|
22
|
+
#include <stdlib.h>
|
|
23
|
+
#include <stdio.h>
|
|
24
|
+
#include <ctype.h>
|
|
25
|
+
|
|
26
|
+
#include "houdini.h"
|
|
27
|
+
|
|
28
|
+
#define USE_XHTML(opt) (opt->flags & HTML_USE_XHTML)
|
|
29
|
+
|
|
30
|
+
int
|
|
31
|
+
sdhtml_is_tag(const uint8_t *tag_data, size_t tag_size, const char *tagname)
|
|
32
|
+
{
|
|
33
|
+
size_t i;
|
|
34
|
+
int closed = 0;
|
|
35
|
+
|
|
36
|
+
if (tag_size < 3 || tag_data[0] != '<')
|
|
37
|
+
return HTML_TAG_NONE;
|
|
38
|
+
|
|
39
|
+
i = 1;
|
|
40
|
+
|
|
41
|
+
if (tag_data[i] == '/') {
|
|
42
|
+
closed = 1;
|
|
43
|
+
i++;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
for (; i < tag_size; ++i, ++tagname) {
|
|
47
|
+
if (*tagname == 0)
|
|
48
|
+
break;
|
|
49
|
+
|
|
50
|
+
if (tag_data[i] != *tagname)
|
|
51
|
+
return HTML_TAG_NONE;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (i == tag_size)
|
|
55
|
+
return HTML_TAG_NONE;
|
|
56
|
+
|
|
57
|
+
if (isspace(tag_data[i]) || tag_data[i] == '>')
|
|
58
|
+
return closed ? HTML_TAG_CLOSE : HTML_TAG_OPEN;
|
|
59
|
+
|
|
60
|
+
return HTML_TAG_NONE;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/********************
|
|
64
|
+
* GENERIC RENDERER *
|
|
65
|
+
********************/
|
|
66
|
+
static int
|
|
67
|
+
rndr_autolink(struct buf *ob, const struct buf *link, enum mkd_autolink type, void *opaque)
|
|
68
|
+
{
|
|
69
|
+
struct html_renderopt *options = opaque;
|
|
70
|
+
|
|
71
|
+
if (!link || !link->size)
|
|
72
|
+
return 0;
|
|
73
|
+
|
|
74
|
+
if ((options->flags & HTML_SAFELINK) != 0 &&
|
|
75
|
+
!sd_autolink_issafe(link->data, link->size) &&
|
|
76
|
+
type != MKDA_EMAIL)
|
|
77
|
+
return 0;
|
|
78
|
+
|
|
79
|
+
BUFPUTSL(ob, "<a href=\"");
|
|
80
|
+
if (type == MKDA_EMAIL)
|
|
81
|
+
BUFPUTSL(ob, "mailto:");
|
|
82
|
+
houdini_escape_href(ob, link->data, link->size);
|
|
83
|
+
|
|
84
|
+
if (options->link_attributes) {
|
|
85
|
+
bufputc(ob, '\"');
|
|
86
|
+
options->link_attributes(ob, link, opaque);
|
|
87
|
+
bufputc(ob, '>');
|
|
88
|
+
} else {
|
|
89
|
+
BUFPUTSL(ob, "\">");
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/*
|
|
93
|
+
* Pretty printing: if we get an email address as
|
|
94
|
+
* an actual URI, e.g. `mailto:foo@bar.com`, we don't
|
|
95
|
+
* want to print the `mailto:` prefix
|
|
96
|
+
*/
|
|
97
|
+
if (bufprefix(link, "mailto:") == 0) {
|
|
98
|
+
houdini_escape_html(ob, link->data + 7, link->size - 7);
|
|
99
|
+
} else {
|
|
100
|
+
houdini_escape_html(ob, link->data, link->size);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
BUFPUTSL(ob, "</a>");
|
|
104
|
+
|
|
105
|
+
return 1;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
static void
|
|
109
|
+
rndr_blockcode(struct buf *ob, const struct buf *text, const struct buf *lang, void *opaque)
|
|
110
|
+
{
|
|
111
|
+
if (ob->size) bufputc(ob, '\n');
|
|
112
|
+
|
|
113
|
+
if (lang && lang->size) {
|
|
114
|
+
size_t i, cls;
|
|
115
|
+
BUFPUTSL(ob, "<pre><code class=\"");
|
|
116
|
+
|
|
117
|
+
for (i = 0, cls = 0; i < lang->size; ++i, ++cls) {
|
|
118
|
+
while (i < lang->size && isspace(lang->data[i]))
|
|
119
|
+
i++;
|
|
120
|
+
|
|
121
|
+
if (i < lang->size) {
|
|
122
|
+
size_t org = i;
|
|
123
|
+
while (i < lang->size && !isspace(lang->data[i]))
|
|
124
|
+
i++;
|
|
125
|
+
|
|
126
|
+
if (lang->data[org] == '.')
|
|
127
|
+
org++;
|
|
128
|
+
|
|
129
|
+
if (cls) bufputc(ob, ' ');
|
|
130
|
+
houdini_escape_html(ob, lang->data + org, i - org);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
BUFPUTSL(ob, "\">");
|
|
135
|
+
} else
|
|
136
|
+
BUFPUTSL(ob, "<pre><code>");
|
|
137
|
+
|
|
138
|
+
if (text)
|
|
139
|
+
houdini_escape_html(ob, text->data, text->size);
|
|
140
|
+
|
|
141
|
+
BUFPUTSL(ob, "</code></pre>\n");
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
static void
|
|
145
|
+
rndr_blockquote(struct buf *ob, const struct buf *text, void *opaque)
|
|
146
|
+
{
|
|
147
|
+
if (ob->size) bufputc(ob, '\n');
|
|
148
|
+
BUFPUTSL(ob, "<blockquote>\n");
|
|
149
|
+
if (text) bufput(ob, text->data, text->size);
|
|
150
|
+
BUFPUTSL(ob, "</blockquote>\n");
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
static int
|
|
154
|
+
rndr_codespan(struct buf *ob, const struct buf *text, void *opaque)
|
|
155
|
+
{
|
|
156
|
+
BUFPUTSL(ob, "<code>");
|
|
157
|
+
if (text) houdini_escape_html(ob, text->data, text->size);
|
|
158
|
+
BUFPUTSL(ob, "</code>");
|
|
159
|
+
return 1;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
static int
|
|
163
|
+
rndr_strikethrough(struct buf *ob, const struct buf *text, void *opaque)
|
|
164
|
+
{
|
|
165
|
+
if (!text || !text->size)
|
|
166
|
+
return 0;
|
|
167
|
+
|
|
168
|
+
BUFPUTSL(ob, "<del>");
|
|
169
|
+
bufput(ob, text->data, text->size);
|
|
170
|
+
BUFPUTSL(ob, "</del>");
|
|
171
|
+
return 1;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
static int
|
|
175
|
+
rndr_double_emphasis(struct buf *ob, const struct buf *text, void *opaque)
|
|
176
|
+
{
|
|
177
|
+
if (!text || !text->size)
|
|
178
|
+
return 0;
|
|
179
|
+
|
|
180
|
+
BUFPUTSL(ob, "<strong>");
|
|
181
|
+
bufput(ob, text->data, text->size);
|
|
182
|
+
BUFPUTSL(ob, "</strong>");
|
|
183
|
+
|
|
184
|
+
return 1;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
static int
|
|
188
|
+
rndr_emphasis(struct buf *ob, const struct buf *text, void *opaque)
|
|
189
|
+
{
|
|
190
|
+
if (!text || !text->size) return 0;
|
|
191
|
+
BUFPUTSL(ob, "<em>");
|
|
192
|
+
if (text) bufput(ob, text->data, text->size);
|
|
193
|
+
BUFPUTSL(ob, "</em>");
|
|
194
|
+
return 1;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
static int
|
|
198
|
+
rndr_linebreak(struct buf *ob, void *opaque)
|
|
199
|
+
{
|
|
200
|
+
struct html_renderopt *options = opaque;
|
|
201
|
+
bufputs(ob, USE_XHTML(options) ? "<br/>\n" : "<br>\n");
|
|
202
|
+
return 1;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
static void
|
|
206
|
+
rndr_header(struct buf *ob, const struct buf *text, int level, void *opaque)
|
|
207
|
+
{
|
|
208
|
+
struct html_renderopt *options = opaque;
|
|
209
|
+
|
|
210
|
+
if (ob->size)
|
|
211
|
+
bufputc(ob, '\n');
|
|
212
|
+
|
|
213
|
+
if (options->flags & HTML_TOC)
|
|
214
|
+
bufprintf(ob, "<h%d id=\"toc_%d\">", level, options->toc_data.header_count++);
|
|
215
|
+
else
|
|
216
|
+
bufprintf(ob, "<h%d>", level);
|
|
217
|
+
|
|
218
|
+
if (text) bufput(ob, text->data, text->size);
|
|
219
|
+
bufprintf(ob, "</h%d>\n", level);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
static int
|
|
223
|
+
rndr_link(struct buf *ob, const struct buf *link, const struct buf *title, const struct buf *content, void *opaque)
|
|
224
|
+
{
|
|
225
|
+
struct html_renderopt *options = opaque;
|
|
226
|
+
|
|
227
|
+
if (link != NULL && (options->flags & HTML_SAFELINK) != 0 && !sd_autolink_issafe(link->data, link->size))
|
|
228
|
+
return 0;
|
|
229
|
+
|
|
230
|
+
BUFPUTSL(ob, "<a href=\"");
|
|
231
|
+
|
|
232
|
+
if (link && link->size)
|
|
233
|
+
houdini_escape_href(ob, link->data, link->size);
|
|
234
|
+
|
|
235
|
+
if (title && title->size) {
|
|
236
|
+
BUFPUTSL(ob, "\" title=\"");
|
|
237
|
+
houdini_escape_html(ob, title->data, title->size);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
if (options->link_attributes) {
|
|
241
|
+
bufputc(ob, '\"');
|
|
242
|
+
options->link_attributes(ob, link, opaque);
|
|
243
|
+
bufputc(ob, '>');
|
|
244
|
+
} else {
|
|
245
|
+
BUFPUTSL(ob, "\">");
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
if (content && content->size) bufput(ob, content->data, content->size);
|
|
249
|
+
BUFPUTSL(ob, "</a>");
|
|
250
|
+
return 1;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
static void
|
|
254
|
+
rndr_list(struct buf *ob, const struct buf *text, int flags, void *opaque)
|
|
255
|
+
{
|
|
256
|
+
if (ob->size) bufputc(ob, '\n');
|
|
257
|
+
bufput(ob, flags & MKD_LIST_ORDERED ? "<ol>\n" : "<ul>\n", 5);
|
|
258
|
+
if (text) bufput(ob, text->data, text->size);
|
|
259
|
+
bufput(ob, flags & MKD_LIST_ORDERED ? "</ol>\n" : "</ul>\n", 6);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
static void
|
|
263
|
+
rndr_listitem(struct buf *ob, const struct buf *text, int flags, void *opaque)
|
|
264
|
+
{
|
|
265
|
+
BUFPUTSL(ob, "<li>");
|
|
266
|
+
if (text) {
|
|
267
|
+
size_t size = text->size;
|
|
268
|
+
while (size && text->data[size - 1] == '\n')
|
|
269
|
+
size--;
|
|
270
|
+
|
|
271
|
+
bufput(ob, text->data, size);
|
|
272
|
+
}
|
|
273
|
+
BUFPUTSL(ob, "</li>\n");
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
static void
|
|
277
|
+
rndr_paragraph(struct buf *ob, const struct buf *text, void *opaque)
|
|
278
|
+
{
|
|
279
|
+
struct html_renderopt *options = opaque;
|
|
280
|
+
size_t i = 0;
|
|
281
|
+
|
|
282
|
+
if (ob->size) bufputc(ob, '\n');
|
|
283
|
+
|
|
284
|
+
if (!text || !text->size)
|
|
285
|
+
return;
|
|
286
|
+
|
|
287
|
+
while (i < text->size && isspace(text->data[i])) i++;
|
|
288
|
+
|
|
289
|
+
if (i == text->size)
|
|
290
|
+
return;
|
|
291
|
+
|
|
292
|
+
BUFPUTSL(ob, "<p>");
|
|
293
|
+
if (options->flags & HTML_HARD_WRAP) {
|
|
294
|
+
size_t org;
|
|
295
|
+
while (i < text->size) {
|
|
296
|
+
org = i;
|
|
297
|
+
while (i < text->size && text->data[i] != '\n')
|
|
298
|
+
i++;
|
|
299
|
+
|
|
300
|
+
if (i > org)
|
|
301
|
+
bufput(ob, text->data + org, i - org);
|
|
302
|
+
|
|
303
|
+
/*
|
|
304
|
+
* do not insert a line break if this newline
|
|
305
|
+
* is the last character on the paragraph
|
|
306
|
+
*/
|
|
307
|
+
if (i >= text->size - 1)
|
|
308
|
+
break;
|
|
309
|
+
|
|
310
|
+
rndr_linebreak(ob, opaque);
|
|
311
|
+
i++;
|
|
312
|
+
}
|
|
313
|
+
} else {
|
|
314
|
+
bufput(ob, &text->data[i], text->size - i);
|
|
315
|
+
}
|
|
316
|
+
BUFPUTSL(ob, "</p>\n");
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
static void
|
|
320
|
+
rndr_raw_block(struct buf *ob, const struct buf *text, void *opaque)
|
|
321
|
+
{
|
|
322
|
+
size_t org, sz;
|
|
323
|
+
if (!text) return;
|
|
324
|
+
sz = text->size;
|
|
325
|
+
while (sz > 0 && text->data[sz - 1] == '\n') sz -= 1;
|
|
326
|
+
org = 0;
|
|
327
|
+
while (org < sz && text->data[org] == '\n') org += 1;
|
|
328
|
+
if (org >= sz) return;
|
|
329
|
+
if (ob->size) bufputc(ob, '\n');
|
|
330
|
+
bufput(ob, text->data + org, sz - org);
|
|
331
|
+
bufputc(ob, '\n');
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
static int
|
|
335
|
+
rndr_triple_emphasis(struct buf *ob, const struct buf *text, void *opaque)
|
|
336
|
+
{
|
|
337
|
+
if (!text || !text->size) return 0;
|
|
338
|
+
BUFPUTSL(ob, "<strong><em>");
|
|
339
|
+
bufput(ob, text->data, text->size);
|
|
340
|
+
BUFPUTSL(ob, "</em></strong>");
|
|
341
|
+
return 1;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
static void
|
|
345
|
+
rndr_hrule(struct buf *ob, void *opaque)
|
|
346
|
+
{
|
|
347
|
+
struct html_renderopt *options = opaque;
|
|
348
|
+
if (ob->size) bufputc(ob, '\n');
|
|
349
|
+
bufputs(ob, USE_XHTML(options) ? "<hr/>\n" : "<hr>\n");
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
static int
|
|
353
|
+
rndr_image(struct buf *ob, const struct buf *link, const struct buf *title, const struct buf *alt, void *opaque)
|
|
354
|
+
{
|
|
355
|
+
struct html_renderopt *options = opaque;
|
|
356
|
+
if (!link || !link->size) return 0;
|
|
357
|
+
|
|
358
|
+
BUFPUTSL(ob, "<img src=\"");
|
|
359
|
+
houdini_escape_href(ob, link->data, link->size);
|
|
360
|
+
BUFPUTSL(ob, "\" alt=\"");
|
|
361
|
+
|
|
362
|
+
if (alt && alt->size)
|
|
363
|
+
houdini_escape_html(ob, alt->data, alt->size);
|
|
364
|
+
|
|
365
|
+
if (title && title->size) {
|
|
366
|
+
BUFPUTSL(ob, "\" title=\"");
|
|
367
|
+
houdini_escape_html(ob, title->data, title->size); }
|
|
368
|
+
|
|
369
|
+
bufputs(ob, USE_XHTML(options) ? "\"/>" : "\">");
|
|
370
|
+
return 1;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
static int
|
|
374
|
+
rndr_raw_html(struct buf *ob, const struct buf *text, void *opaque)
|
|
375
|
+
{
|
|
376
|
+
struct html_renderopt *options = opaque;
|
|
377
|
+
|
|
378
|
+
if ((options->flags & HTML_SKIP_HTML) != 0)
|
|
379
|
+
return 1;
|
|
380
|
+
|
|
381
|
+
if ((options->flags & HTML_SKIP_STYLE) != 0 &&
|
|
382
|
+
sdhtml_is_tag(text->data, text->size, "style"))
|
|
383
|
+
return 1;
|
|
384
|
+
|
|
385
|
+
if ((options->flags & HTML_SKIP_LINKS) != 0 &&
|
|
386
|
+
sdhtml_is_tag(text->data, text->size, "a"))
|
|
387
|
+
return 1;
|
|
388
|
+
|
|
389
|
+
if ((options->flags & HTML_SKIP_IMAGES) != 0 &&
|
|
390
|
+
sdhtml_is_tag(text->data, text->size, "img"))
|
|
391
|
+
return 1;
|
|
392
|
+
|
|
393
|
+
bufput(ob, text->data, text->size);
|
|
394
|
+
return 1;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
static void
|
|
398
|
+
rndr_table(struct buf *ob, const struct buf *header, const struct buf *body, void *opaque)
|
|
399
|
+
{
|
|
400
|
+
if (ob->size) bufputc(ob, '\n');
|
|
401
|
+
BUFPUTSL(ob, "<table><thead>\n");
|
|
402
|
+
if (header)
|
|
403
|
+
bufput(ob, header->data, header->size);
|
|
404
|
+
BUFPUTSL(ob, "</thead><tbody>\n");
|
|
405
|
+
if (body)
|
|
406
|
+
bufput(ob, body->data, body->size);
|
|
407
|
+
BUFPUTSL(ob, "</tbody></table>\n");
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
static void
|
|
411
|
+
rndr_tablerow(struct buf *ob, const struct buf *text, void *opaque)
|
|
412
|
+
{
|
|
413
|
+
BUFPUTSL(ob, "<tr>\n");
|
|
414
|
+
if (text)
|
|
415
|
+
bufput(ob, text->data, text->size);
|
|
416
|
+
BUFPUTSL(ob, "</tr>\n");
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
static void
|
|
420
|
+
rndr_tablecell(struct buf *ob, const struct buf *text, int flags, void *opaque)
|
|
421
|
+
{
|
|
422
|
+
if (flags & MKD_TABLE_HEADER) {
|
|
423
|
+
BUFPUTSL(ob, "<th");
|
|
424
|
+
} else {
|
|
425
|
+
BUFPUTSL(ob, "<td");
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
switch (flags & MKD_TABLE_ALIGNMASK) {
|
|
429
|
+
case MKD_TABLE_ALIGN_CENTER:
|
|
430
|
+
BUFPUTSL(ob, " align=\"center\">");
|
|
431
|
+
break;
|
|
432
|
+
|
|
433
|
+
case MKD_TABLE_ALIGN_L:
|
|
434
|
+
BUFPUTSL(ob, " align=\"left\">");
|
|
435
|
+
break;
|
|
436
|
+
|
|
437
|
+
case MKD_TABLE_ALIGN_R:
|
|
438
|
+
BUFPUTSL(ob, " align=\"right\">");
|
|
439
|
+
break;
|
|
440
|
+
|
|
441
|
+
default:
|
|
442
|
+
BUFPUTSL(ob, ">");
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
if (text)
|
|
446
|
+
bufput(ob, text->data, text->size);
|
|
447
|
+
|
|
448
|
+
if (flags & MKD_TABLE_HEADER) {
|
|
449
|
+
BUFPUTSL(ob, "</th>\n");
|
|
450
|
+
} else {
|
|
451
|
+
BUFPUTSL(ob, "</td>\n");
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
static int
|
|
456
|
+
rndr_superscript(struct buf *ob, const struct buf *text, void *opaque)
|
|
457
|
+
{
|
|
458
|
+
if (!text || !text->size) return 0;
|
|
459
|
+
BUFPUTSL(ob, "<sup>");
|
|
460
|
+
bufput(ob, text->data, text->size);
|
|
461
|
+
BUFPUTSL(ob, "</sup>");
|
|
462
|
+
return 1;
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
static void
|
|
466
|
+
rndr_normal_text(struct buf *ob, const struct buf *text, void *opaque)
|
|
467
|
+
{
|
|
468
|
+
if (text)
|
|
469
|
+
houdini_escape_html(ob, text->data, text->size);
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
static void
|
|
473
|
+
toc_header(struct buf *ob, const struct buf *text, int level, void *opaque)
|
|
474
|
+
{
|
|
475
|
+
struct html_renderopt *options = opaque;
|
|
476
|
+
|
|
477
|
+
if (level > options->toc_data.current_level) {
|
|
478
|
+
while (level > options->toc_data.current_level) {
|
|
479
|
+
BUFPUTSL(ob, "<ul>\n<li>\n");
|
|
480
|
+
options->toc_data.current_level++;
|
|
481
|
+
}
|
|
482
|
+
} else if (level < options->toc_data.current_level) {
|
|
483
|
+
BUFPUTSL(ob, "</li>\n");
|
|
484
|
+
while (level < options->toc_data.current_level) {
|
|
485
|
+
BUFPUTSL(ob, "</ul>\n</li>\n");
|
|
486
|
+
options->toc_data.current_level--;
|
|
487
|
+
}
|
|
488
|
+
BUFPUTSL(ob,"<li>\n");
|
|
489
|
+
} else {
|
|
490
|
+
BUFPUTSL(ob,"</li>\n<li>\n");
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
bufprintf(ob, "<a href=\"#toc_%d\">", options->toc_data.header_count++);
|
|
494
|
+
if (text)
|
|
495
|
+
bufput(ob, text->data, text->size);
|
|
496
|
+
BUFPUTSL(ob, "</a>\n");
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
static void
|
|
500
|
+
toc_finalize(struct buf *ob, void *opaque)
|
|
501
|
+
{
|
|
502
|
+
struct html_renderopt *options = opaque;
|
|
503
|
+
|
|
504
|
+
while (options->toc_data.current_level > 0) {
|
|
505
|
+
BUFPUTSL(ob, "</li>\n</ul>\n");
|
|
506
|
+
options->toc_data.current_level--;
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
void
|
|
511
|
+
sdhtml_toc_renderer(struct sd_callbacks *callbacks, struct html_renderopt *options)
|
|
512
|
+
{
|
|
513
|
+
static const struct sd_callbacks cb_default = {
|
|
514
|
+
NULL,
|
|
515
|
+
NULL,
|
|
516
|
+
NULL,
|
|
517
|
+
toc_header,
|
|
518
|
+
NULL,
|
|
519
|
+
NULL,
|
|
520
|
+
NULL,
|
|
521
|
+
NULL,
|
|
522
|
+
NULL,
|
|
523
|
+
NULL,
|
|
524
|
+
NULL,
|
|
525
|
+
|
|
526
|
+
NULL,
|
|
527
|
+
rndr_codespan,
|
|
528
|
+
rndr_double_emphasis,
|
|
529
|
+
rndr_emphasis,
|
|
530
|
+
NULL,
|
|
531
|
+
NULL,
|
|
532
|
+
NULL,
|
|
533
|
+
NULL,
|
|
534
|
+
rndr_triple_emphasis,
|
|
535
|
+
rndr_strikethrough,
|
|
536
|
+
rndr_superscript,
|
|
537
|
+
|
|
538
|
+
NULL,
|
|
539
|
+
NULL,
|
|
540
|
+
|
|
541
|
+
NULL,
|
|
542
|
+
toc_finalize,
|
|
543
|
+
};
|
|
544
|
+
|
|
545
|
+
memset(options, 0x0, sizeof(struct html_renderopt));
|
|
546
|
+
options->flags = HTML_TOC;
|
|
547
|
+
|
|
548
|
+
memcpy(callbacks, &cb_default, sizeof(struct sd_callbacks));
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
void
|
|
552
|
+
sdhtml_renderer(struct sd_callbacks *callbacks, struct html_renderopt *options, unsigned int render_flags)
|
|
553
|
+
{
|
|
554
|
+
static const struct sd_callbacks cb_default = {
|
|
555
|
+
rndr_blockcode,
|
|
556
|
+
rndr_blockquote,
|
|
557
|
+
rndr_raw_block,
|
|
558
|
+
rndr_header,
|
|
559
|
+
rndr_hrule,
|
|
560
|
+
rndr_list,
|
|
561
|
+
rndr_listitem,
|
|
562
|
+
rndr_paragraph,
|
|
563
|
+
rndr_table,
|
|
564
|
+
rndr_tablerow,
|
|
565
|
+
rndr_tablecell,
|
|
566
|
+
|
|
567
|
+
rndr_autolink,
|
|
568
|
+
rndr_codespan,
|
|
569
|
+
rndr_double_emphasis,
|
|
570
|
+
rndr_emphasis,
|
|
571
|
+
rndr_image,
|
|
572
|
+
rndr_linebreak,
|
|
573
|
+
rndr_link,
|
|
574
|
+
rndr_raw_html,
|
|
575
|
+
rndr_triple_emphasis,
|
|
576
|
+
rndr_strikethrough,
|
|
577
|
+
rndr_superscript,
|
|
578
|
+
|
|
579
|
+
NULL,
|
|
580
|
+
rndr_normal_text,
|
|
581
|
+
|
|
582
|
+
NULL,
|
|
583
|
+
NULL,
|
|
584
|
+
};
|
|
585
|
+
|
|
586
|
+
/* Prepare the options pointer */
|
|
587
|
+
memset(options, 0x0, sizeof(struct html_renderopt));
|
|
588
|
+
options->flags = render_flags;
|
|
589
|
+
|
|
590
|
+
/* Prepare the callbacks */
|
|
591
|
+
memcpy(callbacks, &cb_default, sizeof(struct sd_callbacks));
|
|
592
|
+
|
|
593
|
+
if (render_flags & HTML_SKIP_IMAGES)
|
|
594
|
+
callbacks->image = NULL;
|
|
595
|
+
|
|
596
|
+
if (render_flags & HTML_SKIP_LINKS) {
|
|
597
|
+
callbacks->link = NULL;
|
|
598
|
+
callbacks->autolink = NULL;
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
if (render_flags & HTML_SKIP_HTML)
|
|
602
|
+
callbacks->blockhtml = NULL;
|
|
603
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2011, Vicent Marti
|
|
3
|
+
*
|
|
4
|
+
* Permission to use, copy, modify, and distribute this software for any
|
|
5
|
+
* purpose with or without fee is hereby granted, provided that the above
|
|
6
|
+
* copyright notice and this permission notice appear in all copies.
|
|
7
|
+
*
|
|
8
|
+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
9
|
+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
10
|
+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
11
|
+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
12
|
+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
13
|
+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
14
|
+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
#ifndef UPSKIRT_HTML_H
|
|
18
|
+
#define UPSKIRT_HTML_H
|
|
19
|
+
|
|
20
|
+
#include "markdown.h"
|
|
21
|
+
#include "buffer.h"
|
|
22
|
+
#include <stdlib.h>
|
|
23
|
+
|
|
24
|
+
struct html_renderopt {
|
|
25
|
+
struct {
|
|
26
|
+
int header_count;
|
|
27
|
+
int current_level;
|
|
28
|
+
} toc_data;
|
|
29
|
+
|
|
30
|
+
unsigned int flags;
|
|
31
|
+
|
|
32
|
+
/* extra callbacks */
|
|
33
|
+
void (*link_attributes)(struct buf *ob, const struct buf *url, void *self);
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
typedef enum {
|
|
37
|
+
HTML_SKIP_HTML = (1 << 0),
|
|
38
|
+
HTML_SKIP_STYLE = (1 << 1),
|
|
39
|
+
HTML_SKIP_IMAGES = (1 << 2),
|
|
40
|
+
HTML_SKIP_LINKS = (1 << 3),
|
|
41
|
+
HTML_EXPAND_TABS = (1 << 4),
|
|
42
|
+
HTML_SAFELINK = (1 << 5),
|
|
43
|
+
HTML_TOC = (1 << 6),
|
|
44
|
+
HTML_HARD_WRAP = (1 << 7),
|
|
45
|
+
HTML_USE_XHTML = (1 << 8),
|
|
46
|
+
} html_render_mode;
|
|
47
|
+
|
|
48
|
+
typedef enum {
|
|
49
|
+
HTML_TAG_NONE = 0,
|
|
50
|
+
HTML_TAG_OPEN,
|
|
51
|
+
HTML_TAG_CLOSE,
|
|
52
|
+
} html_tag;
|
|
53
|
+
|
|
54
|
+
int
|
|
55
|
+
sdhtml_is_tag(const uint8_t *tag_data, size_t tag_size, const char *tagname);
|
|
56
|
+
|
|
57
|
+
extern void
|
|
58
|
+
sdhtml_renderer(struct sd_callbacks *callbacks, struct html_renderopt *options_ptr, unsigned int render_flags);
|
|
59
|
+
|
|
60
|
+
extern void
|
|
61
|
+
sdhtml_toc_renderer(struct sd_callbacks *callbacks, struct html_renderopt *options_ptr);
|
|
62
|
+
|
|
63
|
+
extern void
|
|
64
|
+
sdhtml_smartypants(struct buf *ob, const uint8_t *text, size_t size);
|
|
65
|
+
|
|
66
|
+
#endif
|
|
67
|
+
|