html_truncator 0.4.1 → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +17 -0
- data/lib/html_truncator.rb +2 -2
- data/lib/html_truncator/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 59f26ce17968dfae9237e9bd066c1f2c5fa82591
|
4
|
+
data.tar.gz: f55ce8ae96a87392d408cb55ddc5d70590d782c3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2685ebd8e24a34c8a4885f63ed78f2715f557b0c3eb43f8c8fc8f987823a692571d0b10b01ae911af1cd11324359bc19742844ffc54f813bd3dc51eea2a18352
|
7
|
+
data.tar.gz: e7d8e184e074a7357dd80c819972b397e66adbfbba9d081939ef80cce06ec84a7eda138cb5ae3448ce7aeaf80cca760f6153d8501f6ae9191757b94165bec5f7
|
data/README.md
CHANGED
@@ -119,6 +119,23 @@ HTML_Truncator.truncate("<p>Lorem ipsum dolor sit amet.</p>", 3).html_truncated?
|
|
119
119
|
# => true
|
120
120
|
```
|
121
121
|
|
122
|
+
You can ignore images in the text by overriding the `self_closing_tags` attribute:
|
123
|
+
|
124
|
+
```ruby
|
125
|
+
HTML_Truncator.self_closing_tags.delete "img"
|
126
|
+
HTML_Truncator.truncate("<p>Lorem ipsum <img src='...'>dolor sit amet.</p>", 3)
|
127
|
+
# => "<p>Lorem ipsum dolor…</p>"
|
128
|
+
```
|
129
|
+
|
130
|
+
If you already have parsed an HTML document with Nokogiri, you can use it
|
131
|
+
directly to truncate:
|
132
|
+
|
133
|
+
```ruby
|
134
|
+
document = Nokogiri::HTML::DocumentFragment.parse(text)
|
135
|
+
# Doing something with this document
|
136
|
+
options = HTML_Truncator::DEFAULT_OPTIONS.merge(length_in_char: true)
|
137
|
+
document.truncate(12, options)
|
138
|
+
```
|
122
139
|
|
123
140
|
Alternatives
|
124
141
|
------------
|
data/lib/html_truncator.rb
CHANGED
@@ -75,7 +75,7 @@ class Nokogiri::XML::Text
|
|
75
75
|
if opts[:length_in_chars]
|
76
76
|
count = content.length
|
77
77
|
return [to_xhtml, count, opts] if count <= max && max > 0
|
78
|
-
words = content.scan(
|
78
|
+
words = content.scan(/[[:space:]]*[[:graph:]]+/)
|
79
79
|
if words.size > 1
|
80
80
|
words.inject('') do |string, word|
|
81
81
|
if string.length + word.length > max
|
@@ -90,7 +90,7 @@ class Nokogiri::XML::Text
|
|
90
90
|
txt.content = content.slice(0, max)
|
91
91
|
[txt.to_xhtml, count, opts]
|
92
92
|
else
|
93
|
-
words = to_xhtml.scan(
|
93
|
+
words = to_xhtml.scan(/[[:space:]]*[[:graph:]]+/)
|
94
94
|
count = words.length
|
95
95
|
return [to_xhtml, count, opts] if count <= max && max > 0
|
96
96
|
[words.slice(0, max).join, count, opts]
|
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.4.
|
4
|
+
version: 0.4.2
|
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: 2017-11-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nokogiri
|
@@ -69,7 +69,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
69
69
|
version: '0'
|
70
70
|
requirements: []
|
71
71
|
rubyforge_project:
|
72
|
-
rubygems_version: 2.
|
72
|
+
rubygems_version: 2.5.2
|
73
73
|
signing_key:
|
74
74
|
specification_version: 4
|
75
75
|
summary: Wants to truncate an HTML string properly? This gem is for you.
|