truncato 0.7.5 → 0.7.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: dbff10a384de31d8d3236ac33ce109cfaacb3ac2
4
+ data.tar.gz: 606f4c188162162531c8615bde675e64629f5a8d
5
+ SHA512:
6
+ metadata.gz: d6755d932f17233aed34d13e1fa0e78c219e9b3eea669e7629caee5c201bcf4722e3fe9c1e82e1558e40843a73d98e29e9beb819c1939b55277b32126cd3bcea
7
+ data.tar.gz: b45878dc0e6b26c74016c86dbe64defc3ddc8ff99f0342d163f925b5f24ba3ea6645638690ccc1e22d4e9266e3bedcc76c31d9a398cd8651040650cd7b835c99
data/README.md CHANGED
@@ -20,11 +20,12 @@ Truncato.truncate "<p>some text</p>", max_length: 4, count_tags: false => "<p>so
20
20
  The configuration options are:
21
21
 
22
22
  * `max_length`: The size, in characters, to truncate (`30` by default)
23
- * `tail`: The string to append when the truncation occurs ('...' by default)
23
+ * `filtered_attributes`: Array of attribute names that will be removed in the 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.
24
+ * `filtered_tags`: Array of tags that will be removed in the truncated string. If a tag is excluded, all the nested tags under it will be excluded too.
24
25
  * `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.
26
26
  * `tail_before_final_tag`: Boolean value indicating whether to apply a tail before the final closing tag (`false` by default)
27
27
  * `comments`: Boolean value indicating whether to include comments in parsed results (`false` by default)
28
+ * `tail`: The string to append when the truncation occurs ('...' by default)
28
29
  * `count_tail`: Boolean value indicating whether to include the tail within the bounds of the provided max length (`false` by default)
29
30
 
30
31
  ## Performance
data/Rakefile CHANGED
@@ -1,56 +1,32 @@
1
- # encoding: utf-8
2
-
3
- require 'rubygems'
4
- require 'bundler'
5
1
  begin
6
- Bundler.setup(:default, :development)
7
- rescue Bundler::BundlerError => e
8
- $stderr.puts e.message
9
- $stderr.puts "Run `bundle install` to install missing gems"
10
- exit e.status_code
11
- end
12
- require 'rake'
13
-
14
- require 'jeweler'
15
- Jeweler::Tasks.new do |gem|
16
- # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
- gem.name = "truncato"
18
- gem.homepage = "https://github.com/jorgemanrubia/truncato"
19
- gem.license = "MIT"
20
- gem.summary = %Q{A tool for truncating HTML strings efficiently}
21
- gem.description = %Q{Ruby tool for truncating HTML strings keeping a valid HTML markup}
22
- gem.email = "jorge.manrubia@gmail.com"
23
- gem.authors = ["Jorge Manrubia"]
24
- # dependencies defined in Gemfile
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
25
5
  end
26
- Jeweler::RubygemsDotOrgTasks.new
27
6
 
28
- require 'rspec/core'
29
- require 'rspec/core/rake_task'
30
- RSpec::Core::RakeTask.new(:spec) do |spec|
31
- spec.pattern = FileList['spec/**/*_spec.rb']
32
- end
7
+ require 'rdoc/task'
33
8
 
34
- RSpec::Core::RakeTask.new(:rcov) do |spec|
35
- spec.pattern = 'spec/**/*_spec.rb'
36
- spec.rcov = true
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'MailgunRails'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
37
15
  end
38
16
 
39
- task :default => :spec
40
17
 
41
- $:.unshift File.join(File.dirname(__FILE__), 'benchmark')
42
18
 
43
- require 'nokogiri'
44
- require 'truncato'
45
- require 'truncato_benchmark'
46
19
 
47
- namespace :truncato do
48
- task :benchmark do
49
- Truncato::BenchmarkRunner.new.run
50
- end
20
+ Bundler::GemHelper.install_tasks
51
21
 
52
- task :vendor_compare do
53
- Truncato::BenchmarkRunner.new.run_comparison
54
- end
22
+ require 'rake/testtask'
55
23
 
24
+ Rake::TestTask.new(:test) do |t|
25
+ t.libs << 'lib'
26
+ t.libs << 'test'
27
+ t.pattern = 'test/**/*_test.rb'
28
+ t.verbose = false
56
29
  end
30
+
31
+
32
+ task default: :test
@@ -2,7 +2,8 @@ require 'nokogiri'
2
2
  require 'htmlentities'
3
3
 
4
4
  class TruncatedSaxDocument < Nokogiri::XML::SAX::Document
5
- attr_reader :truncated_string, :max_length, :max_length_reached, :tail, :count_tags, :filtered_attributes
5
+ attr_reader :truncated_string, :max_length, :max_length_reached, :tail,
6
+ :count_tags, :filtered_attributes, :filtered_tags, :ignored_levels
6
7
 
7
8
  def initialize(options)
8
9
  @html_coder = HTMLEntities.new
@@ -11,13 +12,14 @@ class TruncatedSaxDocument < Nokogiri::XML::SAX::Document
11
12
  end
12
13
 
13
14
  def start_element name, attributes
14
- return if @max_length_reached || artificial_root_name?(name)
15
+ enter_ignored_level if filtered_tags.include?(name)
16
+ return if @max_length_reached || ignorable_element?(name) || ignore_mode?
15
17
  @closing_tags.push name unless single_tag_element? name
16
18
  append_to_truncated_string opening_tag(name, attributes), overriden_tag_length
17
19
  end
18
20
 
19
21
  def characters decoded_string
20
- return if @max_length_reached
22
+ return if @max_length_reached || ignore_mode?
21
23
  remaining_length = max_length - @estimated_length - 1
22
24
  string_to_append = decoded_string.length > remaining_length ? truncate_string(decoded_string, remaining_length) : decoded_string
23
25
  append_to_truncated_string @html_coder.encode(string_to_append), string_to_append.length
@@ -37,7 +39,13 @@ class TruncatedSaxDocument < Nokogiri::XML::SAX::Document
37
39
  end
38
40
 
39
41
  def end_element name
40
- return if @max_length_reached || artificial_root_name?(name)
42
+ if filtered_tags.include?(name) && ignore_mode?
43
+ exit_ignored_level
44
+ return
45
+ end
46
+
47
+ return if @max_length_reached || ignorable_element?(name) || ignore_mode?
48
+
41
49
  unless single_tag_element? name
42
50
  @closing_tags.pop
43
51
  append_to_truncated_string closing_tag(name), overriden_tag_length
@@ -51,20 +59,22 @@ class TruncatedSaxDocument < Nokogiri::XML::SAX::Document
51
59
  private
52
60
 
53
61
  def capture_options(options)
54
- @max_length = options[:max_length]
55
- @count_tags = options [:count_tags]
56
- @count_tail = options.fetch(:count_tail, false)
57
- @tail = options[:tail]
58
- @filtered_attributes = options[:filtered_attributes] || []
62
+ @max_length = options[:max_length]
63
+ @count_tags = options [:count_tags]
64
+ @count_tail = options.fetch(:count_tail, false)
65
+ @tail = options[:tail]
66
+ @filtered_attributes = options[:filtered_attributes] || []
67
+ @filtered_tags = options[:filtered_tags] || []
59
68
  @tail_before_final_tag = options.fetch(:tail_before_final_tag, false)
60
- @comments = options.fetch(:comments, false)
69
+ @comments = options.fetch(:comments, false)
61
70
  end
62
71
 
63
72
  def init_parsing_state
64
- @truncated_string = ""
65
- @closing_tags = []
66
- @estimated_length = @count_tail ? tail_length : 0
73
+ @truncated_string = ""
74
+ @closing_tags = []
75
+ @estimated_length = @count_tail ? tail_length : 0
67
76
  @max_length_reached = false
77
+ @ignored_levels = 0
68
78
  end
69
79
 
70
80
  def tail_length
@@ -72,7 +82,7 @@ class TruncatedSaxDocument < Nokogiri::XML::SAX::Document
72
82
  end
73
83
 
74
84
  def single_tag_element? name
75
- ["br", "img"].include? name
85
+ %w{br img}.include? name
76
86
  end
77
87
 
78
88
  def append_to_truncated_string string, overriden_length=nil
@@ -83,7 +93,7 @@ class TruncatedSaxDocument < Nokogiri::XML::SAX::Document
83
93
  def opening_tag name, attributes
84
94
  attributes_string = attributes_to_string attributes
85
95
  if single_tag_element? name
86
- "<#{name}#{attributes_string} />"
96
+ "<#{name}#{attributes_string}/>"
87
97
  else
88
98
  "<#{name}#{attributes_string}>"
89
99
  end
@@ -148,6 +158,10 @@ class TruncatedSaxDocument < Nokogiri::XML::SAX::Document
148
158
  @count_tags ? nil : 0
149
159
  end
150
160
 
161
+ def ignorable_element?(name)
162
+ artificial_root_name?(name) || %w(html head body).include?(name.downcase)
163
+ end
164
+
151
165
  def artificial_root_name? name
152
166
  name == Truncato::ARTIFICIAL_ROOT_NAME
153
167
  end
@@ -155,4 +169,16 @@ class TruncatedSaxDocument < Nokogiri::XML::SAX::Document
155
169
  def append_tail_between_closing_tags
156
170
  append_to_truncated_string closing_tag(@closing_tags.delete_at (@closing_tags.length - 1)) if @closing_tags.length > 1
157
171
  end
172
+
173
+ def enter_ignored_level
174
+ @ignored_levels += 1
175
+ end
176
+
177
+ def exit_ignored_level
178
+ @ignored_levels -= 1
179
+ end
180
+
181
+ def ignore_mode?
182
+ @ignored_levels > 0
183
+ end
158
184
  end
@@ -31,7 +31,7 @@ module Truncato
31
31
 
32
32
  def self.do_truncate_html source, options
33
33
  truncated_sax_document = TruncatedSaxDocument.new(options)
34
- parser = Nokogiri::XML::SAX::Parser.new(truncated_sax_document)
34
+ parser = Nokogiri::HTML::SAX::Parser.new(truncated_sax_document)
35
35
  parser.parse(source) { |context| context.replace_entities = false }
36
36
  truncated_string = truncated_sax_document.truncated_string
37
37
  truncated_string.empty? ? nil : truncated_string
@@ -0,0 +1,3 @@
1
+ module Truncato
2
+ VERSION='0.7.7'
3
+ end
metadata CHANGED
@@ -1,36 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: truncato
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.5
5
- prerelease:
4
+ version: 0.7.7
6
5
  platform: ruby
7
6
  authors:
8
7
  - Jorge Manrubia
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-04-27 00:00:00.000000000 Z
11
+ date: 2013-09-10 00:00:00.000000000 Z
13
12
  dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: truncato
16
- requirement: !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
21
- version: '0'
22
- type: :runtime
23
- prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ! '>='
28
- - !ruby/object:Gem::Version
29
- version: '0'
30
13
  - !ruby/object:Gem::Dependency
31
14
  name: nokogiri
32
15
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
16
  requirements:
35
17
  - - ~>
36
18
  - !ruby/object:Gem::Version
@@ -38,7 +20,6 @@ dependencies:
38
20
  type: :runtime
39
21
  prerelease: false
40
22
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
23
  requirements:
43
24
  - - ~>
44
25
  - !ruby/object:Gem::Version
@@ -46,7 +27,6 @@ dependencies:
46
27
  - !ruby/object:Gem::Dependency
47
28
  name: htmlentities
48
29
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
30
  requirements:
51
31
  - - ~>
52
32
  - !ruby/object:Gem::Version
@@ -54,91 +34,38 @@ dependencies:
54
34
  type: :runtime
55
35
  prerelease: false
56
36
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
37
  requirements:
59
38
  - - ~>
60
39
  - !ruby/object:Gem::Version
61
40
  version: 4.3.1
62
41
  - !ruby/object:Gem::Dependency
63
- name: bundler
64
- requirement: !ruby/object:Gem::Requirement
65
- none: false
66
- requirements:
67
- - - ~>
68
- - !ruby/object:Gem::Version
69
- version: '1.3'
70
- type: :development
71
- prerelease: false
72
- version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
- requirements:
75
- - - ~>
76
- - !ruby/object:Gem::Version
77
- version: '1.3'
78
- - !ruby/object:Gem::Dependency
79
- name: bundler
80
- requirement: !ruby/object:Gem::Requirement
81
- none: false
82
- requirements:
83
- - - ~>
84
- - !ruby/object:Gem::Version
85
- version: '1.3'
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.3'
94
- - !ruby/object:Gem::Dependency
95
- name: jeweler
42
+ name: rspec
96
43
  requirement: !ruby/object:Gem::Requirement
97
- none: false
98
44
  requirements:
99
45
  - - ~>
100
46
  - !ruby/object:Gem::Version
101
- version: 1.8.4
47
+ version: 2.14.1
102
48
  type: :development
103
49
  prerelease: false
104
50
  version_requirements: !ruby/object:Gem::Requirement
105
- none: false
106
51
  requirements:
107
52
  - - ~>
108
53
  - !ruby/object:Gem::Version
109
- version: 1.8.4
54
+ version: 2.14.1
110
55
  - !ruby/object:Gem::Dependency
111
- name: bundler
56
+ name: rake
112
57
  requirement: !ruby/object:Gem::Requirement
113
- none: false
114
58
  requirements:
115
59
  - - ~>
116
60
  - !ruby/object:Gem::Version
117
- version: '1.3'
61
+ version: 10.1.0
118
62
  type: :development
119
63
  prerelease: false
120
64
  version_requirements: !ruby/object:Gem::Requirement
121
- none: false
122
65
  requirements:
123
66
  - - ~>
124
67
  - !ruby/object:Gem::Version
125
- version: '1.3'
126
- - !ruby/object:Gem::Dependency
127
- name: jeweler
128
- requirement: !ruby/object:Gem::Requirement
129
- none: false
130
- requirements:
131
- - - ~>
132
- - !ruby/object:Gem::Version
133
- version: 1.8.4
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.8.4
68
+ version: 10.1.0
142
69
  description: Ruby tool for truncating HTML strings keeping a valid HTML markup
143
70
  email: jorge.manrubia@gmail.com
144
71
  executables: []
@@ -147,50 +74,35 @@ extra_rdoc_files:
147
74
  - LICENSE.txt
148
75
  - README.md
149
76
  files:
150
- - .ruby-version
151
- - .rvmrc
152
- - Gemfile
153
- - LICENSE.txt
154
- - README.md
155
- - Rakefile
156
- - VERSION
157
- - benchmark/truncato/benchmark_runner.rb
158
- - benchmark/truncato/vendor/peppercorn_adapter.rb
159
- - benchmark/truncato/vendor/vendor_html_truncator_adapter.rb
160
- - benchmark/truncato_benchmark.rb
161
- - lib/truncato.rb
162
77
  - lib/truncato/truncated_sax_document.rb
163
78
  - lib/truncato/truncato.rb
164
- - spec/spec_helper.rb
165
- - spec/support/spec_helpers/truncato_macros.rb
166
- - spec/truncato/truncato_spec.rb
167
- - truncato.gemspec
79
+ - lib/truncato/version.rb
80
+ - lib/truncato.rb
81
+ - LICENSE.txt
82
+ - Rakefile
83
+ - README.md
168
84
  homepage: https://github.com/jorgemanrubia/truncato
169
85
  licenses:
170
86
  - MIT
87
+ metadata: {}
171
88
  post_install_message:
172
89
  rdoc_options: []
173
90
  require_paths:
174
91
  - lib
175
92
  required_ruby_version: !ruby/object:Gem::Requirement
176
- none: false
177
93
  requirements:
178
- - - ! '>='
94
+ - - '>='
179
95
  - !ruby/object:Gem::Version
180
96
  version: '0'
181
- segments:
182
- - 0
183
- hash: 3957383429942337949
184
97
  required_rubygems_version: !ruby/object:Gem::Requirement
185
- none: false
186
98
  requirements:
187
- - - ! '>='
99
+ - - '>='
188
100
  - !ruby/object:Gem::Version
189
101
  version: '0'
190
102
  requirements: []
191
103
  rubyforge_project:
192
- rubygems_version: 1.8.23
104
+ rubygems_version: 2.0.2
193
105
  signing_key:
194
- specification_version: 3
106
+ specification_version: 4
195
107
  summary: A tool for truncating HTML strings efficiently
196
108
  test_files: []
data/.ruby-version DELETED
@@ -1 +0,0 @@
1
- 1.9.3-p385
data/.rvmrc DELETED
@@ -1,52 +0,0 @@
1
- #!/usr/bin/env bash
2
-
3
- # This is an RVM Project .rvmrc file, used to automatically load the ruby
4
- # development environment upon cd'ing into the directory
5
-
6
- # First we specify our desired <ruby>[@<gemset>], the @gemset string is optional,
7
- # Only full ruby string is supported here, for short names use:
8
- # echo "rvm use 1.9.2" > .rvmrc
9
- environment_id="ruby-1.9.2-p320"
10
-
11
- # Uncomment the following lines if you want to verify rvm version per project
12
- # rvmrc_rvm_version="1.15.8 (stable)" # 1.10.1 seams as a safe start
13
- # eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
14
- # echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
15
- # return 1
16
- # }
17
-
18
- # First we attempt to load the desired environment directly from the environment
19
- # file. This is very fast and efficient compared to running through the entire
20
- # CLI and selector. If you want feedback on which environment was used then
21
- # insert the word 'use' after --create as this triggers verbose mode.
22
- if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
23
- && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
24
- then
25
- \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
26
- [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]] &&
27
- \. "${rvm_path:-$HOME/.rvm}/hooks/after_use" || true
28
- else
29
- # If the environment file has not yet been created, use the RVM CLI to select.
30
- rvm --create "$environment_id" || {
31
- echo "Failed to create RVM environment '${environment_id}'."
32
- return 1
33
- }
34
- fi
35
-
36
- # If you use bundler, this might be useful to you:
37
- # if [[ -s Gemfile ]] && {
38
- # ! builtin command -v bundle >/dev/null ||
39
- # builtin command -v bundle | GREP_OPTIONS= \grep $rvm_path/bin/bundle >/dev/null
40
- # }
41
- # then
42
- # printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
43
- # gem install bundler
44
- # fi
45
- # if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null
46
- # then
47
- # bundle install | GREP_OPTIONS= \grep -vE '^Using|Your bundle is complete'
48
- # fi
49
-
50
- git branch
51
- ruby -v
52
- git stash list
data/Gemfile DELETED
@@ -1,27 +0,0 @@
1
- source :rubygems
2
- gemspec
3
-
4
-
5
- # Add dependencies required to use your gem here.
6
- # Example:
7
- # gem "activesupport", ">= 2.3.5"
8
-
9
- # Add dependencies to develop your gem here.
10
- # Include everything needed to run rake, tests, features, etc.
11
-
12
- gem 'nokogiri', "~> 1.5.5"
13
- gem 'htmlentities', "~> 4.3.1"
14
-
15
- group :development do
16
- gem "bundler", "~> 1.3"
17
- gem "jeweler", "~> 1.8.4"
18
- end
19
-
20
- group :test do
21
- gem "rspec", "~> 2.11.0"
22
- end
23
-
24
- group :benchrmark do
25
- gem 'html_truncator'
26
- gem 'peppercorn'
27
- end
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 0.7.5
@@ -1,61 +0,0 @@
1
- module Truncato
2
- class BenchmarkRunner
3
- SYNTHETIC_XML_LENGTH = 4000000
4
- TRUNCATION_LENGTH = 400000
5
-
6
- attr_reader :synthetic_xml
7
-
8
- def initialize
9
- @synthetic_xml = create_synthetic_xml(SYNTHETIC_XML_LENGTH)
10
- puts "Generated synthethic load with #{@synthetic_xml.length/1000.0}K characters"
11
- end
12
-
13
- def run
14
- run_suite [Truncato]
15
- end
16
-
17
- def run_comparison
18
- run_suite [Truncato, VendorHtmlTruncatorAdapter, PeppercornAdapter]
19
- end
20
-
21
-
22
- private
23
-
24
- def run_suite(truncation_classes)
25
- results = truncation_classes.collect { |klass| {klass => run_with(klass)} }
26
- show_results results
27
- end
28
-
29
- def create_synthetic_xml(length)
30
- xml_content = "<synthetic-root>"
31
- append_random_xml_content xml_content, length
32
- xml_content << "</synthetic-root>"
33
- xml_content
34
- end
35
-
36
- def append_random_xml_content(xml_content, length)
37
- begin
38
- random_tag = random_string(rand(10)+1)
39
- xml_content << %{
40
- <#{random_tag}>#{random_string(rand(300)+1)}</#{random_tag}>
41
- }
42
- end while (xml_content.length < length)
43
- end
44
-
45
- def random_string(length)
46
- (0...length).map { 65.+(rand(26)).chr }.join
47
- end
48
-
49
- def run_with(truncation_klass)
50
- puts "Running benchmark for #{truncation_klass}..."
51
- truncated_string = ""
52
- result = Benchmark.measure { truncated_string = truncation_klass.truncate synthetic_xml, max_length: TRUNCATION_LENGTH, count_tags: true }
53
- {truncated_length: truncated_string.length, time: result.total}
54
- end
55
-
56
- def show_results(results)
57
- puts results.inspect
58
- end
59
-
60
- end
61
- end
@@ -1,9 +0,0 @@
1
- # Adapter for comparing https://github.com/nono/HTML-Truncator
2
- module Truncato
3
- class PeppercornAdapter
4
- def self.truncate string, options
5
- string.truncate_html options[:max_length], :tail=>options[:tail]
6
- end
7
- end
8
- end
9
-
@@ -1,11 +0,0 @@
1
- # Adapter for comparing https://github.com/nono/HTML-Truncator
2
- module Truncato
3
- class VendorHtmlTruncatorAdapter
4
- def self.truncate string, options
5
- HTML_Truncator.truncate string, options[:max_length], ellipsis: "..."
6
- end
7
- end
8
- end
9
-
10
- #[{Truncato::VendorHtmlTruncatorAdapter=>{:truncated_length=>3584682, :time=>223.36}}]
11
-
@@ -1,18 +0,0 @@
1
- $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
2
-
3
- require 'rubygems'
4
- require 'bundler'
5
- require 'nokogiri'
6
- require 'truncato'
7
- require 'html_truncator'
8
- require 'peppercorn'
9
- require 'benchmark'
10
-
11
- Bundler.setup
12
- Bundler.require
13
-
14
- Dir[File.dirname(__FILE__) + '/truncato/**/*.rb'].each do |file|
15
- load file
16
- end
17
-
18
-
data/spec/spec_helper.rb DELETED
@@ -1,17 +0,0 @@
1
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
- $LOAD_PATH.unshift(File.dirname(__FILE__))
3
-
4
- require 'rubygems'
5
- require 'bundler'
6
- require 'nokogiri'
7
-
8
- Bundler.setup
9
- Bundler.require
10
-
11
- # Requires supporting files with custom matchers and macros, etc,
12
- # in ./support/ and its subdirectories.
13
- Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
14
-
15
- RSpec.configure do |config|
16
- config.extend TruncatoMacros
17
- end
@@ -1,8 +0,0 @@
1
- module TruncatoMacros
2
- def it_should_truncate(example_description, options)
3
- it "should truncate #{example_description}" do
4
- expected_options = Truncato::DEFAULT_OPTIONS.merge(options[:with])
5
- Truncato.truncate(options[:source], expected_options).should == options[:expected]
6
- end
7
- end
8
- end
@@ -1,127 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe "Truncato" do
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..."
7
- it_should_truncate "no html text with longer length", with: {max_length: 4}, source: "some", expected: "some"
8
- end
9
-
10
- describe "html tags structure" do
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
-
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>"
14
-
15
- it_should_truncate "html text with nested tags (first node)", with: {max_length: 9},
16
- source: "<div><p>some text 1</p><p>some text 2</p></div>",
17
- expected: "<div><p>s...</p></div>"
18
-
19
- it_should_truncate "html text with nested tags (second node)", with: {max_length: 33},
20
- source: "<div><p>some text 1</p><p>some text 2</p></div>",
21
- expected: "<div><p>some text 1</p><p>some te...</p></div>"
22
-
23
- it_should_truncate "html text with nested tags (empty contents)", with: {max_length: 3},
24
- source: "<div><p>some text 1</p><p>some text 2</p></div>",
25
- expected: "<div>...</div>"
26
-
27
- it_should_truncate "html text with special html entioes", with: {max_length: 5},
28
- source: "<p>&gt;some text</p>",
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>"
34
- end
35
-
36
- describe "include tail as part of max_length" do
37
- it_should_truncate "html text with a tag (counting tail)", with: {max_length: 4, count_tail: true, count_tags: false},
38
- source: "<p>some text</p>",
39
- expected: "<p>s...</p>"
40
-
41
- it_should_truncate "html text with a tag (counting tail)", with: {max_length: 6, count_tail: true, count_tags: false}, source: "<p>some text</p>", expected: "<p>som...</p>"
42
-
43
- it_should_truncate "html text with a tag (counting tail)", with: {max_length: 16, count_tail: true, count_tags: false},
44
- source: "<p>some text</p><div><span>some other text</span></div>",
45
- expected: "<p>some text</p><div><span>some...</span></div>"
46
-
47
- it_should_truncate "html text with a tag (counting tail and including tail before final tag)", with: {max_length: 16, count_tail: true, count_tags: false, tail_before_final_tag: true},
48
- source: "<p>some text</p><div><span>some other text</span></div>",
49
- expected: "<p>some text</p><div><span>some</span>...</div>"
50
-
51
- it_should_truncate "html text, counting special html characters as one character",
52
- with: {max_length: 16, count_tail: true, count_tags: false, tail_before_final_tag: true, tail: '&hellip;'},
53
- source: "<p>some text</p><div><span>some other text</span></div>",
54
- expected: "<p>some text</p><div><span>some o</span>&hellip;</div>"
55
- end
56
-
57
- describe "insert tail between two or more final tags" do
58
- it_should_truncate "html text as normal when tail_before_final_tag option is not set",
59
- with: {max_length: 4, count_tags: false},
60
- source: "<p><span>some text</span>some more text</p>",
61
- expected: "<p><span>some...</span></p>"
62
-
63
- it_should_truncate "html text when tail_before_final_tag: true by inserting tail before the final tag, and after any other closing tags",
64
- with: {max_length: 4, count_tags: false, tail_before_final_tag: true},
65
- source: "<p><span>some text</span>some more text</p>",
66
- expected: "<p><span>some</span>...</p>"
67
- end
68
-
69
- describe "single html tag elements" do
70
- it_should_truncate "html text with <br /> element without adding a closing tag", with: {max_length: 9},
71
- source: "<div><p><br />some text 1</p><p>some text 2</p></div>",
72
- expected: "<div><p><br />...</p></div>"
73
-
74
- it_should_truncate "html text with <img /> element without adding a closing tag", with: {max_length: 9},
75
- source: "<div><p><img src='some_path' />some text 1</p><p>some text 2</p></div>",
76
- expected: "<div><p><img src='some_path' />...</p></div>"
77
- end
78
-
79
- describe "comment html element" do
80
- it_should_truncate "html text and ignore <!-- a comment --> element by default", with: {max_length: 20},
81
- source: "<!-- a comment --><p>some text 1</p>",
82
- expected: "<p>some text 1</p>"
83
-
84
- it_should_truncate "html text with <!-- a comment --> element", with: {max_length: 30, comments: true},
85
- source: "<!-- a comment --><p>some text 1</p>",
86
- expected: "<!-- a comment --><p>some text...</p>"
87
-
88
- it_should_truncate "html text with <!-- a comment --> element that exceeds the max_length", with: {max_length: 5, comments: true},
89
- source: "<!-- a comment --><p>some text 1</p>",
90
- expected: "<!-- ...-->"
91
-
92
- it_should_truncate "html text with <!-- a comment --> element with other elements that exceeds max_length", with: {max_length: 20, comments: true},
93
- source: "<!-- a comment --><p>some text 1</p>",
94
- expected: "<!-- a comment --><p>...</p>"
95
- end
96
-
97
- describe "html attributes" do
98
- it_should_truncate "html text with 1 attributes", with: {max_length: 3, count_tags: false},
99
- source: "<p attr1='1'>some text</p>",
100
- expected: "<p attr1='1'>som...</p>"
101
-
102
- it_should_truncate "html text with 1 attributes counting its size", with: {max_length: 16, count_tags: true},
103
- source: "<p attr1='1'>some text</p>",
104
- expected: "<p attr1='1'>som...</p>"
105
-
106
- it_should_truncate "html text with 2 attributes", with: {max_length: 3, count_tags: false},
107
- source: "<p attr1='1' attr2='2'>some text</p>",
108
- expected: "<p attr1='1' attr2='2'>som...</p>"
109
-
110
- it_should_truncate "html text with attributes in nested tags", with: {max_length: 4, count_tags: false},
111
- source: "<div><p attr1='1'>some text</p></div>",
112
- expected: "<div><p attr1='1'>some...</p></div>"
113
-
114
- it_should_truncate "html text with attribute containing entities respecting them", with: {max_length: 3, count_tags: false, filtered_attributes: ['attr2']},
115
- source: "<p attr1='&gt;some'>text</p>",
116
- expected: "<p attr1='&gt;some'>tex...</p>"
117
-
118
- it_should_truncate "html text with 2 attributes filtering one of them", with: {max_length: 90, count_tags: false, filtered_attributes: ['attr2']},
119
- source: "<p attr1='1'>some text</p><p attr2='2'>filtered text</p>",
120
- expected: "<p attr1='1'>some text</p><p>filtered text</p>"
121
-
122
- it_should_truncate "html text with 2 attributes filtering all of them", with: {max_length: 3, count_tags: false, filtered_attributes: ['attr1', 'attr2']},
123
- source: "<p attr1='1' attr2='2'>some text</p>",
124
- expected: "<p>som...</p>"
125
- end
126
-
127
- end
data/truncato.gemspec DELETED
@@ -1,78 +0,0 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
- # -*- encoding: utf-8 -*-
5
-
6
- Gem::Specification.new do |s|
7
- s.name = "truncato"
8
- s.version = "0.7.5"
9
-
10
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["Jorge Manrubia"]
12
- s.date = "2013-04-27"
13
- s.description = "Ruby tool for truncating HTML strings keeping a valid HTML markup"
14
- s.email = "jorge.manrubia@gmail.com"
15
- s.extra_rdoc_files = [
16
- "LICENSE.txt",
17
- "README.md"
18
- ]
19
- s.files = [
20
- ".ruby-version",
21
- ".rvmrc",
22
- "Gemfile",
23
- "LICENSE.txt",
24
- "README.md",
25
- "Rakefile",
26
- "VERSION",
27
- "benchmark/truncato/benchmark_runner.rb",
28
- "benchmark/truncato/vendor/peppercorn_adapter.rb",
29
- "benchmark/truncato/vendor/vendor_html_truncator_adapter.rb",
30
- "benchmark/truncato_benchmark.rb",
31
- "lib/truncato.rb",
32
- "lib/truncato/truncated_sax_document.rb",
33
- "lib/truncato/truncato.rb",
34
- "spec/spec_helper.rb",
35
- "spec/support/spec_helpers/truncato_macros.rb",
36
- "spec/truncato/truncato_spec.rb",
37
- "truncato.gemspec"
38
- ]
39
- s.homepage = "https://github.com/jorgemanrubia/truncato"
40
- s.licenses = ["MIT"]
41
- s.require_paths = ["lib"]
42
- s.rubygems_version = "1.8.23"
43
- s.summary = "A tool for truncating HTML strings efficiently"
44
-
45
- if s.respond_to? :specification_version then
46
- s.specification_version = 3
47
-
48
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
49
- s.add_runtime_dependency(%q<truncato>, [">= 0"])
50
- s.add_runtime_dependency(%q<nokogiri>, ["~> 1.5.5"])
51
- s.add_runtime_dependency(%q<htmlentities>, ["~> 4.3.1"])
52
- s.add_development_dependency(%q<bundler>, ["~> 1.3"])
53
- s.add_development_dependency(%q<bundler>, ["~> 1.3"])
54
- s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
55
- s.add_development_dependency(%q<bundler>, ["~> 1.3"])
56
- s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
57
- else
58
- s.add_dependency(%q<truncato>, [">= 0"])
59
- s.add_dependency(%q<nokogiri>, ["~> 1.5.5"])
60
- s.add_dependency(%q<htmlentities>, ["~> 4.3.1"])
61
- s.add_dependency(%q<bundler>, ["~> 1.3"])
62
- s.add_dependency(%q<bundler>, ["~> 1.3"])
63
- s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
64
- s.add_dependency(%q<bundler>, ["~> 1.3"])
65
- s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
66
- end
67
- else
68
- s.add_dependency(%q<truncato>, [">= 0"])
69
- s.add_dependency(%q<nokogiri>, ["~> 1.5.5"])
70
- s.add_dependency(%q<htmlentities>, ["~> 4.3.1"])
71
- s.add_dependency(%q<bundler>, ["~> 1.3"])
72
- s.add_dependency(%q<bundler>, ["~> 1.3"])
73
- s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
74
- s.add_dependency(%q<bundler>, ["~> 1.3"])
75
- s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
76
- end
77
- end
78
-