effective_posts 0.4.1 → 0.4.2
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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 798c18ece7296255c241ef3dc6a0be72d6c12286
|
4
|
+
data.tar.gz: 5fa7275c8540538729866b5c7bb86310ec7b458f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d16f3d4c437c38f8f04769fc12b5ce22cead35f4a5a42fe08f03000f92472b72bae71e5d5f79d0fd104b0394ace27debc1e7983d6fccddb994ba809038132275
|
7
|
+
data.tar.gz: b8e671fee265b638f529b1e888efd14ead13e3282012fb119998ca5f6795cb3710d7a85d235409726b0580c9249b3fc19e1adcb128bc2a8eb2d31a3101604a2b
|
@@ -31,7 +31,7 @@ module EffectivePostsHelper
|
|
31
31
|
omission = options.delete(:omission)
|
32
32
|
|
33
33
|
if divider.present?
|
34
|
-
content
|
34
|
+
truncate_html(content, Effective::Snippets::ReadMoreDivider::TOKEN, '') + readmore_link(post, options)
|
35
35
|
elsif length.present?
|
36
36
|
truncate_html(content, length, omission) + readmore_link(post, options)
|
37
37
|
else
|
@@ -1,24 +1,47 @@
|
|
1
1
|
module EffectiveTruncateHtmlHelper
|
2
|
-
def chunk_html(text, max_length = 2, _ellipsis = '...', read_more = nil)
|
3
|
-
doc = Nokogiri::HTML::DocumentFragment.parse text
|
4
|
-
|
5
|
-
if doc.children.length >= max_length
|
6
|
-
doc.children.last.remove while doc.children.length > max_length
|
7
|
-
doc.children.last.add_next_sibling Nokogiri::HTML::DocumentFragment.parse("<p>#{ read_more }</p>")
|
8
|
-
end
|
9
|
-
|
10
|
-
doc.inner_html.html_safe
|
11
|
-
end
|
12
|
-
|
13
2
|
# Truncates HTML or text to a certain inner_text character limit.
|
14
3
|
#
|
15
4
|
# If given HTML, the underlying markup may be much longer than length, but the displayed text
|
16
5
|
# will be no longer than (length + omission) characters.
|
17
|
-
def truncate_html(text,
|
18
|
-
Nokogiri::HTML::DocumentFragment.parse(text)
|
6
|
+
def truncate_html(text, length_or_content = 200, omission = '...')
|
7
|
+
doc = Nokogiri::HTML::DocumentFragment.parse(text)
|
8
|
+
|
9
|
+
if length_or_content.kind_of?(String)
|
10
|
+
content = (Nokogiri::HTML::DocumentFragment.parse(length_or_content).children.first.inner_text rescue length_or_content)
|
11
|
+
doc.tap { |doc| _truncate_node_to_content(doc, content, omission) }.inner_html
|
12
|
+
elsif length_or_content.kind_of?(Integer)
|
13
|
+
doc.tap { |doc| _truncate_node_to_length(doc, length_or_content, omission) }.inner_html
|
14
|
+
else
|
15
|
+
raise 'Unsupported datatype passed to second argument of truncate_html. Expecting integer or string.'
|
16
|
+
end
|
19
17
|
end
|
20
18
|
|
21
|
-
def
|
19
|
+
def _truncate_node_to_content(node, content, omission, seen = false)
|
20
|
+
if seen == true
|
21
|
+
node.remove
|
22
|
+
elsif node.children.blank?
|
23
|
+
index = node.content.index(content)
|
24
|
+
|
25
|
+
if index.present?
|
26
|
+
if node.parent.try(:content) == content # If my parent node just has my text in it, remove parent node too
|
27
|
+
node.parent.remove
|
28
|
+
elsif index == 0
|
29
|
+
node.remove
|
30
|
+
else
|
31
|
+
node.content = truncate(node.content, length: index+omission.to_s.length, separator: ' ', omission: omission)
|
32
|
+
end
|
33
|
+
|
34
|
+
seen = true
|
35
|
+
end
|
36
|
+
else
|
37
|
+
node.children.each { |child| seen = _truncate_node_to_content(child, content, omission, seen) }
|
38
|
+
end
|
39
|
+
|
40
|
+
seen
|
41
|
+
end
|
42
|
+
|
43
|
+
|
44
|
+
def _truncate_node_to_length(node, length, omission)
|
22
45
|
if node.inner_text.length <= length
|
23
46
|
# Do nothing, we're already reached base case
|
24
47
|
elsif node.name == 'a'
|
@@ -26,13 +49,13 @@ module EffectiveTruncateHtmlHelper
|
|
26
49
|
elsif node.children.blank?
|
27
50
|
# I need to truncate myself, and I'm certainly a text node
|
28
51
|
if node.text?
|
29
|
-
node.content = truncate(node.content, length: length, separator: ' ', omission: omission)
|
52
|
+
node.content = truncate(node.content, length: length+omission.to_s.length, separator: ' ', omission: omission)
|
30
53
|
else
|
31
|
-
Rails.logger.info '[WARNING] effective_posts: unexpected node in children.blank? recursive condition'
|
32
54
|
node.remove
|
33
55
|
end
|
34
56
|
else # Go through all the children, and delete anything after the length has been reached
|
35
57
|
child_length = 0
|
58
|
+
|
36
59
|
node.children.each do |child|
|
37
60
|
child_length > length ? (child.remove) : (child_length += child.inner_text.length)
|
38
61
|
end
|
@@ -44,7 +67,7 @@ module EffectiveTruncateHtmlHelper
|
|
44
67
|
child = node.children.last
|
45
68
|
child_max_length = length - (child_length - child.inner_text.length)
|
46
69
|
|
47
|
-
|
70
|
+
_truncate_node_to_length(child, child_max_length, omission)
|
48
71
|
end
|
49
72
|
end
|
50
73
|
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.4.
|
4
|
+
version: 0.4.2
|
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-07-
|
11
|
+
date: 2015-07-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|