effective_posts 0.2.6 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d28952399f3ca899b39e4ebf35db0c27f156e61b
4
- data.tar.gz: 243b57d1bd9ad4f1b221c4e0ceace0d44a238c30
3
+ metadata.gz: 3db4764899f69a68c93c3f9b69be51473a176ef1
4
+ data.tar.gz: 90877459ce8fb72ffceba8470d3743a86777e89c
5
5
  SHA512:
6
- metadata.gz: 57a3333735eaa584289c39677672fb36fff21daa625d12436fd063c64395fc65e798ed29e7682f6720f62fdeee814f5474eb7e390175f760e65cad5cbcc28317
7
- data.tar.gz: 2d1a7c0d4b272de5d3d4e469a440e32cfc9295d493803b9dda06b9e9eb6c6683612447a1a3469be83775c034c581414a1b9d5f2e75d9e88c7b1152f709851ae7
6
+ metadata.gz: 32483bded255aeb0cd7c91320fc56e8ead284b3f9cd321d2f5d78c0ca4f3c2b5affc4402a94e5fe4bc9223eb0922fac17da2102d6fd58fc9545f2d20d97c5211
7
+ data.tar.gz: 98e933b31798a9c6a3127cb48e2a4089998ae74c1d2fe64757688598f50ed9b381d6c3c641720135899de529175c070205a1e78a015754d2f62ffb55863ee01a
@@ -12,8 +12,11 @@ module EffectivePostsHelper
12
12
  ].compact.join(' ').html_safe
13
13
  end
14
14
 
15
+ # Only supported options are:
16
+ # :length => 200 to set the max length of the content if the ReadMore divider is not present
17
+ # :label => 'Read Lots More' to set the label of the 'Read More' link
15
18
  def post_excerpt(post, options = {})
16
- content = effective_region(post, :content) { '<p>Default content</p>'.html_safe }
19
+ content = effective_region(post, :content, :editable => false) { '<p>Default content</p>'.html_safe }
17
20
 
18
21
  divider = content.index(Effective::Snippets::ReadMoreDivider::TOKEN)
19
22
  length = options.delete(:length)
@@ -21,7 +24,7 @@ module EffectivePostsHelper
21
24
  if divider.present?
22
25
  content[0...divider] + readmore_link(post, options)
23
26
  elsif length.present? && content.length > length
24
- truncate_html(content, length, '...', readmore_link(post, options))
27
+ truncate_html(content, length, readmore_link(post, options))
25
28
  else
26
29
  content
27
30
  end.html_safe
@@ -1,6 +1,3 @@
1
- # Modified from
2
- # http://blog.madebydna.com/all/code/2010/06/04/ruby-helper-to-cleanly-truncate-html.html
3
-
4
1
  module EffectiveTruncateHtmlHelper
5
2
  def chunk_html(text, max_length = 2, _ellipsis = '...', read_more = nil)
6
3
  doc = Nokogiri::HTML::DocumentFragment.parse text
@@ -13,65 +10,36 @@ module EffectiveTruncateHtmlHelper
13
10
  doc.inner_html.html_safe
14
11
  end
15
12
 
16
- def truncate_html(text, max_length = 200, ellipsis = '...', read_more = nil)
17
- ellipsis_length = ellipsis.to_s.length
18
- doc = Nokogiri::HTML::DocumentFragment.parse text
19
- content_length = doc.inner_text.length
20
- actual_length = max_length - ellipsis_length
21
-
22
- if content_length > actual_length
23
- truncated_node = doc.truncate_html(actual_length)
24
-
25
- last_node = truncated_node
26
- while last_node.respond_to?(:children) && last_node.children.present?
27
- last_node = last_node.children.reverse.find { |node| node.try(:name) != 'a' } # Find the last non-A node
13
+ def truncate_html(text, max = 200, read_more = '', ellipsis = '...')
14
+ doc = Nokogiri::HTML::DocumentFragment.parse(text)
15
+ length = doc.inner_text.length
16
+
17
+ while length > max
18
+ element = doc
19
+ element = element.last_element_child while element.last_element_child.present?
20
+ # element is now the last nested element (i.e. an HTML node, NOT a text node) in the HTML, or doc itself if doc has no elements (text node)
21
+
22
+ if (length - element.inner_text.length) > max # If deleting this entire node means we're still over max, do it
23
+ element.remove
24
+ else # If we truncate this node, we'll be under max
25
+ if element.name == 'a'
26
+ element.remove # I don't want to cut a link in half
27
+ elsif element.children.length == 1 # There must be a text node here. Can there be more than 1 text node?
28
+ textNode = element.children.first
29
+ textNode.content = truncate(textNode.content, length: (max - length), separator: ' ', omission: ellipsis)
30
+ break # Break out of our while loop, as our ellipsis might invalidate our looping condition
31
+ else # Unexpected, so just remove the whole thing
32
+ Rails.logger.info "effective_posts, Unexpected number of last_element_child children"
33
+ element.remove
34
+ end
28
35
  end
29
36
 
30
- if read_more.present?
31
- read_more_node = Nokogiri::HTML::DocumentFragment.parse(read_more.to_s)
32
- last_node.add_next_sibling(read_more_node)
33
- end
34
-
35
- if ellipsis.present?
36
- ellipsis_node = Nokogiri::XML::Text.new(ellipsis.to_s, doc)
37
- last_node.add_next_sibling(ellipsis_node)
38
- end
39
-
40
- truncated_node.inner_html
41
- else
42
- text.to_s
43
- end.html_safe
44
- end
45
- end
46
-
47
- module NokogiriTruncator
48
- module NodeWithChildren
49
- def truncate_html(max_length)
50
- return self if inner_text.length <= max_length
51
- truncated_node = dup
52
- truncated_node.children.remove
53
-
54
- children.each do |node|
55
- remaining_length = max_length - truncated_node.inner_text.length
56
- break if remaining_length <= 10
57
- truncated_node.add_child node.truncate_html(remaining_length)
58
- end
59
- truncated_node
37
+ length = doc.inner_text.length
60
38
  end
61
- end
62
-
63
- module TextNode
64
- include ActionView::Helpers::TextHelper
65
39
 
66
- def truncate_html(max_length)
67
- truncated = truncate(content, length: max_length, separator: ' ', omission: '')
40
+ # Clean up any empty tags
41
+ doc.last_element_child.remove while doc.last_element_child.try(:inner_html) == ''
68
42
 
69
- # Nokogiri::XML::Text.new(truncate(content.to_s, :length => (max_length-1)), parent)
70
- Nokogiri::XML::Text.new(truncated, parent)
71
- end
43
+ (doc.inner_html + read_more.to_s).html_safe
72
44
  end
73
45
  end
74
-
75
- Nokogiri::HTML::DocumentFragment.send(:include, NokogiriTruncator::NodeWithChildren)
76
- Nokogiri::XML::Element.send(:include, NokogiriTruncator::NodeWithChildren)
77
- Nokogiri::XML::Text.send(:include, NokogiriTruncator::TextNode)
@@ -1,3 +1,3 @@
1
1
  module EffectivePosts
2
- VERSION = '0.2.6'.freeze
2
+ VERSION = '0.3.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: effective_posts
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.6
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Code and Effect
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-16 00:00:00.000000000 Z
11
+ date: 2015-05-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails