class Redcarpet::Markdown

Attributes

renderer[R]

Public Class Methods

new(p1, p2 = v2) click to toggle source
static VALUE rb_redcarpet_md__new(int argc, VALUE *argv, VALUE klass)
{
        VALUE rb_markdown, rb_rndr, hash;
        unsigned int extensions = 0;

        struct rb_redcarpet_rndr *rndr;
        struct sd_markdown *markdown;

        if (rb_scan_args(argc, argv, "11", &rb_rndr, &hash) == 2)
                rb_redcarpet_md_flags(hash, &extensions);

        if (rb_obj_is_kind_of(rb_rndr, rb_cClass))
                rb_rndr = rb_funcall(rb_rndr, rb_intern("new"), 0);

        if (!rb_obj_is_kind_of(rb_rndr, rb_cRenderBase))
                rb_raise(rb_eTypeError, "Invalid Renderer instance given");

        Data_Get_Struct(rb_rndr, struct rb_redcarpet_rndr, rndr);

        markdown = sd_markdown_new(extensions, 16, &rndr->callbacks, &rndr->options);
        if (!markdown)
                rb_raise(rb_eRuntimeError, "Failed to create new Renderer class");

        rb_markdown = Data_Wrap_Struct(klass, NULL, rb_redcarpet_md__free, markdown);
        rb_iv_set(rb_markdown, "@renderer", rb_rndr);

        return rb_markdown;
}

Public Instance Methods

render(p1) click to toggle source
static VALUE rb_redcarpet_md_render(VALUE self, VALUE text)
{
        VALUE rb_rndr;
        struct buf *output_buf;
        struct sd_markdown *markdown;

        Check_Type(text, T_STRING);

        rb_rndr = rb_iv_get(self, "@renderer");
        Data_Get_Struct(self, struct sd_markdown, markdown);

        if (rb_respond_to(rb_rndr, rb_intern("preprocess")))
                text = rb_funcall(rb_rndr, rb_intern("preprocess"), 1, text);
  if (NIL_P(text))
    return Qnil;

#ifdef HAVE_RUBY_ENCODING_H
        {
                struct rb_redcarpet_rndr *renderer;
                Data_Get_Struct(rb_rndr, struct rb_redcarpet_rndr, renderer);
                renderer->options.active_enc = rb_enc_get(text);
        }
#endif

        /* initialize buffers */
        output_buf = bufnew(128);

        /* render the magic */
        sd_markdown_render(
                output_buf,
                RSTRING_PTR(text),
                RSTRING_LEN(text),
                markdown);

        /* build the Ruby string */
        text = redcarpet_str_new(output_buf->data, output_buf->size, rb_enc_get(text));

        bufrelease(output_buf);

        if (rb_respond_to(rb_rndr, rb_intern("postprocess")))
                text = rb_funcall(rb_rndr, rb_intern("postprocess"), 1, text);

        return text;
}