Class: PEGMarkdown
- Inherits:
-
Object
- Object
- PEGMarkdown
- 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
-
#autolink ⇒ Object
Set true to enable autolink extension.
-
#codeblock ⇒ Object
Set true to enable codeblock extension.
-
#filter_html ⇒ Object
Do not output any raw HTML included in the source text.
-
#filter_styles ⇒ Object
Do not output <style> tags included in the source text.
-
#fold_lines ⇒ Object
Included for compatibility with RedCloth’s interface.
-
#hard_wrap ⇒ Object
Set true to enable hard wrap extension.
-
#hashtags ⇒ Object
Set true to enable hashtags extension.
-
#media ⇒ Object
Set true to enable audio/video extension.
-
#no_images ⇒ Object
Set true to disable rendering of images.
-
#notes ⇒ Object
Set true to have footnotes processed.
-
#smart ⇒ Object
Set true to have smarty-like quote translation performed.
-
#spoilerblock ⇒ Object
Set true to enable spoilerblock extension.
-
#strike ⇒ Object
Set true to enable strike-through extension.
-
#text ⇒ Object
(also: #to_s)
readonly
Original Markdown formatted text.
-
#usernames ⇒ Object
Set true to enable usernames extension.
Instance Method Summary collapse
-
#initialize(text, *extensions) ⇒ PEGMarkdown
constructor
Create a new Markdown processor.
- #to_groff_mm(*args) ⇒ Object
- #to_html(*args) ⇒ Object
- #to_latex(*args) ⇒ Object
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). -
:strike- Enable strike-through extension. -
:autolink- Enable autolinking of bare URLs. -
:hard_wrap- Insert line breaks inside paragraphs. -
:no_images- Treat image links as if they were plain links. -
:media- Replace audio/video links with HTML5 audio/video objects. -
:codeblock- Render block enclosed in ~~~ as verbatim section. -
:hashtags- Mark hashtags. -
:usernames- Mark usernames. -
:spoilerblock- Render block prefixed with ?> as spoiler section.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/peg_markdown.rb', line 82 def initialize(text, *extensions) @text = text @smart = false @notes = false @filter_styles = false @filter_html = false @strike = false @autolink = false @hard_wrap = false @no_images = false @media = false @codeblock = false = false @usernames = false @spoilerblock = false extensions.each { |e| send("#{e}=", true) } end |
Instance Attribute Details
#autolink ⇒ Object
Set true to enable autolink extension.
38 39 40 |
# File 'lib/peg_markdown.rb', line 38 def autolink @autolink end |
#codeblock ⇒ Object
Set true to enable codeblock extension.
50 51 52 |
# File 'lib/peg_markdown.rb', line 50 def codeblock @codeblock end |
#filter_html ⇒ Object
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_styles ⇒ Object
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_lines ⇒ Object
Included for compatibility with RedCloth’s interface.
32 33 34 |
# File 'lib/peg_markdown.rb', line 32 def fold_lines @fold_lines end |
#hard_wrap ⇒ Object
Set true to enable hard wrap extension.
41 42 43 |
# File 'lib/peg_markdown.rb', line 41 def hard_wrap @hard_wrap end |
#hashtags ⇒ Object
Set true to enable hashtags extension.
53 54 55 |
# File 'lib/peg_markdown.rb', line 53 def end |
#media ⇒ Object
Set true to enable audio/video extension.
47 48 49 |
# File 'lib/peg_markdown.rb', line 47 def media @media end |
#no_images ⇒ Object
Set true to disable rendering of images.
44 45 46 |
# File 'lib/peg_markdown.rb', line 44 def no_images @no_images end |
#notes ⇒ Object
Set true to have footnotes processed.
23 24 25 |
# File 'lib/peg_markdown.rb', line 23 def notes @notes end |
#smart ⇒ Object
Set true to have smarty-like quote translation performed.
20 21 22 |
# File 'lib/peg_markdown.rb', line 20 def smart @smart end |
#spoilerblock ⇒ Object
Set true to enable spoilerblock extension.
59 60 61 |
# File 'lib/peg_markdown.rb', line 59 def spoilerblock @spoilerblock end |
#strike ⇒ Object
Set true to enable strike-through extension.
35 36 37 |
# File 'lib/peg_markdown.rb', line 35 def strike @strike end |
#text ⇒ Object (readonly) Also known as: to_s
Original Markdown formatted text.
17 18 19 |
# File 'lib/peg_markdown.rb', line 17 def text @text end |
#usernames ⇒ Object
Set true to enable usernames extension.
56 57 58 |
# File 'lib/peg_markdown.rb', line 56 def usernames @usernames end |
Instance Method Details
#to_groff_mm(*args) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'ext/markdown.c', line 73 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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# 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 ; if ( rb_funcall(self, rb_intern("strike"), 0) == Qtrue ) extensions = extensions | EXT_STRIKE ; if ( rb_funcall(self, rb_intern("autolink"), 0) == Qtrue ) extensions = extensions | EXT_AUTOLINK ; if ( rb_funcall(self, rb_intern("hard_wrap"), 0) == Qtrue ) extensions = extensions | EXT_HARD_WRAP ; if ( rb_funcall(self, rb_intern("no_images"), 0) == Qtrue ) extensions = extensions | EXT_NO_IMAGES ; if ( rb_funcall(self, rb_intern("media"), 0) == Qtrue ) extensions = extensions | EXT_MEDIA ; if ( rb_funcall(self, rb_intern("codeblock"), 0) == Qtrue ) extensions = extensions | EXT_CODEBLOCK ; if ( rb_funcall(self, rb_intern("hashtags"), 0) == Qtrue ) extensions = extensions | EXT_HASHTAGS ; if ( rb_funcall(self, rb_intern("usernames"), 0) == Qtrue ) extensions = extensions | EXT_USERNAMES ; if ( rb_funcall(self, rb_intern("spoilerblock"), 0) == Qtrue ) extensions = extensions | EXT_SPOILERBLOCK ; char *html = markdown_to_string(ptext, extensions, HTML_FORMAT); VALUE result = rb_str_new2(html); free(html); return result; } |
#to_latex(*args) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'ext/markdown.c', line 50 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; } |