Module: MyForum::PostsHelper

Included in:
PostsController
Defined in:
app/helpers/my_forum/posts_helper.rb

Instance Method Summary collapse

Instance Method Details

#bbquote(author:, date:) ⇒ Object



72
73
74
75
# File 'app/helpers/my_forum/posts_helper.rb', line 72

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

#format_bbcode(text) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/helpers/my_forum/posts_helper.rb', line 7

def format_bbcode(text)
  # Line Breask
  text.gsub!(/\r\n/, '<br />')
  text.gsub!(/\n/, '<br />')

  # Images
  text.gsub!(/\[img\]/i,   '<img src="')
  text.gsub!(/\[\/img\]/i, '" />')
  text.gsub!(/\[img\](.*?)\[\/img\]/i) { ActionController::Base.helpers.image_tag($1) }

  # Youtube
  text.gsub!(/(?:https?:\/\/)?(?:www\.)?youtu(?:\.be|be\.com)\/(?:watch\?v=)?(?<video_code>[\w-]{10,})/i) do |match|
    "<iframe width='560' height='315' src='https://www.youtube.com/embed/#{$~[:video_code]}' frameborder='0' allowfullscreen></iframe>"
  end

  # Attachments
  text.gsub!(/\[attachment=([0-9]+)\]/i) do |match|
    "<p> <img class='post_attachment' src='#{attachment_img_path($1)}' /> </p>"
  end

  # Bold text
  text.gsub!(/(\[b\](?<bold_text>.+?)\[\/b\])/i) { |match| "<strong>#{$1}</strong>" }

  # Italic
  text.gsub!(/(\[i\])(?<italic_text>.*?)(\[\/i\])/i) { |match| "<i>#{$1}</i>" }

  # Cut
  text.gsub!(/(\[cut\])(?<cut_text>.*?)(\[\/cut\])/i) { |match| "<pre>#{$1}</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>')
  #text.gsub!(/\[quote(.*)\]/i, "<div class='bbqoute'>")
  text.gsub!(/\[quote author=(?<autor>.*)\](?<text>.*)\[\/quote\]/m) do |match|
    "<div class='bbqoute'> <div class='quote_info'>#{$~[:autor]} #{t('my_forum.bbquote.wrote')}:</div> #{$~[:text]} </div>"
  end

  text.gsub!(/(?<open>\[quote author=(?<autor>(.*?)) (.+)\](?<text>(.+))(?<close>\[\/quote\]))/i) do |match|
    # "<div class='bbqoute'> <div class='quote_info'>#{$~[:autor]} #{t('my_forum.bbquote.wrote')} #{Time.now}:</div> #{$~[:text]} </div>"
    "<div class='bbqoute'> <div class='quote_info'>#{$~[:autor]} #{t('my_forum.bbquote.wrote')}:</div> #{$~[:text]} </div>"
  end

  # Emoticons (smiles)
  emoticons_list.each do |code, path|
    text.gsub!(/#{Regexp.quote(code)}/) { "<img src='#{path}' class='smile' />" }
  end

  # Link TODO
  # 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| "<span class='badge'>#{match}</span>" }


  auto_link(text).html_safe
end

#format_post_text(post) ⇒ Object



3
4
5
# File 'app/helpers/my_forum/posts_helper.rb', line 3

def format_post_text(post)
  format_bbcode(post.text)
end