module BBRuby

Constants

VERSION

Public Class Methods

any_bb_tags?(text) click to toggle source

Checks if text contains any bb tag

# File lib/bb-ruby.rb, line 319
def any_bb_tags?(text)
  tag_list.map{|k,v| v[0]}.any?{|regex| text.match(regex) }
end
tag_list() click to toggle source

Returns the list of tags processed by BBRuby in a Hash object

# File lib/bb-ruby.rb, line 314
def tag_list
  @@tags
end
to_html(text, tags_alternative_definition={}, escape_html=true, method=:disable, *tags) click to toggle source

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
to_html_with_formatting(text, tags_alternative_definition={}, escape_html=true, method=:disable, *tags) click to toggle source

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

gsub!(text, pattern, replacement) click to toggle source
# 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
process_tags(text, tags_alternative_definition={}, escape_html=true, method=:disable, *tags) click to toggle source
# File lib/bb-ruby.rb, line 325
def process_tags(text, tags_alternative_definition={}, escape_html=true, method=:disable, *tags)
  text = text.dup

  # escape "<, >, &" and quotes to remove any html
  if escape_html
    text.gsub!( '&', '&amp;' )
    text.gsub!( '<', '&lt;' )
    text.gsub!( '>', '&gt;' )
    text.gsub!( '"', '&quot;' )
    text.gsub!( "'", '&apos;' )
  end

  tags_definition = @@tags.merge(tags_alternative_definition)

  # parse bbcode tags
  case method
  when :enable
    tags_definition.each_value do |t|
      gsub!(text, t[0], t[1]) if tags.include?( t[4] )
    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 do |t|
      gsub!(text, t[0], t[1]) unless tags.include?( t[4] )
    end
  end

  text
end
simple_format( text ) click to toggle source

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