Class: RDiscount

Inherits:
Object
  • Object
show all
Defined in:
lib/rdiscount.rb,
ext/rdiscount.c

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, *extensions) ⇒ RDiscount

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).

NOTE: The :filter_styles extension is not yet implemented.



55
56
57
58
59
60
61
62
# File 'lib/rdiscount.rb', line 55

def initialize(text, *extensions)
  @text  = text
  @smart = nil
  @filter_styles = nil
  @filter_html = nil
  @fold_lines = nil
  extensions.each { |e| send("#{e}=", true) }
end

Instance Attribute Details

#filter_htmlObject

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



38
39
40
# File 'lib/rdiscount.rb', line 38

def filter_html
  @filter_html
end

#filter_stylesObject

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



35
36
37
# File 'lib/rdiscount.rb', line 35

def filter_styles
  @filter_styles
end

#fold_linesObject

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



42
43
44
# File 'lib/rdiscount.rb', line 42

def fold_lines
  @fold_lines
end

#smartObject

Set true to have smarty-like quote translation performed.



32
33
34
# File 'lib/rdiscount.rb', line 32

def smart
  @smart
end

#textObject (readonly)

Original Markdown formatted text.



29
30
31
# File 'lib/rdiscount.rb', line 29

def text
  @text
end

Instance Method Details

#to_html(*args) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'ext/rdiscount.c', line 8

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);

    /* compile flags */
    int flags = MKD_TABSTOP | MKD_NOHEADER;

    /* smart */
    if ( rb_funcall(self, rb_intern("smart"), 0) != Qtrue )
        flags = flags | MKD_NOPANTS;

    /* filter_html */
    if ( rb_funcall(self, rb_intern("filter_html"), 0) == Qtrue )
        flags = flags | MKD_NOHTML;

    MMIOT *doc = mkd_string(RSTRING(text)->ptr, RSTRING(text)->len, flags);
    markdown(doc, stream, flags);

    fclose(stream);

    return buf;
}