Debian Codegraph @

ext/rdiscount.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 #include <stdio.h>
2 #include <limits.h>
3 #include <locale.h>
4 #include "ruby.h"
5 #include "mkdio.h"
6
7 typedef struct {
8 char *accessor_name;
9 int flag;
10 } AccessorFlagPair;
11
12 /*
13 * Maps accessor names on the RDiscount object to Discount flags.
14 *
15 * The following flags are handled specially:
16 * - MKD_TABSTOP: Always set.
17 * - MKD_NOHEADER: Always set.
18 * - MKD_DLEXTRA: Always set. (For compatibility with RDiscount 2.1.8 and earlier.)
19 * - MKD_FENCEDCODE: Always set. (For compatibility with RDiscount 2.1.8 and earlier.)
20 * - MKD_GITHUBTAGS: Always set. (For compatibility with RDiscount 2.1.8 and earlier.)
21 * - MKD_NOPANTS: Set unless the "smart" accessor returns true.
22 * - MKD_NOSTYLE: Set unless the "filter_styles" accessor returns true.
23 *
24 * See rb_rdiscount__get_flags() for the detailed implementation.
25 */
26 static AccessorFlagPair ACCESSOR_2_FLAG[] = {
27 { "filter_html", MKD_NOHTML },
28 { "footnotes", MKD_EXTRA_FOOTNOTE },
29 { "generate_toc", MKD_TOC },
30 { "no_image", MKD_NOIMAGE },
31 { "no_links", MKD_NOLINKS },
32 { "no_tables", MKD_NOTABLES },
33 { "strict", MKD_STRICT },
34 { "autolink", MKD_AUTOLINK },
35 { "safelink", MKD_SAFELINK },
36 { "no_pseudo_protocols", MKD_NO_EXT },
37 { "no_superscript", MKD_NOSUPERSCRIPT },
38 { "no_strikethrough", MKD_NOSTRIKETHROUGH },
39 { "latex", MKD_LATEX },
40 { "explicitlist", MKD_EXPLICITLIST },
41 { "md1compat", MKD_1_COMPAT },
42 { NULL, 0 } /* sentinel */
43 };
44
45 static VALUE rb_cRDiscount;
•46 int rb_rdiscount__get_flags(VALUE ruby_obj);
47
48 static int
49 rb_rdiscount__text_len(VALUE text)
50 {
51 long text_len = RSTRING_LEN(text);
52
53 if (text_len > INT_MAX) {
54 rb_raise(rb_eArgError, "markdown input too large");
55 }
56
57 return (int)text_len;
58 }
59
60 int rb_rdiscount__get_flags(VALUE ruby_obj)
61 {
62 AccessorFlagPair *entry;
63
64 /* compile flags */
65 int flags = MKD_TABSTOP | MKD_NOHEADER | MKD_DLEXTRA | MKD_FENCEDCODE | MKD_GITHUBTAGS;
66
67 /* The "smart" accessor turns OFF the MKD_NOPANTS flag. */
68 if ( rb_funcall(ruby_obj, rb_intern("smart"), 0) != Qtrue ) {
69 flags = flags | MKD_NOPANTS;
70 }
71
72 /* The "filter_styles" accessor turns OFF the MKD_NOSTYLE flag. */
73 if ( rb_funcall(ruby_obj, rb_intern("filter_styles"), 0) != Qtrue ) {
74 flags = flags | MKD_NOSTYLE;
75 }
76
77 /* Handle standard flags declared in ACCESSOR_2_FLAG */
78 for ( entry = ACCESSOR_2_FLAG; entry->accessor_name; entry++ ) {
79 if ( rb_funcall(ruby_obj, rb_intern(entry->accessor_name), 0) == Qtrue ) {
80 flags = flags | entry->flag;
81 }
82 }
83
84 return flags;
85 }
86
87 static VALUE
88 rb_rdiscount_to_html(int argc, VALUE *argv, VALUE self)
89 {
90 /* grab char pointer to markdown input text */
91 char *res;
92 int szres;
93 VALUE encoding;
94 VALUE text = rb_funcall(self, rb_intern("text"), 0);
95 Check_Type(text, T_STRING);
96 int text_len = rb_rdiscount__text_len(text); // may rb_raise
97
98 VALUE buf = rb_str_buf_new(1024);
99
100 int flags = rb_rdiscount__get_flags(self);
101
102 /*
103 * Force Discount to use ASCII character encoding for isalnum(), isalpha(),
104 * and similar functions.
105 *
106 * Ruby tends to use UTF-8 encoding, which is ill-defined for these
107 * functions since they expect 8-bit codepoints (and UTF-8 has codepoints
108 * of at least 21 bits).
109 */
110 char *old_locale = strdup(setlocale(LC_CTYPE, NULL));
111 setlocale(LC_CTYPE, "C"); /* ASCII (and passthru characters > 127) */
112
113 MMIOT *doc = mkd_string(RSTRING_PTR(text), text_len, flags);
114
115 if ( mkd_compile(doc, flags) ) {
116 szres = mkd_document(doc, &res);
117
118 if ( szres != EOF ) {
119 rb_str_cat(buf, res, szres);
120 rb_str_cat(buf, "\n", 1);
121 }
122 }
123 mkd_cleanup(doc);
124
125 setlocale(LC_CTYPE, old_locale);
126 free(old_locale);
127
128 /* force the input encoding */
129 if ( rb_respond_to(text, rb_intern("encoding")) ) {
130 encoding = rb_funcall(text, rb_intern("encoding"), 0);
131 rb_funcall(buf, rb_intern("force_encoding"), 1, encoding);
132 }
133
134 return buf;
135 }
136
137 static VALUE
138 rb_rdiscount_toc_content(int argc, VALUE *argv, VALUE self)
139 {
140 char *res;
141 int szres;
142
143 int flags = rb_rdiscount__get_flags(self);
144
145 /* grab char pointer to markdown input text */
146 VALUE text = rb_funcall(self, rb_intern("text"), 0);
147 Check_Type(text, T_STRING);
148 int text_len = rb_rdiscount__text_len(text); // may rb_raise
149
150 /* allocate a ruby string buffer and wrap it in a stream */
151 VALUE buf = rb_str_buf_new(4096);
152
153 MMIOT *doc = mkd_string(RSTRING_PTR(text), text_len, flags);
154
155 if ( mkd_compile(doc, flags) ) {
156 szres = mkd_toc(doc, &res);
157
158 if ( szres != EOF ) {
159 rb_str_cat(buf, res, szres);
160 rb_str_cat(buf, "\n", 1);
161 }
162 }
163 mkd_cleanup(doc);
164
165 return buf;
166 }
167
168
169 void Init_rdiscount()
170 {
171 rb_cRDiscount = rb_define_class("RDiscount", rb_cObject);
172 rb_define_method(rb_cRDiscount, "to_html", rb_rdiscount_to_html, -1);
173 rb_define_method(rb_cRDiscount, "toc_content", rb_rdiscount_toc_content, -1);
174 }
175
176 /* vim: set ts=4 sw=4: */