crazy_harry 0.1.2 → 0.2.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.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ZTcyY2M3ZDU3ODY3NmZhMjI5YmQzNTViOGJkNDU3OWZhNWZhZGRlMQ==
5
+ data.tar.gz: !binary |-
6
+ MjcyMDFmOWI4NzMxY2RiNzhlYjNlZWQzOGUzNWE3MzdhYmU5Y2MzOA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ MjlhYjM5NjUyYWE1NDg2YzM1MGUwYjdiMzcxNTZhZWMzYTQzZjczNDUzMDdl
10
+ ZWVhZjMyYzdmYjRkNjBkMjJiYjFmNzdlOTliMzZiYzlhMzg0Mjg4MDQ0OWZm
11
+ MjZjODQyZjkwZTQ5MWY2NTMxNTU2NjQyMDRkM2JlOTc1MDM2OTI=
12
+ data.tar.gz: !binary |-
13
+ ZWUxMTEzN2ExYTIxMmI5YTVhMTViNjRjMzExMmVlODY5YWYwZjlhZjIxYzk3
14
+ OGU2MGU1NDg4NTQwMTdhMzEzNzQ3MzYyOTc4YjY4MTAyOThlOTU4YjQxNzYy
15
+ MWYwZjk0NGViMTljODkyM2M0ODEyOWM0YTBlOWRhYzExMjgzNzA=
data/README.md CHANGED
@@ -34,7 +34,7 @@ CrazyHarry wraps up a number these tasks in a simple DSL, while adding commands
34
34
 
35
35
  ## Default Actions
36
36
 
37
- It automatically removes blank tags, converts `<br \>` tags to wrapped
37
+ It automatically removes blank tags, converts `<br />` tags to wrapped
38
38
  paragraphs and de-dupes content.
39
39
 
40
40
  ## Chaining
@@ -107,6 +107,22 @@ only works for li tag for now.**
107
107
 
108
108
  CrazyHarry.fragment('<li>Flying list tag</li>').foster!.to_s
109
109
 
110
+ will return:
111
+
112
+ <ul><li>Flying list tag</li></ul>
113
+
114
+ ## Truncating
115
+
116
+ The `.truncate!` command will truncate input preserving HTML tags.
117
+
118
+ CrazyHarry.fragment( '<p>Long <b>text goes here</b></p>' ).truncate!(3)
119
+
120
+ will return
121
+
122
+ <p>Long <b>text goes</b>…</p>
123
+
124
+ It accepts hash of options and passes it to [HTML_Truncator](https://github.com/nono/HTML-Truncator). It returns self and can be chained with other commands.
125
+
110
126
  ## Known Issues/TODO
111
127
 
112
128
  * De-duping does not take account of whitespace. So, `<p>Some Content</p>` and `<p>Some Content </p>` will not be treated as duplicates.
data/crazy_harry.gemspec CHANGED
@@ -24,6 +24,8 @@ Gem::Specification.new do |s|
24
24
  s.add_development_dependency 'guard-rspec', '>=1.0.1'
25
25
  s.add_development_dependency 'simplecov', '>=0.6.4'
26
26
 
27
- s.add_runtime_dependency 'loofah', '>=1.2.1'
27
+ s.add_runtime_dependency 'loofah', '~>1'
28
28
  s.add_runtime_dependency 'nokogiri', '>=1.5.5'
29
+
30
+ s.add_dependency 'html_truncator', '~>0.3'
29
31
  end
@@ -5,6 +5,7 @@ module CrazyHarry
5
5
  include CrazyHarry::Redact
6
6
  include CrazyHarry::Translate
7
7
  include CrazyHarry::Foster
8
+ include CrazyHarry::Truncate
8
9
 
9
10
  attr_accessor :fragment, :scope, :steps, :text
10
11
 
@@ -0,0 +1,10 @@
1
+ require 'html_truncator'
2
+ module CrazyHarry
3
+ module Truncate
4
+ def truncate!(count, opts = {})
5
+ truncated = HTML_Truncator.truncate(self.fragment.to_s, count, opts)
6
+ self.fragment = Loofah.fragment(truncated)
7
+ self
8
+ end
9
+ end
10
+ end
@@ -1,3 +1,3 @@
1
1
  module CrazyHarry
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/crazy_harry.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  require 'loofah'
2
2
 
3
- %w(default foster change redact translate base version).each do |load_lib|
3
+ %w(default foster change redact translate truncate base version).each do |load_lib|
4
4
  require_relative "crazy_harry/#{load_lib}"
5
5
  end
6
6
 
@@ -5,11 +5,12 @@ describe CrazyHarry do
5
5
  let(:harry) { CrazyHarry }
6
6
 
7
7
  it "should allow method chaining" do
8
- harry.fragment('<script>STEAL COOKIE!</script><em>Place</em><p>Lodging</p><b>Location:</b>')
8
+ harry.fragment('<script>STEAL COOKIE!</script><em>Place</em><p>Lodging</p><b>Location:</b> Warsaw')
9
9
  .redact!( unsafe: true, tags: 'em' )
10
10
  .change!( from: 'b', to: 'h3' )
11
+ .truncate!(3, ellipsis: '...')
11
12
  .translate!( from_text: 'Lodging', to_text: 'Hotel', add_attributes: { class: 'partner' } )
12
- .to_s.should == 'Place <p class="partner">Hotel</p><h3 class="partner">Location:</h3>'
13
+ .to_s.should == 'Place <p class="partner">Hotel</p><h3 class="partner">Location:</h3>...'
13
14
  end
14
15
 
15
16
  it "should not care about the chain order" do
@@ -17,6 +18,7 @@ describe CrazyHarry do
17
18
  .translate!( from_text: 'Lodging', to_text: 'Hotel', add_attributes: { class: 'partner' } )
18
19
  .redact!( unsafe: true, tags: 'em' )
19
20
  .change!( from: 'b', to: 'h3' )
21
+ .truncate!(3)
20
22
  .to_s.should == 'Place <p class="partner">Hotel</p><h3 class="partner">Location:</h3>'
21
23
  end
22
24
 
@@ -75,10 +75,7 @@ describe CrazyHarry do
75
75
  harry.fragment('<h3>Here</h3><p>Yep, here.</p><h3>Here</h3><p>Here again.</p>').to_s.should == '<h3>Here</h3><p>Yep, here.</p><p>Here again.</p>'
76
76
  end
77
77
 
78
- it "should de-dupe if the only content difference is whitespace" do
79
- pending "Too complicated for the first cut."
80
- end
81
-
78
+ it "should de-dupe if the only content difference is whitespace"
82
79
  end
83
80
 
84
81
  context 'with default br processing overridden' do
@@ -0,0 +1,19 @@
1
+ # encoding: UTF-8
2
+ require 'spec_helper'
3
+
4
+ describe CrazyHarry::Truncate do
5
+ let(:harry){ CrazyHarry }
6
+
7
+ it "truncate by number of words" do
8
+ harry.fragment('<p>text goes here</p>').truncate!(2).to_s.should == '<p>text goes…</p>'
9
+ end
10
+
11
+ it "closes html tags properly" do
12
+ harry.fragment('<p>text <b>goes here</b></p>').truncate!(2).to_s.should == '<p>text <b>goes</b>…</p>'
13
+ end
14
+
15
+ it "passes extra options to HTML_Truncator" do
16
+ harry.fragment('<p>text goes here</p>').truncate!(10, length_in_chars: true, ellipsis: ' (...)').to_s
17
+ .should == '<p>text goes (...)</p>'
18
+ end
19
+ end
metadata CHANGED
@@ -1,82 +1,113 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: crazy_harry
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
5
- prerelease:
4
+ version: 0.2.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - TA Tyree
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-11-05 00:00:00.000000000 Z
11
+ date: 2014-09-11 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rspec
16
- requirement: &2155892060 !ruby/object:Gem::Requirement
17
- none: false
15
+ requirement: !ruby/object:Gem::Requirement
18
16
  requirements:
19
17
  - - ! '>='
20
18
  - !ruby/object:Gem::Version
21
19
  version: 2.10.0
22
20
  type: :development
23
21
  prerelease: false
24
- version_requirements: *2155892060
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 2.10.0
25
27
  - !ruby/object:Gem::Dependency
26
28
  name: guard
27
- requirement: &2155891280 !ruby/object:Gem::Requirement
28
- none: false
29
+ requirement: !ruby/object:Gem::Requirement
29
30
  requirements:
30
31
  - - ! '>='
31
32
  - !ruby/object:Gem::Version
32
33
  version: 1.1.1
33
34
  type: :development
34
35
  prerelease: false
35
- version_requirements: *2155891280
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: 1.1.1
36
41
  - !ruby/object:Gem::Dependency
37
42
  name: guard-rspec
38
- requirement: &2155890600 !ruby/object:Gem::Requirement
39
- none: false
43
+ requirement: !ruby/object:Gem::Requirement
40
44
  requirements:
41
45
  - - ! '>='
42
46
  - !ruby/object:Gem::Version
43
47
  version: 1.0.1
44
48
  type: :development
45
49
  prerelease: false
46
- version_requirements: *2155890600
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: 1.0.1
47
55
  - !ruby/object:Gem::Dependency
48
56
  name: simplecov
49
- requirement: &2155889820 !ruby/object:Gem::Requirement
50
- none: false
57
+ requirement: !ruby/object:Gem::Requirement
51
58
  requirements:
52
59
  - - ! '>='
53
60
  - !ruby/object:Gem::Version
54
61
  version: 0.6.4
55
62
  type: :development
56
63
  prerelease: false
57
- version_requirements: *2155889820
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: 0.6.4
58
69
  - !ruby/object:Gem::Dependency
59
70
  name: loofah
60
- requirement: &2155889320 !ruby/object:Gem::Requirement
61
- none: false
71
+ requirement: !ruby/object:Gem::Requirement
62
72
  requirements:
63
- - - ! '>='
73
+ - - ~>
64
74
  - !ruby/object:Gem::Version
65
- version: 1.2.1
75
+ version: '1'
66
76
  type: :runtime
67
77
  prerelease: false
68
- version_requirements: *2155889320
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '1'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: nokogiri
71
- requirement: &2155888760 !ruby/object:Gem::Requirement
72
- none: false
85
+ requirement: !ruby/object:Gem::Requirement
73
86
  requirements:
74
87
  - - ! '>='
75
88
  - !ruby/object:Gem::Version
76
89
  version: 1.5.5
77
90
  type: :runtime
78
91
  prerelease: false
79
- version_requirements: *2155888760
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: 1.5.5
97
+ - !ruby/object:Gem::Dependency
98
+ name: html_truncator
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ~>
102
+ - !ruby/object:Gem::Version
103
+ version: '0.3'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ~>
109
+ - !ruby/object:Gem::Version
110
+ version: '0.3'
80
111
  description: CrazyHarry is a high-level html fragment sanitiser/cleaner based on Loofah.
81
112
  email:
82
113
  - todd.tyree@lonelyplanet.co.uk
@@ -98,6 +129,7 @@ files:
98
129
  - lib/crazy_harry/foster.rb
99
130
  - lib/crazy_harry/redact.rb
100
131
  - lib/crazy_harry/translate.rb
132
+ - lib/crazy_harry/truncate.rb
101
133
  - lib/crazy_harry/version.rb
102
134
  - rvmrc.example
103
135
  - spec/fixtures/golden_nugget.html
@@ -108,30 +140,30 @@ files:
108
140
  - spec/lib/foster_spec.rb
109
141
  - spec/lib/redact_spec.rb
110
142
  - spec/lib/translate_spec.rb
143
+ - spec/lib/truncate_spec.rb
111
144
  - spec/spec_helper.rb
112
145
  homepage: https://github.com/lonelyplanet/crazy_harry
113
146
  licenses: []
147
+ metadata: {}
114
148
  post_install_message:
115
149
  rdoc_options: []
116
150
  require_paths:
117
151
  - lib
118
152
  required_ruby_version: !ruby/object:Gem::Requirement
119
- none: false
120
153
  requirements:
121
154
  - - ! '>='
122
155
  - !ruby/object:Gem::Version
123
156
  version: '0'
124
157
  required_rubygems_version: !ruby/object:Gem::Requirement
125
- none: false
126
158
  requirements:
127
159
  - - ! '>='
128
160
  - !ruby/object:Gem::Version
129
161
  version: '0'
130
162
  requirements: []
131
163
  rubyforge_project: crazy_harry
132
- rubygems_version: 1.8.10
164
+ rubygems_version: 2.2.1
133
165
  signing_key:
134
- specification_version: 3
166
+ specification_version: 4
135
167
  summary: A High level HTML fragment sanitizer.
136
168
  test_files:
137
169
  - spec/fixtures/golden_nugget.html
@@ -142,4 +174,5 @@ test_files:
142
174
  - spec/lib/foster_spec.rb
143
175
  - spec/lib/redact_spec.rb
144
176
  - spec/lib/translate_spec.rb
177
+ - spec/lib/truncate_spec.rb
145
178
  - spec/spec_helper.rb