deas-erbtags 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +19 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +22 -0
- data/README.md +72 -0
- data/Rakefile +1 -0
- data/deas-tags.gemspec +22 -0
- data/lib/deas-erbtags/tag.rb +17 -0
- data/lib/deas-erbtags/utils.rb +37 -0
- data/lib/deas-erbtags/version.rb +4 -0
- data/lib/deas-erbtags.rb +17 -0
- data/log/.gitkeep +0 -0
- data/test/helper.rb +10 -0
- data/test/support/factory.rb +17 -0
- data/test/unit/deas-erbtags_tests.rb +27 -0
- data/test/unit/tag_tests.rb +38 -0
- data/test/unit/utils_tests.rb +76 -0
- data/tmp/.gitkeep +0 -0
- metadata +101 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013-Present Kelly Redding and Collin Redding
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
# Deas::ErbTags
|
2
|
+
|
3
|
+
Template helpers for creating html tags using Erb (or Erubis, etc).
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
require 'deas-erbtags'
|
9
|
+
|
10
|
+
class MyDeasServer
|
11
|
+
include Deas::Server
|
12
|
+
|
13
|
+
# ...
|
14
|
+
|
15
|
+
template_helpers(Deas::ErbTags) # include them all
|
16
|
+
|
17
|
+
# OR just include some...
|
18
|
+
|
19
|
+
template_helpers(Deas::ErbTags::LinkTo)
|
20
|
+
template_helpers(Deas::ErbTags::ImageTag)
|
21
|
+
|
22
|
+
# ...
|
23
|
+
|
24
|
+
end
|
25
|
+
```
|
26
|
+
|
27
|
+
## Tags
|
28
|
+
|
29
|
+
All helpers are based off of the basic `tag` helper:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
tag(:br) # => <br />
|
33
|
+
tag(:h1, 'shizam', :title => "boom") # => <h1 title="boom">shizam</h1>
|
34
|
+
```
|
35
|
+
|
36
|
+
### LinkTo
|
37
|
+
|
38
|
+
TODO
|
39
|
+
|
40
|
+
### MailTo
|
41
|
+
|
42
|
+
TODO
|
43
|
+
|
44
|
+
### ImageTag
|
45
|
+
|
46
|
+
TODO
|
47
|
+
|
48
|
+
### FormTags
|
49
|
+
|
50
|
+
TODO
|
51
|
+
|
52
|
+
## Installation
|
53
|
+
|
54
|
+
Add this line to your application's Gemfile:
|
55
|
+
|
56
|
+
gem 'deas-erbtags'
|
57
|
+
|
58
|
+
And then execute:
|
59
|
+
|
60
|
+
$ bundle
|
61
|
+
|
62
|
+
Or install it yourself as:
|
63
|
+
|
64
|
+
$ gem install deas-erbtags
|
65
|
+
|
66
|
+
## Contributing
|
67
|
+
|
68
|
+
1. Fork it
|
69
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
70
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
71
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
72
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/deas-tags.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "deas-erbtags/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "deas-erbtags"
|
8
|
+
gem.version = Deas::ErbTags::VERSION
|
9
|
+
gem.authors = ["Kelly Redding", "Collin Redding"]
|
10
|
+
gem.email = ["kelly@kellyredding.com", "collin.redding@me.com"]
|
11
|
+
gem.description = %q{Deas template helpers for creating HTML tags using Erb.}
|
12
|
+
gem.summary = %q{Deas template helpers for creating HTML tags using Erb.}
|
13
|
+
gem.homepage = "http://github.com/redding/deas-erbtags"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_development_dependency("assert")
|
21
|
+
|
22
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'deas-erbtags/utils'
|
2
|
+
|
3
|
+
module Deas::ErbTags
|
4
|
+
module Tag
|
5
|
+
|
6
|
+
def tag(name, *args)
|
7
|
+
opts, content = [
|
8
|
+
args.last.kind_of?(::Hash) ? args.pop : {},
|
9
|
+
args.first
|
10
|
+
]
|
11
|
+
attrs = U.html_attrs(opts)
|
12
|
+
|
13
|
+
"<#{name}#{attrs}#{content ? ">#{content}</#{name}" : ' /'}>"
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module Deas; end
|
2
|
+
module Deas::ErbTags
|
3
|
+
|
4
|
+
module Utils
|
5
|
+
|
6
|
+
def self.html_attrs(attrs="", ns=nil)
|
7
|
+
return attrs.to_s if !attrs.kind_of?(::Hash)
|
8
|
+
|
9
|
+
attrs.collect do |k_v|
|
10
|
+
[ns ? "#{ns}_#{k_v.first}" : k_v.first.to_s, k_v.last]
|
11
|
+
end.sort.collect do |k_v|
|
12
|
+
if k_v.last.kind_of?(::Hash)
|
13
|
+
html_attrs(k_v.last, k_v.first)
|
14
|
+
elsif k_v.last.kind_of?(::Array)
|
15
|
+
" #{k_v.first}=\"#{escape_attr_value(k_v.last.join(' '))}\""
|
16
|
+
else
|
17
|
+
" #{k_v.first}=\"#{escape_attr_value(k_v.last)}\""
|
18
|
+
end
|
19
|
+
end.join
|
20
|
+
end
|
21
|
+
|
22
|
+
ESCAPE_ATTRS = {
|
23
|
+
"&" => "&",
|
24
|
+
"<" => "<",
|
25
|
+
'"' => """
|
26
|
+
}
|
27
|
+
ESCAPE_ATTRS_PATTERN = Regexp.union(*ESCAPE_ATTRS.keys)
|
28
|
+
def self.escape_attr_value(value)
|
29
|
+
value.to_s.gsub(ESCAPE_ATTRS_PATTERN){|c| ESCAPE_ATTRS[c] }
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
# alias for brevity
|
35
|
+
U = Utils
|
36
|
+
|
37
|
+
end
|
data/lib/deas-erbtags.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'deas-erbtags/version'
|
2
|
+
require 'deas-erbtags/utils'
|
3
|
+
|
4
|
+
require 'deas-erbtags/tag'
|
5
|
+
|
6
|
+
module Deas; end
|
7
|
+
module Deas::ErbTags
|
8
|
+
|
9
|
+
# this implements the "include them all" behavior
|
10
|
+
|
11
|
+
def self.included(receiver)
|
12
|
+
receiver.class_eval do
|
13
|
+
include Tag
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
data/log/.gitkeep
ADDED
File without changes
|
data/test/helper.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
# this file is automatically required when you run `assert`
|
2
|
+
# put any test helpers here
|
3
|
+
|
4
|
+
# add the root dir to the load path
|
5
|
+
$LOAD_PATH.unshift(File.expand_path("../..", __FILE__))
|
6
|
+
|
7
|
+
# require pry for debugging (`binding.pry`)
|
8
|
+
require 'pry'
|
9
|
+
|
10
|
+
require 'test/support/factory'
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'deas-erbtags/utils'
|
2
|
+
require 'deas-erbtags/tag'
|
3
|
+
|
4
|
+
module Factory
|
5
|
+
|
6
|
+
def self.template(*included_modules)
|
7
|
+
template_class = Class.new do
|
8
|
+
include *included_modules
|
9
|
+
end
|
10
|
+
template_class.new
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.html_attrs(opts)
|
14
|
+
Deas::ErbTags::Utils.html_attrs(opts)
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'assert'
|
2
|
+
require 'deas-erbtags'
|
3
|
+
|
4
|
+
module Deas::ErbTags
|
5
|
+
|
6
|
+
class BaseTests < Assert::Context
|
7
|
+
desc "Deas::ErbTags"
|
8
|
+
setup do
|
9
|
+
@template = Factory.template(Deas::ErbTags)
|
10
|
+
end
|
11
|
+
subject{ @template }
|
12
|
+
|
13
|
+
should have_imeths :tag
|
14
|
+
|
15
|
+
should "include all of the individual modules" do
|
16
|
+
exp_modules = [
|
17
|
+
Tag
|
18
|
+
]
|
19
|
+
|
20
|
+
exp_modules.each do |m|
|
21
|
+
assert_includes m, subject.class.included_modules
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'assert'
|
2
|
+
require 'deas-erbtags/tag'
|
3
|
+
|
4
|
+
module Deas::ErbTags::Tag
|
5
|
+
|
6
|
+
class BaseTests < Assert::Context
|
7
|
+
desc "the basic tag method"
|
8
|
+
setup do
|
9
|
+
@opts = { :class => 'big', :id => '1234' }
|
10
|
+
@content = "Loud Noises"
|
11
|
+
@opts_attrs = Factory.html_attrs(@opts)
|
12
|
+
@template = Factory.template(Deas::ErbTags::Tag)
|
13
|
+
end
|
14
|
+
subject{ @template }
|
15
|
+
|
16
|
+
should have_imeth :tag
|
17
|
+
|
18
|
+
should "create an empty html tag" do
|
19
|
+
assert_equal "<br />", @template.tag(:br)
|
20
|
+
end
|
21
|
+
|
22
|
+
should "create an html tag with attributes" do
|
23
|
+
assert_equal "<br#{@opts_attrs} />", @template.tag(:br, @opts)
|
24
|
+
end
|
25
|
+
|
26
|
+
should "create an html tag with content" do
|
27
|
+
exp_markup = "<h1>#{@content}</h1>"
|
28
|
+
assert_equal exp_markup, @template.tag(:h1, @content)
|
29
|
+
end
|
30
|
+
|
31
|
+
should "create an html tag with attributes and content" do
|
32
|
+
exp_markup = "<h1#{@opts_attrs}>#{@content}</h1>"
|
33
|
+
assert_equal exp_markup, @template.tag(:h1, @content, @opts)
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require "assert"
|
2
|
+
require 'deas-erbtags/utils'
|
3
|
+
|
4
|
+
module Deas::ErbTags::Utils
|
5
|
+
|
6
|
+
class BaseTests < Assert::Context
|
7
|
+
desc 'Deas::ErbTags::Helpers'
|
8
|
+
subject { Deas::ErbTags::Utils }
|
9
|
+
|
10
|
+
should have_imeths :html_attrs, :escape_attr_value
|
11
|
+
|
12
|
+
should "alias itself as `Deas::ErbTags::U`" do
|
13
|
+
assert_same Deas::ErbTags::Utils, Deas::ErbTags::U
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
class HashAttrsTests < BaseTests
|
19
|
+
desc "the element class hash_attrs util"
|
20
|
+
|
21
|
+
should "convert an empty hash to element attrs" do
|
22
|
+
assert_equal '', subject.html_attrs({})
|
23
|
+
end
|
24
|
+
|
25
|
+
should "convert a basic hash to element attrs" do
|
26
|
+
attrs = subject.html_attrs(:class => "test", :id => "test_1")
|
27
|
+
assert_match /^\s{1}/, attrs
|
28
|
+
assert_includes 'class="test"', attrs
|
29
|
+
assert_includes 'id="test_1"', attrs
|
30
|
+
|
31
|
+
attrs = subject.html_attrs('key' => "string")
|
32
|
+
assert_includes 'key="string"', attrs
|
33
|
+
end
|
34
|
+
|
35
|
+
should "escape double-quotes in attr values" do
|
36
|
+
attrs = subject.html_attrs('escaped' => '"this" is double-quoted')
|
37
|
+
assert_includes 'escaped=""this" is double-quoted"', attrs
|
38
|
+
end
|
39
|
+
|
40
|
+
should "escape '<' in attr values" do
|
41
|
+
attrs = subject.html_attrs('escaped' => 'not < escaped')
|
42
|
+
assert_includes 'escaped="not < escaped"', attrs
|
43
|
+
end
|
44
|
+
|
45
|
+
should "escape '&' in attr values" do
|
46
|
+
attrs = subject.html_attrs('escaped' => 'not & escaped')
|
47
|
+
assert_includes 'escaped="not & escaped"', attrs
|
48
|
+
end
|
49
|
+
|
50
|
+
should "convert a nested array to element attrs" do
|
51
|
+
attrs = subject.html_attrs({
|
52
|
+
:class => "testing",
|
53
|
+
:id => "test_2",
|
54
|
+
:nested => [:something, 'is_awesome', 1]
|
55
|
+
})
|
56
|
+
assert_match /^\s{1}/, attrs
|
57
|
+
assert_included 'class="testing"', attrs
|
58
|
+
assert_included 'id="test_2"', attrs
|
59
|
+
assert_included 'nested="something is_awesome 1"', attrs
|
60
|
+
end
|
61
|
+
|
62
|
+
should "convert a nested hash to element attrs" do
|
63
|
+
attrs = subject.html_attrs({
|
64
|
+
:class => "testing",
|
65
|
+
:id => "test_2",
|
66
|
+
:nested => {:something => 'is_awesome'}
|
67
|
+
})
|
68
|
+
assert_match /^\s{1}/, attrs
|
69
|
+
assert_included 'class="testing"', attrs
|
70
|
+
assert_included 'id="test_2"', attrs
|
71
|
+
assert_included 'nested_something="is_awesome"', attrs
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
data/tmp/.gitkeep
ADDED
File without changes
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: deas-erbtags
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Kelly Redding
|
14
|
+
- Collin Redding
|
15
|
+
autorequire:
|
16
|
+
bindir: bin
|
17
|
+
cert_chain: []
|
18
|
+
|
19
|
+
date: 2013-05-16 00:00:00 Z
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: assert
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :development
|
34
|
+
version_requirements: *id001
|
35
|
+
description: Deas template helpers for creating HTML tags using Erb.
|
36
|
+
email:
|
37
|
+
- kelly@kellyredding.com
|
38
|
+
- collin.redding@me.com
|
39
|
+
executables: []
|
40
|
+
|
41
|
+
extensions: []
|
42
|
+
|
43
|
+
extra_rdoc_files: []
|
44
|
+
|
45
|
+
files:
|
46
|
+
- .gitignore
|
47
|
+
- Gemfile
|
48
|
+
- LICENSE.txt
|
49
|
+
- README.md
|
50
|
+
- Rakefile
|
51
|
+
- deas-tags.gemspec
|
52
|
+
- lib/deas-erbtags.rb
|
53
|
+
- lib/deas-erbtags/tag.rb
|
54
|
+
- lib/deas-erbtags/utils.rb
|
55
|
+
- lib/deas-erbtags/version.rb
|
56
|
+
- log/.gitkeep
|
57
|
+
- test/helper.rb
|
58
|
+
- test/support/factory.rb
|
59
|
+
- test/unit/deas-erbtags_tests.rb
|
60
|
+
- test/unit/tag_tests.rb
|
61
|
+
- test/unit/utils_tests.rb
|
62
|
+
- tmp/.gitkeep
|
63
|
+
homepage: http://github.com/redding/deas-erbtags
|
64
|
+
licenses: []
|
65
|
+
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
hash: 3
|
77
|
+
segments:
|
78
|
+
- 0
|
79
|
+
version: "0"
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
hash: 3
|
86
|
+
segments:
|
87
|
+
- 0
|
88
|
+
version: "0"
|
89
|
+
requirements: []
|
90
|
+
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 1.8.24
|
93
|
+
signing_key:
|
94
|
+
specification_version: 3
|
95
|
+
summary: Deas template helpers for creating HTML tags using Erb.
|
96
|
+
test_files:
|
97
|
+
- test/helper.rb
|
98
|
+
- test/support/factory.rb
|
99
|
+
- test/unit/deas-erbtags_tests.rb
|
100
|
+
- test/unit/tag_tests.rb
|
101
|
+
- test/unit/utils_tests.rb
|