truncato 0.7.3 → 0.7.4

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -13,7 +13,7 @@ gem 'nokogiri', "~> 1.5.5"
13
13
  gem 'htmlentities', "~> 4.3.1"
14
14
 
15
15
  group :development do
16
- gem "bundler", "~> 1.2.1"
16
+ gem "bundler", "~> 1.3"
17
17
  gem "jeweler", "~> 1.8.4"
18
18
  end
19
19
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.7.3
1
+ 0.7.4
@@ -11,7 +11,7 @@ class TruncatedSaxDocument < Nokogiri::XML::SAX::Document
11
11
  end
12
12
 
13
13
  def start_element name, attributes
14
- return if @max_length_reached
14
+ return if @max_length_reached || artificial_root_name?(name)
15
15
  @closing_tags.push name
16
16
  append_to_truncated_string opening_tag(name, attributes), overriden_tag_length
17
17
  end
@@ -24,7 +24,7 @@ class TruncatedSaxDocument < Nokogiri::XML::SAX::Document
24
24
  end
25
25
 
26
26
  def end_element name
27
- return if @max_length_reached
27
+ return if @max_length_reached || artificial_root_name?(name)
28
28
  @closing_tags.pop
29
29
  append_to_truncated_string closing_tag(name), overriden_tag_length
30
30
  end
@@ -103,4 +103,8 @@ class TruncatedSaxDocument < Nokogiri::XML::SAX::Document
103
103
  def overriden_tag_length
104
104
  @count_tags ? nil : 0
105
105
  end
106
+
107
+ def artificial_root_name?(name)
108
+ name == Truncato::ARTIFICIAL_ROOT_NAME
109
+ end
106
110
  end
@@ -6,6 +6,8 @@ module Truncato
6
6
  filtered_attributes: []
7
7
  }
8
8
 
9
+ ARTIFICIAL_ROOT_NAME = '__truncato_root__'
10
+
9
11
  # Truncates the source XML string and returns the truncated XML. It will keep a valid XML structure
10
12
  # and insert a _tail_ text indicating the position where content were removed (...).
11
13
  #
@@ -24,6 +26,10 @@ module Truncato
24
26
  private
25
27
 
26
28
  def self.truncate_html source, options
29
+ self.do_truncate_html(source, options) ? self.do_truncate_html(with_articial_root(source), options) : nil
30
+ end
31
+
32
+ def self.do_truncate_html source, options
27
33
  truncated_sax_document = TruncatedSaxDocument.new(options)
28
34
  parser = Nokogiri::XML::SAX::Parser.new(truncated_sax_document)
29
35
  parser.parse(source) { |context| context.replace_entities = false }
@@ -31,9 +37,13 @@ module Truncato
31
37
  truncated_string.empty? ? nil : truncated_string
32
38
  end
33
39
 
40
+ def self.with_articial_root(source)
41
+ "<#{ARTIFICIAL_ROOT_NAME}>#{source}</#{ARTIFICIAL_ROOT_NAME}>"
42
+ end
43
+
34
44
  def self.truncate_no_html source, options
35
45
  max_length = options[:max_length]
36
46
  tail = source.length > max_length ? options[:tail] : ''
37
47
  "#{source[0..max_length-1]}#{tail}"
38
48
  end
39
- end
49
+ end
@@ -2,8 +2,8 @@ require "spec_helper"
2
2
 
3
3
  describe "Truncato" do
4
4
  describe "normal strings" do
5
- it_should_truncate "no html text with longer length", with: {max_length: 13, tail: '...'}, source: "some text", expected: "some text"
6
- it_should_truncate "no html text with shorter length", with: {max_length: 3}, source: "some text", expected: "som..."
5
+ #it_should_truncate "no html text with longer length", with: {max_length: 13, tail: '...'}, source: "some text", expected: "some text"
6
+ #it_should_truncate "no html text with shorter length", with: {max_length: 3}, source: "some text", expected: "som..."
7
7
  it_should_truncate "no html text with longer length", with: {max_length: 4}, source: "some", expected: "some"
8
8
  end
9
9
 
@@ -27,6 +27,10 @@ describe "Truncato" do
27
27
  it_should_truncate "html text with special html entioes", with: {max_length: 5},
28
28
  source: "<p>&gt;some text</p>",
29
29
  expected: "<p>&gt;s...</p>"
