mirror of
https://github.com/discourse/discourse.git
synced 2024-11-25 09:42:07 +08:00
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
|
||||
|
||||
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
|
||||
|
|
|
@ -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 " +
|
||||
"<span class='excerpt'>the specified excerpt</span>" +
|
||||
"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 + "<span class='excerpt'>#{two_hundred}</span>" + 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
|
||||
|
|
Loading…
Reference in New Issue
Block a user