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

Constant Summary collapse

VERSION =
'1.6.8'
INT2NUM(MKD_NOLINKS)
MKD_NOIMAGE =
INT2NUM(MKD_NOIMAGE)
MKD_NOPANTS =
INT2NUM(MKD_NOPANTS)
MKD_NOHTML =
INT2NUM(MKD_NOHTML)
MKD_STRICT =
INT2NUM(MKD_STRICT)
MKD_TAGTEXT =
INT2NUM(MKD_TAGTEXT)
MKD_NO_EXT =
INT2NUM(MKD_NO_EXT)
MKD_CDATA =
INT2NUM(MKD_CDATA)
MKD_NOSUPERSCRIPT =
INT2NUM(MKD_NOSUPERSCRIPT)
MKD_NORELAXED =
INT2NUM(MKD_NORELAXED)
MKD_NOTABLES =
INT2NUM(MKD_NOTABLES)
MKD_NOSTRIKETHROUGH =
INT2NUM(MKD_NOSTRIKETHROUGH)
MKD_TOC =
INT2NUM(MKD_TOC)
MKD_1_COMPAT =
INT2NUM(MKD_1_COMPAT)
INT2NUM(MKD_AUTOLINK)
INT2NUM(MKD_SAFELINK)
MKD_NOHEADER =
INT2NUM(MKD_NOHEADER)
MKD_TABSTOP =
INT2NUM(MKD_TABSTOP)
MKD_NODIVQUOTE =
INT2NUM(MKD_NODIVQUOTE)
MKD_NOALPHALIST =
INT2NUM(MKD_NOALPHALIST)
MKD_NODLIST =
INT2NUM(MKD_NODLIST)

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:

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

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

  • :MKD_NOLINKS - Don’t do link processing, block <a> tags

  • :MKD_NOIMAGE - Don’t do image processing, block <img>

  • :MKD_NOPANTS - Don’t run smartypants()

  • :MKD_NOHTML - Don’t allow raw html through AT ALL

  • :MKD_STRICT - Disable SUPERSCRIPT, RELAXED_EMPHASIS

  • :MKD_TAGTEXT - Process text inside an html tag; no <em>, no <bold>, no html or [] expansion

  • :MKD_NO_EXT - Don’t allow pseudo-protocols

  • :MKD_CDATA - Generate code for xml ![CDATA]

  • :MKD_NOSUPERSCRIPT - No A^B

  • :MKD_NORELAXED - Emphasis happens everywhere

  • :MKD_NOTABLES - Don’t process PHP Markdown Extra tables.

  • :MKD_NOSTRIKETHROUGH - Forbid ~~strikethrough~~

  • :MKD_TOC - Do table-of-contents processing

  • :MKD_1_COMPAT - Compatability with MarkdownTest_1.0

  • :MKD_AUTOLINK - Make foo.com a link even without <>s

  • :MKD_SAFELINK - Paranoid check for link protocol

  • :MKD_NOHEADER - Don’t process document headers

  • :MKD_TABSTOP - Expand tabs to 4 spaces

  • :MKD_NODIVQUOTE - Forbid >%class% blocks

  • :MKD_NOALPHALIST - Forbid alphabetic lists

  • :MKD_NODLIST - Forbid definition lists



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/rdiscount.rb', line 70

def initialize(text, *extensions)
  @text  = text
  @flags = 0
  extensions.each do |ext|
    writer = "#{ext}="
    if respond_to? writer
      send writer, true
    else
      @flags |= RDiscount.const_get(ext)
    end
  end
end

Instance Attribute Details

#filter_stylesObject

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



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

def filter_styles
  @filter_styles
end

#flagsObject

Integer containing bit flags for the underlying Discount library.



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

def flags
  @flags
end

#fold_linesObject

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



40
41
42
# File 'lib/rdiscount.rb', line 40

def fold_lines
  @fold_lines
end

#textObject (readonly)

Original Markdown formatted text.



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

def text
  @text
end

Instance Method Details

#to_html(*args) ⇒ Object



7
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
37
38
39
40
# File 'ext/rdiscount.c', line 7

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

#toc_content(*args) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'ext/rdiscount.c', line 42

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