truncato 0.7.0 → 0.7.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -22,6 +22,7 @@ The configuration options are:
22
22
  * `max_length`: The size, in characters, to truncate (`30` by default)
23
23
  * `tail`: The string to append when the truncation occurs ('...' by default)
24
24
  * `count_tags`: Boolean value indicating whether tags size should be considered when truncating (`true` by default)
25
+ * `filtered_attributes`: Array of attribute names that will be removed from the output. 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.
25
26
 
26
27
  ## Performance
27
28
 
data/VERSION CHANGED
@@ -1,2 +1,2 @@
1
- 0.7.0
1
+ 0.7.1
2
2
 
@@ -1,5 +1,5 @@
1
1
  class TruncatedSaxDocument < Nokogiri::XML::SAX::Document
2
- attr_reader :truncated_string, :max_length, :max_length_reached, :tail, :count_tags
2
+ attr_reader :truncated_string, :max_length, :max_length_reached, :tail, :count_tags, :filtered_attributes
3
3
 
4
4
  def initialize(options)
5
5
  init_from_options(options)
@@ -13,7 +13,7 @@ class TruncatedSaxDocument < Nokogiri::XML::SAX::Document
13
13
  def start_element name, attributes
14
14
  return if @max_length_reached
15
15
  @closing_tags.push name
16
- append_to_truncated_string opening_tag(name), overriden_tag_length
16
+ append_to_truncated_string opening_tag(name, attributes), overriden_tag_length
17
17
  end
18
18
 
19
19
  def characters decoded_string
@@ -39,6 +39,7 @@ class TruncatedSaxDocument < Nokogiri::XML::SAX::Document
39
39
  @max_length = options[:max_length]
40
40
  @count_tags = options [:count_tags]
41
41
  @tail = options[:tail]
42
+ @filtered_attributes = options[:filtered_attributes] || []
42
43
  end
43
44
 
44
45
  def append_to_truncated_string string, overriden_length=nil
@@ -46,8 +47,19 @@ class TruncatedSaxDocument < Nokogiri::XML::SAX::Document
46
47
  increase_estimated_length(overriden_length || string.length)
47
48
  end
48
49
 
49
- def opening_tag name
50
- "<#{name}>"
50
+ def opening_tag name, attributes
51
+ attributes_string = attributes_to_string(attributes)
52
+ "<#{name}#{attributes_string}>"
53
+ end
54
+
55
+ def attributes_to_string(attributes)
56
+ return "" if attributes.empty?
57
+ attributes_string = attributes.inject(' ') do |string, attribute|
58
+ key, value = attribute
59
+ next string if @filtered_attributes.include?(key)
60
+ string << "#{key}='#{value}' "
61
+ end
62
+ attributes_string.rstrip
51
63
  end
52
64
 
53
65
  def closing_tag name
@@ -2,7 +2,8 @@ module Truncato
2
2
  DEFAULT_OPTIONS = {
3
3
  max_length: 30,
4
4
  count_tags: true,
5
- tail: "..."
5
+ tail: "...",
6
+ filtered_attributes: []
6
7
  }
7
8
 
8
9
  # Truncates the source XML string and returns the result
@@ -12,6 +13,7 @@ module Truncato
12
13
  # @option user_options [Integer] :max_length Maximum length
13
14
  # @option user_options [String] :tail text to append when the truncation occurs
14
15
  # @option user_options [Boolean] :count_tags `true` for counting tags for truncation, `false` for not counting them
16
+ # @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.
15
17
  # @return [String] the truncated string
16
18
  def self.truncate source, user_options={}
17
19
  options = DEFAULT_OPTIONS.merge(user_options)
@@ -7,7 +7,7 @@ describe "Truncato" do
7
7
  it_should_truncate "no html text with longer length", with: {max_length: 4}, source: "some", expected: "some"
8
8
  end
9
9
 
10
- describe "html strings" do
10
+ describe "html tags structure" do
11
11
  it_should_truncate "html text with a tag (counting tags)", with: {max_length: 4}, source: "<p>some text</p>", expected: "<p>s...</p>"
12
12
 
13
13
  it_should_truncate "html text with a tag (not counting tags)", with: {max_length: 4, count_tags: false}, source: "<p>some text</p>", expected: "<p>some...</p>"
@@ -29,5 +29,32 @@ describe "Truncato" do
29
29
  expected: "<p>&gt;s...</p>"
30
30
  end
31
31
 
