undies 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +21 -0
- data/README.rdoc +58 -0
- data/Rakefile +9 -0
- data/lib/undies.rb +12 -0
- data/lib/undies/buffer.rb +81 -0
- data/lib/undies/source.rb +25 -0
- data/lib/undies/tag.rb +87 -0
- data/lib/undies/template.rb +22 -0
- data/lib/undies/version.rb +3 -0
- data/test/buffer_test.rb +79 -0
- data/test/env.rb +10 -0
- data/test/helper.rb +6 -0
- data/test/source_test.rb +42 -0
- data/test/tag_test.rb +152 -0
- data/test/template_test.rb +57 -0
- data/test/test_template.html.rb +8 -0
- data/undies.gemspec +23 -0
- metadata +120 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
undies (0.0.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: http://rubygems.org/
|
8
|
+
specs:
|
9
|
+
leftright (0.9.1)
|
10
|
+
rake (0.9.2)
|
11
|
+
test-belt (1.1.2)
|
12
|
+
leftright (~> 0.9.0)
|
13
|
+
|
14
|
+
PLATFORMS
|
15
|
+
ruby
|
16
|
+
|
17
|
+
DEPENDENCIES
|
18
|
+
bundler (~> 1.0)
|
19
|
+
rake (~> 0.9.2)
|
20
|
+
test-belt (~> 1.1.0)
|
21
|
+
undies!
|
data/README.rdoc
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
= Undies
|
2
|
+
A pure-Ruby DSL for writing HTML templates. Named for its gratuitous use of the underscore.
|
3
|
+
== Installation
|
4
|
+
$ gem install undies
|
5
|
+
== Usage
|
6
|
+
Empty tag:
|
7
|
+
_br # => "<br />"
|
8
|
+
|
9
|
+
Tag with content:
|
10
|
+
_h1 {
|
11
|
+
_ "Some Header"
|
12
|
+
} # => "<h1>Some Header</h1>"
|
13
|
+
Nested tags:
|
14
|
+
_body {
|
15
|
+
_div {
|
16
|
+
_ "Some Content"
|
17
|
+
}
|
18
|
+
} # => "<body><div>Some Content</div></body>"
|
19
|
+
Buffer escaped output:
|
20
|
+
_ "this will be escaped & and added to the buffer"
|
21
|
+
# => "this will be escaped & added to the buffer"
|
22
|
+
Buffer un-escaped output:
|
23
|
+
__ "this will <em>not</em> be escaped"
|
24
|
+
# => "this will <em>not</em> be escaped"
|
25
|
+
Tag with attributes
|
26
|
+
_h1(:class => 'title', :title => "A Header")
|
27
|
+
# => "<h1 class=\"title\" title=\"A Header\" />"
|
28
|
+
Tag with id attribute
|
29
|
+
_h1.header!
|
30
|
+
# => "<h1 id=\"header\" />"
|
31
|
+
Tag with class attributes
|
32
|
+
_h1.header.awesome
|
33
|
+
# => "<h1 class=\"header awesome\" />"
|
34
|
+
|
35
|
+
== License
|
36
|
+
|
37
|
+
Copyright (c) 2011 Kelly D. Redding
|
38
|
+
|
39
|
+
Permission is hereby granted, free of charge, to any person
|
40
|
+
obtaining a copy of this software and associated documentation
|
41
|
+
files (the "Software"), to deal in the Software without
|
42
|
+
restriction, including without limitation the rights to use,
|
43
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
44
|
+
copies of the Software, and to permit persons to whom the
|
45
|
+
Software is furnished to do so, subject to the following
|
46
|
+
conditions:
|
47
|
+
|
48
|
+
The above copyright notice and this permission notice shall be
|
49
|
+
included in all copies or substantial portions of the Software.
|
50
|
+
|
51
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
52
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
53
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
54
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
55
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
56
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
57
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
58
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
data/lib/undies.rb
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
module Undies
|
2
|
+
class Buffer < ::Array
|
3
|
+
|
4
|
+
def initialize(*args)
|
5
|
+
super()
|
6
|
+
end
|
7
|
+
|
8
|
+
# Add data and don't escape it
|
9
|
+
def __(data="")
|
10
|
+
append_item(data)
|
11
|
+
end
|
12
|
+
# Add data and escape it
|
13
|
+
def _(data="")
|
14
|
+
append_item(escape_html(data))
|
15
|
+
end
|
16
|
+
|
17
|
+
TAG_METH_REGEX = /^_(.+)$/
|
18
|
+
|
19
|
+
def method_missing(meth, *args, &block)
|
20
|
+
if meth.to_s =~ TAG_METH_REGEX
|
21
|
+
tag($1, *args, &block)
|
22
|
+
else
|
23
|
+
super
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def respond_to?(*args)
|
28
|
+
if args.first.to_s =~ TAG_METH_REGEX
|
29
|
+
true
|
30
|
+
else
|
31
|
+
super
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def tag(name, attrs={}, &block)
|
36
|
+
append_item(new_tag=Tag.new(name, attrs, &block))
|
37
|
+
new_tag
|
38
|
+
end
|
39
|
+
|
40
|
+
def to_s(pp_level=0, pp_indent=nil)
|
41
|
+
self.collect do |i|
|
42
|
+
begin
|
43
|
+
i.to_s(pp_level, pp_indent)
|
44
|
+
rescue ArgumentError => err
|
45
|
+
pretty_print(i.to_s, pp_level, pp_indent)
|
46
|
+
end
|
47
|
+
end.join
|
48
|
+
end
|
49
|
+
|
50
|
+
protected
|
51
|
+
|
52
|
+
def pretty_print(data, level, indent)
|
53
|
+
indent ? "#{' '*level*indent}#{data}\n" : data
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def append_item(data)
|
59
|
+
self << data
|
60
|
+
end
|
61
|
+
|
62
|
+
# Ripped from Rack v1.3.0 ======================================
|
63
|
+
# => ripped b/c I don't want a dependency on Rack for just this
|
64
|
+
ESCAPE_HTML = {
|
65
|
+
"&" => "&",
|
66
|
+
"<" => "<",
|
67
|
+
">" => ">",
|
68
|
+
"'" => "'",
|
69
|
+
'"' => """,
|
70
|
+
"/" => "/"
|
71
|
+
}
|
72
|
+
ESCAPE_HTML_PATTERN = Regexp.union(*ESCAPE_HTML.keys)
|
73
|
+
|
74
|
+
# Escape ampersands, brackets and quotes to their HTML/XML entities.
|
75
|
+
def escape_html(string)
|
76
|
+
string.to_s.gsub(ESCAPE_HTML_PATTERN){|c| ESCAPE_HTML[c] }
|
77
|
+
end
|
78
|
+
# end Rip from Rack v1.3.0 =====================================
|
79
|
+
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Undies
|
2
|
+
class Source
|
3
|
+
|
4
|
+
attr_reader :file, :block, :data
|
5
|
+
|
6
|
+
def initialize(file=nil, block=nil)
|
7
|
+
raise ArgumentError, "file or block required" if (file || block).nil?
|
8
|
+
|
9
|
+
@file = file
|
10
|
+
@block = block
|
11
|
+
|
12
|
+
# load template data and prepare (uses binread to avoid encoding issues)
|
13
|
+
@data = @block || if File.respond_to?(:binread)
|
14
|
+
File.binread(@file)
|
15
|
+
else
|
16
|
+
File.read(@file)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def file?
|
21
|
+
!!self.file
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
data/lib/undies/tag.rb
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
require 'undies/buffer'
|
2
|
+
|
3
|
+
module Undies
|
4
|
+
class Tag < Buffer
|
5
|
+
|
6
|
+
attr_reader :attrs
|
7
|
+
|
8
|
+
def initialize(name=nil, attrs={}, &block)
|
9
|
+
super
|
10
|
+
@name = name
|
11
|
+
@attrs = attrs
|
12
|
+
self.content = block
|
13
|
+
end
|
14
|
+
|
15
|
+
ID_METH_REGEX = /^([^_].+)!$/
|
16
|
+
CLASS_METH_REGEX = /^([^_].+)$/
|
17
|
+
|
18
|
+
def method_missing(meth, *args, &block)
|
19
|
+
if meth.to_s =~ ID_METH_REGEX
|
20
|
+
proxy_id_attr($1, *args, &block)
|
21
|
+
elsif meth.to_s =~ CLASS_METH_REGEX
|
22
|
+
proxy_class_attr($1, *args, &block)
|
23
|
+
else
|
24
|
+
super
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def respond_to?(*args)
|
29
|
+
if args.first.to_s =~ ID_METH_REGEX || args.first.to_s =~ CLASS_METH_REGEX
|
30
|
+
true
|
31
|
+
else
|
32
|
+
super
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def to_s(pp_level=0, pp_indent=nil)
|
37
|
+
out = ""
|
38
|
+
if @content
|
39
|
+
out << pretty_print("<#{@name}#{html_attrs(@attrs)}>", pp_level, pp_indent)
|
40
|
+
out << super(pp_level+1, pp_indent)
|
41
|
+
out << pretty_print("</#{@name}>", pp_level, pp_indent)
|
42
|
+
else
|
43
|
+
out << pretty_print("<#{@name}#{html_attrs(@attrs)} />", pp_level, pp_indent)
|
44
|
+
end
|
45
|
+
out
|
46
|
+
end
|
47
|
+
|
48
|
+
protected
|
49
|
+
|
50
|
+
def html_attrs(attrs={})
|
51
|
+
raise ArgumentError unless attrs.kind_of? ::Hash
|
52
|
+
if attrs.empty?
|
53
|
+
''
|
54
|
+
else
|
55
|
+
' '+attrs.
|
56
|
+
sort {|a,b| a[0].to_s <=> b[0].to_s}.
|
57
|
+
collect {|k_v| "#{k_v[0]}=\"#{k_v[1]}\""}.
|
58
|
+
join(' ').
|
59
|
+
strip
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
def proxy_id_attr(value, attrs={}, &block)
|
66
|
+
@attrs.merge!(:id => value)
|
67
|
+
@attrs.merge!(attrs)
|
68
|
+
self.content = block
|
69
|
+
self
|
70
|
+
end
|
71
|
+
|
72
|
+
def proxy_class_attr(value, attrs={}, &block)
|
73
|
+
@attrs[:class] = [@attrs[:class], value].compact.join(' ')
|
74
|
+
@attrs.merge!(attrs)
|
75
|
+
self.content = block
|
76
|
+
self
|
77
|
+
end
|
78
|
+
|
79
|
+
def content=(block)
|
80
|
+
if block
|
81
|
+
@content = block
|
82
|
+
instance_eval(&@content)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'undies/source'
|
2
|
+
require 'undies/buffer'
|
3
|
+
require 'undies/tag'
|
4
|
+
|
5
|
+
module Undies
|
6
|
+
class Template < Buffer
|
7
|
+
|
8
|
+
def initialize(file=nil, &block)
|
9
|
+
super
|
10
|
+
if (@source = Source.new(file, block)).file?
|
11
|
+
instance_eval(@source.data, @source.file, 1)
|
12
|
+
else
|
13
|
+
instance_eval(&@source.data)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def to_s(pp_indent=nil)
|
18
|
+
super(0, pp_indent)
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
data/test/buffer_test.rb
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
require "test/helper"
|
2
|
+
require "undies/buffer"
|
3
|
+
require "undies/tag"
|
4
|
+
|
5
|
+
class Undies::Buffer
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
|
10
|
+
class BasicTest < Test::Unit::TestCase
|
11
|
+
include TestBelt
|
12
|
+
|
13
|
+
context 'a buffer'
|
14
|
+
subject { Undies::Buffer.new }
|
15
|
+
should have_instance_methods :to_s, :_, :__, :tag
|
16
|
+
|
17
|
+
should "be a kind of ::Array" do
|
18
|
+
assert subject.kind_of?(::Array)
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
class DataTest < BasicTest
|
26
|
+
context "with data"
|
27
|
+
before do
|
28
|
+
@data = "stuff & <em>more stuff</em>"
|
29
|
+
end
|
30
|
+
|
31
|
+
should "add it un-escaped using the '__' method" do
|
32
|
+
subject.__ @data
|
33
|
+
assert_equal "stuff & <em>more stuff</em>", subject.to_s
|
34
|
+
end
|
35
|
+
|
36
|
+
should "add it escaped using the '_' method" do
|
37
|
+
subject._ @data
|
38
|
+
assert_equal "stuff & <em>more stuff</em>", subject.to_s
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
class TagTest < BasicTest
|
46
|
+
context "when using the tag method"
|
47
|
+
|
48
|
+
should "return a new Tag object" do
|
49
|
+
assert_equal Undies::Tag.new(:br), subject.tag(:br)
|
50
|
+
end
|
51
|
+
|
52
|
+
should "add a new Tag object" do
|
53
|
+
subject.tag(:br)
|
54
|
+
assert_equal Undies::Tag.new(:br), subject.first
|
55
|
+
end
|
56
|
+
|
57
|
+
should "respond to any underscore prefix method" do
|
58
|
+
assert subject.respond_to?(:_div)
|
59
|
+
end
|
60
|
+
|
61
|
+
should "not respond to tag methods without an underscore prefix" do
|
62
|
+
assert !subject.respond_to?(:div)
|
63
|
+
assert_raises NoMethodError do
|
64
|
+
subject.div
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
should "interpret underscore prefix methods as a tag" do
|
69
|
+
assert_equal subject._div, subject.tag(:div)
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
|
75
|
+
|
76
|
+
# TODO: pretty printing
|
77
|
+
|
78
|
+
|
79
|
+
end
|
data/test/env.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
# Add test and lib paths to the $LOAD_PATH
|
2
|
+
[ File.dirname(__FILE__),
|
3
|
+
File.join(File.dirname(__FILE__), '..', 'lib')
|
4
|
+
].each do |path|
|
5
|
+
full_path = File.expand_path(path)
|
6
|
+
$LOAD_PATH.unshift(full_path) unless $LOAD_PATH.include?(full_path)
|
7
|
+
end
|
8
|
+
|
9
|
+
require 'undies'
|
10
|
+
|
data/test/helper.rb
ADDED
data/test/source_test.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require "test/helper"
|
2
|
+
require "undies/source"
|
3
|
+
|
4
|
+
class Undies::Source
|
5
|
+
|
6
|
+
class BasicTest < Test::Unit::TestCase
|
7
|
+
include TestBelt
|
8
|
+
|
9
|
+
context 'a source'
|
10
|
+
subject { Undies::Source.new(nil, Proc.new {}) }
|
11
|
+
should have_instance_method :file?
|
12
|
+
should have_readers :file, :block, :data
|
13
|
+
|
14
|
+
should "need a file or block to initialize" do
|
15
|
+
assert_raises ArgumentError do
|
16
|
+
Undies::Source.new
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class BlockTest < BasicTest
|
22
|
+
context 'from a block'
|
23
|
+
subject { Undies::Source.new(nil, Proc.new {}) }
|
24
|
+
|
25
|
+
should "not be a file source" do
|
26
|
+
assert !subject.file?
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
class FileTest < BasicTest
|
31
|
+
context 'from a file'
|
32
|
+
subject do
|
33
|
+
file = 'test/test_template.html.rb'
|
34
|
+
Undies::Source.new(File.expand_path(file))
|
35
|
+
end
|
36
|
+
|
37
|
+
should "be a file source" do
|
38
|
+
assert subject.file?
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
data/test/tag_test.rb
ADDED
@@ -0,0 +1,152 @@
|
|
1
|
+
require "test/helper"
|
2
|
+
require "undies/tag"
|
3
|
+
|
4
|
+
class Undies::Tag
|
5
|
+
|
6
|
+
|
7
|
+
|
8
|
+
class BasicTest < Test::Unit::TestCase
|
9
|
+
include TestBelt
|
10
|
+
|
11
|
+
context 'a tag'
|
12
|
+
subject { Undies::Tag.new(:div) }
|
13
|
+
should have_instance_methods :to_s, :attrs
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
|
18
|
+
class HtmlAttrsTest < BasicTest
|
19
|
+
context "html_attrs util"
|
20
|
+
|
21
|
+
should "convert an empty hash to html attrs" do
|
22
|
+
assert_equal('', subject.send(:html_attrs, {}))
|
23
|
+
end
|
24
|
+
|
25
|
+
should "convert a basic hash to html attrs" do
|
26
|
+
attrs = subject.send(:html_attrs, :class => "test", :id => "test_1")
|
27
|
+
assert_match /^\s{1}/, attrs
|
28
|
+
assert attrs.include?('class="test"')
|
29
|
+
assert attrs.include?('id="test_1"')
|
30
|
+
end
|
31
|
+
|
32
|
+
should "convert a nested hash to html attrs" do
|
33
|
+
attrs = subject.send(:html_attrs, {
|
34
|
+
:class => "testing", :id => "test_2",
|
35
|
+
:nested => {:something => 'is_awesome'}
|
36
|
+
})
|
37
|
+
assert_match /^\s{1}/, attrs
|
38
|
+
assert attrs.include?('class="testing"')
|
39
|
+
assert attrs.include?('id="test_2"')
|
40
|
+
assert attrs.include?('nested="somethingis_awesome"')
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
|
45
|
+
|
46
|
+
class SerializeTest < BasicTest
|
47
|
+
context "when serialized"
|
48
|
+
|
49
|
+
should "buffer an empty html tag with no attrs" do
|
50
|
+
tag = Undies::Tag.new(:br)
|
51
|
+
assert_equal "<br />", tag.to_s
|
52
|
+
end
|
53
|
+
|
54
|
+
should "buffer an html tag with attrs" do
|
55
|
+
tag = Undies::Tag.new(:br, {:class => 'big'})
|
56
|
+
assert_equal '<br class="big" />', tag.to_s
|
57
|
+
end
|
58
|
+
|
59
|
+
should "buffer an html tag with attrs and content" do
|
60
|
+
tag = Undies::Tag.new(:strong, {:class => 'big'}) { __ "Loud Noises!" }
|
61
|
+
assert_equal '<strong class="big">Loud Noises!</strong>', tag.to_s
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
class CSSProxyTest < BasicTest
|
68
|
+
should "respond to any method ending in '!' as an id proxy" do
|
69
|
+
assert subject.respond_to?(:asdgasdg!)
|
70
|
+
end
|
71
|
+
|
72
|
+
should "proxy id attr with methods ending in '!'" do
|
73
|
+
assert_equal({
|
74
|
+
:id => 'thing1'
|
75
|
+
}, subject.thing1!.attrs)
|
76
|
+
end
|
77
|
+
|
78
|
+
should "nest tags from proxy id call" do
|
79
|
+
assert_equal(
|
80
|
+
"<div id=\"thing1\">stuff</div>",
|
81
|
+
subject.thing1! { _ 'stuff' }.to_s
|
82
|
+
)
|
83
|
+
end
|
84
|
+
|
85
|
+
should "proxy id attr with last method call ending in '!'" do
|
86
|
+
assert_equal({
|
87
|
+
:id => 'thing2'
|
88
|
+
}, subject.thing1!.thing2!.attrs)
|
89
|
+
end
|
90
|
+
|
91
|
+
should "set id attr to explicit if called last " do
|
92
|
+
assert_equal({
|
93
|
+
:id => 'thing3'
|
94
|
+
}, subject.thing1!.thing2!(:id => 'thing3').attrs)
|
95
|
+
end
|
96
|
+
|
97
|
+
should "set id attr to proxy if called last" do
|
98
|
+
assert_equal({
|
99
|
+
:id => 'thing1'
|
100
|
+
}, subject.thing2!(:id => 'thing3').thing1!.attrs)
|
101
|
+
end
|
102
|
+
|
103
|
+
should "respond to any other method as a class proxy" do
|
104
|
+
assert subject.respond_to?(:asdgasdg)
|
105
|
+
end
|
106
|
+
|
107
|
+
should "proxy single html class attr" do
|
108
|
+
assert_equal({
|
109
|
+
:class => 'thing'
|
110
|
+
}, subject.thing.attrs)
|
111
|
+
end
|
112
|
+
|
113
|
+
should "nest tags from proxy class call" do
|
114
|
+
assert_equal(
|
115
|
+
"<div class=\"thing\">stuff</div>",
|
116
|
+
subject.thing { _ 'stuff' }.to_s
|
117
|
+
)
|
118
|
+
end
|
119
|
+
|
120
|
+
should "proxy multi html class attrs" do
|
121
|
+
assert_equal({
|
122
|
+
:class => 'list thing awesome'
|
123
|
+
}, subject.list.thing.awesome.attrs)
|
124
|
+
end
|
125
|
+
|
126
|
+
should "set class attr with explicit if called last " do
|
127
|
+
assert_equal({
|
128
|
+
:class => 'list'
|
129
|
+
}, subject.thing.awesome(:class => "list").attrs)
|
130
|
+
end
|
131
|
+
|
132
|
+
should "update class attr with proxy if called last" do
|
133
|
+
assert_equal({
|
134
|
+
:class => 'list is good'
|
135
|
+
}, subject.thing.awesome(:class => "list is").good.attrs)
|
136
|
+
end
|
137
|
+
|
138
|
+
should "proxy mixed class and id selector attrs" do
|
139
|
+
assert_equal({
|
140
|
+
:class => 'list is good',
|
141
|
+
:id => "thing3"
|
142
|
+
}, subject.thing1!.awesome({
|
143
|
+
:class => "list is",
|
144
|
+
:id => "thing2"
|
145
|
+
}).good.thing3!.attrs)
|
146
|
+
end
|
147
|
+
|
148
|
+
end
|
149
|
+
|
150
|
+
|
151
|
+
|
152
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require "test/helper"
|
2
|
+
require "undies/template"
|
3
|
+
|
4
|
+
class Undies::Template
|
5
|
+
|
6
|
+
class BasicTest < Test::Unit::TestCase
|
7
|
+
include TestBelt
|
8
|
+
|
9
|
+
context 'template'
|
10
|
+
subject { Undies::Template.new {} }
|
11
|
+
should have_instance_methods :to_s
|
12
|
+
end
|
13
|
+
|
14
|
+
class DefinitionTest < BasicTest
|
15
|
+
should "generate markup given a block" do
|
16
|
+
assert_equal(
|
17
|
+
"<html><head></head><body><div class=\"loud element\" id=\"header\">YEA!!</div></body></html>",
|
18
|
+
Undies::Template.new do
|
19
|
+
_html {
|
20
|
+
_head {}
|
21
|
+
_body {
|
22
|
+
_div.header!.loud.element {
|
23
|
+
__ "YEA!!"
|
24
|
+
}
|
25
|
+
}
|
26
|
+
}
|
27
|
+
end.to_s
|
28
|
+
)
|
29
|
+
end
|
30
|
+
|
31
|
+
should "generate markup given a file" do
|
32
|
+
file = 'test/test_template.html.rb'
|
33
|
+
assert_equal(
|
34
|
+
"<html><head></head><body><div class=\"file\">FILE!!</div></body></html>",
|
35
|
+
Undies::Template.new(File.expand_path(file)).to_s
|
36
|
+
)
|
37
|
+
end
|
38
|
+
|
39
|
+
should "generate pretty printed markup" do
|
40
|
+
file = 'test/test_template.html.rb'
|
41
|
+
assert_equal(
|
42
|
+
%{<html>
|
43
|
+
<head>
|
44
|
+
</head>
|
45
|
+
<body>
|
46
|
+
<div class="file">
|
47
|
+
FILE!!
|
48
|
+
</div>
|
49
|
+
</body>
|
50
|
+
</html>
|
51
|
+
},
|
52
|
+
Undies::Template.new(File.expand_path(file)).to_s(2)
|
53
|
+
)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
data/undies.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "undies/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "undies"
|
7
|
+
s.version = Undies::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Kelly D. Redding"]
|
10
|
+
s.email = ["kelly@kelredd.com"]
|
11
|
+
s.homepage = "http://github.com/kelredd/undies"
|
12
|
+
s.summary = %q{A pure-Ruby HTML templating DSL.}
|
13
|
+
s.description = %q{A pure-Ruby HTML templating DSL.}
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
|
20
|
+
s.add_development_dependency("bundler", ["~> 1.0"])
|
21
|
+
s.add_development_dependency("test-belt", ["~> 1.1.0"])
|
22
|
+
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: undies
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Kelly D. Redding
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-07-22 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
22
|
+
none: false
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
hash: 15
|
27
|
+
segments:
|
28
|
+
- 1
|
29
|
+
- 0
|
30
|
+
version: "1.0"
|
31
|
+
version_requirements: *id001
|
32
|
+
prerelease: false
|
33
|
+
name: bundler
|
34
|
+
type: :development
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ~>
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
hash: 19
|
42
|
+
segments:
|
43
|
+
- 1
|
44
|
+
- 1
|
45
|
+
- 0
|
46
|
+
version: 1.1.0
|
47
|
+
version_requirements: *id002
|
48
|
+
prerelease: false
|
49
|
+
name: test-belt
|
50
|
+
type: :development
|
51
|
+
description: A pure-Ruby HTML templating DSL.
|
52
|
+
email:
|
53
|
+
- kelly@kelredd.com
|
54
|
+
executables: []
|
55
|
+
|
56
|
+
extensions: []
|
57
|
+
|
58
|
+
extra_rdoc_files: []
|
59
|
+
|
60
|
+
files:
|
61
|
+
- .gitignore
|
62
|
+
- Gemfile
|
63
|
+
- Gemfile.lock
|
64
|
+
- README.rdoc
|
65
|
+
- Rakefile
|
66
|
+
- lib/undies.rb
|
67
|
+
- lib/undies/buffer.rb
|
68
|
+
- lib/undies/source.rb
|
69
|
+
- lib/undies/tag.rb
|
70
|
+
- lib/undies/template.rb
|
71
|
+
- lib/undies/version.rb
|
72
|
+
- test/buffer_test.rb
|
73
|
+
- test/env.rb
|
74
|
+
- test/helper.rb
|
75
|
+
- test/source_test.rb
|
76
|
+
- test/tag_test.rb
|
77
|
+
- test/template_test.rb
|
78
|
+
- test/test_template.html.rb
|
79
|
+
- undies.gemspec
|
80
|
+
homepage: http://github.com/kelredd/undies
|
81
|
+
licenses: []
|
82
|
+
|
83
|
+
post_install_message:
|
84
|
+
rdoc_options: []
|
85
|
+
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
hash: 3
|
94
|
+
segments:
|
95
|
+
- 0
|
96
|
+
version: "0"
|
97
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
98
|
+
none: false
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
hash: 3
|
103
|
+
segments:
|
104
|
+
- 0
|
105
|
+
version: "0"
|
106
|
+
requirements: []
|
107
|
+
|
108
|
+
rubyforge_project:
|
109
|
+
rubygems_version: 1.7.2
|
110
|
+
signing_key:
|
111
|
+
specification_version: 3
|
112
|
+
summary: A pure-Ruby HTML templating DSL.
|
113
|
+
test_files:
|
114
|
+
- test/buffer_test.rb
|
115
|
+
- test/env.rb
|
116
|
+
- test/helper.rb
|
117
|
+
- test/source_test.rb
|
118
|
+
- test/tag_test.rb
|
119
|
+
- test/template_test.rb
|
120
|
+
- test/test_template.html.rb
|