| 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 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 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 } |
| 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 |
|
| 65 |
int flags = MKD_TABSTOP | MKD_NOHEADER | MKD_DLEXTRA | MKD_FENCEDCODE | MKD_GITHUBTAGS; |
| 66 |
|
| 67 |
|
| 68 |
if ( rb_funcall(ruby_obj, rb_intern("smart"), 0) != Qtrue ) { |
| 69 |
flags = flags | MKD_NOPANTS; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
if ( rb_funcall(ruby_obj, rb_intern("filter_styles"), 0) != Qtrue ) { |
| 74 |
flags = flags | MKD_NOSTYLE; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 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 |
|
| 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); |
| 97 |
|
| 98 |
VALUE buf = rb_str_buf_new(1024); |
| 99 |
|
| 100 |
int flags = rb_rdiscount__get_flags(self); |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
char *old_locale = strdup(setlocale(LC_CTYPE, NULL)); |
| 111 |
setlocale(LC_CTYPE, "C"); |
| 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 |
|
| 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 |
|
| 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); |
| 149 |
|
| 150 |
|
| 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 |
|