4% speedup on our test suite

This commit is contained in:
Régis Hanol 2013-10-17 18:44:09 +02:00
parent 71571b9316
commit 82dd9009e3
2 changed files with 11 additions and 15 deletions

View File

@ -125,7 +125,6 @@ class Post < ActiveRecord::Base
Plugin::Filter.apply(:after_post_cook, self, post_analyzer.cook(*args))
end
# Sometimes the post is being edited by someone else, for example, a mod.
# If that's the case, they should not be bound by the original poster's
# restrictions, for example on not posting images.

View File

@ -42,6 +42,7 @@ class PostAnalyzer
# How many attachments are present in the post
def attachment_count
return 0 unless @raw.present?
attachments = cooked_document.css("a.attachment[href^=\"#{Discourse.store.absolute_base_url}\"]")
attachments += cooked_document.css("a.attachment[href^=\"#{Discourse.store.relative_base_url}\"]") if Discourse.store.internal?
attachments.count
@ -49,30 +50,25 @@ class PostAnalyzer
def raw_mentions
return [] if @raw.blank?
# We don't count mentions in quotes
return @raw_mentions if @raw_mentions.present?
raw_stripped = @raw.gsub(/\[quote=(.*)\]([^\[]*?)\[\/quote\]/im, '')
# Process markdown so that code blocks can be generated and subsequently ignored
raw_stripped = PrettyText.markdown(raw_stripped)
# strip quotes and code blocks
cooked_stripped = cooked_document
cooked_stripped.search("aside.quote").remove
cooked_stripped.search("pre").remove
cooked_stripped.search("code").remove
# Strip pre and code tags
doc = Nokogiri::HTML.fragment(raw_stripped)
doc.search("pre").remove
doc.search("code").remove
results = doc.to_html.scan(PrettyText.mention_matcher)
results = cooked_stripped.to_html.scan(PrettyText.mention_matcher)
@raw_mentions = results.uniq.map { |un| un.first.downcase.gsub!(/^@/, '') }
end
# Count how many hosts are linked in the post
def linked_hosts
return {} if raw_links.blank?
return @linked_hosts if @linked_hosts.present?
@linked_hosts = {}
raw_links.each do |u|
begin
uri = URI.parse(u)
@ -83,23 +79,24 @@ class PostAnalyzer
next
end
end
@linked_hosts
end
# Returns an array of all links in a post excluding mentions
def raw_links
return [] unless @raw.present?
return @raw_links if @raw_links.present?
# Don't include @mentions in the link count
@raw_links = []
cooked_document.search("a").each do |l|
next if l.attributes['href'].nil? || link_is_a_mention?(l)
url = l.attributes['href'].to_s
@raw_links << url
end
@raw_links
end