truncato 0.7.7 → 0.7.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dbff10a384de31d8d3236ac33ce109cfaacb3ac2
4
- data.tar.gz: 606f4c188162162531c8615bde675e64629f5a8d
3
+ metadata.gz: 9cb24d2051ad12e17099cc7ec2c3a03240ea1ca6
4
+ data.tar.gz: e4e8af975b663b294522e9da8f0fa734de680e75
5
5
  SHA512:
6
- metadata.gz: d6755d932f17233aed34d13e1fa0e78c219e9b3eea669e7629caee5c201bcf4722e3fe9c1e82e1558e40843a73d98e29e9beb819c1939b55277b32126cd3bcea
7
- data.tar.gz: b45878dc0e6b26c74016c86dbe64defc3ddc8ff99f0342d163f925b5f24ba3ea6645638690ccc1e22d4e9266e3bedcc76c31d9a398cd8651040650cd7b835c99
6
+ metadata.gz: 8b2c503c442955a2095e6578a00f43c93764f40a1ba5f0b4fa1bbeb97d0537ddbd4aaa6d9560adc17ea37aecba4922af1aba65cdd4facad12bebd25770428c6e
7
+ data.tar.gz: 60261242b872ac153e45ab9811f15b8058def2f3825252f4522b3f08fbac7b9237105432e784fe86056cf3c564eded95bd40af0f5caf986cce28098c8f7abf0d
data/README.md CHANGED
@@ -13,8 +13,8 @@ gem 'truncato'
13
13
  ## Usage
14
14
 
15
15
  ```ruby
16
- Truncato.truncate "<p>some text</p>", max_length: 4 => "<p>s...</p>"
17
- Truncato.truncate "<p>some text</p>", max_length: 4, count_tags: false => "<p>some...</p>"
16
+ Truncato.truncate "<p>some text</p>", max_length: 4 #=> "<p>s...</p>"
17
+ Truncato.truncate "<p>some text</p>", max_length: 4, count_tags: false #=> "<p>some...</p>"
18
18
  ```
19
19
 
20
20
  The configuration options are:
@@ -2,6 +2,10 @@ require 'nokogiri'
2
2
  require 'htmlentities'
3
3
 
4
4
  class TruncatedSaxDocument < Nokogiri::XML::SAX::Document
5
+ IGNORABLE_TAGS = %w(html head body)
6
+
7
+ SINGLE_TAGS = %w{br img}
8
+
5
9
  attr_reader :truncated_string, :max_length, :max_length_reached, :tail,
6
10
  :count_tags, :filtered_attributes, :filtered_tags, :ignored_levels
7
11
 
@@ -13,7 +17,7 @@ class TruncatedSaxDocument < Nokogiri::XML::SAX::Document
13
17
 
14
18
  def start_element name, attributes
15
19
  enter_ignored_level if filtered_tags.include?(name)
16
- return if @max_length_reached || ignorable_element?(name) || ignore_mode?
20
+ return if @max_length_reached || ignorable_tag?(name) || ignore_mode?
17
21
  @closing_tags.push name unless single_tag_element? name
18
22
  append_to_truncated_string opening_tag(name, attributes), overriden_tag_length
19
23
  end
@@ -28,23 +32,17 @@ class TruncatedSaxDocument < Nokogiri::XML::SAX::Document
28
32
  def comment string
29
33
  if @comments
30
34
  return if @max_length_reached
31
- remaining_length = max_length - @estimated_length - 1
32
- string_to_append = comment_tag(string).length > remaining_length ? truncate_comment(comment_tag(string), remaining_length) : comment_tag(string)
33
- append_to_truncated_string string_to_append
35
+ process_comment string
34
36
  end
35
37
  end
36
38
 
37
- def comment_tag comment
38
- "<!--#{comment}-->"
39
- end
40
-
41
39
  def end_element name
42
40
  if filtered_tags.include?(name) && ignore_mode?
43
41
  exit_ignored_level
44
42
  return
45
43
  end
46
44
 
47
- return if @max_length_reached || ignorable_element?(name) || ignore_mode?
45
+ return if @max_length_reached || ignorable_tag?(name) || ignore_mode?
48
46
 
49
47
  unless single_tag_element? name
50
48
  @closing_tags.pop
@@ -69,6 +67,16 @@ class TruncatedSaxDocument < Nokogiri::XML::SAX::Document
69
67
  @comments = options.fetch(:comments, false)
70
68
  end
71
69
 
70
+ def process_comment(string)
71
+ remaining_length = max_length - @estimated_length - 1
72
+ string_to_append = comment_tag(string).length > remaining_length ? truncate_comment(comment_tag(string), remaining_length) : comment_tag(string)
73
+ append_to_truncated_string string_to_append
74
+ end
75
+
76
+ def comment_tag comment
77
+ "<!--#{comment}-->"
78
+ end
79
+
72
80
  def init_parsing_state
73
81
  @truncated_string = ""
74
82
  @closing_tags = []
@@ -82,7 +90,7 @@ class TruncatedSaxDocument < Nokogiri::XML::SAX::Document
82
90
  end
83
91
 
84
92
  def single_tag_element? name
85
- %w{br img}.include? name
93
+ SINGLE_TAGS.include? name
86
94
  end
87
95
 
88
96
  def append_to_truncated_string string, overriden_length=nil
@@ -158,8 +166,9 @@ class TruncatedSaxDocument < Nokogiri::XML::SAX::Document
158
166
  @count_tags ? nil : 0
159
167
  end
160
168
 
161
- def ignorable_element?(name)
162
- artificial_root_name?(name) || %w(html head body).include?(name.downcase)
169
+
170
+ def ignorable_tag?(name)
171
+ artificial_root_name?(name) || IGNORABLE_TAGS.include?(name.downcase)
163
172
  end
164
173
 
165
174
  def artificial_root_name? name
@@ -1,3 +1,3 @@
1
1
  module Truncato
2
- VERSION='0.7.7'
2
+ VERSION='0.7.8'
3
3
  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.7
4
+ version: 0.7.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jorge Manrubia
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ~>
18
18
  - !ruby/object:Gem::Version
19
- version: 1.5.5
19
+ version: 1.6.1
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ~>
25
25
  - !ruby/object:Gem::Version
26
- version: 1.5.5
26
+ version: 1.6.1
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: htmlentities
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -58,14 +58,14 @@ dependencies:
58
58
  requirements:
59
59
  - - ~>
60
60
  - !ruby/object:Gem::Version
61
- version: 10.1.0
61
+ version: 10.1.1
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - ~>
67
67
  - !ruby/object:Gem::Version
68
- version: 10.1.0
68
+ version: 10.1.1
69
69
  description: Ruby tool for truncating HTML strings keeping a valid HTML markup
70
70
  email: jorge.manrubia@gmail.com
71
71
  executables: []