truncato 0.7.1 → 0.7.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1,2 +1,2 @@
1
- 0.7.1
1
+ 0.7.2
2
2
 
@@ -2,12 +2,9 @@ class TruncatedSaxDocument < Nokogiri::XML::SAX::Document
2
2
  attr_reader :truncated_string, :max_length, :max_length_reached, :tail, :count_tags, :filtered_attributes
3
3
 
4
4
  def initialize(options)
5
- init_from_options(options)
6
5
  @html_coder = HTMLEntities.new
7
- @truncated_string = ""
8
- @closing_tags = []
9
- @estimated_length = 0
10
- @max_length_reached = false
6
+ capture_options(options)
7
+ init_parsing_state
11
8
  end
12
9
 
13
10
  def start_element name, attributes
@@ -35,13 +32,20 @@ class TruncatedSaxDocument < Nokogiri::XML::SAX::Document
35
32
 
36
33
  private
37
34
 
38
- def init_from_options(options)
35
+ def capture_options(options)
39
36
  @max_length = options[:max_length]
40
37
  @count_tags = options [:count_tags]
41
38
  @tail = options[:tail]
42
39
  @filtered_attributes = options[:filtered_attributes] || []
43
40
  end
44
41
 
42
+ def init_parsing_state
43
+ @truncated_string = ""
44
+ @closing_tags = []
45
+ @estimated_length = 0
46
+ @max_length_reached = false
47
+ end
48
+
45
49
  def append_to_truncated_string string, overriden_length=nil
46
50
  @truncated_string << string
47
51
  increase_estimated_length(overriden_length || string.length)
@@ -54,12 +58,16 @@ class TruncatedSaxDocument < Nokogiri::XML::SAX::Document
54
58
 
55
59
  def attributes_to_string(attributes)
56
60
  return "" if attributes.empty?
57
- attributes_string = attributes.inject(' ') do |string, attribute|
61
+ attributes_string = concatenate_attributes_declaration attributes
62
+ attributes_string.rstrip
63
+ end
64
+
65
+ def concatenate_attributes_declaration(attributes)
66
+ attributes.inject(' ') do |string, attribute|
58
67
  key, value = attribute
59
- next string if @filtered_attributes.include?(key)
60
- string << "#{key}='#{value}' "
68
+ next string if @filtered_attributes.include?(key)
69
+ string << "#{key}='#{@html_coder.encode(value)}' "
61
70
  end
62
- attributes_string.rstrip
63
71
  end
64
72
 
65
73
  def closing_tag name
@@ -6,12 +6,13 @@ module Truncato
6
6
  filtered_attributes: []
7
7
  }
8
8
 
9
- # Truncates the source XML string and returns the result
9
+ # Truncates the source XML string and returns the truncated XML. It will keep a valid XML structure
10
+ # and insert a _tail_ text indicating the position where content were removed (...).
10
11
  #
11
12
  # @param [String] source the XML source to truncate
12
13
  # @param [Hash] user_options truncation options
13
14
  # @option user_options [Integer] :max_length Maximum length
14
- # @option user_options [String] :tail text to append when the truncation occurs
15
+ # @option user_options [String] :tail text to append when the truncation happens
15
16
  # @option user_options [Boolean] :count_tags `true` for counting tags for truncation, `false` for not counting them
16
17
  # @option user_options [Array<String>] :filtered_attributes Array of names of attributes that should be excluded in the resulting truncated string. This allows you to make the truncated string shorter by excluding the content of attributes you can discard in some given context, e.g HTML `style` attribute.
17
18
  # @return [String] the truncated string
@@ -46,9 +46,13 @@ describe "Truncato" do
46
46
  source: "<div><p attr1='1'>some text</p></div>",
47
47
  expected: "<div><p attr1='1'>some...</p></div>"
48
48
 
49
- it_should_truncate "html text with 2 attributes filtering one of them", with: {max_length: 3, count_tags: false, filtered_attributes: ['attr2']},
49
+ it_should_truncate "html text with attribute containing entities respecting them", with: {max_length: 3, count_tags: false, filtered_attributes: ['attr1', 'attr2']},
50
+ source: "<p attr1='&gt;some'>text</p>",
51
+ expected: "<p attr1='&gt;some'>text</p>"
52
+
53
+ it_should_truncate "html text with 2 attributes filtering one of them", with: {max_length: 30, count_tags: false, filtered_attributes: ['attr2']},
50
54
  source: "<p attr1='1' attr2='2'>some text</p>",
51
- expected: "<p attr1='1'>som...</p>"
55
+ expected: "<p>som...</p>"
52
56
 
53
57
  it_should_truncate "html text with 2 attributes filtering all of them", with: {max_length: 3, count_tags: false, filtered_attributes: ['attr1', 'attr2']},
54
58
  source: "<p attr1='1' attr2='2'>some text</p>",
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.1"
8
+ s.version = "0.7.2"
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 = "2012-11-12"
12
+ s.date = "2012-11-13"
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 = [
@@ -53,6 +53,8 @@ Gem::Specification.new do |s|
53
53
  s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
54
54
  s.add_development_dependency(%q<bundler>, ["~> 1.2.1"])
55
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"])
56
58
  else
57
59
  s.add_dependency(%q<truncato>, [">= 0"])
58
60
  s.add_dependency(%q<nokogiri>, ["~> 1.5.5"])
@@ -63,6 +65,8 @@ Gem::Specification.new do |s|
63
65
  s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
64
66
  s.add_dependency(%q<bundler>, ["~> 1.2.1"])
65
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"])
66
70
  end
67
71
  else
68
72
  s.add_dependency(%q<truncato>, [">= 0"])
@@ -74,6 +78,8 @@ Gem::Specification.new do |s|
74
78
  s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
75
79
  s.add_dependency(%q<bundler>, ["~> 1.2.1"])
76
80
  s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
81
+ s.add_dependency(%q<bundler>, ["~> 1.2.1"])
82
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
77
83
  end
78
84
  end
79
85
 
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.1
4
+ version: 0.7.2
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: 2012-11-12 00:00:00.000000000 Z
12
+ date: 2012-11-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: truncato
@@ -155,6 +155,38 @@ dependencies:
155
155
  - - ~>
156
156
  - !ruby/object:Gem::Version
157
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
158
190
  description: Ruby tool for truncating HTML strings keeping a valid HTML markup
159
191
  email: jorge.manrubia@gmail.com
160
192
  executables: []
@@ -194,7 +226,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
194
226
  version: '0'
195
227
  segments:
196
228
  - 0
197
- hash: -4062483884007899469
229
+ hash: -4126738111465282499
198
230
  required_rubygems_version: !ruby/object:Gem::Requirement
199
231
  none: false
200
232
  requirements: