static VALUE rb_rdiscount_to_html(int argc, VALUE *argv, VALUE self) { /* grab char pointer to markdown input text */ char *res; int szres; VALUE encoding; VALUE text = rb_funcall(self, rb_intern("text"), 0); VALUE buf = rb_str_buf_new(1024); Check_Type(text, T_STRING); int flags = rb_rdiscount__get_flags(self); MMIOT *doc = mkd_string(RSTRING_PTR(text), RSTRING_LEN(text), flags); if ( mkd_compile(doc, flags) ) { szres = mkd_document(doc, &res); if ( szres != EOF ) { rb_str_cat(buf, res, szres); rb_str_cat(buf, "\n", 1); } } mkd_cleanup(doc); /* force the input encoding */ if ( rb_respond_to(text, rb_intern("encoding")) ) { encoding = rb_funcall(text, rb_intern("encoding"), 0); rb_funcall(buf, rb_intern("force_encoding"), 1, encoding); } return buf; }
static VALUE rb_rdiscount_toc_content(int argc, VALUE *argv, VALUE self) { char *res; int szres; int flags = rb_rdiscount__get_flags(self); /* grab char pointer to markdown input text */ VALUE text = rb_funcall(self, rb_intern("text"), 0); Check_Type(text, T_STRING); /* allocate a ruby string buffer and wrap it in a stream */ VALUE buf = rb_str_buf_new(4096); MMIOT *doc = mkd_string(RSTRING_PTR(text), RSTRING_LEN(text), flags); if ( mkd_compile(doc, flags) ) { szres = mkd_toc(doc, &res); if ( szres != EOF ) { rb_str_cat(buf, res, szres); rb_str_cat(buf, "\n", 1); } } mkd_cleanup(doc); return buf; }
static VALUE rb_rdiscount_to_html(int argc, VALUE *argv, VALUE self) { /* grab char pointer to markdown input text */ char *res; int szres; VALUE encoding; VALUE text = rb_funcall(self, rb_intern("text"), 0); VALUE buf = rb_str_buf_new(1024); Check_Type(text, T_STRING); int flags = rb_rdiscount__get_flags(self); /* * Force Discount to use ASCII character encoding for isalnum(), isalpha(), * and similar functions. * * Ruby tends to use UTF-8 encoding, which is ill-defined for these * functions since they expect 8-bit codepoints (and UTF-8 has codepoints * of at least 21 bits). */ char *old_locale = strdup(setlocale(LC_CTYPE, NULL)); setlocale(LC_CTYPE, "C"); // ASCII (and passthru characters > 127) MMIOT *doc = mkd_string(RSTRING_PTR(text), RSTRING_LEN(text), flags); if ( mkd_compile(doc, flags) ) { szres = mkd_document(doc, &res); if ( szres != EOF ) { rb_str_cat(buf, res, szres); rb_str_cat(buf, "\n", 1); } } mkd_cleanup(doc); setlocale(LC_CTYPE, old_locale); free(old_locale); /* force the input encoding */ if ( rb_respond_to(text, rb_intern("encoding")) ) { encoding = rb_funcall(text, rb_intern("encoding"), 0); rb_funcall(buf, rb_intern("force_encoding"), 1, encoding); } return buf; }
static VALUE rb_rdiscount_to_html(int argc, VALUE *argv, VALUE self) { /* grab char pointer to markdown input text */ VALUE text = rb_funcall(self, rb_intern("text"), 0); Check_Type(text, T_STRING); /* allocate a ruby string buffer and wrap it in a stream */ VALUE buf = rb_str_buf_new(4096); FILE *stream = rb_str_io_new(buf); int flags = rb_rdiscount__get_flags(self); MMIOT *doc = mkd_string(RSTRING_PTR(text), RSTRING_LEN(text), flags); markdown(doc, stream, flags); fclose(stream); return buf; }