30
+
31
+ it_should_truncate "html text with siblings tags", with: {max_length: 51},
32
+ source: "<div>some text 0</div><div><p>some text 1</p><p>some text 2</p></div>",
33
+ expected: "<div>some text 0</div><div><p>some text 1</p><p>som...</p></div>"
30
34
  end
31
35
 
32
36
  describe "html attributes" do
data/truncato.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "truncato"
8
- s.version = "0.7.3"
8
+ s.version = "0.7.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Jorge Manrubia"]
12
- s.date = "2013-03-13"
12
+ s.date = "2013-04-14"
13
13
  s.description = "Ruby tool for truncating HTML strings keeping a valid HTML markup"
14
14
  s.email = "jorge.manrubia@gmail.com"
15
15
  s.extra_rdoc_files = [
@@ -49,44 +49,23 @@ Gem::Specification.new do |s|
49
49
  s.add_runtime_dependency(%q<truncato>, [">= 0"])
50
50
  s.add_runtime_dependency(%q<nokogiri>, ["~> 1.5.5"])
51
51
  s.add_runtime_dependency(%q<htmlentities>, ["~> 4.3.1"])
52
- s.add_development_dependency(%q<bundler>, ["~> 1.2.1"])
53
- s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
54
- s.add_development_dependency(%q<bundler>, ["~> 1.2.1"])
55
- s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
56
- s.add_development_dependency(%q<bundler>, ["~> 1.2.1"])
57
- s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
58
- s.add_development_dependency(%q<bundler>, ["~> 1.2.1"])
59
- s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
60
- s.add_development_dependency(%q<bundler>, ["~> 1.2.1"])
52
+ s.add_development_dependency(%q<bundler>, ["~> 1.3"])
53
+ s.add_development_dependency(%q<bundler>, ["~> 1.3"])
61
54
  s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
62
55
  else
63
56
  s.add_dependency(%q<truncato>, [">= 0"])
64
57
  s.add_dependency(%q<nokogiri>, ["~> 1.5.5"])
65
58
  s.add_dependency(%q<htmlentities>, ["~> 4.3.1"])
66
- s.add_dependency(%q<bundler>, ["~> 1.2.1"])
67
- s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
68
- s.add_dependency(%q<bundler>, ["~> 1.2.1"])
69
- s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
70
- s.add_dependency(%q<bundler>, ["~> 1.2.1"])
71
- s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
72
- s.add_dependency(%q<bundler>, ["~> 1.2.1"])
73
- s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
74
- s.add_dependency(%q<bundler>, ["~> 1.2.1"])
59
+ s.add_dependency(%q<bundler>, ["~> 1.3"])
60
+ s.add_dependency(%q<bundler>, ["~> 1.3"])
75
61
  s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
76
62
  end
77
63
  else
78
64
  s.add_dependency(%q<truncato>, [">= 0"])
79
65
  s.add_dependency(%q<nokogiri>, ["~> 1.5.5"])
80
66
  s.add_dependency(%q<htmlentities>, ["~> 4.3.1"])
81
- s.add_dependency(%q<bundler>, ["~> 1.2.1"])
82
- s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
83
- s.add_dependency(%q<bundler>, ["~> 1.2.1"])
84
- s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
85
- s.add_dependency(%q<bundler>, ["~> 1.2.1"])
86
- s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
87
- s.add_dependency(%q<bundler>, ["~> 1.2.1"])
88
- s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
89
- s.add_dependency(%q<bundler>, ["~> 1.2.1"])
67
+ s.add_dependency(%q<bundler>, ["~> 1.3"])
68
+ s.add_dependency(%q<bundler>, ["~> 1.3"])
90
69
  s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
91
70
  end
92
71
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: truncato
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.3
4
+ version: 0.7.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-13 00:00:00.000000000 Z
12
+ date: 2013-04-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: truncato
@@ -66,7 +66,7 @@ dependencies:
66
66
  requirements:
67
67
  - - ~>
68
68
  - !ruby/object:Gem::Version
69
- version: 1.2.1
69
+ version: '1.3'
70
70
  type: :development
71
71
  prerelease: false
72
72
  version_requirements: !ruby/object:Gem::Requirement
@@ -74,119 +74,7 @@ dependencies:
74
74
  requirements:
75
75
  - - ~>
76
76
  - !ruby/object:Gem::Version
77
- version: 1.2.1
78
- - !ruby/object:Gem::Dependency
79
- name: jeweler
80
- requirement: !ruby/object:Gem::Requirement
81
- none: false
82
- requirements:
83
- - - ~>
84
- - !ruby/object:Gem::Version
85
- version: 1.8.4
86
- type: :development
87
- prerelease: false
88
- version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
- requirements:
91
- - - ~>
92
- - !ruby/object:Gem::Version
93
- version: 1.8.4
94
- - !ruby/object:Gem::Dependency
95
- name: bundler
96
- requirement: !ruby/object:Gem::Requirement
97
- none: false
98
- requirements:
99
- - - ~>
100
- - !ruby/object:Gem::Version
101
- version: 1.2.1
102
- type: :development
103
- prerelease: false
104
- version_requirements: !ruby/object:Gem::Requirement
105
- none: false
106
- requirements:
107
- - - ~>
108
- - !ruby/object:Gem::Version
109
- version: 1.2.1
110
- - !ruby/object:Gem::Dependency
111
- name: jeweler
112
- requirement: !ruby/object:Gem::Requirement
113
- none: false
114
- requirements:
115
- - - ~>
116
- - !ruby/object:Gem::Version
117
- version: 1.8.4
118
- type: :development
119
- prerelease: false
120
- version_requirements: !ruby/object:Gem::Requirement
121
- none: false
122
- requirements:
123
- - - ~>
124
- - !ruby/object:Gem::Version
125
- version: 1.8.4
126
- - !ruby/object:Gem::Dependency
127
- name: bundler
128
- requirement: !ruby/object:Gem::Requirement
129
- none: false
130
- requirements:
131
- - - ~>
132
- - !ruby/object:Gem::Version
133
- version: 1.2.1
134
- type: :development
135
- prerelease: false
136
- version_requirements: !ruby/object:Gem::Requirement
137
- none: false
138
- requirements:
139
- - - ~>
140
- - !ruby/object:Gem::Version
141
- version: 1.2.1
142
- - !ruby/object:Gem::Dependency
143
- name: jeweler
144
- requirement: !ruby/object:Gem::Requirement
145
- none: false
146
- requirements:
147
- - - ~>
148
- - !ruby/object:Gem::Version
149
- version: 1.8.4
150
- type: :development
151
- prerelease: false
152
- version_requirements: !ruby/object:Gem::Requirement
153
- none: false
154
- requirements:
155
- - - ~>
156
- - !ruby/object:Gem::Version
157
- version: 1.8.4
158
- - !ruby/object:Gem::Dependency
159
- name: bundler
160
- requirement: !ruby/object:Gem::Requirement
161
- none: false
162
- requirements:
163
- - - ~>
164
- - !ruby/object:Gem::Version
165
- version: 1.2.1
166
- type: :development
167
- prerelease: false
168
- version_requirements: !ruby/object:Gem::Requirement
169
- none: false
170
- requirements:
171
- - - ~>
172
- - !ruby/object:Gem::Version
173
- version: 1.2.1
174
- - !ruby/object:Gem::Dependency
175
- name: jeweler
176
- requirement: !ruby/object:Gem::Requirement
177
- none: false
178
- requirements:
179
- - - ~>
180
- - !ruby/object:Gem::Version
181
- version: 1.8.4
182
- type: :development
183
- prerelease: false
184
- version_requirements: !ruby/object:Gem::Requirement
185
- none: false
186
- requirements:
187
- - - ~>
188
- - !ruby/object:Gem::Version
189
- version: 1.8.4
77
+ version: '1.3'
190
78
  - !ruby/object:Gem::Dependency
191
79
  name: bundler
192
80
  requirement: !ruby/object:Gem::Requirement
@@ -194,7 +82,7 @@ dependencies:
194
82
  requirements:
195
83
  - - ~>
196
84
  - !ruby/object:Gem::Version
197
- version: 1.2.1
85
+ version: '1.3'
198
86
  type: :development
199
87
  prerelease: false
200
88
  version_requirements: !ruby/object:Gem::Requirement
@@ -202,7 +90,7 @@ dependencies:
202
90
  requirements:
203
91
  - - ~>
204
92
  - !ruby/object:Gem::Version
205
- version: 1.2.1
93
+ version: '1.3'
206
94
  - !ruby/object:Gem::Dependency
207
95
  name: jeweler
208
96
  requirement: !ruby/object:Gem::Requirement
@@ -260,7 +148,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
260
148
  version: '0'
261
149
  segments:
262
150
  - 0
263
- hash: 1479395706551271193
151
+ hash: 1642571317414252409
264
152
  required_rubygems_version: !ruby/object:Gem::Requirement
265
153
  none: false
266
154
  requirements: