-->

BBCode for Ruby on Rails

2019-04-09 22:50发布

问题:

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!

回答1:

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



回答2:

Here is another gem you may find useful

http://github.com/jarrett/rbbcode



回答3:

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