html_truncator 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +1 -1
- data/init.rb +2 -0
- data/lib/html_truncator.rb +19 -13
- metadata +60 -33
data/README.md
CHANGED
data/init.rb
ADDED
data/lib/html_truncator.rb
CHANGED
@@ -22,7 +22,6 @@ class HTML_Truncator
|
|
22
22
|
self.ellipsable_tags = Set.new(%w(p ol ul li div header article nav section footer aside dd dt dl))
|
23
23
|
end
|
24
24
|
|
25
|
-
|
26
25
|
class Nokogiri::HTML::DocumentFragment
|
27
26
|
def ellipsable?
|
28
27
|
true
|
@@ -33,6 +32,7 @@ class Nokogiri::XML::Node
|
|
33
32
|
def truncate(max, opts)
|
34
33
|
return ["", 1, opts] if max == 0 && !ellipsable?
|
35
34
|
inner, remaining, opts = inner_truncate(max, opts)
|
35
|
+
return ["", max - remaining, opts] if inner.empty?
|
36
36
|
children.remove
|
37
37
|
add_child Nokogiri::HTML::DocumentFragment.parse(inner)
|
38
38
|
[to_html(:indent => 0), max - remaining, opts]
|
@@ -46,7 +46,7 @@ class Nokogiri::XML::Node
|
|
46
46
|
inner += txt
|
47
47
|
next if remaining >= 0
|
48
48
|
if ellipsable?
|
49
|
-
inner
|
49
|
+
inner = inner.rstrip + opts[:ellipsis]
|
50
50
|
opts[:ellipsis] = ""
|
51
51
|
opts[:was_truncated] = true
|
52
52
|
end
|
@@ -61,16 +61,22 @@ class Nokogiri::XML::Node
|
|
61
61
|
end
|
62
62
|
|
63
63
|
class Nokogiri::XML::Text
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
64
|
+
def truncate(max, opts)
|
65
|
+
words = to_xhtml.scan(/\s*\S+/)
|
66
|
+
if opts[:length_in_chars]
|
67
|
+
count = to_xhtml.length
|
68
|
+
return [to_xhtml, count, opts] if count <= max && max > 0
|
69
|
+
if words.size > 1
|
70
|
+
words.inject('') do |string, word|
|
71
|
+
return [string, count, opts] if string.length + word.length > max
|
72
|
+
string + word
|
73
|
+
end
|
74
|
+
end
|
75
|
+
[content.slice(0, max), count, opts]
|
76
|
+
else
|
77
|
+
count = words.length
|
78
|
+
return [to_xhtml, count, opts] if count <= max && max > 0
|
79
|
+
[words.slice(0, max).join, count, opts]
|
80
|
+
end
|
75
81
|
end
|
76
82
|
end
|
metadata
CHANGED
@@ -1,73 +1,100 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: html_truncator
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 19
|
5
5
|
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 2
|
9
|
+
- 2
|
10
|
+
version: 0.2.2
|
6
11
|
platform: ruby
|
7
|
-
authors:
|
12
|
+
authors:
|
8
13
|
- Bruno Michel
|
9
14
|
autorequire:
|
10
15
|
bindir: bin
|
11
16
|
cert_chain: []
|
12
|
-
|
17
|
+
|
18
|
+
date: 2011-04-19 00:00:00 +02:00
|
13
19
|
default_executable:
|
14
|
-
dependencies:
|
15
|
-
- !ruby/object:Gem::Dependency
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
16
22
|
name: nokogiri
|
17
|
-
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
18
25
|
none: false
|
19
|
-
requirements:
|
26
|
+
requirements:
|
20
27
|
- - ~>
|
21
|
-
- !ruby/object:Gem::Version
|
22
|
-
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 7
|
30
|
+
segments:
|
31
|
+
- 1
|
32
|
+
- 4
|
33
|
+
version: "1.4"
|
23
34
|
type: :runtime
|
24
|
-
|
25
|
-
|
26
|
-
- !ruby/object:Gem::Dependency
|
35
|
+
version_requirements: *id001
|
36
|
+
- !ruby/object:Gem::Dependency
|
27
37
|
name: rspec
|
28
|
-
|
38
|
+
prerelease: false
|
39
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
29
40
|
none: false
|
30
|
-
requirements:
|
41
|
+
requirements:
|
31
42
|
- - ~>
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
hash: 11
|
45
|
+
segments:
|
46
|
+
- 2
|
47
|
+
- 4
|
48
|
+
version: "2.4"
|
34
49
|
type: :development
|
35
|
-
|
36
|
-
version_requirements: *79281290
|
50
|
+
version_requirements: *id002
|
37
51
|
description: Wants to truncate an HTML string properly? This gem is for you.
|
38
52
|
email: bmichel@menfin.info
|
39
53
|
executables: []
|
54
|
+
|
40
55
|
extensions: []
|
41
|
-
|
56
|
+
|
57
|
+
extra_rdoc_files:
|
42
58
|
- README.md
|
43
|
-
files:
|
59
|
+
files:
|
44
60
|
- MIT-LICENSE
|
45
61
|
- README.md
|
46
62
|
- Gemfile
|
47
63
|
- lib/html_truncator.rb
|
64
|
+
- init.rb
|
48
65
|
has_rdoc: true
|
49
66
|
homepage: http://github.com/nono/HTML-Truncator
|
50
67
|
licenses: []
|
68
|
+
|
51
69
|
post_install_message:
|
52
70
|
rdoc_options: []
|
53
|
-
|
71
|
+
|
72
|
+
require_paths:
|
54
73
|
- lib
|
55
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
74
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
75
|
none: false
|
57
|
-
requirements:
|
58
|
-
- -
|
59
|
-
- !ruby/object:Gem::Version
|
60
|
-
|
61
|
-
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
hash: 3
|
80
|
+
segments:
|
81
|
+
- 0
|
82
|
+
version: "0"
|
83
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
84
|
none: false
|
63
|
-
requirements:
|
64
|
-
- -
|
65
|
-
- !ruby/object:Gem::Version
|
66
|
-
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
hash: 3
|
89
|
+
segments:
|
90
|
+
- 0
|
91
|
+
version: "0"
|
67
92
|
requirements: []
|
93
|
+
|
68
94
|
rubyforge_project:
|
69
95
|
rubygems_version: 1.5.2
|
70
96
|
signing_key:
|
71
97
|
specification_version: 3
|
72
98
|
summary: Wants to truncate an HTML string properly? This gem is for you.
|
73
99
|
test_files: []
|
100
|
+
|