html_truncator 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +4 -4
- data/lib/html_truncator.rb +20 -13
- metadata +3 -3
data/README.md
CHANGED
@@ -63,10 +63,10 @@ Rails has a `truncate` helper, but as the doc says:
|
|
63
63
|
|
64
64
|
I know there are some Ruby code to truncate HTML, like:
|
65
65
|
|
66
|
-
* https://github.com/hgimenez/truncate_html
|
67
|
-
* https://gist.github.com/101410
|
68
|
-
* http://henrik.nyh.se/2008/01/rails-truncate-html-helper
|
69
|
-
* http://blog.madebydna.com/all/code/2010/06/04/ruby-helper-to-cleanly-truncate-html.html
|
66
|
+
* [https://github.com/hgimenez/truncate_html](https://github.com/hgimenez/truncate_html)
|
67
|
+
* [https://gist.github.com/101410](https://gist.github.com/101410)
|
68
|
+
* [http://henrik.nyh.se/2008/01/rails-truncate-html-helper](http://henrik.nyh.se/2008/01/rails-truncate-html-helper)
|
69
|
+
* [http://blog.madebydna.com/all/code/2010/06/04/ruby-helper-to-cleanly-truncate-html.html](http://blog.madebydna.com/all/code/2010/06/04/ruby-helper-to-cleanly-truncate-html.html)
|
70
70
|
|
71
71
|
But I'm not pleased with these solutions: they are either based on regexp for
|
72
72
|
parsing the content (too fragile), they don't put the ellipsis where expected,
|
data/lib/html_truncator.rb
CHANGED
@@ -4,38 +4,46 @@ require "nokogiri"
|
|
4
4
|
class HTML_Truncator
|
5
5
|
def self.truncate(text, max_words, ellipsis="...")
|
6
6
|
doc = Nokogiri::HTML::DocumentFragment.parse(text)
|
7
|
-
doc.truncate(max_words, ellipsis)
|
7
|
+
doc.truncate(max_words, ellipsis).first
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
11
|
|
12
12
|
class Nokogiri::HTML::DocumentFragment
|
13
|
-
def
|
14
|
-
|
13
|
+
def ellipsable?
|
14
|
+
true
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
18
|
class Nokogiri::XML::Node
|
19
19
|
def truncate(max_words, ellipsis)
|
20
|
-
|
20
|
+
return ["", 1, ellipsis] if max_words == 0 && !ellipsable?
|
21
|
+
inner, remaining, ellipsis = inner_truncate(max_words, ellipsis)
|
21
22
|
children.remove
|
22
23
|
add_child Nokogiri::HTML::DocumentFragment.parse(inner)
|
23
|
-
[to_xml, max_words - remaining]
|
24
|
+
[to_xml(:indent => 0), max_words - remaining, ellipsis]
|
24
25
|
end
|
25
26
|
|
26
27
|
def inner_truncate(max_words, ellipsis)
|
27
28
|
inner, remaining = "", max_words
|
28
29
|
self.children.each do |node|
|
29
|
-
txt, nb = node.truncate(remaining, ellipsis)
|
30
|
+
txt, nb, ellipsis = node.truncate(remaining, ellipsis)
|
30
31
|
remaining -= nb
|
32
|
+
if remaining < 0
|
33
|
+
inner += txt
|
34
|
+
if ellipsable?
|
35
|
+
inner += ellipsis
|
36
|
+
ellipsis = ""
|
37
|
+
end
|
38
|
+
break
|
39
|
+
end
|
31
40
|
inner += txt
|
32
|
-
break if remaining < 0
|
33
41
|
end
|
34
|
-
[inner, remaining]
|
42
|
+
[inner, remaining, ellipsis]
|
35
43
|
end
|
36
44
|
|
37
|
-
def
|
38
|
-
|
45
|
+
def ellipsable?
|
46
|
+
%w(p ol ul li div header article nav section footer aside dd dt dl).include? name
|
39
47
|
end
|
40
48
|
end
|
41
49
|
|
@@ -43,8 +51,7 @@ class Nokogiri::XML::Text
|
|
43
51
|
def truncate(max_words, ellipsis)
|
44
52
|
words = content.split
|
45
53
|
nb_words = words.length
|
46
|
-
return [to_xhtml, nb_words] if nb_words <= max_words
|
47
|
-
|
48
|
-
[words.slice(0, max_words).join(' ') + ellipsis, nb_words]
|
54
|
+
return [to_xhtml, nb_words, ellipsis] if nb_words <= max_words && max_words > 0
|
55
|
+
[words.slice(0, max_words).join(' '), nb_words, ellipsis]
|
49
56
|
end
|
50
57
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 1
|
9
|
+
version: 0.1.1
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Bruno Michel
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-01-
|
17
|
+
date: 2011-01-12 00:00:00 +01:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|