tag_helper 0.0.1
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/README.md +25 -0
- data/Rakefile +9 -0
- data/lib/tag_helper.rb +40 -0
- data/test/test_helper.rb +5 -0
- data/test/unit/tag_helper_test.rb +26 -0
- metadata +71 -0
data/README.md
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
tag_helper
|
2
|
+
==========
|
3
|
+
|
4
|
+
TagHelper is a zero dependency, lightweight Ruby lib for building XHTML tags similar to the API provided by Rails' *TagHelper modules.
|
5
|
+
|
6
|
+
## Install
|
7
|
+
|
8
|
+
gem install tag_helper
|
9
|
+
|
10
|
+
|
11
|
+
## Example usage
|
12
|
+
|
13
|
+
>> require 'tag_helper'
|
14
|
+
=> true
|
15
|
+
|
16
|
+
>> include TagHelper
|
17
|
+
=> Object
|
18
|
+
|
19
|
+
>> image_tag('1.png', :alt => 'number one!')
|
20
|
+
=> "<img alt="number one!" src="1.png" />"
|
21
|
+
|
22
|
+
|
23
|
+
## Credits
|
24
|
+
|
25
|
+
Author: [Dejan Simic](http://github.com/dejan)
|
data/Rakefile
ADDED
data/lib/tag_helper.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'rexml/document'
|
2
|
+
|
3
|
+
module TagHelper
|
4
|
+
|
5
|
+
def image_tag(src, html_options = {})
|
6
|
+
unary_tag(:img, html_options.merge(:src => src))
|
7
|
+
end
|
8
|
+
|
9
|
+
def label_tag(label_for, label=nil, html_options = {})
|
10
|
+
content_tag(:label, label || label_for, html_options.merge(:for => label_for))
|
11
|
+
end
|
12
|
+
|
13
|
+
def text_field_tag(name, value, html_options = {})
|
14
|
+
unary_tag(:input, html_options.merge(:id => name, :value => value, :type => 'text', :name => name))
|
15
|
+
end
|
16
|
+
|
17
|
+
def hidden_field_tag(name, value, html_options = {})
|
18
|
+
unary_tag(:input, html_options.merge(:id => name, :value => value, :type => 'hidden', :name => name))
|
19
|
+
end
|
20
|
+
|
21
|
+
def unary_tag(tag, attrs = {})
|
22
|
+
"<#{tag} #{attributes(attrs)} />"
|
23
|
+
end
|
24
|
+
|
25
|
+
def content_tag(tag, value, attrs = {})
|
26
|
+
b = "<#{tag} #{attributes(attrs)}>"
|
27
|
+
v = normalize(value)
|
28
|
+
e = "</#{tag}>"
|
29
|
+
[b, v, e].join
|
30
|
+
end
|
31
|
+
|
32
|
+
def attributes(hash)
|
33
|
+
hash.to_a.map { |k, v| %{#{k}="#{normalize v}"} }.join(' ')
|
34
|
+
end
|
35
|
+
|
36
|
+
def normalize(s)
|
37
|
+
REXML::Text::normalize(s)
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require "#{File.dirname(__FILE__)}/../test_helper"
|
2
|
+
|
3
|
+
class ConfigTest < Test::Unit::TestCase
|
4
|
+
include TagHelper
|
5
|
+
|
6
|
+
def test_image_tag
|
7
|
+
assert_equal '<img src="1.png" />', image_tag('1.png')
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_image_tag2
|
11
|
+
assert_equal '<img alt="number one!" src="1.png" />', image_tag('1.png', :alt => 'number one!')
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_label_tag2
|
15
|
+
assert_equal '<label for="name">name</label>', label_tag('name')
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_hidden_field_tag
|
19
|
+
assert_equal '<input id="token" value="abc" type="hidden" name="token" />', hidden_field_tag('token', 'abc')
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_hidden_field_tag2
|
23
|
+
assert_equal '<input id="u" value="dejan" type="text" name="u" />', text_field_tag('u', 'dejan')
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tag_helper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Dejan Simic
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-01-01 00:00:00 +01:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: TagHelper is a zero dependency, lightweight Ruby lib for building XHTML tags similar to the API provided by Rails' *TagHelper modules.
|
23
|
+
email: desimic@gmail.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files: []
|
29
|
+
|
30
|
+
files:
|
31
|
+
- Rakefile
|
32
|
+
- lib/tag_helper.rb
|
33
|
+
- test/test_helper.rb
|
34
|
+
- test/unit/tag_helper_test.rb
|
35
|
+
- README.md
|
36
|
+
has_rdoc: true
|
37
|
+
homepage: http://github.com/dejan/tag_helper
|
38
|
+
licenses: []
|
39
|
+
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options: []
|
42
|
+
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
none: false
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
hash: 3
|
51
|
+
segments:
|
52
|
+
- 0
|
53
|
+
version: "0"
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
hash: 3
|
60
|
+
segments:
|
61
|
+
- 0
|
62
|
+
version: "0"
|
63
|
+
requirements: []
|
64
|
+
|
65
|
+
rubyforge_project:
|
66
|
+
rubygems_version: 1.3.7
|
67
|
+
signing_key:
|
68
|
+
specification_version: 3
|
69
|
+
summary: Build XHTML tags off the Rails
|
70
|
+
test_files: []
|
71
|
+
|