32
+ describe "html attributes" do
33
+ it_should_truncate "html text with 1 attributes", with: {max_length: 3, count_tags: false},
34
+ source: "<p attr1='1'>some text</p>",
35
+ expected: "<p attr1='1'>som...</p>"
36
+
37
+ it_should_truncate "html text with 1 attributes counting its size", with: {max_length: 16, count_tags: true},
38
+ source: "<p attr1='1'>some text</p>",
39
+ expected: "<p attr1='1'>som...</p>"
40
+
41
+ it_should_truncate "html text with 2 attributes", with: {max_length: 3, count_tags: false},
42
+ source: "<p attr1='1' attr2='2'>some text</p>",
43
+ expected: "<p attr1='1' attr2='2'>som...</p>"
44
+
45
+ it_should_truncate "html text with attributes in nested tags", with: {max_length: 4, count_tags: false},
46
+ source: "<div><p attr1='1'>some text</p></div>",
47
+ expected: "<div><p attr1='1'>some...</p></div>"
48
+
49
+ it_should_truncate "html text with 2 attributes filtering one of them", with: {max_length: 3, count_tags: false, filtered_attributes: ['attr2']},
50
+ source: "<p attr1='1' attr2='2'>some text</p>",
51
+ expected: "<p attr1='1'>som...</p>"
52
+
53
+ it_should_truncate "html text with 2 attributes filtering all of them", with: {max_length: 3, count_tags: false, filtered_attributes: ['attr1', 'attr2']},
54
+ source: "<p attr1='1' attr2='2'>some text</p>",
55
+ expected: "<p>som...</p>"
56
+ end
57
+
58
+
32
59
  end
33
60
 
data/truncato.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "truncato"
8
- s.version = "0.7.0"
8
+ s.version = "0.7.1"
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"]
@@ -19,7 +19,6 @@ Gem::Specification.new do |s|
19
19
  s.files = [
20
20
  ".rvmrc",
21
21
  "Gemfile",
22
- "Gemfile.lock",
23
22
  "LICENSE.txt",
24
23
  "README.md",
25
24
  "Rakefile",
@@ -52,6 +51,8 @@ Gem::Specification.new do |s|
52
51
  s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
53
52
  s.add_development_dependency(%q<bundler>, ["~> 1.2.1"])
54
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"])
55
56
  else
56
57
  s.add_dependency(%q<truncato>, [">= 0"])
57
58
  s.add_dependency(%q<nokogiri>, ["~> 1.5.5"])
@@ -60,6 +61,8 @@ Gem::Specification.new do |s|
60
61
  s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
61
62
  s.add_dependency(%q<bundler>, ["~> 1.2.1"])
62
63
  s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
64
+ s.add_dependency(%q<bundler>, ["~> 1.2.1"])
65
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
63
66
  end
64
67
  else
65
68
  s.add_dependency(%q<truncato>, [">= 0"])
@@ -69,6 +72,8 @@ Gem::Specification.new do |s|
69
72
  s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
70
73
  s.add_dependency(%q<bundler>, ["~> 1.2.1"])
71
74
  s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
75
+ s.add_dependency(%q<bundler>, ["~> 1.2.1"])
76
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
72
77
  end
73
78
  end
74
79
 
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.0
4
+ version: 0.7.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -123,6 +123,38 @@ dependencies:
123
123
  - - ~>
124
124
  - !ruby/object:Gem::Version
125
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
126
158
  description: Ruby tool for truncating HTML strings keeping a valid HTML markup
127
159
  email: jorge.manrubia@gmail.com
128
160
  executables: []
@@ -133,7 +165,6 @@ extra_rdoc_files:
133
165
  files:
134
166
  - .rvmrc
135
167
  - Gemfile
136
- - Gemfile.lock
137
168
  - LICENSE.txt
138
169
  - README.md
139
170
  - Rakefile
@@ -163,7 +194,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
163
194
  version: '0'
164
195
  segments:
165
196
  - 0
166
- hash: 3596249890284373376
197
+ hash: -4062483884007899469
167
198
  required_rubygems_version: !ruby/object:Gem::Requirement
168
199
  none: false
169
200
  requirements:
data/Gemfile.lock DELETED
@@ -1,43 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- truncato (0.7.0)
5
-
6
- GEM
7
- remote: http://rubygems.org/
8
- specs:
9
- diff-lcs (1.1.3)
10
- git (1.2.5)
11
- html_truncator (0.3.0)
12
- nokogiri (~> 1.4)
13
- htmlentities (4.3.1)
14
- jeweler (1.8.4)
15
- bundler (~> 1.0)
16
- git (>= 1.2.5)
17
- rake
18
- rdoc
19
- json (1.7.5)
20
- nokogiri (1.5.5)
21
- rake (0.9.2.2)
22
- rdoc (3.12)
23
- json (~> 1.4)
24
- rspec (2.11.0)
25
- rspec-core (~> 2.11.0)
26
- rspec-expectations (~> 2.11.0)
27
- rspec-mocks (~> 2.11.0)
28
- rspec-core (2.11.1)
29
- rspec-expectations (2.11.3)
30
- diff-lcs (~> 1.1.3)
31
- rspec-mocks (2.11.3)
32
-
33
- PLATFORMS
34
- ruby
35
-
36
- DEPENDENCIES
37
- bundler (~> 1.2.1)
38
- html_truncator
39
- htmlentities (~> 4.3.1)
40
- jeweler (~> 1.8.4)
41
- nokogiri (~> 1.5.5)
42
- rspec (~> 2.11.0)
43
- truncato!