truncate_html 0.5.1 → 0.9.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +7 -0
- data/.travis.yml +6 -0
- data/Gemfile +4 -7
- data/Gemfile.lock +91 -78
- data/README.md +97 -0
- data/Rakefile +12 -31
- data/VERSION +1 -1
- data/lib/app/helpers/truncate_html_helper.rb +1 -1
- data/lib/truncate_html/configuration.rb +1 -2
- data/lib/truncate_html/html_string.rb +17 -16
- data/lib/truncate_html/html_truncator.rb +69 -39
- data/lib/truncate_html/version.rb +3 -0
- data/lib/truncate_html.rb +6 -4
- data/spec/helpers/truncate_html_helper_spec.rb +7 -7
- data/spec/rails_root/Gemfile +2 -2
- data/spec/rails_root/Gemfile.lock +84 -71
- data/spec/spec_helper.rb +2 -0
- data/spec/truncate_html/html_string_spec.rb +28 -22
- data/spec/truncate_html/html_truncator_spec.rb +165 -88
- data/spec/truncate_html/truncate_html_spec.rb +15 -0
- data/truncate_html.gemspec +14 -101
- metadata +63 -89
- data/README.markdown +0 -65
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8b62f471b431ebc1482936c69165f634440e9f97
|
4
|
+
data.tar.gz: 55123d5396db552d9a0ab64b834de98afbe17688
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9d0fcc597a3e4e8f39a08bf5e0cfda14cf692dc6753727a2812ffa9338e4567ecdbd51a7751f2a8649aca6a8fa8335c8c3609313673e9156225d7df9d5472590
|
7
|
+
data.tar.gz: 78b8a0c36a8bf3c12390d7606ee2a4561006787dd527c166fcdd9c8104c1cd7f23680e6cb337e6b0b9d47bfa680932666c87a39400c07374371381d2d1729e79
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
CHANGED
@@ -1,7 +1,4 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
gem "rails", "= 3.0.3"
|
6
|
-
gem 'rspec-rails', '= 2.3'
|
7
|
-
end
|
1
|
+
source "http://rubygems.org"
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in truncate_html.gemspec
|
4
|
+
gemspec
|
data/Gemfile.lock
CHANGED
@@ -1,92 +1,105 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
truncate_html (0.9.3)
|
5
|
+
|
1
6
|
GEM
|
2
|
-
remote: http://
|
7
|
+
remote: http://rubygems.org/
|
3
8
|
specs:
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
rack (~> 1.2
|
15
|
-
rack-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
arel (
|
32
|
-
builder (
|
33
|
-
diff-lcs (1.
|
34
|
-
erubis (2.
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
git (>= 1.2.5)
|
41
|
-
rake
|
42
|
-
mail (2.2.15)
|
43
|
-
activesupport (>= 2.3.6)
|
9
|
+
actionmailer (3.2.13)
|
10
|
+
actionpack (= 3.2.13)
|
11
|
+
mail (~> 2.5.3)
|
12
|
+
actionpack (3.2.13)
|
13
|
+
activemodel (= 3.2.13)
|
14
|
+
activesupport (= 3.2.13)
|
15
|
+
builder (~> 3.0.0)
|
16
|
+
erubis (~> 2.7.0)
|
17
|
+
journey (~> 1.0.4)
|
18
|
+
rack (~> 1.4.5)
|
19
|
+
rack-cache (~> 1.2)
|
20
|
+
rack-test (~> 0.6.1)
|
21
|
+
sprockets (~> 2.2.1)
|
22
|
+
activemodel (3.2.13)
|
23
|
+
activesupport (= 3.2.13)
|
24
|
+
builder (~> 3.0.0)
|
25
|
+
activerecord (3.2.13)
|
26
|
+
activemodel (= 3.2.13)
|
27
|
+
activesupport (= 3.2.13)
|
28
|
+
arel (~> 3.0.2)
|
29
|
+
tzinfo (~> 0.3.29)
|
30
|
+
activeresource (3.2.13)
|
31
|
+
activemodel (= 3.2.13)
|
32
|
+
activesupport (= 3.2.13)
|
33
|
+
activesupport (3.2.13)
|
34
|
+
i18n (= 0.6.1)
|
35
|
+
multi_json (~> 1.0)
|
36
|
+
arel (3.0.2)
|
37
|
+
builder (3.0.4)
|
38
|
+
diff-lcs (1.2.2)
|
39
|
+
erubis (2.7.0)
|
40
|
+
hike (1.2.1)
|
41
|
+
i18n (0.6.1)
|
42
|
+
journey (1.0.4)
|
43
|
+
json (1.7.7)
|
44
|
+
mail (2.5.3)
|
44
45
|
i18n (>= 0.4.0)
|
45
46
|
mime-types (~> 1.16)
|
46
47
|
treetop (~> 1.4.8)
|
47
|
-
mime-types (1.
|
48
|
-
|
49
|
-
|
50
|
-
rack
|
51
|
-
|
52
|
-
|
48
|
+
mime-types (1.22)
|
49
|
+
multi_json (1.7.2)
|
50
|
+
polyglot (0.3.3)
|
51
|
+
rack (1.4.5)
|
52
|
+
rack-cache (1.2)
|
53
|
+
rack (>= 0.4)
|
54
|
+
rack-ssl (1.3.3)
|
55
|
+
rack
|
56
|
+
rack-test (0.6.2)
|
53
57
|
rack (>= 1.0)
|
54
|
-
rails (3.
|
55
|
-
actionmailer (= 3.
|
56
|
-
actionpack (= 3.
|
57
|
-
activerecord (= 3.
|
58
|
-
activeresource (= 3.
|
59
|
-
activesupport (= 3.
|
58
|
+
rails (3.2.13)
|
59
|
+
actionmailer (= 3.2.13)
|
60
|
+
actionpack (= 3.2.13)
|
61
|
+
activerecord (= 3.2.13)
|
62
|
+
activeresource (= 3.2.13)
|
63
|
+
activesupport (= 3.2.13)
|
60
64
|
bundler (~> 1.0)
|
61
|
-
railties (= 3.
|
62
|
-
railties (3.
|
63
|
-
actionpack (= 3.
|
64
|
-
activesupport (= 3.
|
65
|
+
railties (= 3.2.13)
|
66
|
+
railties (3.2.13)
|
67
|
+
actionpack (= 3.2.13)
|
68
|
+
activesupport (= 3.2.13)
|
69
|
+
rack-ssl (~> 1.3.2)
|
65
70
|
rake (>= 0.8.7)
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
rspec-
|
73
|
-
|
74
|
-
|
75
|
-
rspec-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
rspec (~> 2.
|
81
|
-
|
82
|
-
|
71
|
+
rdoc (~> 3.4)
|
72
|
+
thor (>= 0.14.6, < 2.0)
|
73
|
+
rake (10.0.4)
|
74
|
+
rdoc (3.12.2)
|
75
|
+
json (~> 1.4)
|
76
|
+
rspec-core (2.13.1)
|
77
|
+
rspec-expectations (2.13.0)
|
78
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
79
|
+
rspec-mocks (2.13.0)
|
80
|
+
rspec-rails (2.13.0)
|
81
|
+
actionpack (>= 3.0)
|
82
|
+
activesupport (>= 3.0)
|
83
|
+
railties (>= 3.0)
|
84
|
+
rspec-core (~> 2.13.0)
|
85
|
+
rspec-expectations (~> 2.13.0)
|
86
|
+
rspec-mocks (~> 2.13.0)
|
87
|
+
sprockets (2.2.2)
|
88
|
+
hike (~> 1.2)
|
89
|
+
multi_json (~> 1.0)
|
90
|
+
rack (~> 1.0)
|
91
|
+
tilt (~> 1.1, != 1.3.0)
|
92
|
+
thor (0.18.1)
|
93
|
+
tilt (1.3.6)
|
94
|
+
treetop (1.4.12)
|
95
|
+
polyglot
|
83
96
|
polyglot (>= 0.3.1)
|
84
|
-
tzinfo (0.3.
|
97
|
+
tzinfo (0.3.37)
|
85
98
|
|
86
99
|
PLATFORMS
|
87
100
|
ruby
|
88
101
|
|
89
102
|
DEPENDENCIES
|
90
|
-
|
91
|
-
rails (
|
92
|
-
|
103
|
+
rails (~> 3.2.13)
|
104
|
+
rspec-rails (~> 2.13)
|
105
|
+
truncate_html!
|
data/README.md
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
TruncateHtml
|
2
|
+
============
|
3
|
+
|
4
|
+
[![Build Status](https://secure.travis-ci.org/hgmnz/truncate_html.png?branch=master)](http://travis-ci.org/hgmnz/truncate_html)
|
5
|
+
[![Code Climate](https://codeclimate.com/github/hgmnz/truncate_html.png)](https://codeclimate.com/github/hgmnz/truncate_html)
|
6
|
+
|
7
|
+
truncate_html cuts off a string of HTML and takes care of closing any lingering open tags. There are many ways to solve this. This library does not have any dependencies, and [parses HTML using regular expressions](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454).
|
8
|
+
|
9
|
+
It can be used with or without Rails.
|
10
|
+
|
11
|
+
Example
|
12
|
+
-------
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
some_html = '<ul><li><a href="http://whatever">This is a link</a></li></ul>'
|
16
|
+
truncate_html(some_html, length: 15, omission: '...(continued)')
|
17
|
+
=> <ul><li><a href="http://whatever">This...(continued)</a></li></ul>
|
18
|
+
```
|
19
|
+
|
20
|
+
A few notes:
|
21
|
+
|
22
|
+
* By default, it will truncate on word boundary.
|
23
|
+
To truncate the HTML string strictly at the specified length, pass in the `word_boundary: false` option.
|
24
|
+
* If the input HTML is nil, it will return an empty string.
|
25
|
+
* The omission text's length does count toward the resulting string's length.
|
26
|
+
* `<script>` tags will pass right through - they will not count toward the resulting string's length, or be truncated.
|
27
|
+
|
28
|
+
* The default options are:
|
29
|
+
```
|
30
|
+
length: 100
|
31
|
+
omission: '...'
|
32
|
+
word_boundary: /\S/
|
33
|
+
```
|
34
|
+
|
35
|
+
You may also set global configuration options.
|
36
|
+
For example, place the following on application boot,
|
37
|
+
something like `config/initializers/truncate_html.rb`
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
TruncateHtml.configure do |config|
|
41
|
+
config.length = 50
|
42
|
+
config.omission = '...(continued)'
|
43
|
+
end
|
44
|
+
```
|
45
|
+
|
46
|
+
If you really want, you can even set a custom word boundary regexp.
|
47
|
+
For example, to truncate at the end of the nearest sentence:
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
TruncateHtml.configure do |config|
|
51
|
+
config.word_boundary = /\S[\.\?\!]/
|
52
|
+
end
|
53
|
+
```
|
54
|
+
|
55
|
+
You can also truncate the HTML at a specific point not based on length but content.
|
56
|
+
To do that, place the `:break_token` in your source. This allows the truncation to be
|
57
|
+
data driven, breaking after a leading paragraph or sentence. If the
|
58
|
+
`:break_token` is in your content before the specified `:length`, `:length` will be
|
59
|
+
ignored and the content truncated at `:break_token`.
|
60
|
+
If the `:break_token` is in your content after the specified `:length`,
|
61
|
+
`:break_token` will be ignored and the content truncated at `:length`.
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
TruncateHtml.configure do |config|
|
65
|
+
config.break_token = '<!-- truncate -->'
|
66
|
+
end
|
67
|
+
```
|
68
|
+
Installation
|
69
|
+
------------
|
70
|
+
|
71
|
+
The latest gem version for the Rails 2.x series is 0.3.2.
|
72
|
+
To use truncate_html on a Rails 2 app, please install the 0.3.2 version:
|
73
|
+
|
74
|
+
gem install truncate_html -v 0.3.2
|
75
|
+
|
76
|
+
For Rails 3, use the latest truncate_html:
|
77
|
+
|
78
|
+
gem install truncate_html
|
79
|
+
|
80
|
+
Issues or Suggestions
|
81
|
+
---------------------
|
82
|
+
|
83
|
+
Found an issue or have a suggestion? Please report it on [Github's issue tracker](http://github.com/hgmnz/truncate_html/issues).
|
84
|
+
|
85
|
+
Testing
|
86
|
+
-------
|
87
|
+
|
88
|
+
```shell
|
89
|
+
bundle
|
90
|
+
rake
|
91
|
+
```
|
92
|
+
|
93
|
+
All green? Go hack.
|
94
|
+
|
95
|
+
Copyright (c) 2009 - 2010 Harold A. Giménez, released under the MIT license
|
96
|
+
|
97
|
+
Thanks to all the [contributors](https://github.com/hgmnz/truncate_html/contributors)!
|
data/Rakefile
CHANGED
@@ -34,36 +34,17 @@ RSpec::Core::RakeTask.new(:spec) do |t|
|
|
34
34
|
t.pattern = "./spec/**/*_spec.rb"
|
35
35
|
end
|
36
36
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
end
|
50
|
-
rescue LoadError
|
51
|
-
puts "Install metric_fu to run code metrics"
|
52
|
-
end
|
53
|
-
|
54
|
-
begin
|
55
|
-
require 'jeweler'
|
56
|
-
Jeweler::Tasks.new do |gem|
|
57
|
-
gem.name = "truncate_html"
|
58
|
-
gem.summary = %Q{Uses an API similar to Rails' truncate helper to truncate HTML and close any lingering open tags.}
|
59
|
-
gem.description = %Q{Truncates html so you don't have to}
|
60
|
-
gem.email = "harold.gimenez@gmail.com"
|
61
|
-
gem.homepage = "http://github.com/hgimenez/truncate_html"
|
62
|
-
gem.authors = ["Harold A. Gimenez"]
|
63
|
-
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
37
|
+
namespace :gem do
|
38
|
+
desc "Release to rubygems"
|
39
|
+
task :release do
|
40
|
+
require File.expand_path('lib/truncate_html/version', File.dirname(__FILE__))
|
41
|
+
version = TruncateHtml::VERSION
|
42
|
+
message = "Bump to version #{version}"
|
43
|
+
system "git tag -a -m '#{message}' v#{version}"
|
44
|
+
system "git push origin master"
|
45
|
+
system "git push origin $(git tag | tail -1)"
|
46
|
+
system "gem build truncate_html.gemspec"
|
47
|
+
system "gem push truncate_html-#{version}.gem"
|
48
|
+
system "rm truncate_html-#{version}.gem"
|
64
49
|
end
|
65
|
-
Jeweler::GemcutterTasks.new
|
66
|
-
|
67
|
-
rescue LoadError
|
68
|
-
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
69
50
|
end
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.9.2
|
@@ -3,7 +3,7 @@ module TruncateHtmlHelper
|
|
3
3
|
def truncate_html(html, options={})
|
4
4
|
return '' if html.nil?
|
5
5
|
html_string = TruncateHtml::HtmlString.new(html)
|
6
|
-
TruncateHtml::HtmlTruncator.new(html_string).truncate
|
6
|
+
TruncateHtml::HtmlTruncator.new(html_string, options).truncate.html_safe
|
7
7
|
end
|
8
8
|
|
9
9
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module TruncateHtml
|
2
2
|
class Configuration
|
3
|
-
attr_accessor :length, :omission, :word_boundary
|
3
|
+
attr_accessor :length, :omission, :word_boundary, :break_token
|
4
4
|
end
|
5
5
|
|
6
6
|
class << self
|
@@ -11,5 +11,4 @@ module TruncateHtml
|
|
11
11
|
self.configuration ||= Configuration.new
|
12
12
|
yield configuration
|
13
13
|
end
|
14
|
-
|
15
14
|
end
|
@@ -1,39 +1,40 @@
|
|
1
|
+
# encoding: utf-8
|
1
2
|
module TruncateHtml
|
2
3
|
class HtmlString < String
|
3
4
|
|
4
|
-
UNPAIRED_TAGS = %w(br hr img)
|
5
|
+
UNPAIRED_TAGS = %w(br hr img).freeze
|
6
|
+
REGEX = /(?:<script.*>.*<\/script>)+|<\/?[^>]+>|[[[:alpha:]][0-9]\|`~!@#\$%^&*\(\)\-_\+=\[\]{}:;'²³§",\.\/?]+|\s+|[[:punct:]]/.freeze
|
5
7
|
|
6
8
|
def initialize(original_html)
|
7
9
|
super(original_html)
|
8
10
|
end
|
9
11
|
|
10
12
|
def html_tokens
|
11
|
-
scan(
|
12
|
-
|
13
|
-
|
14
|
-
/\n/,''
|
15
|
-
|
16
|
-
|
17
|
-
|
13
|
+
scan(REGEX).map do |token|
|
14
|
+
HtmlString.new(
|
15
|
+
token.gsub(
|
16
|
+
/\n/,' ' #replace newline characters with a whitespace
|
17
|
+
).gsub(
|
18
|
+
/\s+/, ' ' #clean out extra consecutive whitespace
|
19
|
+
)
|
18
20
|
)
|
19
|
-
end
|
21
|
+
end
|
20
22
|
end
|
21
23
|
|
22
24
|
def html_tag?
|
23
|
-
|
25
|
+
/<\/?[^>]+>/ === self && !html_comment?
|
24
26
|
end
|
25
27
|
|
26
28
|
def open_tag?
|
27
|
-
|
29
|
+
/<(?!(?:#{UNPAIRED_TAGS.join('|')}|script|\/))[^>]+>/i === self
|
28
30
|
end
|
29
31
|
|
30
|
-
def
|
31
|
-
|
32
|
+
def html_comment?
|
33
|
+
/<\s?!--.*-->/ === self
|
32
34
|
end
|
33
35
|
|
34
|
-
|
35
|
-
|
36
|
-
/(?:<script.*>.*<\/script>)+|<\/?[^>]+>|[#{"[[:alpha:]]" if RUBY_VERSION >= '1.9'}\w\|`~!@#\$%^&*\(\)\-_\+=\[\]{}:;'",\.\/?]+|\s+/
|
36
|
+
def matching_close_tag
|
37
|
+
gsub(/<(\w+)\s?.*>/, '</\1>').strip
|
37
38
|
end
|
38
39
|
|
39
40
|
end
|
@@ -1,67 +1,97 @@
|
|
1
1
|
module TruncateHtml
|
2
2
|
class HtmlTruncator
|
3
3
|
|
4
|
-
def initialize(original_html)
|
5
|
-
@original_html
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
@omission = options[:omission] || TruncateHtml.configuration.omission
|
11
|
-
@word_boundary = (options.has_key?(:word_boundary) ? options[:word_boundary] : TruncateHtml.configuration.word_boundary)
|
4
|
+
def initialize(original_html, options = {})
|
5
|
+
@original_html = original_html
|
6
|
+
length = options[:length] || TruncateHtml.configuration.length
|
7
|
+
@omission = options[:omission] || TruncateHtml.configuration.omission
|
8
|
+
@word_boundary = (options.has_key?(:word_boundary) ? options[:word_boundary] : TruncateHtml.configuration.word_boundary)
|
9
|
+
@break_token = options[:break_token] || TruncateHtml.configuration.break_token || nil
|
12
10
|
@chars_remaining = length - @omission.length
|
13
|
-
@open_tags, @truncated_html = [], ['']
|
11
|
+
@open_tags, @closing_tags, @truncated_html = [], [], ['']
|
12
|
+
end
|
14
13
|
|
14
|
+
def truncate
|
15
|
+
return @omission if @chars_remaining < 0
|
15
16
|
@original_html.html_tokens.each do |token|
|
16
|
-
|
17
|
-
if @chars_remaining > 0
|
18
|
-
process_token(token)
|
19
|
-
else
|
17
|
+
if @chars_remaining <= 0 || truncate_token?(token)
|
20
18
|
close_open_tags
|
21
19
|
break
|
20
|
+
else
|
21
|
+
process_token(token)
|
22
22
|
end
|
23
23
|
end
|
24
|
-
|
24
|
+
|
25
|
+
build_output
|
25
26
|
end
|
26
27
|
|
27
28
|
private
|
28
29
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
30
|
+
def word_boundary
|
31
|
+
if @word_boundary == true
|
32
|
+
TruncateHtml.configuration.word_boundary
|
33
|
+
else
|
34
|
+
@word_boundary
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def build_output
|
39
|
+
out = @truncated_html.join
|
40
|
+
if word_boundary
|
41
|
+
term_regexp = Regexp.new("^.*#{word_boundary.source}")
|
42
|
+
match = out.match(term_regexp)
|
43
|
+
if match && match[0] != out
|
44
|
+
out = match[0] + @closing_tags.join
|
39
45
|
end
|
40
46
|
end
|
41
47
|
|
42
|
-
|
43
|
-
|
44
|
-
|
48
|
+
out
|
49
|
+
end
|
50
|
+
|
51
|
+
def process_token(token)
|
52
|
+
append_to_result(token)
|
53
|
+
if token.html_tag?
|
54
|
+
if token.open_tag?
|
55
|
+
@open_tags << token
|
45
56
|
else
|
46
|
-
|
57
|
+
remove_latest_open_tag(token)
|
58
|
+
end
|
59
|
+
elsif !token.html_comment?
|
60
|
+
@chars_remaining -= (@word_boundary ? token.length : token[0, @chars_remaining].length)
|
61
|
+
if @chars_remaining <= 0
|
62
|
+
@truncated_html[-1] = @truncated_html[-1].rstrip + @omission
|
47
63
|
end
|
48
64
|
end
|
65
|
+
end
|
49
66
|
|
50
|
-
|
51
|
-
|
52
|
-
@
|
53
|
-
|
54
|
-
|
67
|
+
def append_to_result(token)
|
68
|
+
if token.html_tag? || token.html_comment?
|
69
|
+
@truncated_html << token
|
70
|
+
elsif @word_boundary
|
71
|
+
@truncated_html << token if (@chars_remaining - token.length) >= 0
|
72
|
+
else
|
73
|
+
@truncated_html << token[0, @chars_remaining]
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def close_open_tags
|
78
|
+
@open_tags.reverse_each do |open_tag|
|
79
|
+
@closing_tags << open_tag.matching_close_tag
|
80
|
+
@truncated_html << open_tag.matching_close_tag
|
55
81
|
end
|
82
|
+
end
|
56
83
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
end
|
84
|
+
def remove_latest_open_tag(close_tag)
|
85
|
+
(0...@open_tags.length).to_a.reverse.each do |index|
|
86
|
+
if @open_tags[index].matching_close_tag == close_tag
|
87
|
+
@open_tags.delete_at(index)
|
88
|
+
break
|
63
89
|
end
|
64
90
|
end
|
91
|
+
end
|
65
92
|
|
93
|
+
def truncate_token?(token)
|
94
|
+
@break_token and token == @break_token
|
95
|
+
end
|
66
96
|
end
|
67
97
|
end
|
data/lib/truncate_html.rb
CHANGED
@@ -1,12 +1,14 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'truncate_html', 'version')
|
1
2
|
require File.join(File.dirname(__FILE__), 'truncate_html', 'html_truncator')
|
2
3
|
require File.join(File.dirname(__FILE__), 'truncate_html', 'html_string')
|
3
4
|
require File.join(File.dirname(__FILE__), 'truncate_html', 'configuration')
|
4
5
|
require File.join(File.dirname(__FILE__), 'app', 'helpers', 'truncate_html_helper')
|
5
6
|
|
6
7
|
TruncateHtml.configure do |config|
|
7
|
-
config.length
|
8
|
-
config.omission
|
9
|
-
config.word_boundary =
|
8
|
+
config.length = 100
|
9
|
+
config.omission = '...'
|
10
|
+
config.word_boundary = /\S/
|
10
11
|
end
|
11
12
|
|
12
|
-
|
13
|
+
|
14
|
+
ActionController::Base.helper(TruncateHtmlHelper) if defined?(ActionController)
|
@@ -17,20 +17,20 @@ describe TruncateHtmlHelper do
|
|
17
17
|
end
|
18
18
|
|
19
19
|
before(:each) do
|
20
|
-
@html_truncator_mock = mock(TruncateHtml::HtmlTruncator)
|
21
20
|
@original_html = '<p>foo</p>'
|
22
21
|
@original_html.stub!(:html_safe).and_return(@original_html)
|
23
22
|
end
|
24
23
|
|
25
|
-
it 'creates an instance of HtmlTruncator and calls truncate
|
26
|
-
|
27
|
-
TruncateHtml::HtmlTruncator.should_receive(:new).and_return(
|
24
|
+
it 'creates an instance of HtmlTruncator and calls truncate on it' do
|
25
|
+
truncator = double(truncate: @original_html)
|
26
|
+
TruncateHtml::HtmlTruncator.should_receive(:new).and_return(truncator)
|
28
27
|
truncator.truncate_html(@original_html)
|
29
28
|
end
|
30
29
|
|
31
|
-
it 'calls truncate
|
32
|
-
|
33
|
-
|
30
|
+
it 'calls truncate on the HtmlTruncator object' do
|
31
|
+
truncator = double(truncate: @original_html)
|
32
|
+
TruncateHtml::HtmlTruncator.stub!(:new).and_return(truncator)
|
33
|
+
truncator.should_receive(:truncate).and_return(@original_html)
|
34
34
|
truncator.truncate_html('foo')
|
35
35
|
end
|
36
36
|
|
data/spec/rails_root/Gemfile
CHANGED