Skip to content

Commit

Permalink
add to_html_with_formatting methods
Browse files Browse the repository at this point in the history
  • Loading branch information
cpjolicoeur committed Feb 26, 2010
1 parent b6bd38d commit f4dca08
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 11 deletions.
63 changes: 52 additions & 11 deletions lib/bb-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,38 @@ class << self
# BBRuby.to_html(text, {}, true, :disable, :image, :video, :color)
#
def to_html(text, tags_alternative_definition={}, escape_html=true, method=:disable, *tags)
text = text.clone
text = process_tags(text, tags_alternative_definition, escape_html, method, *tags)

# parse spacing
text.gsub!( /\r\n?/, "\n" )
text.gsub!( /\n/, "<br />\n" )

# return markup
text
end

# The same as BBRuby.to_html except the output is passed through simple_format first
#
# Returns text transformed into HTML using simple formatting rules. Two or more consecutive newlines(\n\n)
# are considered as a paragraph and wrapped in <p> tags. One newline (\n) is considered as a linebreak and
# a <br /> tag is appended. This method does not remove the newlines from the text.
#
def to_html_with_formatting(text, tags_alternative_definition={}, escape_html=true, method=:disable, *tags)
text = process_tags(text, tags_alternative_definition, escape_html, method, *tags)

# parse spacing
simple_format( text )
end

# Returns the list of tags processed by BBRuby in a Hash object
def tag_list
@@tags
end

private

def process_tags(text, tags_alternative_definition={}, escape_html=true, method=:disable, *tags)
text = text.dup

# escape "<, >, &" to remove any html
if escape_html
Expand All @@ -263,18 +294,19 @@ def to_html(text, tags_alternative_definition={}, escape_html=true, method=:disa
# 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]) }
end

# parse spacing
text.gsub!( /\r\n?/, "\n" )
text.gsub!( /\n/, "<br />\n" )

# return markup

text
end

# Returns the list of tags processed by BBRuby in a Hash object
def tag_list
@@tags

# extracted from Rails ActionPack
def simple_format( text )
start_tag = '<p>'
text = text.to_s.dup
text.gsub!(/\r\n?/, "\n") # \r\n and \r => \n
text.gsub!(/\n\n+/, "</p>\n\n#{start_tag}") # 2+ newline => paragraph
text.gsub!(/([^\n]\n)(?=[^\n])/, '\1<br />') # 1 newline => br
text.insert 0, start_tag
text << "</p>"
end
end # class << self

Expand Down Expand Up @@ -332,4 +364,13 @@ def bbcode_to_html(tags_alternative_definition = {}, escape_html=true, method=:d
def bbcode_to_html!(tags_alternative_definition = {}, escape_html=true, method=:disable, *tags)
self.replace(BBRuby.to_html(self, tags_alternative_definition, escape_html, method, *tags))
end

def bbcode_to_html_with_formatting(tags_alternative_definition = {}, escape_html=true, method=:disable, *tags)
BBRuby.to_html_with_formatting(self, tags_alternative_definition, escape_html, method, *tags)
end

# Replace the string contents with the HTML-converted markup using simple_format
def bbcode_to_html_with_formatting!(tags_alternative_definition = {}, escape_html=true, method=:disable, *tags)
self.replace(BBRuby.to_html_with_formatting(self, tags_alternative_definition, escape_html, method, *tags))
end
end
10 changes: 10 additions & 0 deletions test/test_bb-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,16 @@ def test_to_html_bang_method
assert_equal "<strong>foobar</strong>", foo.bbcode_to_html!
assert_equal "<strong>foobar</strong>", foo
end

def test_to_html_with_no_markup
foo = "first paragraph\n\nsecond paragraph\nwith a linebreak"
assert_equal "first paragraph<br />\n<br />\nsecond paragraph<br />\nwith a linebreak", foo.bbcode_to_html
end

def test_to_html_with_formatting
foo = "first paragraph\n\nsecond paragraph\nwith a linebreak"
assert_equal %Q(<p>first paragraph</p>\n\n<p>second paragraph\n<br />with a linebreak</p>), foo.bbcode_to_html_with_formatting
end

def test_self_tag_list
assert_equal 32, BBRuby.tag_list.size
Expand Down

0 comments on commit f4dca08

Please sign in to comment.