From cbb321658f079786ec5fccf4149e2be38df390f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Mon, 22 Jan 2018 19:17:35 +0100 Subject: [PATCH] FIX: support for generating excerpt when nesting
blocks --- lib/excerpt_parser.rb | 50 +++++++++++++++----------- spec/components/excerpt_parser_spec.rb | 20 +++++++++++ 2 files changed, 49 insertions(+), 21 deletions(-) create mode 100644 spec/components/excerpt_parser_spec.rb diff --git a/lib/excerpt_parser.rb b/lib/excerpt_parser.rb index 0e3f824679b..295515ea2a4 100644 --- a/lib/excerpt_parser.rb +++ b/lib/excerpt_parser.rb @@ -18,6 +18,7 @@ class ExcerptParser < Nokogiri::XML::SAX::Document @keep_onebox_source = options[:keep_onebox_source] == true @remap_emoji = options[:remap_emoji] == true @start_excerpt = false + @in_details_depth = 0 @summary_contents = "" @detail_contents = "" end @@ -91,31 +92,37 @@ class ExcerptParser < Nokogiri::XML::SAX::Document when "aside" attributes = Hash[*attributes.flatten] + unless @keep_onebox_source && attributes['class'].include?('onebox') + @in_quote = true + end - unless @keep_onebox_source && attributes['class'].include?('onebox') - @in_quote = true - end when 'article' if @keep_onebox_source && attributes.include?(['class', 'onebox-body']) @in_quote = true end + when "div", "span" if attributes.include?(["class", "excerpt"]) @excerpt = "" @current_length = 0 @start_excerpt = true end - # Preserve spoilers - if attributes.include?(["class", "spoiler"]) - include_tag("span", attributes) - @in_spoiler = true - end + # Preserve spoilers + if attributes.include?(["class", "spoiler"]) + include_tag("span", attributes) + @in_spoiler = true + end + when "details" - @detail_contents = "" - @in_details = true + @detail_contents = "" if @in_details_depth == 0 + @in_details_depth += 1 + when "summary" - @summary_contents = "" - @in_summary = true + if @in_details_depth == 1 && !@in_summary + @summary_contents = "" + @in_summary = true + end + end end @@ -135,16 +142,17 @@ class ExcerptParser < Nokogiri::XML::SAX::Document when "aside" @in_quote = false when "details" - @in_details = false - full = "
#{clean(@summary_contents)}#{clean(@detail_contents)}
" - if @current_length + full.length > @length - @excerpt << "
#{@summary_contents[0..@length]}
" - else - @excerpt << full + @in_details_depth -= 1 + if @in_details_depth == 0 + full = "
#{clean(@summary_contents)}#{clean(@detail_contents)}
" + if @current_length + full.length > @length + @excerpt << "
#{@summary_contents[0..@length]}
" + else + @excerpt << full + end end - when "summary" - @in_summary = false + @in_summary = false if @in_details_depth == 1 when "div", "span" throw :done if @start_excerpt characters("", false, false, false) if @in_spoiler @@ -161,7 +169,7 @@ class ExcerptParser < Nokogiri::XML::SAX::Document # we call length on this so might as well ensure we have a string string = string.to_s - if @in_details + if @in_details_depth > 0 if @in_summary @summary_contents << string else diff --git a/spec/components/excerpt_parser_spec.rb b/spec/components/excerpt_parser_spec.rb new file mode 100644 index 00000000000..45e6468cbb7 --- /dev/null +++ b/spec/components/excerpt_parser_spec.rb @@ -0,0 +1,20 @@ +require "rails_helper" +require "excerpt_parser" + +describe ExcerptParser do + + it "handles nested
blocks" do + html = <<~HTML +
+ FOO +
+ BAR +

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce ultrices, ex bibendum vestibulum vestibulum, mi velit pulvinar risus, sed consequat eros libero in eros. Fusce luctus mattis mauris, vitae semper lorem sodales quis. Donec pellentesque lacus ac ante aliquam, tincidunt iaculis risus interdum. In ullamcorper cursus massa ut lacinia. Donec quis diam finibus, rutrum odio eu, maximus leo. Nulla facilisi. Nullam suscipit quam et bibendum sagittis. Praesent sollicitudin neque at luctus ornare. Maecenas tristique dapibus risus, ac dictum ipsum gravida aliquam. Phasellus vehicula eu arcu sed imperdiet. Vestibulum ornare eros a nisi faucibus vehicula. Quisque congue placerat nulla, nec finibus nulla ultrices vitae. Quisque ac mi sem. Curabitur eu porttitor justo. Etiam dignissim in orci iaculis congue. Donec tempus cursus orci, a placerat elit varius nec.

+
+
+ HTML + + expect(ExcerptParser.get_excerpt(html, 200, strip_links: true)).to eq(%{
FOO
}) + end + +end