tag_helper 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +17 -15
  3. data/lib/tag_helper.rb +20 -14
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2eabb8b249d6c44b44f92b70f466833eeb165565
4
- data.tar.gz: a1bbaa8fbbc8e7c074c9cbc9c665f34a642a8f63
3
+ metadata.gz: 6b2ddf245d8a964ded991b0b8ffa1b1550bc009c
4
+ data.tar.gz: 11f3ae737de3edadcce4662756c6f105d70f02f6
5
5
  SHA512:
6
- metadata.gz: 48c4004d2647dde1d73a0d67b18c7ae8daef6d000783a3dfd49f32aefc72691a4eb556e3a890cc6afbf99acc2cc62313412b160b18ed9f906bb5518e405e92b1
7
- data.tar.gz: 70c3db7ce70aa6cc34775eb823e429f930d166c31319e86ac149c4ff7df529b52d2f4c9c5398791d366b677b47a656a9c8828450e3ea7ad9d961281e5d1ab07c
6
+ metadata.gz: 09a6eb568cd5786b8ce8af6eeac3ee21259e93b4318cc11d4a828c6f8345501a756b486e4dc57dc290512f846aa0c4c334917ab80ecf1aca1172956123efe87e
7
+ data.tar.gz: bd38809cdb62357d444800e46ff6631d404bb63a675ad0afb00052e7b94c9b828e30dbaa48a281d3c83e817e72c743fb4dd27a074d3653891442f6dff368e5ad
data/README.md CHANGED
@@ -1,5 +1,5 @@
1
- tag_helper
2
- ==========
1
+ TagHelper
2
+ =========
3
3
 
4
4
  Lightweight Ruby lib for building XHTML tags.
5
5
 
@@ -28,27 +28,29 @@ $ gem install tag_helper
28
28
  ```ruby
29
29
  require 'tag_helper'
30
30
 
31
- TagHelper.unary(:img, src: '1.png', alt: 'number one!'))
31
+ include TagHelper
32
+
33
+ tag(:img, src: '1.png', alt: 'number one!')
32
34
  # => '<img src="1.png" alt="number one!" />'
33
35
 
34
- TagHelper.unary(:br)
36
+ tag(:br)
35
37
  # => '<br />'
36
38
 
37
- TagHelper.content(:label, 'Name', for: 'name'))
39
+ tag(:label, for: 'name') { 'Name' }
38
40
  # => '<label for="name">Name</label>'
39
41
 
40
- TagHelper.content(:p, '<script>alert(0)</script>')
41
- # => '<p>&lt;script&gt;alert(0)&lt;/script&gt;</p>'
42
+ tag(:div) do
43
+ tag(:form) do
44
+ tag(:input, type: 'text')
45
+ end
46
+ end
47
+ # => '<div><form><input type="text" /></form></div>'
42
48
  ```
43
49
 
44
- ## LICENSE
45
-
46
- Copyright (c) 2015 Dejan Simic
47
-
48
- MIT License
50
+ ## Code Status
49
51
 
50
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
52
+ [![Circle CI](https://circleci.com/gh/dejan/tag_helper.svg?style=svg&circle-token=cefb9d11fb03efefcf9e35bbc29d9eef68bd980e)](https://circleci.com/gh/dejan/tag_helper)
51
53
 
52
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
54
+ ## License
53
55
 
54
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
56
+ TagHelper is released under the [MIT License](https://raw.githubusercontent.com/dejan/tag_helper/master/MIT-LICENCE).
@@ -2,29 +2,35 @@ require 'cgi'
2
2
 
3
3
  # XHTML tags builder
4
4
  module TagHelper
5
- module_function
5
+ def tag(tag_name, attrs = {})
6
+ if block_given?
7
+ content_tag(tag_name, yield, attrs)
8
+ else
9
+ unary_tag(tag_name, attrs)
10
+ end
11
+ end
12
+
13
+ private
6
14
 
7
- def unary(tag, attrs = {})
8
- "<#{tag_and_attributes(tag, attributes(attrs))} />"
15
+ def unary_tag(tag_name, attrs = {})
16
+ "<#{tag_and_attributes(tag_name, tag_attributes(attrs))} />"
9
17
  end
10
18
 
11
- def content(tag, value, attrs = {})
12
- start_tag = "<#{tag_and_attributes(tag, attributes(attrs))}>"
13
- end_tag = "</#{tag}>"
14
- [start_tag, escape_html(value), end_tag].join
19
+ def content_tag(tag_name, value, attrs = {})
20
+ start_tag = "<#{tag_and_attributes(tag_name, tag_attributes(attrs))}>"
21
+ end_tag = "</#{tag_name}>"
22
+ [start_tag, value, end_tag].join
15
23
  end
16
24
 
17
- def tag_and_attributes(tag, attributes)
18
- attributes.empty? ? tag : "#{tag} #{attributes}"
25
+ def tag_and_attributes(tag_name, attributes)
26
+ attributes.empty? ? tag_name : "#{tag_name} #{attributes}"
19
27
  end
20
28
 
21
- def attributes(hash)
29
+ def tag_attributes(hash)
22
30
  hash.to_a
23
31
  .reject { |_k, v| v.nil? }
24
- .map { |k, v| %(#{escape_html(k)}="#{escape_html(v)}") }.join(' ')
32
+ .map { |k, v| %(#{k}="#{v}") }.join(' ')
25
33
  end
26
34
 
27
- def escape_html(str)
28
- CGI.escapeHTML(str.to_s)
29
- end
35
+ extend self
30
36
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tag_helper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dejan Simic
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-05 00:00:00.000000000 Z
11
+ date: 2015-12-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake