html_truncator 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +52 -26
- data/lib/html_truncator.rb +11 -4
- data/lib/html_truncator/version.rb +1 -1
- metadata +13 -14
- data/init.rb +0 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c5d32333126a13d95485d8099cc9b0c428888524
|
4
|
+
data.tar.gz: 10bf2180590da61247191344cb28f030f0da0f8b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6d53be5769d4a135570d97163cb362c307a7933ce8e456beafd7f1536a8f60a667fff3c130a4cd6fcfe9b7966e80af7c12effc1fbb1539181656dfae2e08395f
|
7
|
+
data.tar.gz: 184db5d95e09c142c11ea64687c062ca654489f15a896c125caec0f4357034fc2f63531ebe4bf67e43b7e37f725173fe5629f22c1f6e244fc2923cccad0d742a
|
data/README.md
CHANGED
@@ -10,17 +10,23 @@ How to use it
|
|
10
10
|
|
11
11
|
It's very simple. Install it with rubygems:
|
12
12
|
|
13
|
-
|
13
|
+
```
|
14
|
+
gem install html_truncator
|
15
|
+
```
|
14
16
|
|
15
17
|
Or, if you use bundler, add it to your `Gemfile`:
|
16
18
|
|
17
|
-
|
19
|
+
```ruby
|
20
|
+
gem "html_truncator", "~>0.2"
|
21
|
+
```
|
18
22
|
|
19
23
|
Then you can use it in your code:
|
20
24
|
|
21
|
-
|
22
|
-
|
23
|
-
|
25
|
+
```ruby
|
26
|
+
require "html_truncator"
|
27
|
+
HTML_Truncator.truncate("<p>Lorem ipsum dolor sit amet.</p>", 3)
|
28
|
+
# => "<p>Lorem ipsum dolor…</p>"
|
29
|
+
```
|
24
30
|
|
25
31
|
The HTML_Truncator class has only one method, `truncate`, with 3 arguments:
|
26
32
|
|
@@ -43,55 +49,75 @@ Examples
|
|
43
49
|
|
44
50
|
A simple example:
|
45
51
|
|
46
|
-
|
47
|
-
|
52
|
+
```ruby
|
53
|
+
HTML_Truncator.truncate("<p>Lorem ipsum dolor sit amet.</p>", 3)
|
54
|
+
# => "<p>Lorem ipsum dolor…</p>"
|
55
|
+
```
|
48
56
|
|
49
57
|
If the text is too short to be truncated, it won't be modified:
|
50
58
|
|
51
|
-
|
52
|
-
|
59
|
+
```ruby
|
60
|
+
HTML_Truncator.truncate("<p>Lorem ipsum dolor sit amet.</p>", 5)
|
61
|
+
# => "<p>Lorem ipsum dolor sit amet.</p>"
|
62
|
+
```
|
53
63
|
|
54
64
|
If you prefer, you can have the length in characters instead of words:
|
55
65
|
|
56
|
-
|
57
|
-
|
66
|
+
```ruby
|
67
|
+
HTML_Truncator.truncate("<p>Lorem ipsum dolor sit amet.</p>", 12, :length_in_chars => true)
|
68
|
+
# => "<p>Lorem ipsum…</p>"
|
69
|
+
```
|
58
70
|
|
59
71
|
It doesn't cut inside a word but goes back to the immediately preceding word
|
60
72
|
boundary:
|
61
73
|
|
62
|
-
|
63
|
-
|
74
|
+
```ruby
|
75
|
+
HTML_Truncator.truncate("<p>Lorem ipsum dolor sit amet.</p>", 10, :length_in_chars => true)
|
76
|
+
# => "<p>Lorem…</p>"
|
77
|
+
```
|
64
78
|
|
65
79
|
You can customize the ellipsis:
|
66
80
|
|
67
|
-
|
68
|
-
|
81
|
+
```ruby
|
82
|
+
HTML_Truncator.truncate("<p>Lorem ipsum dolor sit amet.</p>", 3, :ellipsis => " (truncated)")
|
83
|
+
# => "<p>Lorem ipsum dolor (truncated)</p>"
|
84
|
+
```
|
69
85
|
|
70
86
|
And even have HTML in the ellipsis:
|
71
87
|
|
72
|
-
|
73
|
-
|
88
|
+
```ruby
|
89
|
+
HTML_Truncator.truncate("<p>Lorem ipsum dolor sit amet.</p>", 3, :ellipsis => '<a href="/more-to-read">...</a>')
|
90
|
+
# => "<p>Lorem ipsum dolor<a href="/more-to-read">...</a></p>"
|
91
|
+
```
|
74
92
|
|
75
93
|
The ellipsis is put at the right place, inside `<p>`, but not `<i>`:
|
76
94
|
|
77
|
-
|
78
|
-
|
95
|
+
```ruby
|
96
|
+
HTML_Truncator.truncate("<p><i>Lorem ipsum dolor sit amet.</i></p>", 3)
|
97
|
+
# => "<p><i>Lorem ipsum dolor</i>…</p>"
|
98
|
+
```
|
79
99
|
|
80
100
|
And the punctation just before the ellipsis is not kept:
|
81
101
|
|
82
|
-
|
83
|
-
|
102
|
+
```ruby
|
103
|
+
HTML_Truncator.truncate("<p>Lorem ipsum: lorem ipsum dolor sit amet.</p>", 2)
|
104
|
+
# => "<p>Lorem ipsum…</p>"
|
105
|
+
```
|
84
106
|
|
85
107
|
You can indicate that a tag can contain the ellipsis but adding it to the ellipsable_tags:
|
86
108
|
|
87
|
-
|
88
|
-
|
89
|
-
|
109
|
+
```ruby
|
110
|
+
HTML_Truncator.ellipsable_tags << "blockquote"
|
111
|
+
HTML_Truncator.truncate("<blockquote>Lorem ipsum dolor sit amet.</blockquote>", 3)
|
112
|
+
# => "<blockquote>Lorem ipsum dolor…</blockquote>"
|
113
|
+
```
|
90
114
|
|
91
115
|
You can know if a string was truncated with the `html_truncated?` method:
|
92
116
|
|
93
|
-
|
94
|
-
|
117
|
+
```ruby
|
118
|
+
HTML_Truncator.truncate("<p>Lorem ipsum dolor sit amet.</p>", 3).html_truncated?
|
119
|
+
# => true
|
120
|
+
```
|
95
121
|
|
96
122
|
|
97
123
|
Alternatives
|
data/lib/html_truncator.rb
CHANGED
@@ -72,18 +72,25 @@ end
|
|
72
72
|
|
73
73
|
class Nokogiri::XML::Text
|
74
74
|
def truncate(max, opts)
|
75
|
-
words = to_xhtml.scan(/\s*\S+/)
|
76
75
|
if opts[:length_in_chars]
|
77
|
-
count =
|
76
|
+
count = content.length
|
78
77
|
return [to_xhtml, count, opts] if count <= max && max > 0
|
78
|
+
words = content.scan(/\s*\S+/)
|
79
79
|
if words.size > 1
|
80
80
|
words.inject('') do |string, word|
|
81
|
-
|
81
|
+
if string.length + word.length > max
|
82
|
+
txt = dup
|
83
|
+
txt.content = string
|
84
|
+
return [txt.to_xhtml, count, opts]
|
85
|
+
end
|
82
86
|
string + word
|
83
87
|
end
|
84
88
|
end
|
85
|
-
|
89
|
+
txt = dup
|
90
|
+
txt.content = content.slice(0, max)
|
91
|
+
[txt.to_xhtml, count, opts]
|
86
92
|
else
|
93
|
+
words = to_xhtml.scan(/\s*\S+/)
|
87
94
|
count = words.length
|
88
95
|
return [to_xhtml, count, opts] if count <= max && max > 0
|
89
96
|
[words.slice(0, max).join, count, opts]
|
metadata
CHANGED
@@ -1,43 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: html_truncator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bruno Michel
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-03-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nokogiri
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - ~>
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '1.5'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - ~>
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.5'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rspec
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - ~>
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '3.0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - ~>
|
38
|
+
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '3.0'
|
41
41
|
description: Wants to truncate an HTML string properly? This gem is for you.
|
42
42
|
email: bmichel@menfin.info
|
43
43
|
executables: []
|
@@ -45,12 +45,11 @@ extensions: []
|
|
45
45
|
extra_rdoc_files:
|
46
46
|
- README.md
|
47
47
|
files:
|
48
|
+
- Gemfile
|
48
49
|
- MIT-LICENSE
|
49
50
|
- README.md
|
50
|
-
- Gemfile
|
51
|
-
- lib/html_truncator/version.rb
|
52
51
|
- lib/html_truncator.rb
|
53
|
-
-
|
52
|
+
- lib/html_truncator/version.rb
|
54
53
|
homepage: http://github.com/nono/HTML-Truncator
|
55
54
|
licenses: []
|
56
55
|
metadata: {}
|
@@ -60,17 +59,17 @@ require_paths:
|
|
60
59
|
- lib
|
61
60
|
required_ruby_version: !ruby/object:Gem::Requirement
|
62
61
|
requirements:
|
63
|
-
- -
|
62
|
+
- - ">="
|
64
63
|
- !ruby/object:Gem::Version
|
65
64
|
version: '0'
|
66
65
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
66
|
requirements:
|
68
|
-
- -
|
67
|
+
- - ">="
|
69
68
|
- !ruby/object:Gem::Version
|
70
69
|
version: '0'
|
71
70
|
requirements: []
|
72
71
|
rubyforge_project:
|
73
|
-
rubygems_version: 2.
|
72
|
+
rubygems_version: 2.4.5
|
74
73
|
signing_key:
|
75
74
|
specification_version: 4
|
76
75
|
summary: Wants to truncate an HTML string properly? This gem is for you.
|
data/init.rb
DELETED