Fork me on GitHub

20 Feb 2010 Custom HTML-Maker for RBBCode

Tags: ruby rbbcode

How to write a custom HTML-Maker for RBBCode

For YesPaste i use RBBCode for BBCode parsing and need to have a custom tag: code.

By default TagNode doesn’t have pure (read: non parsed) text, after reading the source code i got it:

class YesPasteHtmlMaker < RbbCode::HtmlMaker
  def html_from_code_tag(node)
    if node.value.nil?
      lang = 'plaintext'
    else
      lang = node.value
    end
    inner_html = node.children.inject('') do |output, child|
      output + make_html(child).gsub(&quot;&lt;br/&gt;&quot;, &quot;\n&quot;)
    end
    tokens = CodeRay.scan(inner_html, lang).div(:line_numbers =&gt; :table)
    return tokens
  end
end

def BBCode_render(text)
  html_maker = YesPasteHtmlMaker.new
  parser = RbbCode::Parser.new(:html_maker =&gt; html_maker)
  html = parser.parse(text)
  return html
end