Sha256: f76b8431296e153d21d0e25eac27608760f02a93405310f5c6b1565c2fd41312

Contents?: true

Size: 1.22 KB

Versions: 1

Compression:

Stored size: 1.22 KB

Contents

module MyForum
  module PostsHelper
    def format_post_text(post)
      format_bbcode(post.text)
    end

    def format_bbcode(text)
      # Images
      text.gsub!(/\[img\]/i,   '<img src="')
      text.gsub!(/\[\/img\]/i, '" />')

      # Bold text
      text.gsub!(/\[b\]/i,   '<strong>')
      text.gsub!(/\[\/b\]/i, '</strong>')

      text.gsub!(/\[i\]/i,   '<i>')
      text.gsub!(/\[\/i\]/i, '</i>')

      # Cut
      text.gsub!(/\[cut\]/i,   '<pre>')
      text.gsub!(/\[\/cut\]/i, '</pre>')

      # Color
      text.gsub!(/\[color=(.*?)\](.*?)\[\/color\]/i) { "<span style='color: #{$1}'>#{$2}</span>" }

      # Size
      text.gsub!(/\[size=(.*?)\](.*?)\[\/size\]/i) { "<span style='font-size: #{$1}'>#{$2}</span>" }

      # Quote
      text.gsub!(/\[quote author=(.*?) link=(.*?) date=(.*?)\]/i) { bbquote(author: $1, date: $3) }
      text.gsub!(/\[\/quote\]/i, '</div>')

      # Link
      text.gsub!(/\[url=(.*?)\](.*?)\[\/url\]/i) { "<a href='#{$1}'>#{$2}</a>" }

      text.html_safe
    end

    def bbquote(author:, date:)
      date_time = time(DateTime.strptime(date, '%s')) rescue ''
      "<div class='bbqoute'> <div class='quote_info'>#{author} #{t('my_forum.bbquote.wrote')} #{date_time}:</div> "
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
my_forum-0.0.1.beta2 app/helpers/my_forum/posts_helper.rb