Class: RbbCode

Inherits:
Object
  • Object
show all
Defined in:
lib/rbbcode.rb,
lib/rbbcode/sanitize.rb,
lib/rbbcode/node_extensions.rb

Defined Under Namespace

Modules: Attributes, BlockquoteNode, DocumentNode, ImgTagNode, ListItemNode, ListNode, LiteralTextNode, ParagraphNode, RecursiveConversion, SingleBreakNode, TagNode, URLTagNode

Constant Summary collapse

DEFAULT_SANITIZE_CONFIG =
{
  :elements => %w[a blockquote br code del em img li p pre strong ul u],
  :attributes => {
    'a'   => %w[href target],
    'img' => %w[alt src]
  },

  :protocols => {
    'a' => {'href' => ['ftp', 'http', 'https', 'mailto', :relative]}
  }
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ RbbCode

Returns a new instance of RbbCode.



29
30
31
32
33
34
# File 'lib/rbbcode.rb', line 29

def initialize(options = {})
  @options = {
    :sanitize => true,
    :sanitize_config => RbbCode::DEFAULT_SANITIZE_CONFIG
  }.merge(options)
end

Class Method Details

.parser_classObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rbbcode.rb', line 12

def self.parser_class
  if !@grammar_loaded
    Treetop.load_from_string(
      ERB.new(
        File.read(
          File.join(
            File.dirname(__FILE__),
            'rbbcode/rbbcode_grammar.treetop'
          )
        )
      ).result
    )
    @grammar_loaded = true
  end
  RbbCodeGrammarParser
end

Instance Method Details

#convert(bb_code) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rbbcode.rb', line 36

def convert(bb_code)
  html = self.class.parser_class.new.parse("\n\n" + bb_code + "\n\n").to_html
  if @options[:emoticons]
    @options[:emoticons].each do |emoticon, url|
      html.gsub!(emoticon, '<img src="' + url + '" alt="Emoticon"/>')
    end
  end
  html
  if @options[:sanitize]
    Sanitize.clean(html, @options[:sanitize_config])
  else
    html
  end
end