| 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 |
|
| 47 |
static int |
| 48 |
rb_rdiscount__text_len(VALUE text) |
| 49 |
{ |
| 50 |
long text_len = RSTRING_LEN(text); |
| 51 |
|
| 52 |
if (text_len > INT_MAX) { |
| 53 |
rb_raise(rb_eArgError, "markdown input too large"); |
| 54 |
} |
| 55 |
|
| 56 |
return (int)text_len; |
| 57 |
} |
| 58 |
|
| 59 |
int rb_rdiscount__get_flags(VALUE ruby_obj) |
| 60 |
{ |
| 61 |
AccessorFlagPair *entry; |
| 62 |
|
| 63 |
/* compile flags */ |
| 64 |
int flags = MKD_TABSTOP | MKD_NOHEADER | MKD_DLEXTRA | MKD_FENCEDCODE | MKD_GITHUBTAGS; |
| 65 |
|
| 66 |
/* The "smart" accessor turns OFF the MKD_NOPANTS flag. */ |
| 67 |
if ( rb_funcall(ruby_obj, rb_intern("smart"), 0) != Qtrue ) { |
| 68 |
flags = flags | MKD_NOPANTS; |
| 69 |
} |
| 70 |
|
| 71 |
/* The "filter_styles" accessor turns OFF the MKD_NOSTYLE flag. */ |
| 72 |
if ( rb_funcall(ruby_obj, rb_intern("filter_styles"), 0) != Qtrue ) { |
| 73 |
flags = flags | MKD_NOSTYLE; |
| 74 |
} |
| 75 |
|
| 76 |
/* Handle standard flags declared in ACCESSOR_2_FLAG */ |
| 77 |
for ( entry = ACCESSOR_2_FLAG; entry->accessor_name; entry++ ) { |
| 78 |
if ( rb_funcall(ruby_obj, rb_intern(entry->accessor_name), 0) == Qtrue ) { |
| 79 |
flags = flags | entry->flag; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
return flags; |
| 84 |
} |
| 85 |
|
| 86 |
static VALUE |
| 87 |
rb_rdiscount_to_html(int argc, VALUE *argv, VALUE self) |
| 88 |
{ |
| 89 |
/* grab char pointer to markdown input text */ |
| 90 |
char *res; |
| 91 |
int szres; |
| 92 |
VALUE encoding; |
| 93 |
VALUE text = rb_funcall(self, rb_intern("text"), 0); |
| 94 |
Check_Type(text, T_STRING); |
| 95 |
int text_len = rb_rdiscount__text_len(text); // may rb_raise |
| 96 |
|
| 97 |
VALUE buf = rb_str_buf_new(1024); |
| 98 |
|
| 99 |
int flags = rb_rdiscount__get_flags(self); |
| 100 |
|
| 101 |
/* |
| 102 |
* Force Discount to use ASCII character encoding for isalnum(), isalpha(), |
| 103 |
* and similar functions. |
| 104 |
* |
| 105 |
* Ruby tends to use UTF-8 encoding, which is ill-defined for these |
| 106 |
* functions since they expect 8-bit codepoints (and UTF-8 has codepoints |
| 107 |
* of at least 21 bits). |
| 108 |
*/ |
| 109 |
char *old_locale = strdup(setlocale(LC_CTYPE, NULL)); |
| 110 |
setlocale(LC_CTYPE, "C"); /* ASCII (and passthru characters > 127) */ |
| 111 |
|
| 112 |
MMIOT *doc = mkd_string(RSTRING_PTR(text), text_len, flags); |
| 113 |
|
| 114 |
if ( mkd_compile(doc, flags) ) { |
| 115 |
szres = mkd_document(doc, &res); |
| 116 |
|
| 117 |
if ( szres != EOF ) { |
| 118 |
rb_str_cat(buf, res, szres); |
| 119 |
rb_str_cat(buf, "\n", 1); |
| 120 |
} |
| 121 |
} |
| 122 |
mkd_cleanup(doc); |
| 123 |
|
| 124 |
setlocale(LC_CTYPE, old_locale); |
| 125 |
free(old_locale); |
| 126 |
|
| 127 |
/* force the input encoding */ |
| 128 |
if ( rb_respond_to(text, rb_intern("encoding")) ) { |
| 129 |
encoding = rb_funcall(text, rb_intern("encoding"), 0); |
| 130 |
rb_funcall(buf, rb_intern("force_encoding"), 1, encoding); |
| 131 |
} |
| 132 |
|
| 133 |
return buf; |
| 134 |
} |
| 135 |
|
| 136 |
static VALUE |
| 137 |
rb_rdiscount_toc_content(int argc, VALUE *argv, VALUE self) |
| 138 |
{ |
| 139 |
char *res; |
| 140 |
int szres; |
| 141 |
|
| 142 |
int flags = rb_rdiscount__get_flags(self); |
| 143 |
|
| 144 |
/* grab char pointer to markdown input text */ |
| 145 |
VALUE text = rb_funcall(self, rb_intern("text"), 0); |
| 146 |
Check_Type(text, T_STRING); |
| 147 |
int text_len = rb_rdiscount__text_len(text); // may rb_raise |
| 148 |
|
| 149 |
/* allocate a ruby string buffer and wrap it in a stream */ |
| 150 |
VALUE buf = rb_str_buf_new(4096); |
| 151 |
|
| 152 |
MMIOT *doc = mkd_string(RSTRING_PTR(text), text_len, flags); |
| 153 |
|
| 154 |
if ( mkd_compile(doc, flags) ) { |
| 155 |
szres = mkd_toc(doc, &res); |
| 156 |
|
| 157 |
if ( szres != EOF ) { |
| 158 |
rb_str_cat(buf, res, szres); |
| 159 |
rb_str_cat(buf, "\n", 1); |
| 160 |
} |
| 161 |
} |
| 162 |
mkd_cleanup(doc); |
| 163 |
|
| 164 |
return buf; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
void Init_rdiscount() |
| 169 |
{ |
| 170 |
rb_cRDiscount = rb_define_class("RDiscount", rb_cObject); |
| 171 |
rb_define_method(rb_cRDiscount, "to_html", rb_rdiscount_to_html, -1); |
| 172 |
rb_define_method(rb_cRDiscount, "toc_content", rb_rdiscount_toc_content, -1); |
| 173 |
} |
| 174 |
|
| 175 |
/* vim: set ts=4 sw=4: */ |