From 82dd9009e3acedbd08c800cc0362fcec91624652 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Thu, 17 Oct 2013 18:44:09 +0200 Subject: [PATCH] 4% speedup on our test suite --- app/models/post.rb | 1 - app/models/post_analyzer.rb | 25 +++++++++++-------------- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/app/models/post.rb b/app/models/post.rb index 4e0c1d13582..d12279e0c59 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -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. diff --git a/app/models/post_analyzer.rb b/app/models/post_analyzer.rb index 8f5d703c199..f5820c40890 100644 --- a/app/models/post_analyzer.rb +++ b/app/models/post_analyzer.rb @@ -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