class RDiscount

Discount is an implementation of John Gruber’s Markdown markup language in C. It implements all of the language as described in Markdown Syntax and passes the Markdown 1.0 test suite. The RDiscount extension makes the Discount processor available via a Ruby C Extension library.

Usage

RDiscount implements the basic protocol popularized by RedCloth and adopted by BlueCloth:

require 'rdiscount'
markdown = RDiscount.new("Hello World!")
puts markdown.to_html

Replacing BlueCloth

Inject RDiscount into your BlueCloth-using code by replacing your bluecloth require statements with the following:

begin
  require 'rdiscount'
  BlueCloth = RDiscount
rescue LoadError
  require 'bluecloth'
end

Constants

VERSION

Attributes

explicitlist[RW]

Don’t merge adjacent list into a single list.

filter_html[RW]

Do not output any raw HTML included in the source text.

filter_styles[RW]

Do not output <style> tags included in the source text.

fold_lines[RW]

RedCloth compatible line folding – not used for Markdown but included for compatibility.

footnotes[RW]

Enable php markdown extra-style footnotes

generate_toc[RW]

Enable Table Of Contents generation

latex[RW]

Keep LaTeX inside $$ intact.

md1compat[RW]

Not documented: run in markdown 1 compat mode (only used for MarkdownTest1.0)

no_image[RW]

Do not process ![] and remove <img> tags from the output.

no_pseudo_protocols[RW]

Do not process pseudo-protocols like [](id:name)

no_strikethrough[RW]

Disable strikethrough processing.

no_superscript[RW]

Disable superscript processing.

no_tables[RW]

Do not process tables

smart[RW]

Set true to have smarty-like quote translation performed.

strict[RW]

Disable superscript and relaxed emphasis processing.

text[R]

Original Markdown formatted text.

Public Class Methods

new(text, *extensions) click to toggle source

Create a RDiscount Markdown processor. The text argument should be a string containing Markdown text. Additional arguments may be supplied to set various processing options:

  • :smart - Enable SmartyPants processing.

  • :filter_styles - Do not output <style> tags.

  • :filter_html - Do not output any raw HTML tags included in the source text.

  • :fold_lines - RedCloth compatible line folding (not used).

  • :footnotes - PHP markdown extra-style footnotes.

  • :generate_toc - Enable Table Of Contents generation

  • :no_image - Do not output any <img> tags.

  • :no_links - Do not output any <a> tags.

  • :no_tables - Do not output any tables.

  • :strict - Disable superscript and relaxed emphasis processing.

  • :autolink - Greedily urlify links.

  • :safelink - Do not make links for unknown URL types.

  • :no_pseudo_protocols - Do not process pseudo-protocols.

  • :no_superscript - Disable superscript processing.

  • :no_strikethrough - Disable strikethrough processing.

  • :latex - Keep LaTeX inside $$ intact.

  • :explicitlist - Don’t merge adjacent list into a single list.

# File lib/rdiscount.rb, line 110
def initialize(text, *extensions)
  @text  = text
  extensions.each { |e| send("#{e}=", true) }
end

Public Instance Methods

to_html(*args) click to toggle source
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;
}
toc_content(*args) click to toggle source
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;
}