truncato 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.rvmrc +52 -0
- data/Gemfile +26 -0
- data/Gemfile.lock +43 -0
- data/LICENSE.txt +20 -0
- data/README.md +65 -0
- data/Rakefile +66 -0
- data/VERSION +2 -0
- data/benchmark/truncato/benchmark_runner.rb +60 -0
- data/benchmark/truncato/vendor/vendor_html_truncator_adapter.rb +11 -0
- data/benchmark/truncato_benchmark.rb +17 -0
- data/lib/truncato/truncated_sax_document.rb +83 -0
- data/lib/truncato/truncato.rb +36 -0
- data/lib/truncato.rb +5 -0
- data/spec/spec_helper.rb +17 -0
- data/spec/support/spec_helpers/truncato_macros.rb +8 -0
- data/spec/truncato/truncato_spec.rb +33 -0
- data/truncato.gemspec +74 -0
- metadata +179 -0
data/.rvmrc
ADDED
@@ -0,0 +1,52 @@
|
|
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
ADDED
@@ -0,0 +1,26 @@
|
|
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.2.1"
|
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
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,43 @@
|
|
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!
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Jorge Manrubia
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
# truncato
|
2
|
+
|
3
|
+
*truncato* is a Ruby library for truncating HTML strings keeping the markup valid.
|
4
|
+
|
5
|
+
## Installing
|
6
|
+
|
7
|
+
In your `Gemfile`
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'truncato'
|
11
|
+
```
|
12
|
+
|
13
|
+
## Usage
|
14
|
+
|
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>"
|
18
|
+
```
|
19
|
+
|
20
|
+
The configuration options are:
|
21
|
+
|
22
|
+
* `max_length`: The size, in characters, to truncate (`30` by default)
|
23
|
+
* `tail`: The string to append when the truncation occurs ('...' by default)
|
24
|
+
* `count_tags`: Boolean value indicating whether tags size should be considered when truncating (`true` by default)
|
25
|
+
|
26
|
+
## Performance
|
27
|
+
|
28
|
+
Truncato was designed with performance in mind. Its main motivation was that existing libs couldn't truncate a multiple-MB document into a few-KB one in a reasonable time. It uses the [Nokogiri](http://nokogiri.org/) SAX parser.
|
29
|
+
|
30
|
+
There is a benchmark included that generates a synthetic XML of 4MB and truncates it to 400 KB. You can run the benchmark using
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
rake truncato:benchmark
|
34
|
+
```
|
35
|
+
|
36
|
+
There is a also a comparison benchmark that tests the previous data with other alternatives
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
rake truncato:vendor_compare
|
40
|
+
```
|
41
|
+
|
42
|
+
The results comparing truncato with other libs:
|
43
|
+
|
44
|
+
<table>
|
45
|
+
<tr>
|
46
|
+
<th></th>
|
47
|
+
<th>Truncato</th>
|
48
|
+
<th><a href="https://github.com/ianwhite/truncate_html">truncate_html</a></th>
|
49
|
+
<th><a href="https://github.com/nono/HTML-Truncator">HTML Truncator</a></th>
|
50
|
+
</tr>
|
51
|
+
<tr>
|
52
|
+
<th>Time for truncating a 4MB XML document to 4KB</th>
|
53
|
+
<td>1.5 s</td>
|
54
|
+
<td>20 s</td>
|
55
|
+
<td>220 s</td>
|
56
|
+
</tr>
|
57
|
+
</table>
|
58
|
+
|
59
|
+
## Running the tests
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
rake spec
|
63
|
+
```
|
64
|
+
|
65
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
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
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
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
|
33
|
+
|
34
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
35
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
36
|
+
spec.rcov = true
|
37
|
+
end
|
38
|
+
|
39
|
+
task :default => :spec
|
40
|
+
|
41
|
+
require 'rake/rdoctask'
|
42
|
+
Rake::RDocTask.new do |rdoc|
|
43
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
44
|
+
|
45
|
+
rdoc.rdoc_dir = 'rdoc'
|
46
|
+
rdoc.title = "solid_assert #{version}"
|
47
|
+
rdoc.rdoc_files.include('README*')
|
48
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
49
|
+
end
|
50
|
+
|
51
|
+
$:.unshift File.join(File.dirname(__FILE__), 'benchmark')
|
52
|
+
|
53
|
+
require 'nokogiri'
|
54
|
+
require 'truncato'
|
55
|
+
require 'truncato_benchmark'
|
56
|
+
|
57
|
+
namespace :truncato do
|
58
|
+
task :benchmark do
|
59
|
+
Truncato::BenchmarkRunner.new.run
|
60
|
+
end
|
61
|
+
|
62
|
+
task :vendor_compare do
|
63
|
+
Truncato::BenchmarkRunner.new.run_comparison
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1,60 @@
|
|
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]
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def run_suite(truncation_classes)
|
24
|
+
results = truncation_classes.collect { |klass| {klass => run_with(klass)} }
|
25
|
+
show_results results
|
26
|
+
end
|
27
|
+
|
28
|
+
def create_synthetic_xml(length)
|
29
|
+
xml_content = "<synthetic-root>"
|
30
|
+
append_random_xml_content xml_content, length
|
31
|
+
xml_content << "</synthetic-root>"
|
32
|
+
xml_content
|
33
|
+
end
|
34
|
+
|
35
|
+
def append_random_xml_content(xml_content, length)
|
36
|
+
begin
|
37
|
+
random_tag = random_string(rand(10)+1)
|
38
|
+
xml_content << %{
|
39
|
+
<#{random_tag}>#{random_string(rand(300)+1)}</#{random_tag}>
|
40
|
+
}
|
41
|
+
end while (xml_content.length < length)
|
42
|
+
end
|
43
|
+
|
44
|
+
def random_string(length)
|
45
|
+
(0...length).map { 65.+(rand(26)).chr }.join
|
46
|
+
end
|
47
|
+
|
48
|
+
def run_with(truncation_klass)
|
49
|
+
puts "Running benchmark for #{truncation_klass}..."
|
50
|
+
truncated_string = ""
|
51
|
+
result = Benchmark.measure { truncated_string = truncation_klass.truncate synthetic_xml, max_length: TRUNCATION_LENGTH, count_tags: true }
|
52
|
+
{truncated_length: truncated_string.length, time: result.total}
|
53
|
+
end
|
54
|
+
|
55
|
+
def show_results(results)
|
56
|
+
puts results.inspect
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,11 @@
|
|
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
|
+
|
@@ -0,0 +1,17 @@
|
|
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 'benchmark'
|
9
|
+
|
10
|
+
Bundler.setup
|
11
|
+
Bundler.require
|
12
|
+
|
13
|
+
Dir[File.dirname(__FILE__) + '/truncato/**/*.rb'].each do |file|
|
14
|
+
load file
|
15
|
+
end
|
16
|
+
|
17
|
+
|
@@ -0,0 +1,83 @@
|
|
1
|
+
class TruncatedSaxDocument < Nokogiri::XML::SAX::Document
|
2
|
+
attr_reader :truncated_string, :max_length, :max_length_reached, :tail, :count_tags
|
3
|
+
|
4
|
+
def initialize(options)
|
5
|
+
init_from_options(options)
|
6
|
+
@html_coder = HTMLEntities.new
|
7
|
+
@truncated_string = ""
|
8
|
+
@closing_tags = []
|
9
|
+
@estimated_length = 0
|
10
|
+
@max_length_reached = false
|
11
|
+
end
|
12
|
+
|
13
|
+
def start_element name, attributes
|
14
|
+
return if @max_length_reached
|
15
|
+
@closing_tags.push name
|
16
|
+
append_to_truncated_string opening_tag(name), overriden_tag_length
|
17
|
+
end
|
18
|
+
|
19
|
+
def characters decoded_string
|
20
|
+
return if @max_length_reached
|
21
|
+
remaining_length = max_length - @estimated_length - 1
|
22
|
+
string_to_append = decoded_string.length > remaining_length ? truncate_string(decoded_string, remaining_length) : decoded_string
|
23
|
+
append_to_truncated_string @html_coder.encode(string_to_append), string_to_append.length
|
24
|
+
end
|
25
|
+
|
26
|
+
def end_element name
|
27
|
+
return if @max_length_reached
|
28
|
+
@closing_tags.pop
|
29
|
+
append_to_truncated_string closing_tag(name), overriden_tag_length
|
30
|
+
end
|
31
|
+
|
32
|
+
def end_document
|
33
|
+
close_truncated_document if max_length_reached
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def init_from_options(options)
|
39
|
+
@max_length = options[:max_length]
|
40
|
+
@count_tags = options [:count_tags]
|
41
|
+
@tail = options[:tail]
|
42
|
+
end
|
43
|
+
|
44
|
+
def append_to_truncated_string string, overriden_length=nil
|
45
|
+
@truncated_string << string
|
46
|
+
increase_estimated_length(overriden_length || string.length)
|
47
|
+
end
|
48
|
+
|
49
|
+
def opening_tag name
|
50
|
+
"<#{name}>"
|
51
|
+
end
|
52
|
+
|
53
|
+
def closing_tag name
|
54
|
+
"</#{name}>"
|
55
|
+
end
|
56
|
+
|
57
|
+
def increase_estimated_length amount
|
58
|
+
@estimated_length += amount
|
59
|
+
check_max_length_reached
|
60
|
+
end
|
61
|
+
|
62
|
+
def check_max_length_reached
|
63
|
+
@max_length_reached = true if @estimated_length >= max_length
|
64
|
+
end
|
65
|
+
|
66
|
+
def truncate_string string, remaining_length
|
67
|
+
@tail_appended = true
|
68
|
+
"#{string[0..remaining_length]}#{tail}"
|
69
|
+
end
|
70
|
+
|
71
|
+
def close_truncated_document
|
72
|
+
append_to_truncated_string tail unless @tail_appended
|
73
|
+
append_closing_tags
|
74
|
+
end
|
75
|
+
|
76
|
+
def append_closing_tags
|
77
|
+
@closing_tags.reverse.each { |name| append_to_truncated_string closing_tag(name) }
|
78
|
+
end
|
79
|
+
|
80
|
+
def overriden_tag_length
|
81
|
+
@count_tags ? nil : 0
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Truncato
|
2
|
+
DEFAULT_OPTIONS = {
|
3
|
+
max_length: 30,
|
4
|
+
count_tags: true,
|
5
|
+
tail: "..."
|
6
|
+
}
|
7
|
+
|
8
|
+
# Truncates the source XML string and returns the result
|
9
|
+
#
|
10
|
+
# @param [String] source the XML source to truncate
|
11
|
+
# @param [Hash] user_options truncation options
|
12
|
+
# @option user_options [Integer] :max_length Maximum length
|
13
|
+
# @option user_options [String] :tail text to append when the truncation occurs
|
14
|
+
# @option user_options [Boolean] :count_tags `true` for counting tags for truncation, `false` for not counting them
|
15
|
+
# @return [String] the truncated string
|
16
|
+
def self.truncate source, user_options={}
|
17
|
+
options = DEFAULT_OPTIONS.merge(user_options)
|
18
|
+
self.truncate_html(source, options) || self.truncate_no_html(source, options)
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def self.truncate_html source, options
|
24
|
+
truncated_sax_document = TruncatedSaxDocument.new(options)
|
25
|
+
parser = Nokogiri::XML::SAX::Parser.new(truncated_sax_document)
|
26
|
+
parser.parse(source) { |context| context.replace_entities = false }
|
27
|
+
truncated_string = truncated_sax_document.truncated_string
|
28
|
+
truncated_string.empty? ? nil : truncated_string
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.truncate_no_html source, options
|
32
|
+
max_length = options[:max_length]
|
33
|
+
tail = source.length > max_length ? options[:tail] : ''
|
34
|
+
"#{source[0..max_length-1]}#{tail}"
|
35
|
+
end
|
36
|
+
end
|
data/lib/truncato.rb
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
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
|
@@ -0,0 +1,8 @@
|
|
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
|
@@ -0,0 +1,33 @@
|
|
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 strings" 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>>some text</p>",
|
29
|
+
expected: "<p>>s...</p>"
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
data/truncato.gemspec
ADDED
@@ -0,0 +1,74 @@
|
|
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.0"
|
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 = "2012-11-12"
|
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
|
+
".rvmrc",
|
21
|
+
"Gemfile",
|
22
|
+
"Gemfile.lock",
|
23
|
+
"LICENSE.txt",
|
24
|
+
"README.md",
|
25
|
+
"Rakefile",
|
26
|
+
"VERSION",
|
27
|
+
"benchmark/truncato/benchmark_runner.rb",
|
28
|
+
"benchmark/truncato/vendor/vendor_html_truncator_adapter.rb",
|
29
|
+
"benchmark/truncato_benchmark.rb",
|
30
|
+
"lib/truncato.rb",
|
31
|
+
"lib/truncato/truncated_sax_document.rb",
|
32
|
+
"lib/truncato/truncato.rb",
|
33
|
+
"spec/spec_helper.rb",
|
34
|
+
"spec/support/spec_helpers/truncato_macros.rb",
|
35
|
+
"spec/truncato/truncato_spec.rb",
|
36
|
+
"truncato.gemspec"
|
37
|
+
]
|
38
|
+
s.homepage = "https://github.com/jorgemanrubia/truncato"
|
39
|
+
s.licenses = ["MIT"]
|
40
|
+
s.require_paths = ["lib"]
|
41
|
+
s.rubygems_version = "1.8.24"
|
42
|
+
s.summary = "A tool for truncating HTML strings efficiently"
|
43
|
+
|
44
|
+
if s.respond_to? :specification_version then
|
45
|
+
s.specification_version = 3
|
46
|
+
|
47
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
48
|
+
s.add_runtime_dependency(%q<truncato>, [">= 0"])
|
49
|
+
s.add_runtime_dependency(%q<nokogiri>, ["~> 1.5.5"])
|
50
|
+
s.add_runtime_dependency(%q<htmlentities>, ["~> 4.3.1"])
|
51
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.2.1"])
|
52
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
|
53
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.2.1"])
|
54
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
|
55
|
+
else
|
56
|
+
s.add_dependency(%q<truncato>, [">= 0"])
|
57
|
+
s.add_dependency(%q<nokogiri>, ["~> 1.5.5"])
|
58
|
+
s.add_dependency(%q<htmlentities>, ["~> 4.3.1"])
|
59
|
+
s.add_dependency(%q<bundler>, ["~> 1.2.1"])
|
60
|
+
s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
|
61
|
+
s.add_dependency(%q<bundler>, ["~> 1.2.1"])
|
62
|
+
s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
|
63
|
+
end
|
64
|
+
else
|
65
|
+
s.add_dependency(%q<truncato>, [">= 0"])
|
66
|
+
s.add_dependency(%q<nokogiri>, ["~> 1.5.5"])
|
67
|
+
s.add_dependency(%q<htmlentities>, ["~> 4.3.1"])
|
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
|
+
end
|
73
|
+
end
|
74
|
+
|
metadata
ADDED
@@ -0,0 +1,179 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: truncato
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.7.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jorge Manrubia
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-12 00:00:00.000000000 Z
|
13
|
+
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
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: nokogiri
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 1.5.5
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 1.5.5
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: htmlentities
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 4.3.1
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 4.3.1
|
62
|
+
- !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.2.1
|
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.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
|
+
description: Ruby tool for truncating HTML strings keeping a valid HTML markup
|
127
|
+
email: jorge.manrubia@gmail.com
|
128
|
+
executables: []
|
129
|
+
extensions: []
|
130
|
+
extra_rdoc_files:
|
131
|
+
- LICENSE.txt
|
132
|
+
- README.md
|
133
|
+
files:
|
134
|
+
- .rvmrc
|
135
|
+
- Gemfile
|
136
|
+
- Gemfile.lock
|
137
|
+
- LICENSE.txt
|
138
|
+
- README.md
|
139
|
+
- Rakefile
|
140
|
+
- VERSION
|
141
|
+
- benchmark/truncato/benchmark_runner.rb
|
142
|
+
- benchmark/truncato/vendor/vendor_html_truncator_adapter.rb
|
143
|
+
- benchmark/truncato_benchmark.rb
|
144
|
+
- lib/truncato.rb
|
145
|
+
- lib/truncato/truncated_sax_document.rb
|
146
|
+
- lib/truncato/truncato.rb
|
147
|
+
- spec/spec_helper.rb
|
148
|
+
- spec/support/spec_helpers/truncato_macros.rb
|
149
|
+
- spec/truncato/truncato_spec.rb
|
150
|
+
- truncato.gemspec
|
151
|
+
homepage: https://github.com/jorgemanrubia/truncato
|
152
|
+
licenses:
|
153
|
+
- MIT
|
154
|
+
post_install_message:
|
155
|
+
rdoc_options: []
|
156
|
+
require_paths:
|
157
|
+
- lib
|
158
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
159
|
+
none: false
|
160
|
+
requirements:
|
161
|
+
- - ! '>='
|
162
|
+
- !ruby/object:Gem::Version
|
163
|
+
version: '0'
|
164
|
+
segments:
|
165
|
+
- 0
|
166
|
+
hash: 3596249890284373376
|
167
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
168
|
+
none: false
|
169
|
+
requirements:
|
170
|
+
- - ! '>='
|
171
|
+
- !ruby/object:Gem::Version
|
172
|
+
version: '0'
|
173
|
+
requirements: []
|
174
|
+
rubyforge_project:
|
175
|
+
rubygems_version: 1.8.24
|
176
|
+
signing_key:
|
177
|
+
specification_version: 3
|
178
|
+
summary: A tool for truncating HTML strings efficiently
|
179
|
+
test_files: []
|