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.
Files changed (47) hide show
  1. checksums.yaml +7 -0
  2. data/COPYING +20 -0
  3. data/Gemfile +9 -0
  4. data/README.markdown +394 -0
  5. data/Rakefile +60 -0
  6. data/bin/redcarpet +7 -0
  7. data/ext/redcarpet/autolink.c +302 -0
  8. data/ext/redcarpet/autolink.h +55 -0
  9. data/ext/redcarpet/buffer.c +203 -0
  10. data/ext/redcarpet/buffer.h +89 -0
  11. data/ext/redcarpet/extconf.rb +6 -0
  12. data/ext/redcarpet/houdini.h +51 -0
  13. data/ext/redcarpet/houdini_href_e.c +124 -0
  14. data/ext/redcarpet/houdini_html_e.c +105 -0
  15. data/ext/redcarpet/html.c +825 -0
  16. data/ext/redcarpet/html.h +84 -0
  17. data/ext/redcarpet/html_blocks.h +229 -0
  18. data/ext/redcarpet/html_smartypants.c +457 -0
  19. data/ext/redcarpet/markdown.c +2917 -0
  20. data/ext/redcarpet/markdown.h +143 -0
  21. data/ext/redcarpet/rc_markdown.c +168 -0
  22. data/ext/redcarpet/rc_render.c +545 -0
  23. data/ext/redcarpet/redcarpet.h +52 -0
  24. data/ext/redcarpet/stack.c +84 -0
  25. data/ext/redcarpet/stack.h +48 -0
  26. data/lib/redcarpet/cli.rb +86 -0
  27. data/lib/redcarpet/compat.rb +73 -0
  28. data/lib/redcarpet/render_man.rb +65 -0
  29. data/lib/redcarpet/render_strip.rb +60 -0
  30. data/lib/redcarpet_yt.rb +103 -0
  31. data/redcarpet_yt.gemspec +71 -0
  32. data/test/benchmark.rb +24 -0
  33. data/test/custom_render_test.rb +28 -0
  34. data/test/fixtures/benchmark.md +232 -0
  35. data/test/html5_test.rb +69 -0
  36. data/test/html_render_test.rb +254 -0
  37. data/test/html_toc_render_test.rb +75 -0
  38. data/test/markdown_test.rb +371 -0
  39. data/test/pathological_inputs_test.rb +34 -0
  40. data/test/redcarpet_bin_test.rb +80 -0
  41. data/test/redcarpet_compat_test.rb +38 -0
  42. data/test/safe_render_test.rb +35 -0
  43. data/test/smarty_html_test.rb +45 -0
  44. data/test/smarty_pants_test.rb +53 -0
  45. data/test/stripdown_render_test.rb +61 -0
  46. data/test/test_helper.rb +39 -0
  47. metadata +151 -0
@@ -0,0 +1,143 @@
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
+ #ifndef MARKDOWN_H__
25
+ #define MARKDOWN_H__
26
+
27
+ #include "buffer.h"
28
+ #include "autolink.h"
29
+
30
+ #ifdef __cplusplus
31
+ extern "C" {
32
+ #endif
33
+
34
+ /********************
35
+ * TYPE DEFINITIONS *
36
+ ********************/
37
+
38
+ /* mkd_autolink - type of autolink */
39
+ enum mkd_autolink {
40
+ MKDA_NOT_AUTOLINK, /* used internally when it is not an autolink*/
41
+ MKDA_NORMAL, /* normal http/http/ftp/mailto/etc link */
42
+ MKDA_EMAIL, /* e-mail link without explit mailto: */
43
+ };
44
+
45
+ enum mkd_tableflags {
46
+ MKD_TABLE_ALIGN_L = 1,
47
+ MKD_TABLE_ALIGN_R = 2,
48
+ MKD_TABLE_ALIGN_CENTER = 3,
49
+ MKD_TABLE_ALIGNMASK = 3,
50
+ MKD_TABLE_HEADER = 4
51
+ };
52
+
53
+ enum mkd_extensions {
54
+ MKDEXT_NO_INTRA_EMPHASIS = (1 << 0),
55
+ MKDEXT_TABLES = (1 << 1),
56
+ MKDEXT_FENCED_CODE = (1 << 2),
57
+ MKDEXT_AUTOLINK = (1 << 3),
58
+ MKDEXT_STRIKETHROUGH = (1 << 4),
59
+ MKDEXT_UNDERLINE = (1 << 5),
60
+ MKDEXT_SPACE_HEADERS = (1 << 6),
61
+ MKDEXT_SUPERSCRIPT = (1 << 7),
62
+ MKDEXT_LAX_SPACING = (1 << 8),
63
+ MKDEXT_DISABLE_INDENTED_CODE = (1 << 9),
64
+ MKDEXT_HIGHLIGHT = (1 << 10),
65
+ MKDEXT_FOOTNOTES = (1 << 11),
66
+ MKDEXT_QUOTE = (1 << 12)
67
+ };
68
+
69
+ /* sd_callbacks - functions for rendering parsed data */
70
+ struct sd_callbacks {
71
+ /* block level callbacks - NULL skips the block */
72
+ void (*blockcode)(struct buf *ob, const struct buf *text, const struct buf *lang, void *opaque);
73
+ void (*blockquote)(struct buf *ob, const struct buf *text, void *opaque);
74
+ void (*blockhtml)(struct buf *ob,const struct buf *text, void *opaque);
75
+ void (*header)(struct buf *ob, const struct buf *text, int level, void *opaque);
76
+ void (*hrule)(struct buf *ob, void *opaque);
77
+ void (*list)(struct buf *ob, const struct buf *text, int flags, void *opaque);
78
+ void (*listitem)(struct buf *ob, const struct buf *text, int flags, void *opaque);
79
+ void (*paragraph)(struct buf *ob, const struct buf *text, void *opaque);
80
+ void (*table)(struct buf *ob, const struct buf *header, const struct buf *body, void *opaque);
81
+ void (*table_row)(struct buf *ob, const struct buf *text, void *opaque);
82
+ void (*table_cell)(struct buf *ob, const struct buf *text, int flags, void *opaque);
83
+ void (*footnotes)(struct buf *ob, const struct buf *text, void *opaque);
84
+ void (*footnote_def)(struct buf *ob, const struct buf *text, unsigned int num, void *opaque);
85
+
86
+ /* span level callbacks - NULL or return 0 prints the span verbatim */
87
+ int (*autolink)(struct buf *ob, const struct buf *link, enum mkd_autolink type, void *opaque);
88
+ int (*codespan)(struct buf *ob, const struct buf *text, void *opaque);
89
+ int (*double_emphasis)(struct buf *ob, const struct buf *text, void *opaque);
90
+ int (*emphasis)(struct buf *ob, const struct buf *text, void *opaque);
91
+ int (*underline)(struct buf *ob, const struct buf *text, void *opaque);
92
+ int (*highlight)(struct buf *ob, const struct buf *text, void *opaque);
93
+ int (*quote)(struct buf *ob, const struct buf *text, void *opaque);
94
+ int (*image)(struct buf *ob, const struct buf *link, const struct buf *title, const struct buf *alt, void *opaque);
95
+ int (*linebreak)(struct buf *ob, void *opaque);
96
+ int (*link)(struct buf *ob, const struct buf *link, const struct buf *title, const struct buf *content, void *opaque);
97
+ int (*raw_html_tag)(struct buf *ob, const struct buf *tag, void *opaque);
98
+ int (*triple_emphasis)(struct buf *ob, const struct buf *text, void *opaque);
99
+ int (*strikethrough)(struct buf *ob, const struct buf *text, void *opaque);
100
+ int (*superscript)(struct buf *ob, const struct buf *text, void *opaque);
101
+ int (*footnote_ref)(struct buf *ob, unsigned int num, void *opaque);
102
+
103
+ /* low level callbacks - NULL copies input directly into the output */
104
+ void (*entity)(struct buf *ob, const struct buf *entity, void *opaque);
105
+ void (*normal_text)(struct buf *ob, const struct buf *text, void *opaque);
106
+
107
+ /* header and footer */
108
+ void (*doc_header)(struct buf *ob, void *opaque);
109
+ void (*doc_footer)(struct buf *ob, void *opaque);
110
+ };
111
+
112
+ struct sd_markdown;
113
+
114
+ /*********
115
+ * FLAGS *
116
+ *********/
117
+
118
+ /* list/listitem flags */
119
+ #define MKD_LIST_ORDERED 1
120
+ #define MKD_LI_BLOCK 2 /* <li> containing block data */
121
+
122
+ /**********************
123
+ * EXPORTED FUNCTIONS *
124
+ **********************/
125
+
126
+ extern struct sd_markdown *
127
+ sd_markdown_new(
128
+ unsigned int extensions,
129
+ size_t max_nesting,
130
+ const struct sd_callbacks *callbacks,
131
+ void *opaque);
132
+
133
+ extern void
134
+ sd_markdown_render(struct buf *ob, const uint8_t *document, size_t doc_size, struct sd_markdown *md);
135
+
136
+ extern void
137
+ sd_markdown_free(struct sd_markdown *md);
138
+
139
+ #ifdef __cplusplus
140
+ }
141
+ #endif
142
+
143
+ #endif
@@ -0,0 +1,168 @@
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 "redcarpet.h"
24
+
25
+ VALUE rb_mRedcarpet;
26
+ VALUE rb_cMarkdown;
27
+
28
+ extern VALUE rb_cRenderBase;
29
+
30
+ static void rb_redcarpet_md_flags(VALUE hash, unsigned int *enabled_extensions_p)
31
+ {
32
+ unsigned int extensions = 0;
33
+
34
+ Check_Type(hash, T_HASH);
35
+
36
+ /**
37
+ * Markdown extensions -- all disabled by default
38
+ */
39
+ if (rb_hash_lookup(hash, CSTR2SYM("no_intra_emphasis")) == Qtrue)
40
+ extensions |= MKDEXT_NO_INTRA_EMPHASIS;
41
+
42
+ if (rb_hash_lookup(hash, CSTR2SYM("tables")) == Qtrue)
43
+ extensions |= MKDEXT_TABLES;
44
+
45
+ if (rb_hash_lookup(hash, CSTR2SYM("fenced_code_blocks")) == Qtrue)
46
+ extensions |= MKDEXT_FENCED_CODE;
47
+
48
+ if (rb_hash_lookup(hash, CSTR2SYM("disable_indented_code_blocks")) == Qtrue)
49
+ extensions |= MKDEXT_DISABLE_INDENTED_CODE;
50
+
51
+ if (rb_hash_lookup(hash, CSTR2SYM("autolink")) == Qtrue)
52
+ extensions |= MKDEXT_AUTOLINK;
53
+
54
+ if (rb_hash_lookup(hash, CSTR2SYM("strikethrough")) == Qtrue)
55
+ extensions |= MKDEXT_STRIKETHROUGH;
56
+
57
+ if (rb_hash_lookup(hash, CSTR2SYM("underline")) == Qtrue)
58
+ extensions |= MKDEXT_UNDERLINE;
59
+
60
+ if (rb_hash_lookup(hash, CSTR2SYM("highlight")) == Qtrue)
61
+ extensions |= MKDEXT_HIGHLIGHT;
62
+
63
+ if (rb_hash_lookup(hash, CSTR2SYM("quote")) == Qtrue)
64
+ extensions |= MKDEXT_QUOTE;
65
+
66
+ if (rb_hash_lookup(hash, CSTR2SYM("lax_spacing")) == Qtrue)
67
+ extensions |= MKDEXT_LAX_SPACING;
68
+
69
+ if (rb_hash_lookup(hash, CSTR2SYM("space_after_headers")) == Qtrue)
70
+ extensions |= MKDEXT_SPACE_HEADERS;
71
+
72
+ if (rb_hash_lookup(hash, CSTR2SYM("superscript")) == Qtrue)
73
+ extensions |= MKDEXT_SUPERSCRIPT;
74
+
75
+ if (rb_hash_lookup(hash, CSTR2SYM("footnotes")) == Qtrue)
76
+ extensions |= MKDEXT_FOOTNOTES;
77
+
78
+ *enabled_extensions_p = extensions;
79
+ }
80
+
81
+ static void
82
+ rb_redcarpet_md__free(void *markdown)
83
+ {
84
+ sd_markdown_free((struct sd_markdown *)markdown);
85
+ }
86
+
87
+ static VALUE rb_redcarpet_md__new(int argc, VALUE *argv, VALUE klass)
88
+ {
89
+ VALUE rb_markdown, rb_rndr, hash;
90
+ unsigned int extensions = 0;
91
+
92
+ struct rb_redcarpet_rndr *rndr;
93
+ struct sd_markdown *markdown;
94
+
95
+ if (rb_scan_args(argc, argv, "11", &rb_rndr, &hash) == 2)
96
+ rb_redcarpet_md_flags(hash, &extensions);
97
+
98
+ if (rb_obj_is_kind_of(rb_rndr, rb_cClass))
99
+ rb_rndr = rb_funcall(rb_rndr, rb_intern("new"), 0);
100
+
101
+ if (!rb_obj_is_kind_of(rb_rndr, rb_cRenderBase))
102
+ rb_raise(rb_eTypeError, "Invalid Renderer instance given");
103
+
104
+ Data_Get_Struct(rb_rndr, struct rb_redcarpet_rndr, rndr);
105
+
106
+ markdown = sd_markdown_new(extensions, 16, &rndr->callbacks, &rndr->options);
107
+ if (!markdown)
108
+ rb_raise(rb_eRuntimeError, "Failed to create new Renderer class");
109
+
110
+ rb_markdown = Data_Wrap_Struct(klass, NULL, rb_redcarpet_md__free, markdown);
111
+ rb_iv_set(rb_markdown, "@renderer", rb_rndr);
112
+
113
+ return rb_markdown;
114
+ }
115
+
116
+ static VALUE rb_redcarpet_md_render(VALUE self, VALUE text)
117
+ {
118
+ VALUE rb_rndr;
119
+ struct buf *output_buf;
120
+ struct sd_markdown *markdown;
121
+
122
+ Check_Type(text, T_STRING);
123
+
124
+ rb_rndr = rb_iv_get(self, "@renderer");
125
+ Data_Get_Struct(self, struct sd_markdown, markdown);
126
+
127
+ if (rb_respond_to(rb_rndr, rb_intern("preprocess")))
128
+ text = rb_funcall(rb_rndr, rb_intern("preprocess"), 1, text);
129
+ if (NIL_P(text))
130
+ return Qnil;
131
+
132
+ struct rb_redcarpet_rndr *renderer;
133
+ Data_Get_Struct(rb_rndr, struct rb_redcarpet_rndr, renderer);
134
+ renderer->options.active_enc = rb_enc_get(text);
135
+
136
+ /* initialize buffers */
137
+ output_buf = bufnew(128);
138
+
139
+ /* render the magic */
140
+ sd_markdown_render(
141
+ output_buf,
142
+ (const uint8_t*)RSTRING_PTR(text),
143
+ RSTRING_LEN(text),
144
+ markdown);
145
+
146
+ /* build the Ruby string */
147
+ text = rb_enc_str_new((const char*)output_buf->data, output_buf->size, rb_enc_get(text));
148
+
149
+ bufrelease(output_buf);
150
+
151
+ if (rb_respond_to(rb_rndr, rb_intern("postprocess")))
152
+ text = rb_funcall(rb_rndr, rb_intern("postprocess"), 1, text);
153
+
154
+ return text;
155
+ }
156
+
157
+ __attribute__((visibility("default")))
158
+ void Init_redcarpet()
159
+ {
160
+ rb_mRedcarpet = rb_define_module("Redcarpet");
161
+
162
+ rb_cMarkdown = rb_define_class_under(rb_mRedcarpet, "Markdown", rb_cObject);
163
+ rb_define_singleton_method(rb_cMarkdown, "new", rb_redcarpet_md__new, -1);
164
+ rb_define_method(rb_cMarkdown, "render", rb_redcarpet_md_render, 1);
165
+
166
+ Init_redcarpet_rndr();
167
+ }
168
+
@@ -0,0 +1,545 @@
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 "redcarpet.h"
24
+
25
+ #define SPAN_CALLBACK(method_name, ...) {\
26
+ struct redcarpet_renderopt *opt = opaque;\
27
+ VALUE ret = rb_funcall(opt->self, rb_intern(method_name), __VA_ARGS__);\
28
+ if (NIL_P(ret)) return 0;\
29
+ Check_Type(ret, T_STRING);\
30
+ bufput(ob, RSTRING_PTR(ret), RSTRING_LEN(ret));\
31
+ return 1;\
32
+ }
33
+
34
+ #define BLOCK_CALLBACK(method_name, ...) {\
35
+ struct redcarpet_renderopt *opt = opaque;\
36
+ VALUE ret = rb_funcall(opt->self, rb_intern(method_name), __VA_ARGS__);\
37
+ if (NIL_P(ret)) return;\
38
+ Check_Type(ret, T_STRING);\
39
+ bufput(ob, RSTRING_PTR(ret), RSTRING_LEN(ret));\
40
+ }
41
+
42
+ extern VALUE rb_mRedcarpet;
43
+ VALUE rb_mRender;
44
+ VALUE rb_cRenderBase;
45
+ VALUE rb_cRenderHTML;
46
+ VALUE rb_cRenderHTML_TOC;
47
+ VALUE rb_mSmartyPants;
48
+
49
+ #define buf2str(t) ((t) ? rb_enc_str_new((const char*)(t)->data, (t)->size, opt->active_enc) : Qnil)
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
+ static void
140
+ rndr_footnotes(struct buf *ob, const struct buf *text, void *opaque)
141
+ {
142
+ BLOCK_CALLBACK("footnotes", 1, buf2str(text));
143
+ }
144
+
145
+ static void
146
+ rndr_footnote_def(struct buf *ob, const struct buf *text, unsigned int num, void *opaque)
147
+ {
148
+ BLOCK_CALLBACK("footnote_def", 2, buf2str(text), INT2FIX(num));
149
+ }
150
+
151
+
152
+ /***
153
+ * SPAN LEVEL
154
+ */
155
+ static int
156
+ rndr_autolink(struct buf *ob, const struct buf *link, enum mkd_autolink type, void *opaque)
157
+ {
158
+ SPAN_CALLBACK("autolink", 2, buf2str(link),
159
+ type == MKDA_NORMAL ? CSTR2SYM("url") : CSTR2SYM("email"));
160
+ }
161
+
162
+ static int
163
+ rndr_codespan(struct buf *ob, const struct buf *text, void *opaque)
164
+ {
165
+ SPAN_CALLBACK("codespan", 1, buf2str(text));
166
+ }
167
+
168
+ static int
169
+ rndr_double_emphasis(struct buf *ob, const struct buf *text, void *opaque)
170
+ {
171
+ SPAN_CALLBACK("double_emphasis", 1, buf2str(text));
172
+ }
173
+
174
+ static int
175
+ rndr_emphasis(struct buf *ob, const struct buf *text, void *opaque)
176
+ {
177
+ SPAN_CALLBACK("emphasis", 1, buf2str(text));
178
+ }
179
+
180
+ static int
181
+ rndr_underline(struct buf *ob, const struct buf *text, void *opaque)
182
+ {
183
+ SPAN_CALLBACK("underline", 1, buf2str(text));
184
+ }
185
+
186
+ static int
187
+ rndr_highlight(struct buf *ob, const struct buf *text, void *opaque)
188
+ {
189
+ SPAN_CALLBACK("highlight", 1, buf2str(text));
190
+ }
191
+
192
+ static int
193
+ rndr_quote(struct buf *ob, const struct buf *text, void *opaque)
194
+ {
195
+ SPAN_CALLBACK("quote", 1, buf2str(text));
196
+ }
197
+
198
+ static int
199
+ rndr_image(struct buf *ob, const struct buf *link, const struct buf *title, const struct buf *alt, void *opaque)
200
+ {
201
+ SPAN_CALLBACK("image", 3, buf2str(link), buf2str(title), buf2str(alt));
202
+ }
203
+
204
+ static int
205
+ rndr_linebreak(struct buf *ob, void *opaque)
206
+ {
207
+ SPAN_CALLBACK("linebreak", 0);
208
+ }
209
+
210
+ static int
211
+ rndr_link(struct buf *ob, const struct buf *link, const struct buf *title, const struct buf *content, void *opaque)
212
+ {
213
+ SPAN_CALLBACK("link", 3, buf2str(link), buf2str(title), buf2str(content));
214
+ }
215
+
216
+ static int
217
+ rndr_raw_html(struct buf *ob, const struct buf *text, void *opaque)
218
+ {
219
+ SPAN_CALLBACK("raw_html", 1, buf2str(text));
220
+ }
221
+
222
+ static int
223
+ rndr_triple_emphasis(struct buf *ob, const struct buf *text, void *opaque)
224
+ {
225
+ SPAN_CALLBACK("triple_emphasis", 1, buf2str(text));
226
+ }
227
+
228
+ static int
229
+ rndr_strikethrough(struct buf *ob, const struct buf *text, void *opaque)
230
+ {
231
+ SPAN_CALLBACK("strikethrough", 1, buf2str(text));
232
+ }
233
+
234
+ static int
235
+ rndr_superscript(struct buf *ob, const struct buf *text, void *opaque)
236
+ {
237
+ SPAN_CALLBACK("superscript", 1, buf2str(text));
238
+ }
239
+
240
+ static int
241
+ rndr_footnote_ref(struct buf *ob, unsigned int num, void *opaque)
242
+ {
243
+ SPAN_CALLBACK("footnote_ref", 1, INT2FIX(num));
244
+ }
245
+
246
+ /**
247
+ * direct writes
248
+ */
249
+ static void
250
+ rndr_entity(struct buf *ob, const struct buf *text, void *opaque)
251
+ {
252
+ BLOCK_CALLBACK("entity", 1, buf2str(text));
253
+ }
254
+
255
+ static void
256
+ rndr_normal_text(struct buf *ob, const struct buf *text, void *opaque)
257
+ {
258
+ BLOCK_CALLBACK("normal_text", 1, buf2str(text));
259
+ }
260
+
261
+ static void
262
+ rndr_doc_header(struct buf *ob, void *opaque)
263
+ {
264
+ BLOCK_CALLBACK("doc_header", 0);
265
+ }
266
+
267
+ static void
268
+ rndr_doc_footer(struct buf *ob, void *opaque)
269
+ {
270
+ BLOCK_CALLBACK("doc_footer", 0);
271
+ }
272
+
273
+ static int
274
+ cb_link_attribute(VALUE key, VALUE val, VALUE payload)
275
+ {
276
+ struct buf *ob = (struct buf *)payload;
277
+ key = rb_obj_as_string(key);
278
+ val = rb_obj_as_string(val);
279
+ bufprintf(ob, " %s=\"%s\"", StringValueCStr(key), StringValueCStr(val));
280
+ return 0;
281
+ }
282
+
283
+ static void
284
+ rndr_link_attributes(struct buf *ob, const struct buf *url, void *opaque)
285
+ {
286
+ struct redcarpet_renderopt *opt = opaque;
287
+ struct rb_redcarpet_rndr *rndr;
288
+
289
+ Data_Get_Struct(opt->self, struct rb_redcarpet_rndr, rndr);
290
+ Check_Type(opt->link_attributes, T_HASH);
291
+ rb_hash_foreach(opt->link_attributes, &cb_link_attribute, (VALUE)ob);
292
+ }
293
+
294
+ static struct sd_callbacks rb_redcarpet_callbacks = {
295
+ rndr_blockcode,
296
+ rndr_blockquote,
297
+ rndr_raw_block,
298
+ rndr_header,
299
+ rndr_hrule,
300
+ rndr_list,
301
+ rndr_listitem,
302
+ rndr_paragraph,
303
+ rndr_table,
304
+ rndr_tablerow,
305
+ rndr_tablecell,
306
+ rndr_footnotes,
307
+ rndr_footnote_def,
308
+
309
+ rndr_autolink,
310
+ rndr_codespan,
311
+ rndr_double_emphasis,
312
+ rndr_emphasis,
313
+ rndr_underline,
314
+ rndr_highlight,
315
+ rndr_quote,
316
+ rndr_image,
317
+ rndr_linebreak,
318
+ rndr_link,
319
+ rndr_raw_html,
320
+ rndr_triple_emphasis,
321
+ rndr_strikethrough,
322
+ rndr_superscript,
323
+ rndr_footnote_ref,
324
+
325
+ rndr_entity,
326
+ rndr_normal_text,
327
+
328
+ rndr_doc_header,
329
+ rndr_doc_footer,
330
+ };
331
+
332
+ static const char *rb_redcarpet_method_names[] = {
333
+ "block_code",
334
+ "block_quote",
335
+ "block_html",
336
+ "header",
337
+ "hrule",
338
+ "list",
339
+ "list_item",
340
+ "paragraph",
341
+ "table",
342
+ "table_row",
343
+ "table_cell",
344
+ "footnotes",
345
+ "footnote_def",
346
+
347
+ "autolink",
348
+ "codespan",
349
+ "double_emphasis",
350
+ "emphasis",
351
+ "underline",
352
+ "highlight",
353
+ "quote",
354
+ "image",
355
+ "linebreak",
356
+ "link",
357
+ "raw_html",
358
+ "triple_emphasis",
359
+ "strikethrough",
360
+ "superscript",
361
+ "footnote_ref",
362
+
363
+ "entity",
364
+ "normal_text",
365
+
366
+ "doc_header",
367
+ "doc_footer"
368
+ };
369
+
370
+ static const size_t rb_redcarpet_method_count = sizeof(rb_redcarpet_method_names)/sizeof(char *);
371
+
372
+ static void rb_redcarpet_rbase_mark(struct rb_redcarpet_rndr *rndr)
373
+ {
374
+ if (rndr->options.link_attributes)
375
+ rb_gc_mark(rndr->options.link_attributes);
376
+ }
377
+
378
+ static void rndr_deallocate(void *rndr)
379
+ {
380
+ xfree(rndr);
381
+ }
382
+
383
+ static VALUE rb_redcarpet_rbase_alloc(VALUE klass)
384
+ {
385
+ struct rb_redcarpet_rndr *rndr = ALLOC(struct rb_redcarpet_rndr);
386
+ memset(rndr, 0x0, sizeof(struct rb_redcarpet_rndr));
387
+ return Data_Wrap_Struct(klass, rb_redcarpet_rbase_mark, rndr_deallocate, rndr);
388
+ }
389
+
390
+ static void rb_redcarpet__overload(VALUE self, VALUE base_class)
391
+ {
392
+ struct rb_redcarpet_rndr *rndr;
393
+
394
+ Data_Get_Struct(self, struct rb_redcarpet_rndr, rndr);
395
+ rndr->options.self = self;
396
+ rndr->options.base_class = base_class;
397
+
398
+ if (rb_obj_class(self) == rb_cRenderBase)
399
+ rb_raise(rb_eRuntimeError,
400
+ "The Redcarpet::Render::Base class cannot be instantiated. "
401
+ "Create an inheriting class instead to implement a custom renderer.");
402
+
403
+ if (rb_obj_class(self) != base_class) {
404
+ void **source = (void **)&rb_redcarpet_callbacks;
405
+ void **dest = (void **)&rndr->callbacks;
406
+ size_t i;
407
+
408
+ for (i = 0; i < rb_redcarpet_method_count; ++i) {
409
+ if (rb_respond_to(self, rb_intern(rb_redcarpet_method_names[i])))
410
+ dest[i] = source[i];
411
+ }
412
+ }
413
+ }
414
+
415
+ static VALUE rb_redcarpet_rbase_init(VALUE self)
416
+ {
417
+ rb_redcarpet__overload(self, rb_cRenderBase);
418
+ return Qnil;
419
+ }
420
+
421
+ static VALUE rb_redcarpet_html_init(int argc, VALUE *argv, VALUE self)
422
+ {
423
+ struct rb_redcarpet_rndr *rndr;
424
+ unsigned int render_flags = 0;
425
+ VALUE hash, link_attr = Qnil;
426
+
427
+ Data_Get_Struct(self, struct rb_redcarpet_rndr, rndr);
428
+
429
+ if (rb_scan_args(argc, argv, "01", &hash) == 1) {
430
+ Check_Type(hash, T_HASH);
431
+
432
+ /* escape_html */
433
+ if (rb_hash_aref(hash, CSTR2SYM("escape_html")) == Qtrue)
434
+ render_flags |= HTML_ESCAPE;
435
+
436
+ /* filter_html */
437
+ if (rb_hash_aref(hash, CSTR2SYM("filter_html")) == Qtrue)
438
+ render_flags |= HTML_SKIP_HTML;
439
+
440
+ /* no_image */
441
+ if (rb_hash_aref(hash, CSTR2SYM("no_images")) == Qtrue)
442
+ render_flags |= HTML_SKIP_IMAGES;
443
+
444
+ /* no_links */
445
+ if (rb_hash_aref(hash, CSTR2SYM("no_links")) == Qtrue)
446
+ render_flags |= HTML_SKIP_LINKS;
447
+
448
+ /* prettify */
449
+ if (rb_hash_aref(hash, CSTR2SYM("prettify")) == Qtrue)
450
+ render_flags |= HTML_PRETTIFY;
451
+
452
+ /* filter_style */
453
+ if (rb_hash_aref(hash, CSTR2SYM("no_styles")) == Qtrue)
454
+ render_flags |= HTML_SKIP_STYLE;
455
+
456
+ /* safelink */
457
+ if (rb_hash_aref(hash, CSTR2SYM("safe_links_only")) == Qtrue)
458
+ render_flags |= HTML_SAFELINK;
459
+
460
+ if (rb_hash_aref(hash, CSTR2SYM("with_toc_data")) == Qtrue)
461
+ render_flags |= HTML_TOC;
462
+
463
+ if (rb_hash_aref(hash, CSTR2SYM("hard_wrap")) == Qtrue)
464
+ render_flags |= HTML_HARD_WRAP;
465
+
466
+ if (rb_hash_aref(hash, CSTR2SYM("xhtml")) == Qtrue)
467
+ render_flags |= HTML_USE_XHTML;
468
+
469
+ link_attr = rb_hash_aref(hash, CSTR2SYM("link_attributes"));
470
+ }
471
+
472
+ sdhtml_renderer(&rndr->callbacks, (struct html_renderopt *)&rndr->options.html, render_flags);
473
+ rb_redcarpet__overload(self, rb_cRenderHTML);
474
+
475
+ if (!NIL_P(link_attr)) {
476
+ rndr->options.link_attributes = link_attr;
477
+ rndr->options.html.link_attributes = &rndr_link_attributes;
478
+ }
479
+
480
+ return Qnil;
481
+ }
482
+
483
+ static VALUE rb_redcarpet_htmltoc_init(int argc, VALUE *argv, VALUE self)
484
+ {
485
+ struct rb_redcarpet_rndr *rndr;
486
+ unsigned int render_flags = HTML_TOC;
487
+ VALUE hash, nesting_level = Qnil;
488
+
489
+ Data_Get_Struct(self, struct rb_redcarpet_rndr, rndr);
490
+
491
+ if (rb_scan_args(argc, argv, "01", &hash) == 1) {
492
+ Check_Type(hash, T_HASH);
493
+
494
+ /* escape_html */
495
+ if (rb_hash_aref(hash, CSTR2SYM("escape_html")) == Qtrue)
496
+ render_flags |= HTML_ESCAPE;
497
+
498
+ /* Nesting level */
499
+ nesting_level = rb_hash_aref(hash, CSTR2SYM("nesting_level"));
500
+ }
501
+
502
+ sdhtml_toc_renderer(&rndr->callbacks, (struct html_renderopt *)&rndr->options.html, render_flags);
503
+ rb_redcarpet__overload(self, rb_cRenderHTML_TOC);
504
+
505
+ if (!(NIL_P(nesting_level)))
506
+ rndr->options.html.toc_data.nesting_level = NUM2INT(nesting_level);
507
+ else
508
+ rndr->options.html.toc_data.nesting_level = 6;
509
+
510
+ return Qnil;
511
+ }
512
+
513
+ static VALUE rb_redcarpet_smartypants_render(VALUE self, VALUE text)
514
+ {
515
+ VALUE result;
516
+ struct buf *output_buf;
517
+
518
+ Check_Type(text, T_STRING);
519
+
520
+ output_buf = bufnew(128);
521
+
522
+ sdhtml_smartypants(output_buf, (const uint8_t*)RSTRING_PTR(text), RSTRING_LEN(text));
523
+ result = rb_enc_str_new((const char*)output_buf->data, output_buf->size, rb_enc_get(text));
524
+
525
+ bufrelease(output_buf);
526
+ return result;
527
+ }
528
+
529
+ void Init_redcarpet_rndr()
530
+ {
531
+ rb_mRender = rb_define_module_under(rb_mRedcarpet, "Render");
532
+
533
+ rb_cRenderBase = rb_define_class_under(rb_mRender, "Base", rb_cObject);
534
+ rb_define_alloc_func(rb_cRenderBase, rb_redcarpet_rbase_alloc);
535
+ rb_define_method(rb_cRenderBase, "initialize", rb_redcarpet_rbase_init, 0);
536
+
537
+ rb_cRenderHTML = rb_define_class_under(rb_mRender, "HTML", rb_cRenderBase);
538
+ rb_define_method(rb_cRenderHTML, "initialize", rb_redcarpet_html_init, -1);
539
+
540
+ rb_cRenderHTML_TOC = rb_define_class_under(rb_mRender, "HTML_TOC", rb_cRenderBase);
541
+ rb_define_method(rb_cRenderHTML_TOC, "initialize", rb_redcarpet_htmltoc_init, -1);
542
+
543
+ rb_mSmartyPants = rb_define_module_under(rb_mRender, "SmartyPants");
544
+ rb_define_method(rb_mSmartyPants, "postprocess", rb_redcarpet_smartypants_render, 1);
545
+ }