Class: RDiscount
- Inherits:
-
Object
- Object
- RDiscount
- 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 =
'2.0.7'
Instance Attribute Summary collapse
-
#autolink ⇒ Object
Convert URL in links, even if they aren’t encased in
<>
. -
#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
RedCloth compatible line folding – not used for Markdown but included for compatibility.
-
#footnotes ⇒ Object
Enable php markdown extra-style footnotes.
-
#generate_toc ⇒ Object
Enable Table Of Contents generation.
-
#no_image ⇒ Object
Do not process
![]
and remove<img>
tags from the output. -
#no_links ⇒ Object
Do not process
[]
and remove<a>
tags from the output. -
#no_pseudo_protocols ⇒ Object
Do not process pseudo-protocols like
[](id:name)
. -
#no_tables ⇒ Object
Do not process tables.
-
#safelink ⇒ Object
Don’t make hyperlinks from
[][]
links that have unknown URL types. -
#smart ⇒ Object
Set true to have smarty-like quote translation performed.
-
#strict ⇒ Object
Disable superscript and relaxed emphasis processing.
-
#text ⇒ Object
readonly
Original Markdown formatted text.
Instance Method Summary collapse
-
#initialize(text, *extensions) ⇒ RDiscount
constructor
Create a RDiscount Markdown processor.
- #to_html(*args) ⇒ Object
- #toc_content(*args) ⇒ Object
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). -
: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.
91 92 93 94 |
# File 'lib/rdiscount.rb', line 91 def initialize(text, *extensions) @text = text extensions.each { |e| send("#{e}=", true) } end |
Instance Attribute Details
#autolink ⇒ Object
Convert URL in links, even if they aren’t encased in <>
64 65 66 |
# File 'lib/rdiscount.rb', line 64 def autolink @autolink end |
#filter_html ⇒ Object
Do not output any raw HTML included in the source text.
39 40 41 |
# File 'lib/rdiscount.rb', line 39 def filter_html @filter_html end |
#filter_styles ⇒ Object
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 |
#fold_lines ⇒ Object
RedCloth compatible line folding – not used for Markdown but included for compatibility.
43 44 45 |
# File 'lib/rdiscount.rb', line 43 def fold_lines @fold_lines end |
#footnotes ⇒ Object
Enable php markdown extra-style footnotes
46 47 48 |
# File 'lib/rdiscount.rb', line 46 def footnotes @footnotes end |
#generate_toc ⇒ Object
Enable Table Of Contents generation
49 50 51 |
# File 'lib/rdiscount.rb', line 49 def generate_toc @generate_toc end |
#no_image ⇒ Object
Do not process ![]
and remove <img>
tags from the output.
52 53 54 |
# File 'lib/rdiscount.rb', line 52 def no_image @no_image end |
#no_links ⇒ Object
Do not process []
and remove <a>
tags from the output.
55 56 57 |
# File 'lib/rdiscount.rb', line 55 def no_links @no_links end |
#no_pseudo_protocols ⇒ Object
Do not process pseudo-protocols like [](id:name)
70 71 72 |
# File 'lib/rdiscount.rb', line 70 def no_pseudo_protocols @no_pseudo_protocols end |
#no_tables ⇒ Object
Do not process tables
58 59 60 |
# File 'lib/rdiscount.rb', line 58 def no_tables @no_tables end |
#safelink ⇒ Object
Don’t make hyperlinks from [][]
links that have unknown URL types.
67 68 69 |
# File 'lib/rdiscount.rb', line 67 def safelink @safelink end |
#smart ⇒ Object
Set true to have smarty-like quote translation performed.
33 34 35 |
# File 'lib/rdiscount.rb', line 33 def smart @smart end |
#strict ⇒ Object
Disable superscript and relaxed emphasis processing.
61 62 63 |
# File 'lib/rdiscount.rb', line 61 def strict @strict end |
#text ⇒ Object (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
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 49 50 51 52 53 |
# 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 */
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 = 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);
/* 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
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'ext/rdiscount.c', line 55
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;
}
|