Topic: Centering with BBCode

Like many of you I am using BBCode to allow my users to respond graphically to my many text entries. It would be wonderful to enable them to [center] text value [/center].
Has anyone observed how this capability might be offered? When I Google I find PHP modifications but nothing in the Rails world.
Thank you,
Kathleen

Re: Centering with BBCode

What plugin are you using for your BBcode functionality?

I suppose I could put together a mod for it but I'd have to know what you're using to start with smile

Re: Centering with BBCode

Firstly, thank you for your kind reply.
Secondly, I have zero experience and have observed that many sites use BBCode (this one in particular) and thought it might be the way to go.
In my application_helper.rb I expect these lines mean something to you to show my approach;

  def bb(text)
    text = simple_format(bbcodeize(white_list(h(text))))
    auto_link(text) do |t|
      truncate(t, 50)
    end
  end

There are certain portions of my project (I'll explain further if you're interested) where the website will be generated by me in the form of BBCode. Thus I'd like to be able to do simple things like align resources and provide the functionality of 'link_to'.
I observe in my BBCodeizer logic that they appear to run RegExp against certain strings and thought if someone had some of this 'TAB' logic for additional capabilities, perhaps I could just 'drop it in'?
I guess I'm forced to make a decision about what 'bbcode' type tool to use and must run with one. If this is the one, I'd be grateful for suggestions about how it might be slightly embellished?
Thank you,
Kathleen

Re: Centering with BBCode

Ok, I think you are using the BBCodeizer plugin, so I have a fix for you.

Open up vendor/plugins/bbcodeizer/lib/bbcodeizer.rb and at about line 48, just before def bbcodeizer(text), put:

Tags[:center] = [ /\[center\](.+?)\[\/center\]/i, '<p style="text-align: center;">\1</p>' ]
TagList << :center

This is probably not the best place for the code because it modifies the plugin (which may at some point get updated by the vendor), but it's the only place I can think of off the top of my head, and it works. I originally had it in app/controllers/application.rb, outside the class definition with the "module" declaration from the plugin around it, but a bit of testing revealed that this would add another copy of the tag on every page view.

If anyone else can think of a better place to put this code please tell me. (Yeah I know, I'm a novice...)

Last edited by pglh (2008-01-28 13:01:10)

Re: Centering with BBCode

At the risk of 'trashing' this thread I couldn't find an appropriate place in my BBCodeizer.rb file. If you're still with me, I enclose the entire .rb file and ask you where you'd put it?
Thank you,
Kathleen

module BBCodeizer  
  class << self

    #:nodoc:
    Tags = {
      :start_code            => [ /\[code\]/i,                           '<pre>' ],
      :end_code              => [ /\[\/code\]/i,                         '</pre>' ],
      :start_quote           => [ /\[quote(?:=".*?")?\]/i,               nil ],
      :start_quote_with_cite => [ /\

"(.*?)"\ wrote:

/i,                  '<blockquote><cite>\1 wrote:</cite><br />' ],
      :start_quote_sans_cite => [ /\[quote\]/i,                          '<blockquote>' ],
      :end_quote             => [ /\[\/quote\]/i,                        '</blockquote>' ],
      :bold                  => [ /\[b\](.+?)\[\/b\]/i,                  '<strong>\1</strong>' ],
      :italic                => [ /\[i\](.+?)\[\/i\]/i,                  '<em>\1</em>' ],
      :underline             => [ /\[u\](.+?)\[\/u\]/i,                  '<u>\1</u>' ],
      :email_with_name       => [ /\[email=(.+?)\](.+?)\[\/email\]/i,    '<a href="mailto:\1">\2</a>' ],
      :email_sans_name       => [ /\[email\](.+?)\[\/email\]/i,          '<a href="mailto:\1">\1</a>' ],
      :url_with_title        => [ /\[url=(.+?)\](.+?)\[\/url\]/i,        '<a href="\1">\2</a>' ],
      :url_sans_title        => [ /\[url\](.+?)\[\/url\]/i,              '<a href="\1">\1</a>' ],
      :image                 => [ /\[img\](.+?)\[\/img\]/i,              '<img src="\1" />' ],
      :size                  => [ /\[size=(\d{1,2})\](.+?)\[\/size\]/i,  '<span style="font-size: \1px">\2</span>' ],
      :color                 => [ /\[color=([^;]+?)\](.+?)\[\/color\]/i, '<span style="color: \1">\2</span>' ]
    }

    # Tags in this list are invoked. To deactivate a particular tag, call BBCodeizer.deactivate.
    # These names correspond to either names above or methods in this module.
    TagList = [ :bold, :italic, :underline, :email_with_name, :email_sans_name,
                :url_with_title, :url_sans_title, :image, :size, :color,
                :code, :quote ]

    # Parses all bbcode in +text+ and returns a new HTML-formatted string.
    def bbcodeize(text)
      text = text.dup
      TagList.each do |tag|
        if Tags.has_key?(tag)
          apply_tag(text, tag)
        else
          self.send(tag, text)
        end
      end
      text
    end

    # Configuration option to deactivate particular +tags+.
    def deactivate(*tags)
      tags.each { |t| TagList.delete(t) }
    end

    # Configuration option to change the replacement string used for a particular +tag+. The source
    # code should be referenced to determine what an appropriate replacement +string+ would be.
    def replace_using(tag, string)
      Tags[tag][1] = string
    end
   
  private

    def code(string)
      # code tags must match, else don't do any replacing.
      if string.scan(Tags[:start_code].first).size == string.scan(Tags[:end_code].first).size
        apply_tags(string, :start_code, :end_code)
      end
    end
 
    def quote(string)
      # quotes must match, else don't do any replacing
      if string.scan(Tags[:start_quote].first).size == string.scan(Tags[:end_quote].first).size
        apply_tags(string, :start_quote_with_cite, :start_quote_sans_cite, :end_quote)
      end
    end

    def apply_tags(string, *tags)
      tags.each do |tag|
        string.gsub!(*Tags[tag])
      end
    end
    alias_method :apply_tag, :apply_tags
  end
end

Re: Centering with BBCode

At line 29 of what you have there.

I was being lazy and looking at the version from eldorado - I think the guy who wrote that may have added a few modifications of his own! Either that or there's a newer version out.

Re: Centering with BBCode

Mr. Mustache,
Belay that! I was able to get it to work.
Thank you so much!
Kathy