Class: PEGMarkdown

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

Overview

Front-end to jgm’s peg-markdown implementation of Markdown, the humane text markup language.

A simple processor:

>>> puts Markdown.new("Hello, World.").to_html
<p>Hello, World.</p>

With other stuff:

>>> puts Markdown.new("_Hello -- World!_", :smart, :filter_html).to_html
<p><em>Hello World!</em></p>

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, *extensions) ⇒ PEGMarkdown

Create a new Markdown processor. The text argument is a string containing Markdown text. Variable other arguments may be supplied to set various processing options:

  • :smart - Enable SmartyPants processing.

  • :notes - Enable footnotes.

  • :filter_styles - Do not output <style> tags included in the source text.

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

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



46
47
48
49
50
51
52
53
# File 'lib/peg_markdown.rb', line 46

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

Instance Attribute Details

#filter_htmlObject

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



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

def filter_html
  @filter_html
end

#filter_stylesObject

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



26
27
28
# File 'lib/peg_markdown.rb', line 26

def filter_styles
  @filter_styles
end

#fold_linesObject

Included for compatibility with RedCloth’s interface.



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

def fold_lines
  @fold_lines
end

#notesObject

Set true to have footnotes processed.



23
24
25
# File 'lib/peg_markdown.rb', line 23

def notes
  @notes
end

#smartObject

Set true to have smarty-like quote translation performed.



20
21
22
# File 'lib/peg_markdown.rb', line 20

def smart
  @smart
end

#textObject (readonly) Also known as: to_s

Original Markdown formatted text.



17
18
19
# File 'lib/peg_markdown.rb', line 17

def text
  @text
end

Instance Method Details

#to_groff_mm(*args) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'ext/markdown.c', line 55

static VALUE
rb_markdown_to_groff_mm(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);
    char * ptext = StringValuePtr(text);

    /* flip extension bits - note that defaults are different than
     * for HTML */
    int extensions = EXT_SMART | EXT_NOTES | EXT_FILTER_HTML | EXT_FILTER_STYLES;
    if ( rb_funcall(self, rb_intern("smart"), 0) == Qfalse )
        extensions = extensions & ~ EXT_SMART ;
    if ( rb_funcall(self, rb_intern("notes"), 0) == Qfalse )
        extensions = extensions & ~ EXT_NOTES ;

    char *groff = markdown_to_string(ptext, extensions, GROFF_MM_FORMAT);
    VALUE result = rb_str_new2(groff);
    free(groff);

    return result;
}

#to_html(*args) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'ext/markdown.c', line 6

static VALUE
rb_markdown_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);
    char * ptext = StringValuePtr(text);

    /* flip extension bits */
    int extensions = 0;
    if ( rb_funcall(self, rb_intern("smart"), 0) == Qtrue )
        extensions = extensions | EXT_SMART ;
    if ( rb_funcall(self, rb_intern("notes"), 0) == Qtrue )
        extensions = extensions | EXT_NOTES ;
    if ( rb_funcall(self, rb_intern("filter_html"), 0) == Qtrue )
        extensions = extensions | EXT_FILTER_HTML ;
    if ( rb_funcall(self, rb_intern("filter_styles"), 0) == Qtrue )
        extensions = extensions | EXT_FILTER_STYLES ;

    char *html = markdown_to_string(ptext, extensions, HTML_FORMAT);
    VALUE result = rb_str_new2(html);
    free(html);

    return result;
}

#to_latex(*args) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'ext/markdown.c', line 32

static VALUE
rb_markdown_to_latex(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);
    char * ptext = StringValuePtr(text);

    /* flip extension bits - note that defaults are different than
     * for HTML */
    int extensions = EXT_SMART | EXT_NOTES | EXT_FILTER_HTML | EXT_FILTER_STYLES;
    if ( rb_funcall(self, rb_intern("smart"), 0) == Qfalse )
        extensions = extensions & ~ EXT_SMART ;
    if ( rb_funcall(self, rb_intern("notes"), 0) == Qfalse )
        extensions = extensions & ~ EXT_NOTES ;

    char *latex = markdown_to_string(ptext, extensions, LATEX_FORMAT);
    VALUE result = rb_str_new2(latex);
    free(latex);

    return result;
}