Module: SmileyHelpers

Defined in:
lib/generators/templates/smileys.rb

Overview

This file loads simileys

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.regexObject



3
4
5
6
7
8
9
10
# File 'lib/generators/templates/smileys.rb', line 3

def self.regex
  return @regex if defined?(@regex)

  before_and_after = "[.,;:!\\?\\(\\[\\{\\)\\]\\}\\-]|\\s"
  @regex = Regexp.compile("(^|#{before_and_after})(" +
          smilies.keys.map { |token| Regexp.escape(token) }.join("|") +
          ")($|#{before_and_after})", Regexp::MULTILINE)
end

.replace_smilies(str, animate = false) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/generators/templates/smileys.rb', line 24

def self.replace_smilies(str, animate=false)
  str = str.to_str
  str = str.gsub(self.regex) do
    if animate
      %(#{$1}<span class="smilies animate#{self.smilies[$2]}" title="#{$2}">&nbsp;</span>#{$3})
    else
      %(#{$1}<span class="smilies #{self.smilies[$2]}" title="#{$2}">&nbsp;</span>#{$3})
    end
  end

  str.respond_to?(:html_safe) ? str.html_safe : str
end

.smiliesObject



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/generators/templates/smileys.rb', line 12

def self.smilies
  return @smilies unless @smilies.nil?

  @smilies = {}
  
  YAML.load_file("#{Rails.root}/config/smilies.yml").each do |smiley_name, symbols|
    symbols.each{|s| @smilies[s] = smiley_name }
  end

  @smilies
end

Instance Method Details

#replace_smilies(str, animate = false) ⇒ Object



37
38
39
40
# File 'lib/generators/templates/smileys.rb', line 37

def replace_smilies(str, animate=false)
  # Just call class method.
  SmileyHelpers.replace_smilies(str, animate)
end