lib/gemoji-parser.rb
# Creates an optimized regular expression for matching emoticon symbols.
# - Options: rehash:boolean
Creates an optimized regular expression for matching emoticon symbols.
Options: rehash:boolean
Creates an optimized regular expression for matching emoticon symbols . Options : rehash : boolean
def emoticon_regex(opts={})
    return @emoticon_regex if defined?(@emoticon_regex) && !opts[:rehash]
    pattern = {}
    emoticons.keys.each do |icon|
      compact_icon = icon.gsub('-', '')
      if compact_icon != icon && emoticons[compact_icon]
        compact_regex = Regexp.escape(icon).gsub('-', '-?')
        if !pattern[compact_icon] || pattern[compact_icon].length < compact_regex.length
          pattern[compact_icon] = compact_regex
        end
      elsif !pattern[icon]
        pattern[icon] = Regexp.escape(icon)
      end
    end
    lookaround = emoticon_lookaround
    @emoticon_pattern = "(?<=#{ lookaround[:behind] })(?:#{ pattern.values.join('|') })(?=#{ lookaround[:ahead] })"
    @emoticon_regex = Regexp.new("(#{@emoticon_pattern})")
  end
def emoticon_regex ( opts = { } ) return @emoticon_regex if defined? ( @emoticon_regex ) && ! opts [ :rehash ] pattern = { } emoticons . keys . each do | icon | compact_icon = icon . gsub ( '-' , '' ) if compact_icon != icon && emoticons [ compact_icon ] compact_regex = Regexp . escape ( icon ) . gsub ( '-' , '-?' ) if ! pattern [ compact_icon ] || pattern [ compact_icon ] . length < compact_regex . length pattern [ compact_icon ] = compact_regex end elsif ! pattern [ icon ] pattern [ icon ] = Regexp . escape ( icon ) end end lookaround = emoticon_lookaround @emoticon_pattern = "(?<=#{ lookaround[:behind] })(?:#{ pattern.values.join('|') })(?=#{ lookaround[:ahead] })" @emoticon_regex = Regexp . new ( "(#{@emoticon_pattern})" ) end
Creates an optimized regular expression for matching emoticon symbols.
Creates an optimized regular expression for matching emoticon symbols .
# Check to see if this icon has a compact version, ex:  :-)  versus  :) # One expression will match as many nose/noseless variants as possible. # Keep this expression if it hasn't been defined yet, # or if it's longer than a previously defined pattern.
opts