Skip to content

Commit

Permalink
add options for new Discount flags
Browse files Browse the repository at this point in the history
  • Loading branch information
nono authored and rtomayko committed Mar 9, 2010
1 parent b61a5b3 commit e43f286
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 6 deletions.
29 changes: 29 additions & 0 deletions ext/rdiscount.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,35 @@ int rb_rdiscount__get_flags(VALUE ruby_obj)
if ( rb_funcall(ruby_obj, rb_intern("generate_toc"), 0) == Qtrue)
flags = flags | MKD_TOC;

/* no_image */
if ( rb_funcall(ruby_obj, rb_intern("no_image"), 0) == Qtrue)
flags = flags | MKD_NOIMAGE;

/* no_links */
if ( rb_funcall(ruby_obj, rb_intern("no_links"), 0) == Qtrue)
flags = flags | MKD_NOLINKS;

/* no_tables */
if ( rb_funcall(ruby_obj, rb_intern("no_tables"), 0) == Qtrue)
flags = flags | MKD_NOTABLES;

/* strict */
if ( rb_funcall(ruby_obj, rb_intern("strict"), 0) == Qtrue)
flags = flags | MKD_STRICT;

/* autolink */
if ( rb_funcall(ruby_obj, rb_intern("autolink"), 0) == Qtrue)
flags = flags | MKD_AUTOLINK;

/* safelink */
if ( rb_funcall(ruby_obj, rb_intern("safelink"), 0) == Qtrue)
flags = flags | MKD_SAFELINK;

/* no_pseudo_protocols */
if ( rb_funcall(ruby_obj, rb_intern("no_pseudo_protocols"), 0) == Qtrue)
flags = flags | MKD_NO_EXT;


return flags;
}

Expand Down
36 changes: 30 additions & 6 deletions lib/rdiscount.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class RDiscount
# Set true to have smarty-like quote translation performed.
attr_accessor :smart

# Do not output <style> tags included in the source text.
# Do not output <tt><style></tt> tags included in the source text.
attr_accessor :filter_styles

# Do not output any raw HTML included in the source text.
Expand All @@ -45,6 +45,27 @@ class RDiscount
# Enable Table Of Contents generation
attr_accessor :generate_toc

# Do not process <tt>![]</tt> and remove <tt><img></tt> tags from the output.
attr_accessor :no_image

# Do not process <tt>[]</tt> and remove <tt><a></tt> tags from the output.
attr_accessor :no_links

# Do not process tables
attr_accessor :no_tables

# Disable superscript and relaxed emphasis processing.
attr_accessor :strict

# Convert URL in links, even if they aren't encased in <tt><></tt>
attr_accessor :autolink

# Don't make hyperlinks from <tt>[][]</tt> links that have unknown URL types.
attr_accessor :safelink

# Do not process pseudo-protocols like <tt>[](id:name)</tt>
attr_accessor :no_pseudo_protocols

# 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:
Expand All @@ -54,14 +75,17 @@ class RDiscount
# * <tt>:filter_html</tt> - Do not output any raw HTML tags included in
# the source text.
# * <tt>:fold_lines</tt> - RedCloth compatible line folding (not used).
# * <tt>:generate_toc</tt> - Enable Table Of Contents generation
# * <tt>:no_image</tt> - Do not output any <tt><img></tt> tags.
# * <tt>:no_links</tt> - Do not output any <tt><a></tt> tags.
# * <tt>:no_tables</tt> - Do not output any tables.
# * <tt>:strict</tt> - Disable superscript and relaxed emphasis processing.
# * <tt>:autolink</tt> - Greedily urlify links.
# * <tt>:safelink</tt> - Do not make links for unknown URL types.
# * <tt>:no_pseudo_protocols</tt> - Do not process pseudo-protocols.
#
# NOTE: The <tt>:filter_styles</tt> extension is not yet implemented.
def initialize(text, *extensions)
@text = text
@smart = nil
@filter_styles = nil
@filter_html = nil
@fold_lines = nil
extensions.each { |e| send("#{e}=", true) }
end

Expand Down
39 changes: 39 additions & 0 deletions test/rdiscount_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,43 @@ def test_should_return_string_in_same_encoding_as_input
assert_equal input.encoding.name, output.encoding.name
end
end

def test_that_no_image_flag_works
rd = RDiscount.new(%(![dust mite](http://dust.mite/image.png) <img src="image.png" />), :no_image)
assert rd.to_html !~ /<img/
end

def test_that_no_links_flag_works
rd = RDiscount.new(%([This link](http://example.net/) <a href="links.html">links</a>), :no_links)
assert rd.to_html !~ /<a /
end

def test_that_no_tables_flag_works
rd = RDiscount.new(<<EOS, :no_tables)
aaa | bbbb
-----|------
hello|sailor
EOS
assert rd.to_html !~ /<table/
end

def test_that_strict_flag_works
rd = RDiscount.new("foo_bar_baz", :strict)
assert_equal "<p>foo<em>bar</em>baz</p>\n", rd.to_html
end

def test_that_autolink_flag_works
rd = RDiscount.new("http://github.com/rtomayko/rdiscount", :autolink)
assert_equal "<p><a href=\"http://github.com/rtomayko/rdiscount\">http://github.com/rtomayko/rdiscount</a></p>\n", rd.to_html
end

def test_that_safelink_flag_works
rd = RDiscount.new("[IRC](irc://chat.freenode.org/#freenode)", :safelink)
assert_equal "<p>[IRC](irc://chat.freenode.org/#freenode)</p>\n", rd.to_html
end

def test_that_no_pseudo_protocols_flag_works
rd = RDiscount.new("[foo](id:bar)", :no_pseudo_protocols)
assert_equal "<p>[foo](id:bar)</p>\n", rd.to_html
end
end

0 comments on commit e43f286

Please sign in to comment.