redcarpet_yt 0.0.1
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.
- checksums.yaml +7 -0
- data/COPYING +20 -0
- data/Gemfile +9 -0
- data/README.markdown +394 -0
- data/Rakefile +60 -0
- data/bin/redcarpet +7 -0
- data/ext/redcarpet/autolink.c +302 -0
- data/ext/redcarpet/autolink.h +55 -0
- data/ext/redcarpet/buffer.c +203 -0
- data/ext/redcarpet/buffer.h +89 -0
- data/ext/redcarpet/extconf.rb +6 -0
- data/ext/redcarpet/houdini.h +51 -0
- data/ext/redcarpet/houdini_href_e.c +124 -0
- data/ext/redcarpet/houdini_html_e.c +105 -0
- data/ext/redcarpet/html.c +825 -0
- data/ext/redcarpet/html.h +84 -0
- data/ext/redcarpet/html_blocks.h +229 -0
- data/ext/redcarpet/html_smartypants.c +457 -0
- data/ext/redcarpet/markdown.c +2917 -0
- data/ext/redcarpet/markdown.h +143 -0
- data/ext/redcarpet/rc_markdown.c +168 -0
- data/ext/redcarpet/rc_render.c +545 -0
- data/ext/redcarpet/redcarpet.h +52 -0
- data/ext/redcarpet/stack.c +84 -0
- data/ext/redcarpet/stack.h +48 -0
- data/lib/redcarpet/cli.rb +86 -0
- data/lib/redcarpet/compat.rb +73 -0
- data/lib/redcarpet/render_man.rb +65 -0
- data/lib/redcarpet/render_strip.rb +60 -0
- data/lib/redcarpet_yt.rb +103 -0
- data/redcarpet_yt.gemspec +71 -0
- data/test/benchmark.rb +24 -0
- data/test/custom_render_test.rb +28 -0
- data/test/fixtures/benchmark.md +232 -0
- data/test/html5_test.rb +69 -0
- data/test/html_render_test.rb +254 -0
- data/test/html_toc_render_test.rb +75 -0
- data/test/markdown_test.rb +371 -0
- data/test/pathological_inputs_test.rb +34 -0
- data/test/redcarpet_bin_test.rb +80 -0
- data/test/redcarpet_compat_test.rb +38 -0
- data/test/safe_render_test.rb +35 -0
- data/test/smarty_html_test.rb +45 -0
- data/test/smarty_pants_test.rb +53 -0
- data/test/stripdown_render_test.rb +61 -0
- data/test/test_helper.rb +39 -0
- metadata +151 -0
@@ -0,0 +1,89 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2008, Natacha Porté
|
3
|
+
* Copyright (c) 2015, Vicent Marti
|
4
|
+
*
|
5
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
* of this software and associated documentation files (the "Software"), to deal
|
7
|
+
* in the Software without restriction, including without limitation the rights
|
8
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
* copies of the Software, and to permit persons to whom the Software is
|
10
|
+
* furnished to do so, subject to the following conditions:
|
11
|
+
*
|
12
|
+
* The above copyright notice and this permission notice shall be included in
|
13
|
+
* all copies or substantial portions of the Software.
|
14
|
+
*
|
15
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
* THE SOFTWARE.
|
22
|
+
*/
|
23
|
+
|
24
|
+
#ifndef BUFFER_H__
|
25
|
+
#define BUFFER_H__
|
26
|
+
|
27
|
+
#include <stddef.h>
|
28
|
+
#include <stdarg.h>
|
29
|
+
#include <stdint.h>
|
30
|
+
|
31
|
+
#ifdef __cplusplus
|
32
|
+
extern "C" {
|
33
|
+
#endif
|
34
|
+
|
35
|
+
#if defined(_MSC_VER)
|
36
|
+
#define __attribute__(x)
|
37
|
+
#define inline
|
38
|
+
#endif
|
39
|
+
|
40
|
+
typedef enum {
|
41
|
+
BUF_OK = 0,
|
42
|
+
BUF_ENOMEM = -1,
|
43
|
+
} buferror_t;
|
44
|
+
|
45
|
+
/* struct buf: character array buffer */
|
46
|
+
struct buf {
|
47
|
+
uint8_t *data; /* actual character data */
|
48
|
+
size_t size; /* size of the string */
|
49
|
+
size_t asize; /* allocated size (0 = volatile buffer) */
|
50
|
+
size_t unit; /* reallocation unit size (0 = read-only buffer) */
|
51
|
+
};
|
52
|
+
|
53
|
+
/* BUFPUTSL: optimized bufputs of a string literal */
|
54
|
+
#define BUFPUTSL(output, literal) \
|
55
|
+
bufput(output, literal, sizeof literal - 1)
|
56
|
+
|
57
|
+
/* bufgrow: increasing the allocated size to the given value */
|
58
|
+
int bufgrow(struct buf *, size_t);
|
59
|
+
|
60
|
+
/* bufnew: allocation of a new buffer */
|
61
|
+
struct buf *bufnew(size_t) __attribute__ ((malloc));
|
62
|
+
|
63
|
+
/* bufnullterm: NUL-termination of the string array (making a C-string) */
|
64
|
+
const char *bufcstr(const struct buf *);
|
65
|
+
|
66
|
+
/* bufprefix: compare the beginning of a buffer with a string */
|
67
|
+
int bufprefix(const struct buf *buf, const char *prefix);
|
68
|
+
|
69
|
+
/* bufput: appends raw data to a buffer */
|
70
|
+
void bufput(struct buf *, const void *, size_t);
|
71
|
+
|
72
|
+
/* bufputs: appends a NUL-terminated string to a buffer */
|
73
|
+
void bufputs(struct buf *, const char *);
|
74
|
+
|
75
|
+
/* bufputc: appends a single char to a buffer */
|
76
|
+
void bufputc(struct buf *, int);
|
77
|
+
|
78
|
+
/* bufrelease: decrease the reference count and free the buffer if needed */
|
79
|
+
void bufrelease(struct buf *);
|
80
|
+
|
81
|
+
/* bufprintf: formatted printing to a buffer */
|
82
|
+
void bufprintf(struct buf *, const char *, ...) __attribute__ ((format (printf, 2, 3)));
|
83
|
+
|
84
|
+
#ifdef __cplusplus
|
85
|
+
}
|
86
|
+
#endif
|
87
|
+
|
88
|
+
#endif
|
89
|
+
|
@@ -0,0 +1,51 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2015, Vicent Marti
|
3
|
+
*
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
5
|
+
* of this software and associated documentation files (the "Software"), to deal
|
6
|
+
* in the Software without restriction, including without limitation the rights
|
7
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
8
|
+
* copies of the Software, and to permit persons to whom the Software is
|
9
|
+
* furnished to do so, subject to the following conditions:
|
10
|
+
*
|
11
|
+
* The above copyright notice and this permission notice shall be included in
|
12
|
+
* all copies or substantial portions of the Software.
|
13
|
+
*
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
16
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
17
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
18
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
19
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
20
|
+
* THE SOFTWARE.
|
21
|
+
*/
|
22
|
+
|
23
|
+
#ifndef HOUDINI_H__
|
24
|
+
#define HOUDINI_H__
|
25
|
+
|
26
|
+
#include "buffer.h"
|
27
|
+
|
28
|
+
#ifdef __cplusplus
|
29
|
+
extern "C" {
|
30
|
+
#endif
|
31
|
+
|
32
|
+
#ifdef HOUDINI_USE_LOCALE
|
33
|
+
# define _isxdigit(c) isxdigit(c)
|
34
|
+
# define _isdigit(c) isdigit(c)
|
35
|
+
#else
|
36
|
+
/*
|
37
|
+
* Helper _isdigit methods -- do not trust the current locale
|
38
|
+
* */
|
39
|
+
# define _isxdigit(c) (strchr("0123456789ABCDEFabcdef", (c)) != NULL)
|
40
|
+
# define _isdigit(c) ((c) >= '0' && (c) <= '9')
|
41
|
+
#endif
|
42
|
+
|
43
|
+
extern void houdini_escape_html(struct buf *ob, const uint8_t *src, size_t size);
|
44
|
+
extern void houdini_escape_html0(struct buf *ob, const uint8_t *src, size_t size, int secure);
|
45
|
+
extern void houdini_escape_href(struct buf *ob, const uint8_t *src, size_t size);
|
46
|
+
|
47
|
+
#ifdef __cplusplus
|
48
|
+
}
|
49
|
+
#endif
|
50
|
+
|
51
|
+
#endif
|
@@ -0,0 +1,124 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2015, Vicent Marti
|
3
|
+
*
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
5
|
+
* of this software and associated documentation files (the "Software"), to deal
|
6
|
+
* in the Software without restriction, including without limitation the rights
|
7
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
8
|
+
* copies of the Software, and to permit persons to whom the Software is
|
9
|
+
* furnished to do so, subject to the following conditions:
|
10
|
+
*
|
11
|
+
* The above copyright notice and this permission notice shall be included in
|
12
|
+
* all copies or substantial portions of the Software.
|
13
|
+
*
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
16
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
17
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
18
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
19
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
20
|
+
* THE SOFTWARE.
|
21
|
+
*/
|
22
|
+
|
23
|
+
#include <assert.h>
|
24
|
+
#include <stdio.h>
|
25
|
+
#include <string.h>
|
26
|
+
|
27
|
+
#include "houdini.h"
|
28
|
+
|
29
|
+
#define ESCAPE_GROW_FACTOR(x) (((x) * 12) / 10)
|
30
|
+
|
31
|
+
/*
|
32
|
+
* The following characters will not be escaped:
|
33
|
+
*
|
34
|
+
* -_.+!*'(),%#@?=;:/,+&$ alphanum
|
35
|
+
*
|
36
|
+
* Note that this character set is the addition of:
|
37
|
+
*
|
38
|
+
* - The characters which are safe to be in an URL
|
39
|
+
* - The characters which are *not* safe to be in
|
40
|
+
* an URL because they are RESERVED characters.
|
41
|
+
*
|
42
|
+
* We asume (lazily) that any RESERVED char that
|
43
|
+
* appears inside an URL is actually meant to
|
44
|
+
* have its native function (i.e. as an URL
|
45
|
+
* component/separator) and hence needs no escaping.
|
46
|
+
*
|
47
|
+
* There is one exception: the ' (single quote)
|
48
|
+
* character does not appear in the table.
|
49
|
+
* It is meant to appear in the URL as components,
|
50
|
+
* however it require special HTML-entity escaping
|
51
|
+
* to generate valid HTML markup.
|
52
|
+
*
|
53
|
+
* All other characters will be escaped to %XX.
|
54
|
+
*
|
55
|
+
*/
|
56
|
+
static const char HREF_SAFE[] = {
|
57
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
58
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
59
|
+
0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1,
|
60
|
+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1,
|
61
|
+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
62
|
+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,
|
63
|
+
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
64
|
+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
|
65
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
66
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
67
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
68
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
69
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
70
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
71
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
72
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
73
|
+
};
|
74
|
+
|
75
|
+
void
|
76
|
+
houdini_escape_href(struct buf *ob, const uint8_t *src, size_t size)
|
77
|
+
{
|
78
|
+
static const char hex_chars[] = "0123456789ABCDEF";
|
79
|
+
size_t i = 0, org;
|
80
|
+
char hex_str[3];
|
81
|
+
|
82
|
+
bufgrow(ob, ESCAPE_GROW_FACTOR(size));
|
83
|
+
hex_str[0] = '%';
|
84
|
+
|
85
|
+
while (i < size) {
|
86
|
+
org = i;
|
87
|
+
while (i < size && HREF_SAFE[src[i]] != 0)
|
88
|
+
i++;
|
89
|
+
|
90
|
+
if (i > org)
|
91
|
+
bufput(ob, src + org, i - org);
|
92
|
+
|
93
|
+
/* escaping */
|
94
|
+
if (i >= size)
|
95
|
+
break;
|
96
|
+
|
97
|
+
switch (src[i]) {
|
98
|
+
/* the single quote is a valid URL character
|
99
|
+
* according to the standard; it needs HTML
|
100
|
+
* entity escaping too */
|
101
|
+
case '\'':
|
102
|
+
BUFPUTSL(ob, "'");
|
103
|
+
break;
|
104
|
+
|
105
|
+
/* the space can be escaped to %20 or a plus
|
106
|
+
* sign. we're going with the generic escape
|
107
|
+
* for now. the plus thing is more commonly seen
|
108
|
+
* when building GET strings */
|
109
|
+
#if 0
|
110
|
+
case ' ':
|
111
|
+
bufputc(ob, '+');
|
112
|
+
break;
|
113
|
+
#endif
|
114
|
+
|
115
|
+
/* every other character goes with a %XX escaping */
|
116
|
+
default:
|
117
|
+
hex_str[1] = hex_chars[(src[i] >> 4) & 0xF];
|
118
|
+
hex_str[2] = hex_chars[src[i] & 0xF];
|
119
|
+
bufput(ob, hex_str, 3);
|
120
|
+
}
|
121
|
+
|
122
|
+
i++;
|
123
|
+
}
|
124
|
+
}
|
@@ -0,0 +1,105 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2015, Vicent Marti
|
3
|
+
*
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
5
|
+
* of this software and associated documentation files (the "Software"), to deal
|
6
|
+
* in the Software without restriction, including without limitation the rights
|
7
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
8
|
+
* copies of the Software, and to permit persons to whom the Software is
|
9
|
+
* furnished to do so, subject to the following conditions:
|
10
|
+
*
|
11
|
+
* The above copyright notice and this permission notice shall be included in
|
12
|
+
* all copies or substantial portions of the Software.
|
13
|
+
*
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
16
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
17
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
18
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
19
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
20
|
+
* THE SOFTWARE.
|
21
|
+
*/
|
22
|
+
|
23
|
+
#include <assert.h>
|
24
|
+
#include <stdio.h>
|
25
|
+
#include <string.h>
|
26
|
+
|
27
|
+
#include "houdini.h"
|
28
|
+
|
29
|
+
#define ESCAPE_GROW_FACTOR(x) (((x) * 12) / 10) /* this is very scientific, yes */
|
30
|
+
|
31
|
+
/**
|
32
|
+
* According to the OWASP rules:
|
33
|
+
*
|
34
|
+
* & --> &
|
35
|
+
* < --> <
|
36
|
+
* > --> >
|
37
|
+
* " --> "
|
38
|
+
* ' --> ' ' is not recommended
|
39
|
+
* / --> / forward slash is included as it helps end an HTML entity
|
40
|
+
*
|
41
|
+
*/
|
42
|
+
static const char HTML_ESCAPE_TABLE[] = {
|
43
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
44
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
45
|
+
0, 0, 1, 0, 0, 0, 2, 3, 0, 0, 0, 0, 0, 0, 0, 4,
|
46
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 6, 0,
|
47
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
48
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
49
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
50
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
51
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
52
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
53
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
54
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
55
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
56
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
57
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
58
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
59
|
+
};
|
60
|
+
|
61
|
+
static const char *HTML_ESCAPES[] = {
|
62
|
+
"",
|
63
|
+
""",
|
64
|
+
"&",
|
65
|
+
"'",
|
66
|
+
"/",
|
67
|
+
"<",
|
68
|
+
">"
|
69
|
+
};
|
70
|
+
|
71
|
+
void
|
72
|
+
houdini_escape_html0(struct buf *ob, const uint8_t *src, size_t size, int secure)
|
73
|
+
{
|
74
|
+
size_t i = 0, org, esc = 0;
|
75
|
+
|
76
|
+
bufgrow(ob, ESCAPE_GROW_FACTOR(size));
|
77
|
+
|
78
|
+
while (i < size) {
|
79
|
+
org = i;
|
80
|
+
while (i < size && (esc = HTML_ESCAPE_TABLE[src[i]]) == 0)
|
81
|
+
i++;
|
82
|
+
|
83
|
+
if (i > org)
|
84
|
+
bufput(ob, src + org, i - org);
|
85
|
+
|
86
|
+
/* escaping */
|
87
|
+
if (i >= size)
|
88
|
+
break;
|
89
|
+
|
90
|
+
/* The forward slash is only escaped in secure mode */
|
91
|
+
if (src[i] == '/' && !secure)
|
92
|
+
bufputc(ob, '/');
|
93
|
+
else
|
94
|
+
bufputs(ob, HTML_ESCAPES[esc]);
|
95
|
+
|
96
|
+
i++;
|
97
|
+
}
|
98
|
+
}
|
99
|
+
|
100
|
+
void
|
101
|
+
houdini_escape_html(struct buf *ob, const uint8_t *src, size_t size)
|
102
|
+
{
|
103
|
+
houdini_escape_html0(ob, src, size, 1);
|
104
|
+
}
|
105
|
+
|
@@ -0,0 +1,825 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2009, Natacha Porté
|
3
|
+
* Copyright (c) 2015, Vicent Marti
|
4
|
+
*
|
5
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
* of this software and associated documentation files (the "Software"), to deal
|
7
|
+
* in the Software without restriction, including without limitation the rights
|
8
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
* copies of the Software, and to permit persons to whom the Software is
|
10
|
+
* furnished to do so, subject to the following conditions:
|
11
|
+
*
|
12
|
+
* The above copyright notice and this permission notice shall be included in
|
13
|
+
* all copies or substantial portions of the Software.
|
14
|
+
*
|
15
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
* THE SOFTWARE.
|
22
|
+
*/
|
23
|
+
|
24
|
+
#include "markdown.h"
|
25
|
+
#include "html.h"
|
26
|
+
#include <string.h>
|
27
|
+
#include <stdlib.h>
|
28
|
+
#include <stdio.h>
|
29
|
+
#include <ctype.h>
|
30
|
+
|
31
|
+
#include "houdini.h"
|
32
|
+
|
33
|
+
#define USE_XHTML(opt) (opt->flags & HTML_USE_XHTML)
|
34
|
+
|
35
|
+
int
|
36
|
+
sdhtml_is_tag(const uint8_t *tag_data, size_t tag_size, const char *tagname)
|
37
|
+
{
|
38
|
+
size_t i;
|
39
|
+
int closed = 0;
|
40
|
+
|
41
|
+
if (tag_size < 3 || tag_data[0] != '<')
|
42
|
+
return HTML_TAG_NONE;
|
43
|
+
|
44
|
+
i = 1;
|
45
|
+
|
46
|
+
if (tag_data[i] == '/') {
|
47
|
+
closed = 1;
|
48
|
+
i++;
|
49
|
+
}
|
50
|
+
|
51
|
+
for (; i < tag_size; ++i, ++tagname) {
|
52
|
+
if (*tagname == 0)
|
53
|
+
break;
|
54
|
+
|
55
|
+
if (tag_data[i] != *tagname)
|
56
|
+
return HTML_TAG_NONE;
|
57
|
+
}
|
58
|
+
|
59
|
+
if (i == tag_size)
|
60
|
+
return HTML_TAG_NONE;
|
61
|
+
|
62
|
+
if (isspace(tag_data[i]) || tag_data[i] == '>')
|
63
|
+
return closed ? HTML_TAG_CLOSE : HTML_TAG_OPEN;
|
64
|
+
|
65
|
+
return HTML_TAG_NONE;
|
66
|
+
}
|
67
|
+
|
68
|
+
static inline void escape_html(struct buf *ob, const uint8_t *source, size_t length)
|
69
|
+
{
|
70
|
+
houdini_escape_html0(ob, source, length, 0);
|
71
|
+
}
|
72
|
+
|
73
|
+
static inline void escape_href(struct buf *ob, const uint8_t *source, size_t length)
|
74
|
+
{
|
75
|
+
houdini_escape_href(ob, source, length);
|
76
|
+
}
|
77
|
+
|
78
|
+
/********************
|
79
|
+
* GENERIC RENDERER *
|
80
|
+
********************/
|
81
|
+
static int
|
82
|
+
rndr_autolink(struct buf *ob, const struct buf *link, enum mkd_autolink type, void *opaque)
|
83
|
+
{
|
84
|
+
struct html_renderopt *options = opaque;
|
85
|
+
|
86
|
+
if (!link || !link->size)
|
87
|
+
return 0;
|
88
|
+
|
89
|
+
if ((options->flags & HTML_SAFELINK) != 0 &&
|
90
|
+
!sd_autolink_issafe(link->data, link->size) &&
|
91
|
+
type != MKDA_EMAIL)
|
92
|
+
return 0;
|
93
|
+
|
94
|
+
BUFPUTSL(ob, "<a href=\"");
|
95
|
+
if (type == MKDA_EMAIL)
|
96
|
+
BUFPUTSL(ob, "mailto:");
|
97
|
+
escape_href(ob, link->data, link->size);
|
98
|
+
|
99
|
+
if (options->link_attributes) {
|
100
|
+
bufputc(ob, '\"');
|
101
|
+
options->link_attributes(ob, link, opaque);
|
102
|
+
bufputc(ob, '>');
|
103
|
+
} else {
|
104
|
+
BUFPUTSL(ob, "\">");
|
105
|
+
}
|
106
|
+
|
107
|
+
/*
|
108
|
+
* Pretty printing: if we get an email address as
|
109
|
+
* an actual URI, e.g. `mailto:foo@bar.com`, we don't
|
110
|
+
* want to print the `mailto:` prefix
|
111
|
+
*/
|
112
|
+
if (bufprefix(link, "mailto:") == 0) {
|
113
|
+
escape_html(ob, link->data + 7, link->size - 7);
|
114
|
+
} else {
|
115
|
+
escape_html(ob, link->data, link->size);
|
116
|
+
}
|
117
|
+
|
118
|
+
BUFPUTSL(ob, "</a>");
|
119
|
+
|
120
|
+
return 1;
|
121
|
+
}
|
122
|
+
|
123
|
+
static void
|
124
|
+
rndr_blockcode(struct buf *ob, const struct buf *text, const struct buf *lang, void *opaque)
|
125
|
+
{
|
126
|
+
struct html_renderopt *options = opaque;
|
127
|
+
|
128
|
+
if (ob->size) bufputc(ob, '\n');
|
129
|
+
|
130
|
+
if (lang && lang->size) {
|
131
|
+
size_t i, cls;
|
132
|
+
if (options->flags & HTML_PRETTIFY) {
|
133
|
+
BUFPUTSL(ob, "<pre><code class=\"prettyprint lang-");
|
134
|
+
cls++;
|
135
|
+
} else {
|
136
|
+
BUFPUTSL(ob, "<pre><code class=\"");
|
137
|
+
}
|
138
|
+
|
139
|
+
for (i = 0, cls = 0; i < lang->size; ++i, ++cls) {
|
140
|
+
while (i < lang->size && isspace(lang->data[i]))
|
141
|
+
i++;
|
142
|
+
|
143
|
+
if (i < lang->size) {
|
144
|
+
size_t org = i;
|
145
|
+
while (i < lang->size && !isspace(lang->data[i]))
|
146
|
+
i++;
|
147
|
+
|
148
|
+
if (lang->data[org] == '.')
|
149
|
+
org++;
|
150
|
+
|
151
|
+
if (cls) bufputc(ob, ' ');
|
152
|
+
escape_html(ob, lang->data + org, i - org);
|
153
|
+
}
|
154
|
+
}
|
155
|
+
|
156
|
+
BUFPUTSL(ob, "\">");
|
157
|
+
} else if (options->flags & HTML_PRETTIFY) {
|
158
|
+
BUFPUTSL(ob, "<pre><code class=\"prettyprint\">");
|
159
|
+
} else {
|
160
|
+
BUFPUTSL(ob, "<pre><code>");
|
161
|
+
}
|
162
|
+
|
163
|
+
if (text)
|
164
|
+
escape_html(ob, text->data, text->size);
|
165
|
+
|
166
|
+
BUFPUTSL(ob, "</code></pre>\n");
|
167
|
+
}
|
168
|
+
|
169
|
+
static void
|
170
|
+
rndr_blockquote(struct buf *ob, const struct buf *text, void *opaque)
|
171
|
+
{
|
172
|
+
if (ob->size) bufputc(ob, '\n');
|
173
|
+
BUFPUTSL(ob, "<blockquote>\n");
|
174
|
+
if (text) bufput(ob, text->data, text->size);
|
175
|
+
BUFPUTSL(ob, "</blockquote>\n");
|
176
|
+
}
|
177
|
+
|
178
|
+
static int
|
179
|
+
rndr_codespan(struct buf *ob, const struct buf *text, void *opaque)
|
180
|
+
{
|
181
|
+
struct html_renderopt *options = opaque;
|
182
|
+
if (options->flags & HTML_PRETTIFY)
|
183
|
+
BUFPUTSL(ob, "<code class=\"prettyprint\">");
|
184
|
+
else
|
185
|
+
BUFPUTSL(ob, "<code>");
|
186
|
+
if (text) escape_html(ob, text->data, text->size);
|
187
|
+
BUFPUTSL(ob, "</code>");
|
188
|
+
return 1;
|
189
|
+
}
|
190
|
+
|
191
|
+
static int
|
192
|
+
rndr_strikethrough(struct buf *ob, const struct buf *text, void *opaque)
|
193
|
+
{
|
194
|
+
if (!text || !text->size)
|
195
|
+
return 0;
|
196
|
+
|
197
|
+
BUFPUTSL(ob, "<del>");
|
198
|
+
bufput(ob, text->data, text->size);
|
199
|
+
BUFPUTSL(ob, "</del>");
|
200
|
+
return 1;
|
201
|
+
}
|
202
|
+
|
203
|
+
static int
|
204
|
+
rndr_double_emphasis(struct buf *ob, const struct buf *text, void *opaque)
|
205
|
+
{
|
206
|
+
if (!text || !text->size)
|
207
|
+
return 0;
|
208
|
+
|
209
|
+
BUFPUTSL(ob, "<strong>");
|
210
|
+
bufput(ob, text->data, text->size);
|
211
|
+
BUFPUTSL(ob, "</strong>");
|
212
|
+
|
213
|
+
return 1;
|
214
|
+
}
|
215
|
+
|
216
|
+
static int
|
217
|
+
rndr_emphasis(struct buf *ob, const struct buf *text, void *opaque)
|
218
|
+
{
|
219
|
+
if (!text || !text->size) return 0;
|
220
|
+
BUFPUTSL(ob, "<em>");
|
221
|
+
if (text) bufput(ob, text->data, text->size);
|
222
|
+
BUFPUTSL(ob, "</em>");
|
223
|
+
return 1;
|
224
|
+
}
|
225
|
+
|
226
|
+
static int
|
227
|
+
rndr_underline(struct buf *ob, const struct buf *text, void *opaque)
|
228
|
+
{
|
229
|
+
if (!text || !text->size)
|
230
|
+
return 0;
|
231
|
+
|
232
|
+
BUFPUTSL(ob, "<u>");
|
233
|
+
bufput(ob, text->data, text->size);
|
234
|
+
BUFPUTSL(ob, "</u>");
|
235
|
+
|
236
|
+
return 1;
|
237
|
+
}
|
238
|
+
|
239
|
+
static int
|
240
|
+
rndr_highlight(struct buf *ob, const struct buf *text, void *opaque)
|
241
|
+
{
|
242
|
+
if (!text || !text->size)
|
243
|
+
return 0;
|
244
|
+
|
245
|
+
BUFPUTSL(ob, "<mark>");
|
246
|
+
bufput(ob, text->data, text->size);
|
247
|
+
BUFPUTSL(ob, "</mark>");
|
248
|
+
|
249
|
+
return 1;
|
250
|
+
}
|
251
|
+
|
252
|
+
static int
|
253
|
+
rndr_quote(struct buf *ob, const struct buf *text, void *opaque)
|
254
|
+
{
|
255
|
+
if (!text || !text->size)
|
256
|
+
return 0;
|
257
|
+
|
258
|
+
BUFPUTSL(ob, "<q>");
|
259
|
+
bufput(ob, text->data, text->size);
|
260
|
+
BUFPUTSL(ob, "</q>");
|
261
|
+
|
262
|
+
return 1;
|
263
|
+
}
|
264
|
+
|
265
|
+
static int
|
266
|
+
rndr_linebreak(struct buf *ob, void *opaque)
|
267
|
+
{
|
268
|
+
struct html_renderopt *options = opaque;
|
269
|
+
bufputs(ob, USE_XHTML(options) ? "<br/>\n" : "<br>\n");
|
270
|
+
return 1;
|
271
|
+
}
|
272
|
+
|
273
|
+
static void
|
274
|
+
rndr_header_anchor(struct buf *out, const struct buf *anchor)
|
275
|
+
{
|
276
|
+
static const char *STRIPPED = " -&+$,/:;=?@\"#{}|^~[]`\\*()%.!'";
|
277
|
+
|
278
|
+
const uint8_t *a = anchor->data;
|
279
|
+
const size_t size = anchor->size;
|
280
|
+
size_t i = 0;
|
281
|
+
int stripped = 0, inserted = 0;
|
282
|
+
|
283
|
+
for (; i < size; ++i) {
|
284
|
+
if (a[i] == '<') {
|
285
|
+
while (i < size && a[i] != '>')
|
286
|
+
i++;
|
287
|
+
}
|
288
|
+
else if (!isascii(a[i]) || strchr(STRIPPED, a[i])) {
|
289
|
+
if (inserted && !stripped)
|
290
|
+
bufputc(out, '-');
|
291
|
+
stripped = 1;
|
292
|
+
}
|
293
|
+
else {
|
294
|
+
bufputc(out, tolower(a[i]));
|
295
|
+
stripped = 0;
|
296
|
+
inserted++;
|
297
|
+
}
|
298
|
+
}
|
299
|
+
|
300
|
+
if (stripped)
|
301
|
+
out->size--;
|
302
|
+
}
|
303
|
+
|
304
|
+
static void
|
305
|
+
rndr_header(struct buf *ob, const struct buf *text, int level, void *opaque)
|
306
|
+
{
|
307
|
+
struct html_renderopt *options = opaque;
|
308
|
+
|
309
|
+
if (ob->size)
|
310
|
+
bufputc(ob, '\n');
|
311
|
+
|
312
|
+
if ((options->flags & HTML_TOC) && (level <= options->toc_data.nesting_level)) {
|
313
|
+
bufprintf(ob, "<h%d id=\"", level);
|
314
|
+
rndr_header_anchor(ob, text);
|
315
|
+
BUFPUTSL(ob, "\">");
|
316
|
+
}
|
317
|
+
else
|
318
|
+
bufprintf(ob, "<h%d>", level);
|
319
|
+
|
320
|
+
if (text) bufput(ob, text->data, text->size);
|
321
|
+
bufprintf(ob, "</h%d>\n", level);
|
322
|
+
}
|
323
|
+
|
324
|
+
static int
|
325
|
+
rndr_link(struct buf *ob, const struct buf *link, const struct buf *title, const struct buf *content, void *opaque)
|
326
|
+
{
|
327
|
+
struct html_renderopt *options = opaque;
|
328
|
+
|
329
|
+
if (link != NULL && (options->flags & HTML_SAFELINK) != 0 && !sd_autolink_issafe(link->data, link->size))
|
330
|
+
return 0;
|
331
|
+
|
332
|
+
BUFPUTSL(ob, "<a href=\"");
|
333
|
+
|
334
|
+
if (link && link->size)
|
335
|
+
escape_href(ob, link->data, link->size);
|
336
|
+
|
337
|
+
if (title && title->size) {
|
338
|
+
BUFPUTSL(ob, "\" title=\"");
|
339
|
+
escape_html(ob, title->data, title->size);
|
340
|
+
}
|
341
|
+
|
342
|
+
if (options->link_attributes) {
|
343
|
+
bufputc(ob, '\"');
|
344
|
+
options->link_attributes(ob, link, opaque);
|
345
|
+
bufputc(ob, '>');
|
346
|
+
} else {
|
347
|
+
BUFPUTSL(ob, "\">");
|
348
|
+
}
|
349
|
+
|
350
|
+
if (content && content->size) bufput(ob, content->data, content->size);
|
351
|
+
BUFPUTSL(ob, "</a>");
|
352
|
+
return 1;
|
353
|
+
}
|
354
|
+
|
355
|
+
static void
|
356
|
+
rndr_list(struct buf *ob, const struct buf *text, int flags, void *opaque)
|
357
|
+
{
|
358
|
+
if (ob->size) bufputc(ob, '\n');
|
359
|
+
bufput(ob, flags & MKD_LIST_ORDERED ? "<ol>\n" : "<ul>\n", 5);
|
360
|
+
if (text) bufput(ob, text->data, text->size);
|
361
|
+
bufput(ob, flags & MKD_LIST_ORDERED ? "</ol>\n" : "</ul>\n", 6);
|
362
|
+
}
|
363
|
+
|
364
|
+
static void
|
365
|
+
rndr_listitem(struct buf *ob, const struct buf *text, int flags, void *opaque)
|
366
|
+
{
|
367
|
+
BUFPUTSL(ob, "<li>");
|
368
|
+
if (text) {
|
369
|
+
size_t size = text->size;
|
370
|
+
while (size && text->data[size - 1] == '\n')
|
371
|
+
size--;
|
372
|
+
|
373
|
+
bufput(ob, text->data, size);
|
374
|
+
}
|
375
|
+
BUFPUTSL(ob, "</li>\n");
|
376
|
+
}
|
377
|
+
|
378
|
+
static void
|
379
|
+
rndr_paragraph(struct buf *ob, const struct buf *text, void *opaque)
|
380
|
+
{
|
381
|
+
struct html_renderopt *options = opaque;
|
382
|
+
size_t i = 0;
|
383
|
+
|
384
|
+
if (ob->size) bufputc(ob, '\n');
|
385
|
+
|
386
|
+
if (!text || !text->size)
|
387
|
+
return;
|
388
|
+
|
389
|
+
while (i < text->size && isspace(text->data[i])) i++;
|
390
|
+
|
391
|
+
if (i == text->size)
|
392
|
+
return;
|
393
|
+
|
394
|
+
BUFPUTSL(ob, "<p>");
|
395
|
+
if (options->flags & HTML_HARD_WRAP) {
|
396
|
+
size_t org;
|
397
|
+
while (i < text->size) {
|
398
|
+
org = i;
|
399
|
+
while (i < text->size && text->data[i] != '\n')
|
400
|
+
i++;
|
401
|
+
|
402
|
+
if (i > org)
|
403
|
+
bufput(ob, text->data + org, i - org);
|
404
|
+
|
405
|
+
/*
|
406
|
+
* do not insert a line break if this newline
|
407
|
+
* is the last character on the paragraph
|
408
|
+
*/
|
409
|
+
if (i >= text->size - 1)
|
410
|
+
break;
|
411
|
+
|
412
|
+
rndr_linebreak(ob, opaque);
|
413
|
+
i++;
|
414
|
+
}
|
415
|
+
} else {
|
416
|
+
bufput(ob, &text->data[i], text->size - i);
|
417
|
+
}
|
418
|
+
BUFPUTSL(ob, "</p>\n");
|
419
|
+
}
|
420
|
+
|
421
|
+
static void
|
422
|
+
rndr_raw_block(struct buf *ob, const struct buf *text, void *opaque)
|
423
|
+
{
|
424
|
+
size_t org, size;
|
425
|
+
struct html_renderopt *options = opaque;
|
426
|
+
|
427
|
+
if (!text)
|
428
|
+
return;
|
429
|
+
|
430
|
+
size = text->size;
|
431
|
+
while (size > 0 && text->data[size - 1] == '\n')
|
432
|
+
size--;
|
433
|
+
|
434
|
+
for (org = 0; org < size && text->data[org] == '\n'; ++org)
|
435
|
+
|
436
|
+
if (org >= size)
|
437
|
+
return;
|
438
|
+
|
439
|
+
/* Remove style tags if the `:no_styles` option is enabled */
|
440
|
+
if ((options->flags & HTML_SKIP_STYLE) != 0 &&
|
441
|
+
sdhtml_is_tag(text->data, size, "style"))
|
442
|
+
return;
|
443
|
+
|
444
|
+
if (ob->size)
|
445
|
+
bufputc(ob, '\n');
|
446
|
+
|
447
|
+
bufput(ob, text->data + org, size - org);
|
448
|
+
bufputc(ob, '\n');
|
449
|
+
}
|
450
|
+
|
451
|
+
static int
|
452
|
+
rndr_triple_emphasis(struct buf *ob, const struct buf *text, void *opaque)
|
453
|
+
{
|
454
|
+
if (!text || !text->size) return 0;
|
455
|
+
BUFPUTSL(ob, "<strong><em>");
|
456
|
+
bufput(ob, text->data, text->size);
|
457
|
+
BUFPUTSL(ob, "</em></strong>");
|
458
|
+
return 1;
|
459
|
+
}
|
460
|
+
|
461
|
+
static void
|
462
|
+
rndr_hrule(struct buf *ob, void *opaque)
|
463
|
+
{
|
464
|
+
struct html_renderopt *options = opaque;
|
465
|
+
if (ob->size) bufputc(ob, '\n');
|
466
|
+
bufputs(ob, USE_XHTML(options) ? "<hr/>\n" : "<hr>\n");
|
467
|
+
}
|
468
|
+
|
469
|
+
static int
|
470
|
+
rndr_image(struct buf *ob, const struct buf *link, const struct buf *title, const struct buf *alt, void *opaque)
|
471
|
+
{
|
472
|
+
struct html_renderopt *options = opaque;
|
473
|
+
|
474
|
+
if (link != NULL && (options->flags & HTML_SAFELINK) != 0 && !sd_autolink_issafe(link->data, link->size))
|
475
|
+
return 0;
|
476
|
+
|
477
|
+
BUFPUTSL(ob, "<img src=\"");
|
478
|
+
|
479
|
+
if (link && link->size)
|
480
|
+
escape_href(ob, link->data, link->size);
|
481
|
+
|
482
|
+
BUFPUTSL(ob, "\" alt=\"");
|
483
|
+
|
484
|
+
if (alt && alt->size)
|
485
|
+
escape_html(ob, alt->data, alt->size);
|
486
|
+
|
487
|
+
if (title && title->size) {
|
488
|
+
BUFPUTSL(ob, "\" title=\"");
|
489
|
+
escape_html(ob, title->data, title->size);
|
490
|
+
}
|
491
|
+
|
492
|
+
bufputs(ob, USE_XHTML(options) ? "\"/>" : "\">");
|
493
|
+
return 1;
|
494
|
+
}
|
495
|
+
|
496
|
+
static int
|
497
|
+
rndr_raw_html(struct buf *ob, const struct buf *text, void *opaque)
|
498
|
+
{
|
499
|
+
struct html_renderopt *options = opaque;
|
500
|
+
|
501
|
+
/* HTML_ESCAPE overrides SKIP_HTML, SKIP_STYLE, SKIP_LINKS and SKIP_IMAGES
|
502
|
+
It doesn't see if there are any valid tags, just escape all of them. */
|
503
|
+
if((options->flags & HTML_ESCAPE) != 0) {
|
504
|
+
escape_html(ob, text->data, text->size);
|
505
|
+
return 1;
|
506
|
+
}
|
507
|
+
|
508
|
+
if ((options->flags & HTML_SKIP_HTML) != 0)
|
509
|
+
return 1;
|
510
|
+
|
511
|
+
if ((options->flags & HTML_SKIP_STYLE) != 0 &&
|
512
|
+
sdhtml_is_tag(text->data, text->size, "style"))
|
513
|
+
return 1;
|
514
|
+
|
515
|
+
if ((options->flags & HTML_SKIP_LINKS) != 0 &&
|
516
|
+
sdhtml_is_tag(text->data, text->size, "a"))
|
517
|
+
return 1;
|
518
|
+
|
519
|
+
if ((options->flags & HTML_SKIP_IMAGES) != 0 &&
|
520
|
+
sdhtml_is_tag(text->data, text->size, "img"))
|
521
|
+
return 1;
|
522
|
+
|
523
|
+
bufput(ob, text->data, text->size);
|
524
|
+
return 1;
|
525
|
+
}
|
526
|
+
|
527
|
+
static void
|
528
|
+
rndr_table(struct buf *ob, const struct buf *header, const struct buf *body, void *opaque)
|
529
|
+
{
|
530
|
+
if (ob->size) bufputc(ob, '\n');
|
531
|
+
BUFPUTSL(ob, "<table><thead>\n");
|
532
|
+
if (header)
|
533
|
+
bufput(ob, header->data, header->size);
|
534
|
+
BUFPUTSL(ob, "</thead><tbody>\n");
|
535
|
+
if (body)
|
536
|
+
bufput(ob, body->data, body->size);
|
537
|
+
BUFPUTSL(ob, "</tbody></table>\n");
|
538
|
+
}
|
539
|
+
|
540
|
+
static void
|
541
|
+
rndr_tablerow(struct buf *ob, const struct buf *text, void *opaque)
|
542
|
+
{
|
543
|
+
BUFPUTSL(ob, "<tr>\n");
|
544
|
+
if (text)
|
545
|
+
bufput(ob, text->data, text->size);
|
546
|
+
BUFPUTSL(ob, "</tr>\n");
|
547
|
+
}
|
548
|
+
|
549
|
+
static void
|
550
|
+
rndr_tablecell(struct buf *ob, const struct buf *text, int flags, void *opaque)
|
551
|
+
{
|
552
|
+
if (flags & MKD_TABLE_HEADER) {
|
553
|
+
BUFPUTSL(ob, "<th");
|
554
|
+
} else {
|
555
|
+
BUFPUTSL(ob, "<td");
|
556
|
+
}
|
557
|
+
|
558
|
+
switch (flags & MKD_TABLE_ALIGNMASK) {
|
559
|
+
case MKD_TABLE_ALIGN_CENTER:
|
560
|
+
BUFPUTSL(ob, " style=\"text-align: center\">");
|
561
|
+
break;
|
562
|
+
|
563
|
+
case MKD_TABLE_ALIGN_L:
|
564
|
+
BUFPUTSL(ob, " style=\"text-align: left\">");
|
565
|
+
break;
|
566
|
+
|
567
|
+
case MKD_TABLE_ALIGN_R:
|
568
|
+
BUFPUTSL(ob, " style=\"text-align: right\">");
|
569
|
+
break;
|
570
|
+
|
571
|
+
default:
|
572
|
+
BUFPUTSL(ob, ">");
|
573
|
+
}
|
574
|
+
|
575
|
+
if (text)
|
576
|
+
bufput(ob, text->data, text->size);
|
577
|
+
|
578
|
+
if (flags & MKD_TABLE_HEADER) {
|
579
|
+
BUFPUTSL(ob, "</th>\n");
|
580
|
+
} else {
|
581
|
+
BUFPUTSL(ob, "</td>\n");
|
582
|
+
}
|
583
|
+
}
|
584
|
+
|
585
|
+
static int
|
586
|
+
rndr_superscript(struct buf *ob, const struct buf *text, void *opaque)
|
587
|
+
{
|
588
|
+
if (!text || !text->size) return 0;
|
589
|
+
BUFPUTSL(ob, "<sup>");
|
590
|
+
bufput(ob, text->data, text->size);
|
591
|
+
BUFPUTSL(ob, "</sup>");
|
592
|
+
return 1;
|
593
|
+
}
|
594
|
+
|
595
|
+
static void
|
596
|
+
rndr_normal_text(struct buf *ob, const struct buf *text, void *opaque)
|
597
|
+
{
|
598
|
+
if (text)
|
599
|
+
escape_html(ob, text->data, text->size);
|
600
|
+
}
|
601
|
+
|
602
|
+
static void
|
603
|
+
rndr_footnotes(struct buf *ob, const struct buf *text, void *opaque)
|
604
|
+
{
|
605
|
+
struct html_renderopt *options = opaque;
|
606
|
+
|
607
|
+
if (ob->size) bufputc(ob, '\n');
|
608
|
+
|
609
|
+
BUFPUTSL(ob, "<div class=\"footnotes\">\n");
|
610
|
+
bufputs(ob, USE_XHTML(options) ? "<hr/>\n" : "<hr>\n");
|
611
|
+
BUFPUTSL(ob, "<ol>\n");
|
612
|
+
|
613
|
+
if (text)
|
614
|
+
bufput(ob, text->data, text->size);
|
615
|
+
|
616
|
+
BUFPUTSL(ob, "\n</ol>\n</div>\n");
|
617
|
+
}
|
618
|
+
|
619
|
+
static void
|
620
|
+
rndr_footnote_def(struct buf *ob, const struct buf *text, unsigned int num, void *opaque)
|
621
|
+
{
|
622
|
+
size_t i = 0;
|
623
|
+
int pfound = 0;
|
624
|
+
|
625
|
+
/* insert anchor at the end of first paragraph block */
|
626
|
+
if (text) {
|
627
|
+
while ((i+3) < text->size) {
|
628
|
+
if (text->data[i++] != '<') continue;
|
629
|
+
if (text->data[i++] != '/') continue;
|
630
|
+
if (text->data[i++] != 'p' && text->data[i] != 'P') continue;
|
631
|
+
if (text->data[i] != '>') continue;
|
632
|
+
i -= 3;
|
633
|
+
pfound = 1;
|
634
|
+
break;
|
635
|
+
}
|
636
|
+
}
|
637
|
+
|
638
|
+
bufprintf(ob, "\n<li id=\"fn%d\">\n", num);
|
639
|
+
if (pfound) {
|
640
|
+
bufput(ob, text->data, i);
|
641
|
+
bufprintf(ob, " <a href=\"#fnref%d\" rev=\"footnote\">↩</a>", num);
|
642
|
+
bufput(ob, text->data + i, text->size - i);
|
643
|
+
} else if (text) {
|
644
|
+
bufput(ob, text->data, text->size);
|
645
|
+
}
|
646
|
+
BUFPUTSL(ob, "</li>\n");
|
647
|
+
}
|
648
|
+
|
649
|
+
static int
|
650
|
+
rndr_footnote_ref(struct buf *ob, unsigned int num, void *opaque)
|
651
|
+
{
|
652
|
+
bufprintf(ob, "<sup id=\"fnref%d\"><a href=\"#fn%d\" rel=\"footnote\">%d</a></sup>", num, num, num);
|
653
|
+
return 1;
|
654
|
+
}
|
655
|
+
|
656
|
+
static void
|
657
|
+
toc_header(struct buf *ob, const struct buf *text, int level, void *opaque)
|
658
|
+
{
|
659
|
+
struct html_renderopt *options = opaque;
|
660
|
+
|
661
|
+
if (level <= options->toc_data.nesting_level) {
|
662
|
+
/* set the level offset if this is the first header
|
663
|
+
* we're parsing for the document */
|
664
|
+
if (options->toc_data.current_level == 0)
|
665
|
+
options->toc_data.level_offset = level - 1;
|
666
|
+
|
667
|
+
level -= options->toc_data.level_offset;
|
668
|
+
|
669
|
+
if (level > options->toc_data.current_level) {
|
670
|
+
while (level > options->toc_data.current_level) {
|
671
|
+
BUFPUTSL(ob, "<ul>\n<li>\n");
|
672
|
+
options->toc_data.current_level++;
|
673
|
+
}
|
674
|
+
} else if (level < options->toc_data.current_level) {
|
675
|
+
BUFPUTSL(ob, "</li>\n");
|
676
|
+
while (level < options->toc_data.current_level) {
|
677
|
+
BUFPUTSL(ob, "</ul>\n</li>\n");
|
678
|
+
options->toc_data.current_level--;
|
679
|
+
}
|
680
|
+
BUFPUTSL(ob,"<li>\n");
|
681
|
+
} else {
|
682
|
+
BUFPUTSL(ob,"</li>\n<li>\n");
|
683
|
+
}
|
684
|
+
|
685
|
+
bufprintf(ob, "<a href=\"#");
|
686
|
+
rndr_header_anchor(ob, text);
|
687
|
+
BUFPUTSL(ob, "\">");
|
688
|
+
|
689
|
+
if (text) {
|
690
|
+
if (options->flags & HTML_ESCAPE)
|
691
|
+
escape_html(ob, text->data, text->size);
|
692
|
+
else
|
693
|
+
bufput(ob, text->data, text->size);
|
694
|
+
}
|
695
|
+
|
696
|
+
BUFPUTSL(ob, "</a>\n");
|
697
|
+
}
|
698
|
+
}
|
699
|
+
|
700
|
+
static int
|
701
|
+
toc_link(struct buf *ob, const struct buf *link, const struct buf *title, const struct buf *content, void *opaque)
|
702
|
+
{
|
703
|
+
if (content && content->size)
|
704
|
+
bufput(ob, content->data, content->size);
|
705
|
+
return 1;
|
706
|
+
}
|
707
|
+
|
708
|
+
static void
|
709
|
+
toc_finalize(struct buf *ob, void *opaque)
|
710
|
+
{
|
711
|
+
struct html_renderopt *options = opaque;
|
712
|
+
|
713
|
+
while (options->toc_data.current_level > 0) {
|
714
|
+
BUFPUTSL(ob, "</li>\n</ul>\n");
|
715
|
+
options->toc_data.current_level--;
|
716
|
+
}
|
717
|
+
}
|
718
|
+
|
719
|
+
void
|
720
|
+
sdhtml_toc_renderer(struct sd_callbacks *callbacks, struct html_renderopt *options, unsigned int render_flags)
|
721
|
+
{
|
722
|
+
static const struct sd_callbacks cb_default = {
|
723
|
+
NULL,
|
724
|
+
NULL,
|
725
|
+
NULL,
|
726
|
+
toc_header,
|
727
|
+
NULL,
|
728
|
+
NULL,
|
729
|
+
NULL,
|
730
|
+
NULL,
|
731
|
+
NULL,
|
732
|
+
NULL,
|
733
|
+
NULL,
|
734
|
+
rndr_footnotes,
|
735
|
+
rndr_footnote_def,
|
736
|
+
|
737
|
+
NULL,
|
738
|
+
rndr_codespan,
|
739
|
+
rndr_double_emphasis,
|
740
|
+
rndr_emphasis,
|
741
|
+
rndr_underline,
|
742
|
+
rndr_highlight,
|
743
|
+
rndr_quote,
|
744
|
+
NULL,
|
745
|
+
NULL,
|
746
|
+
toc_link,
|
747
|
+
NULL,
|
748
|
+
rndr_triple_emphasis,
|
749
|
+
rndr_strikethrough,
|
750
|
+
rndr_superscript,
|
751
|
+
rndr_footnote_ref,
|
752
|
+
|
753
|
+
NULL,
|
754
|
+
NULL,
|
755
|
+
|
756
|
+
NULL,
|
757
|
+
toc_finalize,
|
758
|
+
};
|
759
|
+
|
760
|
+
memset(options, 0x0, sizeof(struct html_renderopt));
|
761
|
+
options->flags = render_flags;
|
762
|
+
|
763
|
+
memcpy(callbacks, &cb_default, sizeof(struct sd_callbacks));
|
764
|
+
}
|
765
|
+
|
766
|
+
void
|
767
|
+
sdhtml_renderer(struct sd_callbacks *callbacks, struct html_renderopt *options, unsigned int render_flags)
|
768
|
+
{
|
769
|
+
static const struct sd_callbacks cb_default = {
|
770
|
+
rndr_blockcode,
|
771
|
+
rndr_blockquote,
|
772
|
+
rndr_raw_block,
|
773
|
+
rndr_header,
|
774
|
+
rndr_hrule,
|
775
|
+
rndr_list,
|
776
|
+
rndr_listitem,
|
777
|
+
rndr_paragraph,
|
778
|
+
rndr_table,
|
779
|
+
rndr_tablerow,
|
780
|
+
rndr_tablecell,
|
781
|
+
rndr_footnotes,
|
782
|
+
rndr_footnote_def,
|
783
|
+
|
784
|
+
rndr_autolink,
|
785
|
+
rndr_codespan,
|
786
|
+
rndr_double_emphasis,
|
787
|
+
rndr_emphasis,
|
788
|
+
rndr_underline,
|
789
|
+
rndr_highlight,
|
790
|
+
rndr_quote,
|
791
|
+
rndr_image,
|
792
|
+
rndr_linebreak,
|
793
|
+
rndr_link,
|
794
|
+
rndr_raw_html,
|
795
|
+
rndr_triple_emphasis,
|
796
|
+
rndr_strikethrough,
|
797
|
+
rndr_superscript,
|
798
|
+
rndr_footnote_ref,
|
799
|
+
|
800
|
+
NULL,
|
801
|
+
rndr_normal_text,
|
802
|
+
|
803
|
+
NULL,
|
804
|
+
NULL,
|
805
|
+
};
|
806
|
+
|
807
|
+
/* Prepare the options pointer */
|
808
|
+
memset(options, 0x0, sizeof(struct html_renderopt));
|
809
|
+
options->flags = render_flags;
|
810
|
+
options->toc_data.nesting_level = 99;
|
811
|
+
|
812
|
+
/* Prepare the callbacks */
|
813
|
+
memcpy(callbacks, &cb_default, sizeof(struct sd_callbacks));
|
814
|
+
|
815
|
+
if (render_flags & HTML_SKIP_IMAGES)
|
816
|
+
callbacks->image = NULL;
|
817
|
+
|
818
|
+
if (render_flags & HTML_SKIP_LINKS) {
|
819
|
+
callbacks->link = NULL;
|
820
|
+
callbacks->autolink = NULL;
|
821
|
+
}
|
822
|
+
|
823
|
+
if (render_flags & HTML_SKIP_HTML || render_flags & HTML_ESCAPE)
|
824
|
+
callbacks->blockhtml = NULL;
|
825
|
+
}
|