FEATURE: Add option for `ExcerptParser` to keep onebox source.

This commit is contained in:
Guo Xiang Tan 2017-04-10 16:11:58 +08:00
parent e7c2f5fd96
commit e49f3a408e
2 changed files with 19 additions and 3 deletions

View File

@ -14,6 +14,7 @@ class ExcerptParser < Nokogiri::XML::SAX::Document
@markdown_images = options[:markdown_images] == true @markdown_images = options[:markdown_images] == true
@keep_newlines = options[:keep_newlines] == true @keep_newlines = options[:keep_newlines] == true
@keep_emoji_images = options[:keep_emoji_images] == true @keep_emoji_images = options[:keep_emoji_images] == true
@keep_onebox_source = options[:keep_onebox_source] == true
@remap_emoji = options[:remap_emoji] == true @remap_emoji = options[:remap_emoji] == true
@start_excerpt = false @start_excerpt = false
end end
@ -27,6 +28,7 @@ class ExcerptParser < Nokogiri::XML::SAX::Document
parser.parse(html) parser.parse(html)
end end
excerpt = me.excerpt.strip excerpt = me.excerpt.strip
excerpt = excerpt.gsub(/\s*\n+\s*/, "\n\n") if options[:keep_onebox_source]
excerpt = CGI.unescapeHTML(excerpt) if options[:text_entities] == true excerpt = CGI.unescapeHTML(excerpt) if options[:text_entities] == true
excerpt excerpt
end end
@ -83,8 +85,11 @@ class ExcerptParser < Nokogiri::XML::SAX::Document
end end
when "aside" when "aside"
@in_quote = true @in_quote = true unless @keep_onebox_source
when 'article'
if @keep_onebox_source && attributes.include?(['class', 'onebox-body'])
@in_quote = true
end
when "div", "span" when "div", "span"
if attributes.include?(["class", "excerpt"]) if attributes.include?(["class", "excerpt"])
@excerpt = "" @excerpt = ""

View File

@ -273,6 +273,17 @@ HTML
expect(PrettyText.excerpt(emoji_code, 100)).to eq(":heart:") expect(PrettyText.excerpt(emoji_code, 100)).to eq(":heart:")
end end
it "should have an option to preserver onebox source" do
onebox = "<aside class=\"onebox whitelistedgeneric\">\n <header class=\"source\">\n <a href=\"https://meta.discourse.org/t/infrequent-translation-updates-in-stable-branch/31213/9\">meta.discourse.org</a>\n </header>\n <article class=\"onebox-body\">\n <img src=\"https://cdn-enterprise.discourse.org/meta/user_avatar/meta.discourse.org/gerhard/200/70381_1.png\" width=\"\" height=\"\" class=\"thumbnail\">\n\n<h3><a href=\"https://meta.discourse.org/t/infrequent-translation-updates-in-stable-branch/31213/9\">Infrequent translation updates in stable branch</a></h3>\n\n<p>Well, there's an Italian translation for \"New Topic\" in beta, it's been there since November 2014 and it works here on meta. Do you have any plugins installed? Try disabling them. I'm quite confident that it's either a plugin or a site...</p>\n\n </article>\n <div class=\"onebox-metadata\">\n \n \n </div>\n <div style=\"clear: both\"></div>\n</aside>\n\n\n"
expected = "<a href=\"https://meta.discourse.org/t/infrequent-translation-updates-in-stable-branch/31213/9\">meta.discourse.org</a>"
expect(PrettyText.excerpt(onebox, 100, keep_onebox_source: true))
.to eq(expected)
expect(PrettyText.excerpt("#{onebox}\n \n \n \n\n\n #{onebox}", 100, keep_onebox_source: true))
.to eq("#{expected}\n\n#{expected}")
end
end end
describe "strip links" do describe "strip links" do
@ -433,7 +444,7 @@ HTML
it "replaces the custom emoji" do it "replaces the custom emoji" do
CustomEmoji.create!(name: 'trout', upload: Fabricate(:upload)) CustomEmoji.create!(name: 'trout', upload: Fabricate(:upload))
Emoji.clear_cache Emoji.clear_cache
expect(PrettyText.cook("hello :trout:")).to match(/<img src[^>]+trout[^>]+>/) expect(PrettyText.cook("hello :trout:")).to match(/<img src[^>]+trout[^>]+>/)
end end
end end