redcarpet 1.17.2 → 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 +294 -37
- data/Rakefile +13 -15
- data/bin/redcarpet +1 -1
- data/ext/redcarpet/autolink.c +18 -19
- data/ext/redcarpet/autolink.h +7 -4
- data/ext/redcarpet/buffer.c +135 -229
- data/ext/redcarpet/buffer.h +47 -113
- 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 +181 -237
- data/ext/redcarpet/html.h +28 -14
- data/ext/redcarpet/html_blocks.h +206 -0
- data/ext/redcarpet/html_smartypants.c +99 -49
- data/ext/redcarpet/markdown.c +712 -493
- data/ext/redcarpet/markdown.h +48 -41
- 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 +62 -101
- data/redcarpet.gemspec +18 -13
- data/test/redcarpet_test.rb +209 -122
- metadata +33 -20
- data/ext/redcarpet/array.c +0 -300
- data/ext/redcarpet/array.h +0 -147
- data/ext/redcarpet/redcarpet.c +0 -161
- 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/redcarpet/markdown.h
CHANGED
|
@@ -31,13 +31,21 @@
|
|
|
31
31
|
* TYPE DEFINITIONS *
|
|
32
32
|
********************/
|
|
33
33
|
|
|
34
|
-
/* mkd_autolink
|
|
34
|
+
/* mkd_autolink - type of autolink */
|
|
35
35
|
enum mkd_autolink {
|
|
36
36
|
MKDA_NOT_AUTOLINK, /* used internally when it is not an autolink*/
|
|
37
37
|
MKDA_NORMAL, /* normal http/http/ftp/mailto/etc link */
|
|
38
38
|
MKDA_EMAIL, /* e-mail link without explit mailto: */
|
|
39
39
|
};
|
|
40
40
|
|
|
41
|
+
enum mkd_tableflags {
|
|
42
|
+
MKD_TABLE_ALIGN_L = 1,
|
|
43
|
+
MKD_TABLE_ALIGN_R = 2,
|
|
44
|
+
MKD_TABLE_ALIGN_CENTER = 3,
|
|
45
|
+
MKD_TABLE_ALIGNMASK = 3,
|
|
46
|
+
MKD_TABLE_HEADER = 4
|
|
47
|
+
};
|
|
48
|
+
|
|
41
49
|
enum mkd_extensions {
|
|
42
50
|
MKDEXT_NO_INTRA_EMPHASIS = (1 << 0),
|
|
43
51
|
MKDEXT_TABLES = (1 << 1),
|
|
@@ -46,48 +54,49 @@ enum mkd_extensions {
|
|
|
46
54
|
MKDEXT_STRIKETHROUGH = (1 << 4),
|
|
47
55
|
MKDEXT_LAX_HTML_BLOCKS = (1 << 5),
|
|
48
56
|
MKDEXT_SPACE_HEADERS = (1 << 6),
|
|
57
|
+
MKDEXT_SUPERSCRIPT = (1 << 7),
|
|
49
58
|
};
|
|
50
59
|
|
|
51
|
-
/*
|
|
52
|
-
struct
|
|
60
|
+
/* sd_callbacks - functions for rendering parsed data */
|
|
61
|
+
struct sd_callbacks {
|
|
53
62
|
/* block level callbacks - NULL skips the block */
|
|
54
|
-
void (*blockcode)(struct buf *ob, struct buf *text, struct buf *lang, void *opaque);
|
|
55
|
-
void (*blockquote)(struct buf *ob, struct buf *text, void *opaque);
|
|
56
|
-
void (*blockhtml)(struct buf *ob,
|
|
57
|
-
void (*header)(struct buf *ob, struct buf *text, int level, void *opaque);
|
|
63
|
+
void (*blockcode)(struct buf *ob, const struct buf *text, const struct buf *lang, void *opaque);
|
|
64
|
+
void (*blockquote)(struct buf *ob, const struct buf *text, void *opaque);
|
|
65
|
+
void (*blockhtml)(struct buf *ob,const struct buf *text, void *opaque);
|
|
66
|
+
void (*header)(struct buf *ob, const struct buf *text, int level, void *opaque);
|
|
58
67
|
void (*hrule)(struct buf *ob, void *opaque);
|
|
59
|
-
void (*list)(struct buf *ob, struct buf *text, int flags, void *opaque);
|
|
60
|
-
void (*listitem)(struct buf *ob, struct buf *text, int flags, void *opaque);
|
|
61
|
-
void (*paragraph)(struct buf *ob, struct buf *text, void *opaque);
|
|
62
|
-
void (*table)(struct buf *ob, struct buf *header, struct buf *body, void *opaque);
|
|
63
|
-
void (*table_row)(struct buf *ob, struct buf *text, void *opaque);
|
|
64
|
-
void (*table_cell)(struct buf *ob, struct buf *text, int flags, void *opaque);
|
|
68
|
+
void (*list)(struct buf *ob, const struct buf *text, int flags, void *opaque);
|
|
69
|
+
void (*listitem)(struct buf *ob, const struct buf *text, int flags, void *opaque);
|
|
70
|
+
void (*paragraph)(struct buf *ob, const struct buf *text, void *opaque);
|
|
71
|
+
void (*table)(struct buf *ob, const struct buf *header, const struct buf *body, void *opaque);
|
|
72
|
+
void (*table_row)(struct buf *ob, const struct buf *text, void *opaque);
|
|
73
|
+
void (*table_cell)(struct buf *ob, const struct buf *text, int flags, void *opaque);
|
|
65
74
|
|
|
66
75
|
|
|
67
76
|
/* span level callbacks - NULL or return 0 prints the span verbatim */
|
|
68
|
-
int (*autolink)(struct buf *ob, struct buf *link, enum mkd_autolink type, void *opaque);
|
|
69
|
-
int (*codespan)(struct buf *ob, struct buf *text, void *opaque);
|
|
70
|
-
int (*double_emphasis)(struct buf *ob, struct buf *text, void *opaque);
|
|
71
|
-
int (*emphasis)(struct buf *ob, struct buf *text, void *opaque);
|
|
72
|
-
int (*image)(struct buf *ob, struct buf *link, struct buf *title, struct buf *alt, void *opaque);
|
|
77
|
+
int (*autolink)(struct buf *ob, const struct buf *link, enum mkd_autolink type, void *opaque);
|
|
78
|
+
int (*codespan)(struct buf *ob, const struct buf *text, void *opaque);
|
|
79
|
+
int (*double_emphasis)(struct buf *ob, const struct buf *text, void *opaque);
|
|
80
|
+
int (*emphasis)(struct buf *ob, const struct buf *text, void *opaque);
|
|
81
|
+
int (*image)(struct buf *ob, const struct buf *link, const struct buf *title, const struct buf *alt, void *opaque);
|
|
73
82
|
int (*linebreak)(struct buf *ob, void *opaque);
|
|
74
|
-
int (*link)(struct buf *ob, struct buf *link, struct buf *title, struct buf *content, void *opaque);
|
|
75
|
-
int (*raw_html_tag)(struct buf *ob, struct buf *tag, void *opaque);
|
|
76
|
-
int (*triple_emphasis)(struct buf *ob, struct buf *text, void *opaque);
|
|
77
|
-
int (*strikethrough)(struct buf *ob, struct buf *text, void *opaque);
|
|
83
|
+
int (*link)(struct buf *ob, const struct buf *link, const struct buf *title, const struct buf *content, void *opaque);
|
|
84
|
+
int (*raw_html_tag)(struct buf *ob, const struct buf *tag, void *opaque);
|
|
85
|
+
int (*triple_emphasis)(struct buf *ob, const struct buf *text, void *opaque);
|
|
86
|
+
int (*strikethrough)(struct buf *ob, const struct buf *text, void *opaque);
|
|
87
|
+
int (*superscript)(struct buf *ob, const struct buf *text, void *opaque);
|
|
78
88
|
|
|
79
89
|
/* low level callbacks - NULL copies input directly into the output */
|
|
80
|
-
void (*entity)(struct buf *ob, struct buf *entity, void *opaque);
|
|
81
|
-
void (*normal_text)(struct buf *ob, struct buf *text, void *opaque);
|
|
90
|
+
void (*entity)(struct buf *ob, const struct buf *entity, void *opaque);
|
|
91
|
+
void (*normal_text)(struct buf *ob, const struct buf *text, void *opaque);
|
|
82
92
|
|
|
83
93
|
/* header and footer */
|
|
84
94
|
void (*doc_header)(struct buf *ob, void *opaque);
|
|
85
95
|
void (*doc_footer)(struct buf *ob, void *opaque);
|
|
86
|
-
|
|
87
|
-
/* user data */
|
|
88
|
-
void *opaque;
|
|
89
96
|
};
|
|
90
97
|
|
|
98
|
+
struct sd_markdown;
|
|
99
|
+
|
|
91
100
|
/*********
|
|
92
101
|
* FLAGS *
|
|
93
102
|
*********/
|
|
@@ -96,27 +105,25 @@ struct mkd_renderer {
|
|
|
96
105
|
#define MKD_LIST_ORDERED 1
|
|
97
106
|
#define MKD_LI_BLOCK 2 /* <li> containing block data */
|
|
98
107
|
|
|
99
|
-
#define MKD_TABLE_ALIGN_L (1 << 0)
|
|
100
|
-
#define MKD_TABLE_ALIGN_R (1 << 1)
|
|
101
|
-
#define MKD_TABLE_ALIGN_CENTER (MKD_TABLE_ALIGN_L | MKD_TABLE_ALIGN_R)
|
|
102
|
-
|
|
103
|
-
/*******************
|
|
104
|
-
* Auxiliar methods
|
|
105
|
-
*******************/
|
|
106
|
-
int
|
|
107
|
-
is_safe_link(const char *link, size_t link_len);
|
|
108
|
-
|
|
109
108
|
/**********************
|
|
110
109
|
* EXPORTED FUNCTIONS *
|
|
111
110
|
**********************/
|
|
112
111
|
|
|
113
|
-
|
|
112
|
+
extern struct sd_markdown *
|
|
113
|
+
sd_markdown_new(
|
|
114
|
+
unsigned int extensions,
|
|
115
|
+
size_t max_nesting,
|
|
116
|
+
const struct sd_callbacks *callbacks,
|
|
117
|
+
void *opaque);
|
|
118
|
+
|
|
119
|
+
extern void
|
|
120
|
+
sd_markdown_render(struct buf *ob, const uint8_t *document, size_t doc_size, struct sd_markdown *md);
|
|
121
|
+
|
|
114
122
|
extern void
|
|
115
|
-
|
|
123
|
+
sd_markdown_free(struct sd_markdown *md);
|
|
116
124
|
|
|
117
|
-
/* ups_version * returns the library version as major.minor.rev */
|
|
118
125
|
extern void
|
|
119
|
-
|
|
126
|
+
sd_version(int *major, int *minor, int *revision);
|
|
120
127
|
|
|
121
128
|
#endif
|
|
122
129
|
|
|
@@ -0,0 +1,137 @@
|
|
|
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
|
+
#include "redcarpet.h"
|
|
17
|
+
|
|
18
|
+
VALUE rb_mRedcarpet;
|
|
19
|
+
VALUE rb_cMarkdown;
|
|
20
|
+
|
|
21
|
+
extern VALUE rb_cRenderBase;
|
|
22
|
+
|
|
23
|
+
static void rb_redcarpet_md_flags(VALUE hash, unsigned int *enabled_extensions_p)
|
|
24
|
+
{
|
|
25
|
+
unsigned int extensions = 0;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Markdown extensions -- all disabled by default
|
|
29
|
+
*/
|
|
30
|
+
if (rb_hash_lookup(hash, CSTR2SYM("no_intra_emphasis")) == Qtrue)
|
|
31
|
+
extensions |= MKDEXT_NO_INTRA_EMPHASIS;
|
|
32
|
+
|
|
33
|
+
if (rb_hash_lookup(hash, CSTR2SYM("tables")) == Qtrue)
|
|
34
|
+
extensions |= MKDEXT_TABLES;
|
|
35
|
+
|
|
36
|
+
if (rb_hash_lookup(hash, CSTR2SYM("fenced_code_blocks")) == Qtrue)
|
|
37
|
+
extensions |= MKDEXT_FENCED_CODE;
|
|
38
|
+
|
|
39
|
+
if (rb_hash_lookup(hash, CSTR2SYM("autolink")) == Qtrue)
|
|
40
|
+
extensions |= MKDEXT_AUTOLINK;
|
|
41
|
+
|
|
42
|
+
if (rb_hash_lookup(hash, CSTR2SYM("strikethrough")) == Qtrue)
|
|
43
|
+
extensions |= MKDEXT_STRIKETHROUGH;
|
|
44
|
+
|
|
45
|
+
if (rb_hash_lookup(hash, CSTR2SYM("lax_html_blocks")) == Qtrue)
|
|
46
|
+
extensions |= MKDEXT_LAX_HTML_BLOCKS;
|
|
47
|
+
|
|
48
|
+
if (rb_hash_lookup(hash, CSTR2SYM("space_after_headers")) == Qtrue)
|
|
49
|
+
extensions |= MKDEXT_SPACE_HEADERS;
|
|
50
|
+
|
|
51
|
+
if (rb_hash_lookup(hash, CSTR2SYM("superscript")) == Qtrue)
|
|
52
|
+
extensions |= MKDEXT_SUPERSCRIPT;
|
|
53
|
+
|
|
54
|
+
*enabled_extensions_p = extensions;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
static void
|
|
58
|
+
rb_redcarpet_md__free(void *markdown)
|
|
59
|
+
{
|
|
60
|
+
sd_markdown_free((struct sd_markdown *)markdown);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
static VALUE rb_redcarpet_md__new(int argc, VALUE *argv, VALUE klass)
|
|
64
|
+
{
|
|
65
|
+
VALUE rb_markdown, rb_rndr, hash;
|
|
66
|
+
unsigned int extensions = 0;
|
|
67
|
+
|
|
68
|
+
struct rb_redcarpet_rndr *rndr;
|
|
69
|
+
struct sd_markdown *markdown;
|
|
70
|
+
|
|
71
|
+
if (rb_scan_args(argc, argv, "11", &rb_rndr, &hash) == 2)
|
|
72
|
+
rb_redcarpet_md_flags(hash, &extensions);
|
|
73
|
+
|
|
74
|
+
if (rb_obj_is_kind_of(rb_rndr, rb_cClass))
|
|
75
|
+
rb_rndr = rb_funcall(rb_rndr, rb_intern("new"), 0);
|
|
76
|
+
|
|
77
|
+
if (!rb_obj_is_kind_of(rb_rndr, rb_cRenderBase))
|
|
78
|
+
rb_raise(rb_eTypeError, "Invalid Renderer instance given");
|
|
79
|
+
|
|
80
|
+
Data_Get_Struct(rb_rndr, struct rb_redcarpet_rndr, rndr);
|
|
81
|
+
|
|
82
|
+
markdown = sd_markdown_new(extensions, 16, &rndr->callbacks, &rndr->options);
|
|
83
|
+
if (!markdown)
|
|
84
|
+
rb_raise(rb_eRuntimeError, "Failed to create new Renderer class");
|
|
85
|
+
|
|
86
|
+
rb_markdown = Data_Wrap_Struct(klass, NULL, rb_redcarpet_md__free, markdown);
|
|
87
|
+
rb_iv_set(rb_markdown, "@renderer", rb_rndr);
|
|
88
|
+
|
|
89
|
+
return rb_markdown;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
static VALUE rb_redcarpet_md_render(VALUE self, VALUE text)
|
|
93
|
+
{
|
|
94
|
+
VALUE rb_rndr;
|
|
95
|
+
struct buf *output_buf;
|
|
96
|
+
struct sd_markdown *markdown;
|
|
97
|
+
|
|
98
|
+
Check_Type(text, T_STRING);
|
|
99
|
+
|
|
100
|
+
rb_rndr = rb_iv_get(self, "@renderer");
|
|
101
|
+
Data_Get_Struct(self, struct sd_markdown, markdown);
|
|
102
|
+
|
|
103
|
+
if (rb_respond_to(rb_rndr, rb_intern("preprocess")))
|
|
104
|
+
text = rb_funcall(rb_rndr, rb_intern("preprocess"), 1, text);
|
|
105
|
+
|
|
106
|
+
/* initialize buffers */
|
|
107
|
+
output_buf = bufnew(128);
|
|
108
|
+
|
|
109
|
+
/* render the magic */
|
|
110
|
+
sd_markdown_render(
|
|
111
|
+
output_buf,
|
|
112
|
+
RSTRING_PTR(text),
|
|
113
|
+
RSTRING_LEN(text),
|
|
114
|
+
markdown);
|
|
115
|
+
|
|
116
|
+
/* build the Ruby string */
|
|
117
|
+
text = redcarpet_str_new(output_buf->data, output_buf->size);
|
|
118
|
+
|
|
119
|
+
bufrelease(output_buf);
|
|
120
|
+
|
|
121
|
+
if (rb_respond_to(rb_rndr, rb_intern("postprocess")))
|
|
122
|
+
text = rb_funcall(rb_rndr, rb_intern("postprocess"), 1, text);
|
|
123
|
+
|
|
124
|
+
return text;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
void Init_redcarpet()
|
|
128
|
+
{
|
|
129
|
+
rb_mRedcarpet = rb_define_module("Redcarpet");
|
|
130
|
+
|
|
131
|
+
rb_cMarkdown = rb_define_class_under(rb_mRedcarpet, "Markdown", rb_cObject);
|
|
132
|
+
rb_define_singleton_method(rb_cMarkdown, "new", rb_redcarpet_md__new, -1);
|
|
133
|
+
rb_define_method(rb_cMarkdown, "render", rb_redcarpet_md_render, 1);
|
|
134
|
+
|
|
135
|
+
Init_redcarpet_rndr();
|
|
136
|
+
}
|
|
137
|
+
|
|
@@ -0,0 +1,434 @@
|
|
|
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
|
+
#include "redcarpet.h"
|
|
18
|
+
|
|
19
|
+
#define SPAN_CALLBACK(method_name, ...) {\
|
|
20
|
+
struct redcarpet_renderopt *opt = opaque;\
|
|
21
|
+
VALUE ret = rb_funcall(opt->self, rb_intern(method_name), __VA_ARGS__);\
|
|
22
|
+
if (NIL_P(ret)) return 0;\
|
|
23
|
+
Check_Type(ret, T_STRING);\
|
|
24
|
+
bufput(ob, RSTRING_PTR(ret), RSTRING_LEN(ret));\
|
|
25
|
+
return 1;\
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
#define BLOCK_CALLBACK(method_name, ...) {\
|
|
29
|
+
struct redcarpet_renderopt *opt = opaque;\
|
|
30
|
+
VALUE ret = rb_funcall(opt->self, rb_intern(method_name), __VA_ARGS__);\
|
|
31
|
+
if (NIL_P(ret)) return;\
|
|
32
|
+
Check_Type(ret, T_STRING);\
|
|
33
|
+
bufput(ob, RSTRING_PTR(ret), RSTRING_LEN(ret));\
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
extern VALUE rb_mRedcarpet;
|
|
37
|
+
VALUE rb_mRender;
|
|
38
|
+
VALUE rb_cRenderBase;
|
|
39
|
+
VALUE rb_cRenderHTML;
|
|
40
|
+
VALUE rb_cRenderHTML_TOC;
|
|
41
|
+
VALUE rb_mSmartyPants;
|
|
42
|
+
|
|
43
|
+
static inline VALUE
|
|
44
|
+
buf2str(const struct buf *text)
|
|
45
|
+
{
|
|
46
|
+
if (!text || !text->size) return Qnil;
|
|
47
|
+
return redcarpet_str_new(text->data, text->size);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
static void
|
|
52
|
+
rndr_blockcode(struct buf *ob, const struct buf *text, const struct buf *lang, void *opaque)
|
|
53
|
+
{
|
|
54
|
+
BLOCK_CALLBACK("block_code", 2, buf2str(text), buf2str(lang));
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
static void
|
|
58
|
+
rndr_blockquote(struct buf *ob, const struct buf *text, void *opaque)
|
|
59
|
+
{
|
|
60
|
+
BLOCK_CALLBACK("block_quote", 1, buf2str(text));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
static void
|
|
64
|
+
rndr_raw_block(struct buf *ob, const struct buf *text, void *opaque)
|
|
65
|
+
{
|
|
66
|
+
BLOCK_CALLBACK("block_html", 1, buf2str(text));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
static void
|
|
70
|
+
rndr_header(struct buf *ob, const struct buf *text, int level, void *opaque)
|
|
71
|
+
{
|
|
72
|
+
BLOCK_CALLBACK("header", 2, buf2str(text), INT2FIX(level));
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
static void
|
|
76
|
+
rndr_hrule(struct buf *ob, void *opaque)
|
|
77
|
+
{
|
|
78
|
+
BLOCK_CALLBACK("hrule", 0);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
static void
|
|
82
|
+
rndr_list(struct buf *ob, const struct buf *text, int flags, void *opaque)
|
|
83
|
+
{
|
|
84
|
+
BLOCK_CALLBACK("list", 2, buf2str(text),
|
|
85
|
+
(flags & MKD_LIST_ORDERED) ? CSTR2SYM("ordered") : CSTR2SYM("unordered"));
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
static void
|
|
89
|
+
rndr_listitem(struct buf *ob, const struct buf *text, int flags, void *opaque)
|
|
90
|
+
{
|
|
91
|
+
BLOCK_CALLBACK("list_item", 2, buf2str(text),
|
|
92
|
+
(flags & MKD_LIST_ORDERED) ? CSTR2SYM("ordered") : CSTR2SYM("unordered"));
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
static void
|
|
96
|
+
rndr_paragraph(struct buf *ob, const struct buf *text, void *opaque)
|
|
97
|
+
{
|
|
98
|
+
BLOCK_CALLBACK("paragraph", 1, buf2str(text));
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
static void
|
|
102
|
+
rndr_table(struct buf *ob, const struct buf *header, const struct buf *body, void *opaque)
|
|
103
|
+
{
|
|
104
|
+
BLOCK_CALLBACK("table", 2, buf2str(header), buf2str(body));
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
static void
|
|
108
|
+
rndr_tablerow(struct buf *ob, const struct buf *text, void *opaque)
|
|
109
|
+
{
|
|
110
|
+
BLOCK_CALLBACK("table_row", 1, buf2str(text));
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
static void
|
|
114
|
+
rndr_tablecell(struct buf *ob, const struct buf *text, int align, void *opaque)
|
|
115
|
+
{
|
|
116
|
+
VALUE rb_align;
|
|
117
|
+
|
|
118
|
+
switch (align) {
|
|
119
|
+
case MKD_TABLE_ALIGN_L:
|
|
120
|
+
rb_align = CSTR2SYM("left");
|
|
121
|
+
break;
|
|
122
|
+
|
|
123
|
+
case MKD_TABLE_ALIGN_R:
|
|
124
|
+
rb_align = CSTR2SYM("right");
|
|
125
|
+
break;
|
|
126
|
+
|
|
127
|
+
case MKD_TABLE_ALIGN_CENTER:
|
|
128
|
+
rb_align = CSTR2SYM("center");
|
|
129
|
+
break;
|
|
130
|
+
|
|
131
|
+
default:
|
|
132
|
+
rb_align = Qnil;
|
|
133
|
+
break;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
BLOCK_CALLBACK("table_cell", 2, buf2str(text), rb_align);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
/***
|
|
143
|
+
* SPAN LEVEL
|
|
144
|
+
*/
|
|
145
|
+
static int
|
|
146
|
+
rndr_autolink(struct buf *ob, const struct buf *link, enum mkd_autolink type, void *opaque)
|
|
147
|
+
{
|
|
148
|
+
SPAN_CALLBACK("autolink", 2, buf2str(link),
|
|
149
|
+
type == MKDA_NORMAL ? CSTR2SYM("url") : CSTR2SYM("email"));
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
static int
|
|
153
|
+
rndr_codespan(struct buf *ob, const struct buf *text, void *opaque)
|
|
154
|
+
{
|
|
155
|
+
SPAN_CALLBACK("codespan", 1, buf2str(text));
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
static int
|
|
159
|
+
rndr_double_emphasis(struct buf *ob, const struct buf *text, void *opaque)
|
|
160
|
+
{
|
|
161
|
+
SPAN_CALLBACK("double_emphasis", 1, buf2str(text));
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
static int
|
|
165
|
+
rndr_emphasis(struct buf *ob, const struct buf *text, void *opaque)
|
|
166
|
+
{
|
|
167
|
+
SPAN_CALLBACK("emphasis", 1, buf2str(text));
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
static int
|
|
171
|
+
rndr_image(struct buf *ob, const struct buf *link, const struct buf *title, const struct buf *alt, void *opaque)
|
|
172
|
+
{
|
|
173
|
+
SPAN_CALLBACK("image", 3, buf2str(link), buf2str(title), buf2str(alt));
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
static int
|
|
177
|
+
rndr_linebreak(struct buf *ob, void *opaque)
|
|
178
|
+
{
|
|
179
|
+
SPAN_CALLBACK("linebreak", 0);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
static int
|
|
183
|
+
rndr_link(struct buf *ob, const struct buf *link, const struct buf *title, const struct buf *content, void *opaque)
|
|
184
|
+
{
|
|
185
|
+
SPAN_CALLBACK("link", 3, buf2str(link), buf2str(title), buf2str(content));
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
static int
|
|
189
|
+
rndr_raw_html(struct buf *ob, const struct buf *text, void *opaque)
|
|
190
|
+
{
|
|
191
|
+
SPAN_CALLBACK("raw_html", 1, buf2str(text));
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
static int
|
|
195
|
+
rndr_triple_emphasis(struct buf *ob, const struct buf *text, void *opaque)
|
|
196
|
+
{
|
|
197
|
+
SPAN_CALLBACK("triple_emphasis", 1, buf2str(text));
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
static int
|
|
201
|
+
rndr_strikethrough(struct buf *ob, const struct buf *text, void *opaque)
|
|
202
|
+
{
|
|
203
|
+
SPAN_CALLBACK("strikethrough", 1, buf2str(text));
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
static int
|
|
207
|
+
rndr_superscript(struct buf *ob, const struct buf *text, void *opaque)
|
|
208
|
+
{
|
|
209
|
+
SPAN_CALLBACK("superscript", 1, buf2str(text));
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* direct writes
|
|
214
|
+
*/
|
|
215
|
+
static void
|
|
216
|
+
rndr_entity(struct buf *ob, const struct buf *text, void *opaque)
|
|
217
|
+
{
|
|
218
|
+
BLOCK_CALLBACK("entity", 1, buf2str(text));
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
static void
|
|
222
|
+
rndr_normal_text(struct buf *ob, const struct buf *text, void *opaque)
|
|
223
|
+
{
|
|
224
|
+
BLOCK_CALLBACK("normal_text", 1, buf2str(text));
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
static void
|
|
228
|
+
rndr_doc_header(struct buf *ob, void *opaque)
|
|
229
|
+
{
|
|
230
|
+
BLOCK_CALLBACK("doc_header", 0);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
static void
|
|
234
|
+
rndr_doc_footer(struct buf *ob, void *opaque)
|
|
235
|
+
{
|
|
236
|
+
BLOCK_CALLBACK("doc_footer", 0);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
static struct sd_callbacks rb_redcarpet_callbacks = {
|
|
240
|
+
rndr_blockcode,
|
|
241
|
+
rndr_blockquote,
|
|
242
|
+
rndr_raw_block,
|
|
243
|
+
rndr_header,
|
|
244
|
+
rndr_hrule,
|
|
245
|
+
rndr_list,
|
|
246
|
+
rndr_listitem,
|
|
247
|
+
rndr_paragraph,
|
|
248
|
+
rndr_table,
|
|
249
|
+
rndr_tablerow,
|
|
250
|
+
rndr_tablecell,
|
|
251
|
+
|
|
252
|
+
rndr_autolink,
|
|
253
|
+
rndr_codespan,
|
|
254
|
+
rndr_double_emphasis,
|
|
255
|
+
rndr_emphasis,
|
|
256
|
+
rndr_image,
|
|
257
|
+
rndr_linebreak,
|
|
258
|
+
rndr_link,
|
|
259
|
+
rndr_raw_html,
|
|
260
|
+
rndr_triple_emphasis,
|
|
261
|
+
rndr_strikethrough,
|
|
262
|
+
rndr_superscript,
|
|
263
|
+
|
|
264
|
+
rndr_entity,
|
|
265
|
+
rndr_normal_text,
|
|
266
|
+
|
|
267
|
+
rndr_doc_header,
|
|
268
|
+
rndr_doc_footer,
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
static const char *rb_redcarpet_method_names[] = {
|
|
272
|
+
"block_code",
|
|
273
|
+
"block_quote",
|
|
274
|
+
"block_html",
|
|
275
|
+
"header",
|
|
276
|
+
"hrule",
|
|
277
|
+
"list",
|
|
278
|
+
"list_item",
|
|
279
|
+
"paragraph",
|
|
280
|
+
"table",
|
|
281
|
+
"table_row",
|
|
282
|
+
"table_cell",
|
|
283
|
+
|
|
284
|
+
"autolink",
|
|
285
|
+
"codespan",
|
|
286
|
+
"double_emphasis",
|
|
287
|
+
"emphasis",
|
|
288
|
+
"image",
|
|
289
|
+
"linebreak",
|
|
290
|
+
"link",
|
|
291
|
+
"raw_html",
|
|
292
|
+
"triple_emphasis",
|
|
293
|
+
"strikethrough",
|
|
294
|
+
"superscript",
|
|
295
|
+
|
|
296
|
+
"entity",
|
|
297
|
+
"normal_text",
|
|
298
|
+
|
|
299
|
+
"doc_header",
|
|
300
|
+
"doc_footer"
|
|
301
|
+
};
|
|
302
|
+
|
|
303
|
+
static const size_t rb_redcarpet_method_count = sizeof(rb_redcarpet_method_names)/sizeof(char *);
|
|
304
|
+
|
|
305
|
+
static VALUE rb_redcarpet_rbase_alloc(VALUE klass)
|
|
306
|
+
{
|
|
307
|
+
struct rb_redcarpet_rndr *rndr = ALLOC(struct rb_redcarpet_rndr);
|
|
308
|
+
memset(rndr, 0x0, sizeof(struct rb_redcarpet_rndr));
|
|
309
|
+
return Data_Wrap_Struct(klass, NULL, NULL, rndr);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
static void rb_redcarpet__overload(VALUE self, VALUE base_class)
|
|
313
|
+
{
|
|
314
|
+
struct rb_redcarpet_rndr *rndr;
|
|
315
|
+
|
|
316
|
+
Data_Get_Struct(self, struct rb_redcarpet_rndr, rndr);
|
|
317
|
+
rndr->options.self = self;
|
|
318
|
+
rndr->options.base_class = base_class;
|
|
319
|
+
|
|
320
|
+
if (rb_obj_class(self) == rb_cRenderBase)
|
|
321
|
+
rb_raise(rb_eRuntimeError,
|
|
322
|
+
"The Redcarpet::Render::Base class cannot be instantiated. "
|
|
323
|
+
"Create an inheriting class instead to implement a custom renderer.");
|
|
324
|
+
|
|
325
|
+
if (rb_obj_class(self) != base_class) {
|
|
326
|
+
void **source = (void **)&rb_redcarpet_callbacks;
|
|
327
|
+
void **dest = (void **)&rndr->callbacks;
|
|
328
|
+
size_t i;
|
|
329
|
+
|
|
330
|
+
for (i = 0; i < rb_redcarpet_method_count; ++i) {
|
|
331
|
+
if (rb_respond_to(self, rb_intern(rb_redcarpet_method_names[i])))
|
|
332
|
+
dest[i] = source[i];
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
static VALUE rb_redcarpet_rbase_init(VALUE self)
|
|
338
|
+
{
|
|
339
|
+
rb_redcarpet__overload(self, rb_cRenderBase);
|
|
340
|
+
return Qnil;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
static VALUE rb_redcarpet_html_init(int argc, VALUE *argv, VALUE self)
|
|
344
|
+
{
|
|
345
|
+
struct rb_redcarpet_rndr *rndr;
|
|
346
|
+
unsigned int render_flags = 0;
|
|
347
|
+
VALUE hash;
|
|
348
|
+
|
|
349
|
+
Data_Get_Struct(self, struct rb_redcarpet_rndr, rndr);
|
|
350
|
+
|
|
351
|
+
if (rb_scan_args(argc, argv, "01", &hash) == 1)
|
|
352
|
+
{
|
|
353
|
+
Check_Type(hash, T_HASH);
|
|
354
|
+
|
|
355
|
+
/* filter_html */
|
|
356
|
+
if (rb_hash_aref(hash, CSTR2SYM("filter_html")) == Qtrue)
|
|
357
|
+
render_flags |= HTML_SKIP_HTML;
|
|
358
|
+
|
|
359
|
+
/* no_image */
|
|
360
|
+
if (rb_hash_aref(hash, CSTR2SYM("no_images")) == Qtrue)
|
|
361
|
+
render_flags |= HTML_SKIP_IMAGES;
|
|
362
|
+
|
|
363
|
+
/* no_links */
|
|
364
|
+
if (rb_hash_aref(hash, CSTR2SYM("no_links")) == Qtrue)
|
|
365
|
+
render_flags |= HTML_SKIP_LINKS;
|
|
366
|
+
|
|
367
|
+
/* filter_style */
|
|
368
|
+
if (rb_hash_aref(hash, CSTR2SYM("no_styles")) == Qtrue)
|
|
369
|
+
render_flags |= HTML_SKIP_STYLE;
|
|
370
|
+
|
|
371
|
+
/* safelink */
|
|
372
|
+
if (rb_hash_aref(hash, CSTR2SYM("safe_links_only")) == Qtrue)
|
|
373
|
+
render_flags |= HTML_SAFELINK;
|
|
374
|
+
|
|
375
|
+
if (rb_hash_aref(hash, CSTR2SYM("with_toc_data")) == Qtrue)
|
|
376
|
+
render_flags |= HTML_TOC;
|
|
377
|
+
|
|
378
|
+
if (rb_hash_aref(hash, CSTR2SYM("hard_wrap")) == Qtrue)
|
|
379
|
+
render_flags |= HTML_HARD_WRAP;
|
|
380
|
+
|
|
381
|
+
if (rb_hash_aref(hash, CSTR2SYM("xhtml")) == Qtrue)
|
|
382
|
+
render_flags |= HTML_USE_XHTML;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
sdhtml_renderer(&rndr->callbacks, (struct html_renderopt *)&rndr->options.html, render_flags);
|
|
386
|
+
rb_redcarpet__overload(self, rb_cRenderHTML);
|
|
387
|
+
|
|
388
|
+
return Qnil;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
static VALUE rb_redcarpet_htmltoc_init(VALUE self)
|
|
392
|
+
{
|
|
393
|
+
struct rb_redcarpet_rndr *rndr;
|
|
394
|
+
Data_Get_Struct(self, struct rb_redcarpet_rndr, rndr);
|
|
395
|
+
|
|
396
|
+
sdhtml_toc_renderer(&rndr->callbacks, (struct html_renderopt *)&rndr->options.html);
|
|
397
|
+
rb_redcarpet__overload(self, rb_cRenderHTML_TOC);
|
|
398
|
+
|
|
399
|
+
return Qnil;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
static VALUE rb_redcarpet_smartypants_render(VALUE self, VALUE text)
|
|
403
|
+
{
|
|
404
|
+
VALUE result;
|
|
405
|
+
struct buf *output_buf;
|
|
406
|
+
|
|
407
|
+
Check_Type(text, T_STRING);
|
|
408
|
+
|
|
409
|
+
output_buf = bufnew(128);
|
|
410
|
+
|
|
411
|
+
sdhtml_smartypants(output_buf, RSTRING_PTR(text), RSTRING_LEN(text));
|
|
412
|
+
result = redcarpet_str_new(output_buf->data, output_buf->size);
|
|
413
|
+
|
|
414
|
+
bufrelease(output_buf);
|
|
415
|
+
return result;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
void Init_redcarpet_rndr()
|
|
419
|
+
{
|
|
420
|
+
rb_mRender = rb_define_module_under(rb_mRedcarpet, "Render");
|
|
421
|
+
|
|
422
|
+
rb_cRenderBase = rb_define_class_under(rb_mRender, "Base", rb_cObject);
|
|
423
|
+
rb_define_alloc_func(rb_cRenderBase, rb_redcarpet_rbase_alloc);
|
|
424
|
+
rb_define_method(rb_cRenderBase, "initialize", rb_redcarpet_rbase_init, 0);
|
|
425
|
+
|
|
426
|
+
rb_cRenderHTML = rb_define_class_under(rb_mRender, "HTML", rb_cRenderBase);
|
|
427
|
+
rb_define_method(rb_cRenderHTML, "initialize", rb_redcarpet_html_init, -1);
|
|
428
|
+
|
|
429
|
+
rb_cRenderHTML_TOC = rb_define_class_under(rb_mRender, "HTML_TOC", rb_cRenderBase);
|
|
430
|
+
rb_define_method(rb_cRenderHTML_TOC, "initialize", rb_redcarpet_htmltoc_init, 0);
|
|
431
|
+
|
|
432
|
+
rb_mSmartyPants = rb_define_module_under(rb_mRender, "SmartyPants");
|
|
433
|
+
rb_define_method(rb_mSmartyPants, "postprocess", rb_redcarpet_smartypants_render, 1);
|
|
434
|
+
}
|