html_truncator 0.1.1 → 0.1.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.
- data/README.md +22 -3
- data/lib/html_truncator.rb +13 -9
- metadata +3 -3
data/README.md
CHANGED
@@ -28,6 +28,9 @@ The HTML_Truncator class has only one method, `truncate`, with 3 arguments:
|
|
28
28
|
* the number of words to keep (real words, tags and attributes aren't count)
|
29
29
|
* the ellipsis (optional, '...' by default).
|
30
30
|
|
31
|
+
And an attribute, `ellipsable_tags`, which lists the tags that can contain the ellipsis
|
32
|
+
(by default: p ol ul li div header article nav section footer aside dd dt dl).
|
33
|
+
|
31
34
|
|
32
35
|
Examples
|
33
36
|
--------
|
@@ -40,17 +43,28 @@ A simple example:
|
|
40
43
|
If the text is too short to be truncated, it won't be modified:
|
41
44
|
|
42
45
|
HTML_Truncator.truncate("<p>Lorem ipsum dolor sit amet.</p>", 5)
|
43
|
-
|
46
|
+
# => "<p>Lorem ipsum dolor sit amet.</p>"
|
44
47
|
|
45
48
|
You can customize the ellipsis:
|
46
49
|
|
47
50
|
HTML_Truncator.truncate("<p>Lorem ipsum dolor sit amet.</p>", 3, " (truncated)")
|
48
|
-
|
51
|
+
# => "<p>Lorem ipsum dolor (truncated)</p>"
|
49
52
|
|
50
53
|
And even have HTML in the ellipsis:
|
51
54
|
|
52
55
|
HTML_Truncator.truncate("<p>Lorem ipsum dolor sit amet.</p>", 3, '<a href="/more-to-read">...</a>')
|
53
|
-
|
56
|
+
# => "<p>Lorem ipsum dolor<a href="/more-to-read">...</a></p>"
|
57
|
+
|
58
|
+
The ellipsis is put at the right place, inside `<p>`, but not `<i>`:
|
59
|
+
|
60
|
+
HTML_Truncator.truncate("<p><i>Lorem ipsum dolor sit amet.</i></p>", 3)
|
61
|
+
# => "<p><i>Lorem ipsum dolor</i>...</p>"
|
62
|
+
|
63
|
+
You can indicate that a tag can contain the ellipsis but adding it to the ellipsable_tags:
|
64
|
+
|
65
|
+
HTML_Truncator.ellipsable_tags << "blockquote"
|
66
|
+
HTML_Truncator.truncate("<blockquote>Lorem ipsum dolor sit amet.</blockquote>", 3)
|
67
|
+
# => "<blockquote>Lorem ipsum dolor...</blockquote>"
|
54
68
|
|
55
69
|
|
56
70
|
Alternatives
|
@@ -84,4 +98,9 @@ If you wants to make a pull request, please check the specs before:
|
|
84
98
|
rspec spec
|
85
99
|
|
86
100
|
|
101
|
+
Credits
|
102
|
+
-------
|
103
|
+
|
104
|
+
Thanks to François de Metz for his awesome help!
|
105
|
+
|
87
106
|
Copyright (c) 2011 Bruno Michel <bmichel@menfin.info>, released under the MIT license
|
data/lib/html_truncator.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require "nokogiri"
|
2
|
+
require "set"
|
2
3
|
|
3
4
|
|
4
5
|
class HTML_Truncator
|
@@ -6,6 +7,11 @@ class HTML_Truncator
|
|
6
7
|
doc = Nokogiri::HTML::DocumentFragment.parse(text)
|
7
8
|
doc.truncate(max_words, ellipsis).first
|
8
9
|
end
|
10
|
+
|
11
|
+
class <<self
|
12
|
+
attr_accessor :ellipsable_tags
|
13
|
+
end
|
14
|
+
self.ellipsable_tags = Set.new(%w(p ol ul li div header article nav section footer aside dd dt dl))
|
9
15
|
end
|
10
16
|
|
11
17
|
|
@@ -29,21 +35,19 @@ class Nokogiri::XML::Node
|
|
29
35
|
self.children.each do |node|
|
30
36
|
txt, nb, ellipsis = node.truncate(remaining, ellipsis)
|
31
37
|
remaining -= nb
|
32
|
-
if remaining < 0
|
33
|
-
inner += txt
|
34
|
-
if ellipsable?
|
35
|
-
inner += ellipsis
|
36
|
-
ellipsis = ""
|
37
|
-
end
|
38
|
-
break
|
39
|
-
end
|
40
38
|
inner += txt
|
39
|
+
next if remaining >= 0
|
40
|
+
if ellipsable?
|
41
|
+
inner += ellipsis
|
42
|
+
ellipsis = ""
|
43
|
+
end
|
44
|
+
break
|
41
45
|
end
|
42
46
|
[inner, remaining, ellipsis]
|
43
47
|
end
|
44
48
|
|
45
49
|
def ellipsable?
|
46
|
-
|
50
|
+
HTML_Truncator.ellipsable_tags.include? name
|
47
51
|
end
|
48
52
|
end
|
49
53
|
|
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
|
+
- 2
|
9
|
+
version: 0.1.2
|
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-14 00:00:00 +01:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|