module MyForum module PostsHelper def format_post_text(post) format_bbcode(post.text) end def format_bbcode(text) # Line Breask text.gsub!(/\r\n/, '
') text.gsub!(/\n/, '
') # Images text.gsub!(/\[img\]/i, '') # Youtube text.gsub!(/(?:https?:\/\/)?(?:www\.)?youtu(?:\.be|be\.com)\/(?:watch\?v=)?(?[\w-]{10,})/i) do |match| "" end # Attachments text.gsub!(/\[attachment=([0-9]+)\]/i) do |match| "

" end # Bold text text.gsub!(/(\[b\](?.+?)\[\/b\])/i) { |match| "#{$1}" } # Italic text.gsub!(/(\[i\])(?.*?)(\[\/i\])/i) { |match| "#{$1}" } # Cut text.gsub!(/(\[cut\])(?.*?)(\[\/cut\])/i) { |match| "
#{$1}
" } # Color text.gsub!(/\[color=(.*?)\](.*?)\[\/color\]/i) { "#{$2}" } # Size text.gsub!(/\[size=(.*?)\](.*?)\[\/size\]/i) { "#{$2}" } # Quote #text.gsub!(/\[quote author=(.*?) link=(.*?) date=(.*?)\]/i) { bbquote(author: $1, date: $3) } #text.gsub!(/\[\/quote\]/i, '') #text.gsub!(/\[quote(.*)\]/i, "
") text.gsub!(/\[quote author=(?.*)\](?.*)\[\/quote\]/m) do |match| "
#{$~[:autor]} #{t('my_forum.bbquote.wrote')}:
#{$~[:text]}
" end text.gsub!(/(?\[quote author=(?(.*?)) (.+)\](?(.+))(?\[\/quote\]))/i) do |match| # "
#{$~[:autor]} #{t('my_forum.bbquote.wrote')} #{Time.now}:
#{$~[:text]}
" "
#{$~[:autor]} #{t('my_forum.bbquote.wrote')}:
#{$~[:text]}
" end # Emoticons (smiles) emoticons_list.each do |code, path| text.gsub!(/#{Regexp.quote(code)}/) { "" } end # Link # ActionController::Base.helpers.... - this method uses in controller, which don`t know link_to text.gsub!(/\[url=(.*?)\](.*?)\[\/url\]/i) { ActionController::Base.helpers.link_to($2, $1, target: '_blank') } text.gsub!(/(http:\/\/[\S]+|www:\/\/[\S]+)/i) { ActionController::Base.helpers.link_to($1, $1, target: '_blank') } # Ping user text.gsub!(/@\S+/) { |match| "#{match}" } text.html_safe end def bbquote(author:, date:) date_time = time(DateTime.strptime(date, '%s')) rescue '' "
#{author} #{t('my_forum.bbquote.wrote')} #{date_time}:
" end end end