Skip to content

Commit

Permalink
enable Proc (inclusive lambda) as replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin Ivanov committed May 10, 2013
1 parent 826812b commit bc90630
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 3 deletions.
14 changes: 14 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,20 @@ Define your own translation, in order to be more flexible:
}

text.bbcode_to_html(my_blockquote)

Define Proc as replacement:

module BBRuby
@@tags = @@tags.merge({
'File' => [
/\[file(:.*)?=(.*?)\](.*?)\[\/file\1?\]/mi,
lambda{ |e| "<div class="file"><p><cite>#{e[3]}</cite></p><blockquote>#{file_read_method(e[2])}</blockquote></div>"},
'File content with citation',
'[file=script.rb]Script Caption[/file]',
:file
],
})
end

You can also use the simple_format method of ActionPack by using the *_with_formatting methods:

Expand Down
24 changes: 21 additions & 3 deletions lib/bb-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module BBRuby
# tag name => [regex, replace, description, example, enable/disable symbol]
'Bold' => [
/\[b(:.*)?\](.*?)\[\/b\1?\]/mi,
'<strong>\2</strong>',
'<strong>\2</strong>', #Proc alternative for example: lambda{ |e| "<strong>#{e[2]}</strong>" }
'Embolden text',
'Look [b]here[/b]',
:bold],
Expand Down Expand Up @@ -298,10 +298,28 @@ def process_tags(text, tags_alternative_definition={}, escape_html=true, method=
# parse bbcode tags
case method
when :enable
tags_definition.each_value { |t| text.gsub!(t[0], t[1]) if tags.include?(t[4]) }
tags_definition.each_value do |t|
if tags.include?( t[4] )
if t[1].class == String
text.gsub!( t[0], t[1] ) # just replace if replacement is String
else
text.gsub!( t[0] ){ t[1].call($~) } # call replacement
# It may be Proc or lambda with one argument
# Argument is MatchData. See 'Bold' tag name for example.
end
end
end
when :disable
# this works nicely because the default is disable and the default set of tags is [] (so none disabled) :)
tags_definition.each_value { |t| text.gsub!(t[0], t[1]) unless tags.include?(t[4]) }
tags_definition.each_value do |t|
unless tags.include?( t[4] )
if t[1].class == String
text.gsub!( t[0], t[1] )
else
text.gsub!( t[0] ){ t[1].call($~) }
end
end
end
end

text
Expand Down
54 changes: 54 additions & 0 deletions test/bb-ruby_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,58 @@ def test_different_start_and_ending_tags
assert_equal "this [b]should not do formatting[/i]", "this [b]should not do formatting[/i]".bbcode_to_html
end

## proc tests below
def test_redefinition_replacement_to_proc # contrived example
mydef = {
'Quote' => [
/\[quote(:.*)?=(?:&quot;)?(.*?)(?:&quot;)?\](.*?)\[\/quote\1?\]/mi,
lambda { |e| "<div class=\"quote\"><p><cite>#{e[2]}</cite></p><blockquote>#{e[3]}</blockquote></div>"},
'Quote with citation (lambda)',
nil, nil,
:quote]
}
assert_equal '<div class="quote"><p><cite>Who</cite></p><blockquote>said that</blockquote></div>', '[quote=Who]said that[/quote]'.bbcode_to_html(mydef)
end

def test_proc_modifer # sum as example
mydef = {
'Sum (lambda)' => [
/\[sum(:.*)?=(?:&quot;)?(.*?)(?:&quot;)?\](\d+?)\+(\d+?)\[\/sum\1?\]/mi,
lambda { |e| "<span class=\"#{e[2]}\">#{e[3].to_i + e[4].to_i}</span>"},
'Sum (lambda)',
nil, nil,
:sum]
}
assert_equal '<span class="sum">4</span>', '[sum=sum]2+2[/sum]'.bbcode_to_html(mydef)
end

# for next test
def sum a, b; a + b end

def test_proc_include_method # sum as example
mydef = {
'Sum (lambda)' => [
/\[sum(:.*)?=(?:&quot;)?(.*?)(?:&quot;)?\](\d+?)\+(\d+?)\[\/sum\1?\]/mi,
lambda { |e| "<span class=\"#{e[2]}\">#{sum(e[3].to_i, e[4].to_i)}</span>"},
'Sum (lambda)',
nil, nil,
:sum]
}
assert_equal '<span class="sum">4</span>', '[sum=sum]2+2[/sum]'.bbcode_to_html(mydef)
end

# Proc.new{} as opposed to lambda{} may have not params
def test_proc_instead_of_lambda # copyright
copyright = "2913-3013 The Company, Ltd."
mydef = {
'copy' => [
/\[copy\/\]/mi,
Proc.new{"<span class=\"copy\">&copy; #{copyright}</span>"},
'Copy (Proc)',
nil, nil,
:copy]
}
assert_equal '<span class="copy">&copy; 2913-3013 The Company, Ltd.</span>', '[copy/]'.bbcode_to_html(mydef)
end

end

0 comments on commit bc90630

Please sign in to comment.