html_truncator 0.3.1 → 0.4.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: 8acdba13144c7c2f1d8ede94f1c21dc60ab6566c
4
- data.tar.gz: 1ce538702c8e24d2a0781f08657a61bbe2d0300d
3
+ metadata.gz: d49de35db932a0034c8a82e62f0c5d48a097c1f4
4
+ data.tar.gz: a070c038a14da39ea4603280fc427a102ecc2155
5
5
  SHA512:
6
- metadata.gz: 4011e5a3f94446bc9cddb5aa8af1afb5fc174aaa14250dfb076fcf802a3e40ef5f18b89d29f84c1ce0e69320a8d354d040517edb8a6973a5bf3a38735c10c887
7
- data.tar.gz: 5241bfa96596b4cd2785666b62ffa5311be99f097dfbd0fc4478e3926abe27f93cf69195b8773a8d4cf39cdee27ace15089d6ab5c22524fd003dcbb60ed658e3
6
+ metadata.gz: cf893133668e3f929f264209061d4861f0670e73756c75100f9cfa6b38a0ee584ae0d4914bd8d54a73aa40d8e9b26b2de953adda75576a05694337e74bb61a15
7
+ data.tar.gz: 4409e5eb01fe42cbebcb78ffc49e9d8753ea61989c991a285c9f0064874dfbd4865ba3a66fc95664d6cbb8b0c274e74cfd3cee0f21079d5dd4998f9c70c0d77c
data/README.md CHANGED
@@ -28,8 +28,14 @@ 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
  * some options like 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).
31
+ And 3 attributes:
32
+
33
+ * `ellipsable_tags`, which lists the tags that can contain the ellipsis
34
+ (by default: p ol ul li div header article nav section footer aside dd dt dl)
35
+ * `self_closing_tags`, with the tags to keep when empty
36
+ (by default: br hr img param embed)
37
+ * `punctuation_chars`, with the punctation characters to remove before the
38
+ ellipsis (by default: , . : ; ! ?).
33
39
 
34
40
 
35
41
  Examples
@@ -48,7 +54,13 @@ If the text is too short to be truncated, it won't be modified:
48
54
  If you prefer, you can have the length in characters instead of words:
49
55
 
50
56
  HTML_Truncator.truncate("<p>Lorem ipsum dolor sit amet.</p>", 12, :length_in_chars => true)
51
- # => "<p>Lorem ipsum …</p>"
57
+ # => "<p>Lorem ipsum…</p>"
58
+
59
+ It doesn't cut inside a word but goes back to the immediately preceding word
60
+ boundary:
61
+
62
+ HTML_Truncator.truncate("<p>Lorem ipsum dolor sit amet.</p>", 10, :length_in_chars => true)
63
+ # => "<p>Lorem…</p>"
52
64
 
53
65
  You can customize the ellipsis:
54
66
 
@@ -65,6 +77,11 @@ The ellipsis is put at the right place, inside `<p>`, but not `<i>`:
65
77
  HTML_Truncator.truncate("<p><i>Lorem ipsum dolor sit amet.</i></p>", 3)
66
78
  # => "<p><i>Lorem ipsum dolor</i>…</p>"
67
79
 
80
+ And the punctation just before the ellipsis is not kept:
81
+
82
+ HTML_Truncator.truncate("<p>Lorem ipsum: lorem ipsum dolor sit amet.</p>", 2)
83
+ # => "<p>Lorem ipsum…</p>"
84
+
68
85
  You can indicate that a tag can contain the ellipsis but adding it to the ellipsable_tags:
69
86
 
70
87
  HTML_Truncator.ellipsable_tags << "blockquote"
@@ -74,7 +91,7 @@ You can indicate that a tag can contain the ellipsis but adding it to the ellips
74
91
  You can know if a string was truncated with the `html_truncated?` method:
75
92
 
76
93
  HTML_Truncator.truncate("<p>Lorem ipsum dolor sit amet.</p>", 3).html_truncated?
77
- # => true
94
+ # => true
78
95
 
79
96
 
80
97
  Alternatives
@@ -112,6 +129,10 @@ Credits
112
129
  -------
113
130
 
114
131
  Thanks to François de Metz for his awesome help!
115
- Thanks to [kuroir](https://github.com/kuroir) and [benhutton](https://github.com/benhutton) for their suggestions.
132
+ Thanks to [kuroir](https://github.com/kuroir) and
133
+ [benhutton](https://github.com/benhutton) for their suggestions.
134
+
135
+ The code is released under the MIT license.
136
+ See the MIT-LICENSE file for the full license.
116
137
 
117
- Copyright (c) 2011 Bruno Michel <bmichel@menfin.info>, released under the MIT license
138
+ ♡2011 by Bruno Michel. Copying is an act of love. Please copy and share.
@@ -18,10 +18,11 @@ class HTML_Truncator
18
18
  end
19
19
 
20
20
  class <<self
21
- attr_accessor :ellipsable_tags, :self_closing_tags
21
+ attr_accessor :ellipsable_tags, :self_closing_tags, :punctuation_chars
22
22
  end
23
23
  self.ellipsable_tags = Set.new(%w(p ol ul li div header article nav section footer aside dd dt dl))
24
24
  self.self_closing_tags = Set.new(%w(br hr img param embed))
25
+ self.punctuation_chars = %w(, . : ; ! ?)
25
26
  end
26
27
 
27
28
  class Nokogiri::HTML::DocumentFragment
@@ -50,7 +51,8 @@ class Nokogiri::XML::Node
50
51
  inner += txt
51
52
  next if remaining >= 0
52
53
  if ellipsable?
53
- inner = inner.rstrip + opts[:ellipsis]
54
+ r = %r/[\s#{HTML_Truncator.punctuation_chars.join}]+$/
55
+ inner = inner.sub(r, '') + opts[:ellipsis]
54
56
  opts[:ellipsis] = ""
55
57
  opts[:was_truncated] = true
56
58
  end
@@ -1,3 +1,3 @@
1
1
  class HTML_Truncator
2
- VERSION = "0.3.1"
2
+ VERSION = "0.4.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: html_truncator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bruno Michel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-21 00:00:00.000000000 Z
11
+ date: 2014-03-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri