hom 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/hom.gemspec +1 -1
- data/lib/hom.rb +98 -4
- metadata +4 -11
- data/lib/hom/attribute.rb +0 -9
- data/lib/hom/attribute_list.rb +0 -46
- data/lib/hom/element.rb +0 -49
- data/lib/hom/entity.rb +0 -21
- data/test/hom_attribute_list.rb +0 -53
- data/test/hom_element.rb +0 -46
- data/test/hom_entity.rb +0 -33
data/hom.gemspec
CHANGED
data/lib/hom.rb
CHANGED
@@ -1,6 +1,100 @@
|
|
1
|
+
require 'cgi'
|
2
|
+
|
1
3
|
module HOM
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
4
|
+
class Entity
|
5
|
+
attr_reader :value
|
6
|
+
|
7
|
+
def initialize(value)
|
8
|
+
@value = value
|
9
|
+
end
|
10
|
+
|
11
|
+
def html
|
12
|
+
numeric? ? "&\##{value};" : "&#{value};"
|
13
|
+
end
|
14
|
+
|
15
|
+
def numeric?
|
16
|
+
Numeric === @value
|
17
|
+
end
|
18
|
+
|
19
|
+
def named?
|
20
|
+
!numeric?
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class Element
|
25
|
+
def initialize(tag_name, attributes = nil, content = nil)
|
26
|
+
@tag_name, @attributes, @content = tag_name, AttributeList.new.update(attributes), content
|
27
|
+
end
|
28
|
+
|
29
|
+
def html
|
30
|
+
@content.nil? ? start_tag : "#{start_tag}#{encode(@content)}</#{@tag_name}>"
|
31
|
+
end
|
32
|
+
|
33
|
+
def to_s
|
34
|
+
html_safe(html)
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def html_safe(string)
|
40
|
+
string.respond_to?(:html_safe) ? string.html_safe : string
|
41
|
+
end
|
42
|
+
|
43
|
+
def encode(object)
|
44
|
+
if object.is_a?(Array)
|
45
|
+
object.map { |item| encode(item) }.join
|
46
|
+
elsif object.respond_to?(:html)
|
47
|
+
object.html
|
48
|
+
elsif object.respond_to?(:html_safe?) && object.html_safe?
|
49
|
+
object.to_s
|
50
|
+
else
|
51
|
+
HOM.escape(object)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def start_tag
|
56
|
+
"<#{@tag_name}#{@attributes.html}>"
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
class AttributeList
|
61
|
+
def initialize
|
62
|
+
@index = {}
|
63
|
+
end
|
64
|
+
|
65
|
+
def html
|
66
|
+
@index.map { |name, value| attribute_html(name, value) }.join
|
67
|
+
end
|
68
|
+
|
69
|
+
Undefined = Class.new
|
70
|
+
|
71
|
+
def set(name, value = Undefined)
|
72
|
+
@index[name.to_s] = value
|
73
|
+
end
|
74
|
+
|
75
|
+
def update(object)
|
76
|
+
case object
|
77
|
+
when NilClass
|
78
|
+
:pass
|
79
|
+
when Hash
|
80
|
+
object.each { |k, v| set(k, v) }
|
81
|
+
when Array
|
82
|
+
object.each { |item| update(item) }
|
83
|
+
else
|
84
|
+
set(object)
|
85
|
+
end
|
86
|
+
|
87
|
+
return self
|
88
|
+
end
|
89
|
+
|
90
|
+
private
|
91
|
+
|
92
|
+
def attribute_html(name, value)
|
93
|
+
value == Undefined ? " #{name}" : %( #{name}="#{HOM.escape value}")
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def self.escape(object)
|
98
|
+
CGI.escapeHTML(object.to_s)
|
99
|
+
end
|
6
100
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hom
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-02-23 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
16
|
-
requirement: &
|
16
|
+
requirement: &10113880 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: 3.0.3
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *10113880
|
25
25
|
description: A straightforward API for generating HTML programmatically
|
26
26
|
email:
|
27
27
|
- mail@timcraft.com
|
@@ -29,14 +29,7 @@ executables: []
|
|
29
29
|
extensions: []
|
30
30
|
extra_rdoc_files: []
|
31
31
|
files:
|
32
|
-
- lib/hom/attribute.rb
|
33
|
-
- lib/hom/attribute_list.rb
|
34
|
-
- lib/hom/element.rb
|
35
|
-
- lib/hom/entity.rb
|
36
32
|
- lib/hom.rb
|
37
|
-
- test/hom_attribute_list.rb
|
38
|
-
- test/hom_element.rb
|
39
|
-
- test/hom_entity.rb
|
40
33
|
- README.txt
|
41
34
|
- hom.gemspec
|
42
35
|
homepage: http://github.com/timcraft/hom
|
data/lib/hom/attribute.rb
DELETED
data/lib/hom/attribute_list.rb
DELETED
@@ -1,46 +0,0 @@
|
|
1
|
-
module HOM
|
2
|
-
class AttributeList
|
3
|
-
def initialize
|
4
|
-
@index = {}
|
5
|
-
end
|
6
|
-
|
7
|
-
def length
|
8
|
-
@index.size
|
9
|
-
end
|
10
|
-
|
11
|
-
def html
|
12
|
-
@index.values.map(&:html).join
|
13
|
-
end
|
14
|
-
|
15
|
-
def lookup(name)
|
16
|
-
@index[name.to_s]
|
17
|
-
end
|
18
|
-
|
19
|
-
alias :[] :lookup
|
20
|
-
|
21
|
-
def set(name, value = nil)
|
22
|
-
attribute = lookup(name)
|
23
|
-
|
24
|
-
if attribute.nil?
|
25
|
-
@index[name.to_s] = Attribute.new(name, value)
|
26
|
-
else
|
27
|
-
attribute.value = value
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
def update(object)
|
32
|
-
case object
|
33
|
-
when NilClass
|
34
|
-
:pass
|
35
|
-
when Hash
|
36
|
-
object.each { |k, v| set(k, v) }
|
37
|
-
when Array
|
38
|
-
object.each { |item| update(item) }
|
39
|
-
else
|
40
|
-
set(object)
|
41
|
-
end
|
42
|
-
|
43
|
-
return self
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
data/lib/hom/element.rb
DELETED
@@ -1,49 +0,0 @@
|
|
1
|
-
require 'cgi'
|
2
|
-
|
3
|
-
module HOM
|
4
|
-
class Element
|
5
|
-
def initialize(tag_name, attributes = nil, content = nil)
|
6
|
-
@tag_name, @attributes, @content = tag_name, AttributeList.new.update(attributes), content
|
7
|
-
end
|
8
|
-
|
9
|
-
def html
|
10
|
-
@content.nil? ? start_tag : "#{start_tag}#{encode(@content)}</#{@tag_name}>"
|
11
|
-
end
|
12
|
-
|
13
|
-
def to_s
|
14
|
-
html_safe(html)
|
15
|
-
end
|
16
|
-
|
17
|
-
def lookup(name)
|
18
|
-
@attributes.lookup(name)
|
19
|
-
end
|
20
|
-
|
21
|
-
alias :[] :lookup
|
22
|
-
|
23
|
-
private
|
24
|
-
|
25
|
-
def html_safe(string)
|
26
|
-
string.respond_to?(:html_safe) ? string.html_safe : string
|
27
|
-
end
|
28
|
-
|
29
|
-
def encode(object)
|
30
|
-
if object.is_a?(Array)
|
31
|
-
object.map { |item| encode(item) }.join
|
32
|
-
elsif object.respond_to?(:html)
|
33
|
-
object.html
|
34
|
-
elsif object.respond_to?(:html_safe?) && object.html_safe?
|
35
|
-
object.to_s
|
36
|
-
else
|
37
|
-
escape(object)
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
def start_tag
|
42
|
-
"<#{@tag_name}#{@attributes.html}>"
|
43
|
-
end
|
44
|
-
|
45
|
-
def escape(object)
|
46
|
-
CGI.escapeHTML(object.to_s)
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
data/lib/hom/entity.rb
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
module HOM
|
2
|
-
class Entity
|
3
|
-
attr_reader :value
|
4
|
-
|
5
|
-
def initialize(value)
|
6
|
-
@value = value
|
7
|
-
end
|
8
|
-
|
9
|
-
def html
|
10
|
-
numeric? ? "&\##{value};" : "&#{value};"
|
11
|
-
end
|
12
|
-
|
13
|
-
def numeric?
|
14
|
-
Numeric === @value
|
15
|
-
end
|
16
|
-
|
17
|
-
def named?
|
18
|
-
!numeric?
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
data/test/hom_attribute_list.rb
DELETED
@@ -1,53 +0,0 @@
|
|
1
|
-
require 'minitest/autorun'
|
2
|
-
|
3
|
-
$:.unshift 'lib'
|
4
|
-
|
5
|
-
require 'hom'
|
6
|
-
|
7
|
-
class HOM::AttributeList::TestCase < MiniTest::Unit::TestCase
|
8
|
-
def setup
|
9
|
-
@list = HOM::AttributeList.new
|
10
|
-
end
|
11
|
-
|
12
|
-
def test_update_with_nil
|
13
|
-
@list.update(nil)
|
14
|
-
|
15
|
-
assert_equal 0, @list.length
|
16
|
-
end
|
17
|
-
|
18
|
-
def test_update_with_symbol
|
19
|
-
@list.update(:disabled)
|
20
|
-
|
21
|
-
assert_equal 1, @list.length
|
22
|
-
assert_equal :disabled, @list[:disabled].name
|
23
|
-
assert_equal nil, @list[:disabled].value
|
24
|
-
end
|
25
|
-
|
26
|
-
def test_update_with_hash
|
27
|
-
@list.update({type: :text, size: 30})
|
28
|
-
|
29
|
-
assert_equal 2, @list.length
|
30
|
-
assert_equal :type, @list[:type].name
|
31
|
-
assert_equal :text, @list[:type].value
|
32
|
-
assert_equal :size, @list[:size].name
|
33
|
-
assert_equal 30, @list[:size].value
|
34
|
-
end
|
35
|
-
|
36
|
-
def test_update_with_array
|
37
|
-
@list.update([{type: :text}, :disabled])
|
38
|
-
|
39
|
-
assert_equal 2, @list.length
|
40
|
-
assert_equal :type, @list[:type].name
|
41
|
-
assert_equal :text, @list[:type].value
|
42
|
-
assert_equal :disabled, @list[:disabled].name
|
43
|
-
assert_equal nil, @list[:disabled].value
|
44
|
-
end
|
45
|
-
|
46
|
-
def test_update_with_duplicate_symbols
|
47
|
-
@list.update([:disabled, :disabled])
|
48
|
-
|
49
|
-
assert_equal 1, @list.length
|
50
|
-
assert_equal :disabled, @list[:disabled].name
|
51
|
-
assert_equal nil, @list[:disabled].value
|
52
|
-
end
|
53
|
-
end
|
data/test/hom_element.rb
DELETED
@@ -1,46 +0,0 @@
|
|
1
|
-
require 'minitest/autorun'
|
2
|
-
require 'active_support/core_ext/string/output_safety'
|
3
|
-
|
4
|
-
$:.unshift 'lib'
|
5
|
-
|
6
|
-
require 'hom'
|
7
|
-
|
8
|
-
class HOM::Element::TestCase < MiniTest::Unit::TestCase
|
9
|
-
def setup
|
10
|
-
@br = HOM::Element.new(:br)
|
11
|
-
@i1 = HOM::Element.new(:input, :disabled)
|
12
|
-
@i2 = HOM::Element.new(:input, {type: :text, size: 30})
|
13
|
-
@i3 = HOM::Element.new(:input, [{type: :text, size: 30}, :disabled])
|
14
|
-
@h1 = HOM::Element.new(:h1, nil, '')
|
15
|
-
@h2 = HOM::Element.new(:h2, nil, 'hello world')
|
16
|
-
@h3 = HOM::Element.new(:h3, nil, 'a && b, x > y')
|
17
|
-
@h4 = HOM::Element.new(:h4, nil, 1234567890)
|
18
|
-
@h5 = HOM::Element.new(:h5, nil, HOM::Element.new(:b, nil, 'how bold'))
|
19
|
-
@ul = HOM::Element.new(:ul, nil, (1..3).map { |n| HOM::Element.new(:li, nil, n) })
|
20
|
-
end
|
21
|
-
|
22
|
-
def test_stringification
|
23
|
-
assert_equal '<br>', @br.to_s
|
24
|
-
assert_equal '<input disabled>', @i1.to_s
|
25
|
-
assert_equal '<input type="text" size="30">', @i2.to_s
|
26
|
-
assert_equal '<input type="text" size="30" disabled>', @i3.to_s
|
27
|
-
assert_equal '<h1></h1>', @h1.to_s
|
28
|
-
assert_equal '<h2>hello world</h2>', @h2.to_s
|
29
|
-
assert_equal '<h3>a && b, x > y</h3>', @h3.to_s
|
30
|
-
assert_equal '<h4>1234567890</h4>', @h4.to_s
|
31
|
-
assert_equal '<h5><b>how bold</b></h5>', @h5.to_s
|
32
|
-
assert_equal '<ul><li>1</li><li>2</li><li>3</li></ul>', @ul.to_s
|
33
|
-
end
|
34
|
-
|
35
|
-
def test_output_safety
|
36
|
-
assert @br.to_s.html_safe?
|
37
|
-
|
38
|
-
assert '<span><br></span>', HOM::Element.new(:span, nil, '<br>'.html_safe).to_s
|
39
|
-
end
|
40
|
-
|
41
|
-
def test_attribute_access
|
42
|
-
assert_equal :text, @i3[:type].value
|
43
|
-
assert_equal 30, @i3[:size].value
|
44
|
-
assert_equal nil, @i3[:disabled].value
|
45
|
-
end
|
46
|
-
end
|
data/test/hom_entity.rb
DELETED
@@ -1,33 +0,0 @@
|
|
1
|
-
require 'minitest/autorun'
|
2
|
-
|
3
|
-
$:.unshift 'lib'
|
4
|
-
|
5
|
-
require 'hom'
|
6
|
-
|
7
|
-
class HOM::Entity::TestCase < MiniTest::Unit::TestCase
|
8
|
-
def setup
|
9
|
-
@e160 = HOM::Entity.new(160)
|
10
|
-
|
11
|
-
@nbsp = HOM::Entity.new(:nbsp)
|
12
|
-
end
|
13
|
-
|
14
|
-
def test_value_method
|
15
|
-
assert_equal 160, @e160.value
|
16
|
-
assert_equal :nbsp, @nbsp.value
|
17
|
-
end
|
18
|
-
|
19
|
-
def test_numeric_query_method
|
20
|
-
assert_equal true, @e160.numeric?
|
21
|
-
assert_equal false, @nbsp.numeric?
|
22
|
-
end
|
23
|
-
|
24
|
-
def test_named_query_method
|
25
|
-
assert_equal false, @e160.named?
|
26
|
-
assert_equal true, @nbsp.named?
|
27
|
-
end
|
28
|
-
|
29
|
-
def test_html_method
|
30
|
-
assert_equal ' ', @e160.html
|
31
|
-
assert_equal ' ', @nbsp.html
|
32
|
-
end
|
33
|
-
end
|