FEATURE: Allow manual excerpt to be specified anywhere in the post and override max excerpt length
This commit is contained in:
parent
0513d02e23
commit
d567093756
|
@ -2,6 +2,8 @@ class ExcerptParser < Nokogiri::XML::SAX::Document
|
||||||
|
|
||||||
attr_reader :excerpt
|
attr_reader :excerpt
|
||||||
|
|
||||||
|
SPAN_REGEX = /<\s*span[^>]*class\s*=\s*['|"]excerpt['|"][^>]*>/
|
||||||
|
|
||||||
def initialize(length, options=nil)
|
def initialize(length, options=nil)
|
||||||
@length = length
|
@length = length
|
||||||
@excerpt = ""
|
@excerpt = ""
|
||||||
|
@ -14,10 +16,14 @@ class ExcerptParser < Nokogiri::XML::SAX::Document
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.get_excerpt(html, length, options)
|
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)
|
parser = Nokogiri::HTML::SAX::Parser.new(me)
|
||||||
catch(:done) do
|
catch(:done) do
|
||||||
parser.parse(html) unless html.nil?
|
parser.parse(html)
|
||||||
end
|
end
|
||||||
me.excerpt.strip!
|
me.excerpt.strip!
|
||||||
me.excerpt
|
me.excerpt
|
||||||
|
|
|
@ -196,14 +196,12 @@ describe PrettyText do
|
||||||
post.excerpt.should == "hi"
|
post.excerpt.should == "hi"
|
||||||
end
|
end
|
||||||
|
|
||||||
it "handles span excerpt that starts before the max length" do
|
it "ignores max excerpt length if a span excerpt is specified" do
|
||||||
text = "123456789 123456789 12345679 123456789 123456789 " +
|
two_hundred = "123456789 " * 20 + "."
|
||||||
"as long as span starts before max length of the " +
|
text = two_hundred + "<span class='excerpt'>#{two_hundred}</span>" + two_hundred
|
||||||
"<span class='excerpt'>the specified excerpt</span>" +
|
PrettyText.excerpt(text, 100).should == two_hundred
|
||||||
"of the post will be used"
|
|
||||||
PrettyText.excerpt(text, 100).should == 'the specified excerpt'
|
|
||||||
post = Fabricate(:post, raw: text)
|
post = Fabricate(:post, raw: text)
|
||||||
post.excerpt.should == 'the specified excerpt'
|
post.excerpt.should == two_hundred
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue