HTML-AutoTag 1.0.5 → 1.0.6

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: efb0d13dd8dbda045953096fb70847a370eb073c
4
- data.tar.gz: c7d4c78fe89a7b973d18a2bb09d471452b4ae58c
3
+ metadata.gz: 4e62c12acd8d530e8e9d7fb5a8bd3ad917b0b13f
4
+ data.tar.gz: 654a095d32d9e1c1c592163cb56c93814bf0d835
5
5
  SHA512:
6
- metadata.gz: ea13dfdb575f7f3caab7fb20b3aecee404b6b476b33824a7044f82f47c417623d1328f7988da05f0eb4c9b303f488923028890b441c4901e386b2c23ebaa3737
7
- data.tar.gz: 8563123b26d7276f1985ba9cb9c1f807b294f74a06509305fe3c540151e5b7a6c5673ab8035a57d59d786f59a13c9180c28e35ecb53fa32c5d07afb37f46f94e
6
+ metadata.gz: 8f4cf5d79e775ea646d56a4fdca1f9be040829033bbe66454ca7a9173ce8ba4d7ea74824f046034dfc4c92777d2b9f9fbd4424a4c97abba5c6daf2384e443b10
7
+ data.tar.gz: 393be2a6b9ecd0a34233599312525285fa642d974c013e4f8cbdaf1f8b9d0b30f54c33c374e1b02449229dc00f402fa05fac46fba090fe54491b08b5b755ce12
data/Changes CHANGED
@@ -1,5 +1,9 @@
1
1
  Revision history for html-autotag-ruby
2
2
 
3
+ 1.0.6
4
+ - more true implementation of Perl's HTML::Entities
5
+ - removed dependency from docs
6
+
3
7
  1.0.5
4
8
  - fixed double ampersand encoding bug
5
9
 
data/README CHANGED
@@ -28,15 +28,15 @@ Also includes HTML::AutoAttr which provides rotating attributes:
28
28
  attr = HTML::AutoAttr.new( { 'foo' => ['bar','baz','qux'] } )
29
29
  4.times { puts attr.to_s }
30
30
 
31
- == Dependencies
32
- * htmlentities
33
-
34
31
  == Methods
35
32
  With the exception of new, all methods return an HTML table as a string.
36
33
 
37
34
  * new( params )
35
+ * encode=(boolean)
36
+ Whether or not to Encode entities.
38
37
  * encodes=(string)
39
- Encode HTML entities. Pass empty string for default HTML entity encoding or a string with chars you want encoded.
38
+ Encode these HTML entities. Pass string with chars you want encoded
39
+ or leave blank for default control and high bit chars and <>!'"
40
40
  * indent=(string)
41
41
  Pretty print results.
42
42
  * level=(integer)
@@ -1,5 +1,5 @@
1
1
  module HTML
2
2
  class AutoTag
3
- VERSION = "1.0.5"
3
+ VERSION = "1.0.6"
4
4
  end
5
5
  end
data/lib/HTML/AutoTag.rb CHANGED
@@ -6,12 +6,13 @@ module HTML
6
6
 
7
7
  class AutoTag
8
8
 
9
- attr_accessor 'encodes', 'indent', 'level', 'sorted', 'newline'
9
+ attr_accessor 'encode', 'encodes', 'indent', 'level', 'sorted', 'newline'
10
10
 
11
11
  # Defaults to empty string which produces no encoding.
12
12
 
13
13
  def initialize( params = {} )
14
- @encodes = params.has_key?('encodes') ? params['encodes'] : ''
14
+ @encodes = params['encodes']
15
+ @encode = params['encode'] || !@encodes.nil?
15
16
  @indent = params['indent'] || ''
16
17
  @level = params['level'] || 0
17
18
  @sorted = params['sorted'] ? 1 : 0
@@ -62,7 +63,7 @@ module HTML
62
63
  @level -= 1
63
64
 
64
65
  else
65
- rendered = @encoder.encode( cdata, @encodes )
66
+ rendered = @encode ? @encoder.encode( cdata, @encodes ) : cdata
66
67
  no_post_indent = 1
67
68
  end
68
69
 
data/lib/HTML/Encoder.rb CHANGED
@@ -93,7 +93,7 @@ module HTML
93
93
  'uml' => 168.chr,
94
94
  'ordf' => 170.chr,
95
95
  'laquo' => 171.chr,
96
- 'not' => 172.chr, # not is a keyword in perl
96
+ 'not' => 172.chr,
97
97
  'shy' => 173.chr,
98
98
  'macr' => 175.chr,
99
99
  'deg' => 176.chr,
@@ -112,7 +112,7 @@ module HTML
112
112
  'frac12' => 189.chr,
113
113
  'frac34' => 190.chr,
114
114
  'iquest' => 191.chr,
115
- 'times' => 215.chr, # times is a keyword in perl
115
+ 'times' => 215.chr,
116
116
  'divide' => 247.chr,
117
117
  }
118
118
 
@@ -132,26 +132,22 @@ module HTML
132
132
 
133
133
  end
134
134
 
135
- def encode( *args )
135
+ def encode( string, *args )
136
136
 
137
- string = args[0]
138
- chars = args[1].nil? ? '&<>"\'' : args[1]
139
-
140
- lookup = {}
141
- chars.to_s.each_char{ |c|
142
- lookup[c] = @char2entity[c] || num_entity(c)
143
- }
144
-
145
- encoded = ''
146
- string.to_s.each_char { |c|
147
- if lookup[c]
148
- encoded += @char2entity[c] || num_entity(c)
149
- else
150
- encoded += c
151
- end
152
- }
137
+ if (! args[0].nil? and ! args[0].to_s.empty?)
138
+ lookup = {}
139
+ args[0].to_s.each_char{ |c|
140
+ lookup[c] = @char2entity[c].nil? ? num_entity(c) : @char2entity[c]
141
+ }
142
+ string = string.to_s.gsub( /./ ) {|c| lookup[c].nil? ? c : lookup[c] }
143
+ else
144
+ # Encode control chars, high bit chars and '<', '&', '>', ''' and '"'
145
+ string = string.to_s.gsub( /([^\n\r\t !\#\$%\(-;=?-~])/ ) {|c|
146
+ @char2entity[c].nil? ? num_entity(c) : @char2entity[c]
147
+ }
148
+ end
153
149
 
154
- return encoded
150
+ return string
155
151
 
156
152
  end
157
153
 
data/t/02-tag.rb CHANGED
@@ -5,14 +5,16 @@ class TestTag < Test::Unit::TestCase
5
5
 
6
6
  def test_init
7
7
  auto = HTML::AutoTag.new
8
- assert_equal( '', auto.encodes, "no args encodes correct" )
9
- assert_equal( '', auto.indent, "no args indent correct" )
10
- assert_equal( 0, auto.level, "no args level correct" )
11
- assert_equal( 0, auto.sorted, "no args sorted correct" )
12
- assert_equal( '', auto.newline, "no args newline correct" )
13
-
14
- auto = HTML::AutoTag.new( 'encodes' => nil, 'indent' => ' ' )
15
- assert_equal( nil, auto.encodes, "encodes set correct" )
8
+ assert_equal( false, auto.encode, "no args encode correct" )
9
+ assert_equal( nil, auto.encodes, "no args encodes correct" )
10
+ assert_equal( '', auto.indent, "no args indent correct" )
11
+ assert_equal( 0, auto.level, "no args level correct" )
12
+ assert_equal( 0, auto.sorted, "no args sorted correct" )
13
+ assert_equal( '', auto.newline, "no args newline correct" )
14
+
15
+ auto = HTML::AutoTag.new( 'encodes' => '<>', 'indent' => ' ' )
16
+ assert_equal( true, auto.encode, "encode set correct" )
17
+ assert_equal( '<>', auto.encodes, "encodes set correct" )
16
18
  assert_equal( ' ', auto.indent, "indent set correct" )
17
19
  assert_equal( "\n", auto.newline, "newline set correct" )
18
20
  end
@@ -48,7 +50,7 @@ class TestTag < Test::Unit::TestCase
48
50
  assert_equal( '<p>&lt;0&gt;</p>', auto.tag( 'tag' => 'p', 'cdata' => '<0>' ), "specific encodes work" )
49
51
 
50
52
  auto = HTML::AutoTag.new( 'encodes' => nil )
51
- assert_equal( '<p>&lt;&quot;0&apos;&amp;&gt;</p>', auto.tag( 'tag' => 'p', 'cdata' => '<"0\'&>' ), "default encodes work" )
53
+ assert_equal( '<p><"0\'&></p>', auto.tag( 'tag' => 'p', 'cdata' => '<"0\'&>' ), "no encoding when encodes is nil" )
52
54
 
53
55
  auto = HTML::AutoTag.new( 'encodes' => 0 )
54
56
  assert_equal( '<p><&#48;></p>', auto.tag( 'tag' => 'p', 'cdata' => '<0>' ), "zero only encode work" )
data/t/05-encode.rb CHANGED
@@ -10,13 +10,13 @@ class TestHTML_Encoder < Test::Unit::TestCase
10
10
  assert_equal(
11
11
  '&amp;&lt;&gt;&quot;&apos;',
12
12
  encoder.encode( '&<>"\'' ),
13
- 'default chars encoded correctly'
13
+ 'default chars encoded when chars is nil'
14
14
  )
15
15
 
16
16
  assert_equal(
17
- '&<>"\'',
17
+ '&amp;&lt;&gt;&quot;&apos;',
18
18
  encoder.encode( '&<>"\'', '' ),
19
- 'no encodes when chars is empty'
19
+ 'encodes when chars is empty'
20
20
  )
21
21
 
22
22
  assert_equal(
@@ -40,13 +40,13 @@ class TestHTML_Encoder < Test::Unit::TestCase
40
40
  assert_equal(
41
41
  'hello',
42
42
  encoder.encode( 'hello' ),
43
- 'no encodes when default chars requested'
43
+ 'no encodes when default chars is nil'
44
44
  )
45
45
 
46
46
  assert_equal(
47
47
  'hello',
48
48
  encoder.encode( 'hello', '' ),
49
- 'no chars encoded when no chars requested'
49
+ 'no encodes when default chars is empty'
50
50
  )
51
51
 
52
52
  deadbeef = 222.chr + 173.chr + 190.chr + 239.chr
@@ -58,15 +58,15 @@ class TestHTML_Encoder < Test::Unit::TestCase
58
58
  )
59
59
 
60
60
  assert_equal(
61
- deadbeef,
61
+ '&THORN;&shy;&frac34;&iuml;',
62
62
  encoder.encode( deadbeef ),
63
- 'no encodes when default chars requested'
63
+ 'hex codes encoded correctly when chars is nil'
64
64
  )
65
65
 
66
66
  assert_equal(
67
- deadbeef,
67
+ '&THORN;&shy;&frac34;&iuml;',
68
68
  encoder.encode( deadbeef, '' ),
69
- 'no encodes when no chars requested'
69
+ 'hex codes encoded correctly when chars is empty'
70
70
  )
71
71
 
72
72
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: HTML-AutoTag
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.5
4
+ version: 1.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - jeffa
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-04 00:00:00.000000000 Z
11
+ date: 2015-11-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler