PERF: Use a regexp for unicode replacements

This commit is contained in:
Robin Ward 2015-12-30 14:35:25 -05:00
parent e45caa7e2e
commit b17e5c99cc
2 changed files with 14 additions and 11 deletions

View File

@ -118,4 +118,16 @@ class Emoji
"#{Discourse.base_uri}/uploads/#{db}/_emoji" "#{Discourse.base_uri}/uploads/#{db}/_emoji"
end end
def self.unicode_replacements
@unicode_replacements ||= Hash[db.map {|e| [e['emoji'], e['aliases'][0]] }]
end
def self.unicode_regexp
@unicode_regexp ||= Regexp.union(unicode_replacements.keys)
end
def self.sub_unicode!(text)
text.gsub!(unicode_regexp) {|m| ":#{unicode_replacements[m]}:"}
end
end end

View File

@ -246,23 +246,14 @@ module PrettyText
end end
end end
def self.emoji_unicode(text)
return text unless SiteSetting.enable_emoji?
Emoji.db.each do |e|
text.gsub!(e['emoji'], ":#{e['aliases'][0]}:")
end
text
end
def self.cook(text, opts={}) def self.cook(text, opts={})
options = opts.dup options = opts.dup
# we have a minor inconsistency # we have a minor inconsistency
options[:topicId] = opts[:topic_id] options[:topicId] = opts[:topic_id]
working_text = emoji_unicode(text.dup) working_text = text.dup
Emoji.sub_unicode!(working_text) if SiteSetting.enable_emoji?
sanitized = markdown(working_text, options) sanitized = markdown(working_text, options)
doc = Nokogiri::HTML.fragment(sanitized) doc = Nokogiri::HTML.fragment(sanitized)