3

So I'm putting together a simple forum. I'd like to allow my users limited formatting options and BBCode would be plenty for my users. Knowing that I'm assuredly not the first one to want to use BBCode with RoR I googled but couldn't find a straight forward tutorial on how to create a editor which accepts BBCode nor a way to parse and display BBCode formatted input.

Any help or guides would be appreciated!

Drew
  • 15,158
  • 17
  • 68
  • 77

3 Answers3

4

You should give bb-ruby a try. Its documentation on the web page seems to be very clear and straightforward.

Thiago Silveira
  • 5,033
  • 4
  • 26
  • 29
kirushik
  • 1,296
  • 1
  • 10
  • 20
  • last version of this was however released 2 years ago, and it has poor support for nested tags since it is regex based. so [ul][li][ul][li][/li][/ul][/li][/ul] will yield bad results – Ben Oct 28 '11 at 00:02
  • 1
    >last version of this was however released 2 years ago Just up-to-date at the time when the answer was posted,I must note. – kirushik Oct 30 '11 at 00:16
2

Here is another gem you may find useful

http://github.com/jarrett/rbbcode

Spasm
  • 805
  • 9
  • 17
  • I just released a new version of rbbcode. It's waaaay better than the original. For starters, it's based on Treetop rather than my own hacked-together state machine. – rlkw1024 Oct 27 '12 at 23:39
1

Gemfile

gem 'bb-ruby'
# run `bundle`

In the place (haml):

%h1= put_header_string.bbcode_to_html.html_safe
%p= "[b]bold text[/b]".bbcode_to_html.html_safe

Besides a builtins you could also extend your own bbcode as you need. For example:

module BBRuby
  @@tags = @@tags.merge({
      'Email' => [
        /\[email(:.*)?\](.*?)\[\/file\1?\]/mi,
        lambda{ |e| "<span class='email'>#{e[2].gsub('@','<i>(at)</i>')}</span>"},
        'protect email from spam',
        '[email]electronic@test.ru[/email]',
        :email
      ],
    })
end

In place

[b]Contact me:[/b][email]email@test.ru[/email]

Contact me: email(at)test.ru


bb-ruby on github | bb-ruby on rubygems | bb-ruby home | tags processed list

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Ivan Black
  • 4,827
  • 1
  • 35
  • 33