BBCode for Ruby on Rails

2019-04-09 21:56发布

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!

3条回答
放荡不羁爱自由
2楼-- · 2019-04-09 22:34

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

查看更多
混吃等死
3楼-- · 2019-04-09 22:55

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

查看更多
Ridiculous、
4楼-- · 2019-04-09 22:59

Here is another gem you may find useful

http://github.com/jarrett/rbbcode

查看更多
登录 后发表回答