From d567093756e93ad30c49d520756668a4df902c4c Mon Sep 17 00:00:00 2001 From: David McClure Date: Wed, 3 Sep 2014 22:03:12 -0700 Subject: [PATCH] FEATURE: Allow manual excerpt to be specified anywhere in the post and override max excerpt length --- lib/excerpt_parser.rb | 10 ++++++++-- spec/components/pretty_text_spec.rb | 12 +++++------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/lib/excerpt_parser.rb b/lib/excerpt_parser.rb index af38f8635db..133d0875f74 100644 --- a/lib/excerpt_parser.rb +++ b/lib/excerpt_parser.rb @@ -2,6 +2,8 @@ class ExcerptParser < Nokogiri::XML::SAX::Document attr_reader :excerpt + SPAN_REGEX = /<\s*span[^>]*class\s*=\s*['|"]excerpt['|"][^>]*>/ + def initialize(length, options=nil) @length = length @excerpt = "" @@ -14,10 +16,14 @@ class ExcerptParser < Nokogiri::XML::SAX::Document end def self.get_excerpt(html, length, options) - me = self.new(length,options) + html ||= '' + if (html.include? 'excerpt') && (SPAN_REGEX === html) + length = html.length + end + me = self.new(length, options) parser = Nokogiri::HTML::SAX::Parser.new(me) catch(:done) do - parser.parse(html) unless html.nil? + parser.parse(html) end me.excerpt.strip! me.excerpt diff --git a/spec/components/pretty_text_spec.rb b/spec/components/pretty_text_spec.rb index 51ebd41ac43..90147cfbb9f 100644 --- a/spec/components/pretty_text_spec.rb +++ b/spec/components/pretty_text_spec.rb @@ -196,14 +196,12 @@ describe PrettyText do post.excerpt.should == "hi" end - it "handles span excerpt that starts before the max length" do - text = "123456789 123456789 12345679 123456789 123456789 " + - "as long as span starts before max length of the " + - "the specified excerpt" + - "of the post will be used" - PrettyText.excerpt(text, 100).should == 'the specified excerpt' + it "ignores max excerpt length if a span excerpt is specified" do + two_hundred = "123456789 " * 20 + "." + text = two_hundred + "#{two_hundred}" + two_hundred + PrettyText.excerpt(text, 100).should == two_hundred post = Fabricate(:post, raw: text) - post.excerpt.should == 'the specified excerpt' + post.excerpt.should == two_hundred end end