module BBRuby
Constants
- VERSION
Public Class Methods
Returns the list of tags processed by BBRuby
in a Hash object
# File lib/bb-ruby.rb, line 314 def tag_list @@tags end
Convert a string with BBCode markup into its corresponding HTML markup
Basic Usage¶ ↑
The first parameter is the string off BBCode markup to be processed
text = "[b]some bold text to markup[/b]" output = BBRuby.to_html(text) # output => "<strong>some bold text to markup</strong>"
Custom BBCode translations¶ ↑
You can supply your own BBCode markup translations to create your own custom markup or override the default BBRuby
translations (parameter is a hash of custom translations).
The hash takes the following format: “name” => [regexp, replacement, description, example, enable_symbol]
custom_blockquote = { 'Quote' => [ /\[quote(:.*)?=(.*?)\](.*?)\[\/quote\1?\]/mi, '<div class="quote"><p><cite>\2</cite></p><blockquote>\3</blockquote></div>', 'Quote with citation', '[quote=mike]please quote me[/quote]', :quote ] }
Enable and Disable specific tags¶ ↑
BBRuby
will allow you to only enable certain BBCode tags, or to explicitly disable certain tags. Pass in either :disable or :enable to set your method, followed by the comma-separated list of tags you wish to disable or enable
BBRuby.to_html(text, {}, true, :enable, :image, :bold, :quote) BBRuby.to_html(text, {}, true, :disable, :image, :video, :color)
# File lib/bb-ruby.rb, line 290 def to_html(text, tags_alternative_definition={}, escape_html=true, method=:disable, *tags) text = process_tags(text, tags_alternative_definition, escape_html, method, *tags) # parse spacing text.gsub!( /\r\n?/, "\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(nn) 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.
# File lib/bb-ruby.rb, line 306 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
Private Class Methods
# File lib/bb-ruby.rb, line 355 def gsub!(text, pattern, replacement) if replacement.class == String # just replace if replacement is String while text.gsub!( pattern, replacement ); end else # call replacement # It may be Proc or lambda with one argument # Argument is MatchData. See 'Bold' tag name for example. while text.gsub!( pattern ){ replacement.call($~) }; end end end
extracted from Rails ActionPack
# File lib/bb-ruby.rb, line 368 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