smtlaissezfaire-simply 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README +4 -0
- data/Rakefile +17 -0
- data/VERSION.yml +4 -0
- data/lib/simply/html_builder.rb +127 -0
- data/lib/simply/html_tags.rb +126 -0
- data/lib/simply/indentation.rb +37 -0
- data/lib/simply/locals.rb +29 -0
- data/lib/simply/version.rb +12 -0
- data/lib/simply.rb +18 -0
- data/spec/simply/html_builder/indentation_spec.rb +44 -0
- data/spec/simply/html_builder/locals_spec.rb +49 -0
- data/spec/simply/html_builder_spec.rb +136 -0
- data/spec/simply/indentation_spec.rb +72 -0
- data/spec/simply/simply_spec.rb +45 -0
- data/spec/simply/version_spec.rb +7 -0
- data/spec/spec.opts +3 -0
- data/spec/spec_helper.rb +5 -0
- metadata +75 -0
data/README
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
PROJECT_NAME = "simply"
|
4
|
+
|
5
|
+
begin
|
6
|
+
require 'jeweler'
|
7
|
+
Jeweler::Tasks.new do |s|
|
8
|
+
s.name = "#{PROJECT_NAME}"
|
9
|
+
s.summary = "TODO"
|
10
|
+
s.email = "scott@railsnewbie.com"
|
11
|
+
s.homepage = "http://github.com/smtlaissezfaire/#{PROJECT_NAME.downcase}"
|
12
|
+
s.description = "TODO"
|
13
|
+
s.authors = ["Scott Taylor"]
|
14
|
+
end
|
15
|
+
rescue LoadError
|
16
|
+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
17
|
+
end
|
data/VERSION.yml
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
module Simply
|
2
|
+
class HTMLBuilder
|
3
|
+
include Locals
|
4
|
+
|
5
|
+
unless defined?(SELF_CLOSING_TAGS)
|
6
|
+
SELF_CLOSING_TAGS = HTMLTags::SELF_CLOSING_TAGS
|
7
|
+
BLOCK_TAGS = HTMLTags::BLOCK_TAGS
|
8
|
+
end
|
9
|
+
|
10
|
+
def initialize(options={ }, &block)
|
11
|
+
@out = ""
|
12
|
+
|
13
|
+
if locals_hash = options[:locals]
|
14
|
+
self.locals = locals_hash
|
15
|
+
end
|
16
|
+
|
17
|
+
@indented = true if options[:indent] == true
|
18
|
+
@out.extend Indentation if indented?
|
19
|
+
|
20
|
+
instance_eval(&block) if block_given?
|
21
|
+
end
|
22
|
+
|
23
|
+
def indented?
|
24
|
+
@indented ? true : false
|
25
|
+
end
|
26
|
+
|
27
|
+
SELF_CLOSING_TAGS.each do |tag|
|
28
|
+
class_eval <<-HERE, __FILE__, __LINE__
|
29
|
+
def #{tag}(attributes={})
|
30
|
+
__self_closing_tag(:#{tag}, attributes)
|
31
|
+
end
|
32
|
+
HERE
|
33
|
+
end
|
34
|
+
|
35
|
+
BLOCK_TAGS.each do |tag|
|
36
|
+
class_eval <<-HERE, __FILE__, __LINE__
|
37
|
+
def #{tag}(*args, &block)
|
38
|
+
raise ArgumentError if !block_given? && args.empty?
|
39
|
+
|
40
|
+
if block_given? || args.first.is_a?(Hash)
|
41
|
+
options = args.first || { }
|
42
|
+
else
|
43
|
+
block = lambda { clean_text args.first.to_s }
|
44
|
+
options = args[1] || { }
|
45
|
+
end
|
46
|
+
|
47
|
+
__tag(:#{tag}, options, &block)
|
48
|
+
end
|
49
|
+
HERE
|
50
|
+
end
|
51
|
+
|
52
|
+
def xhtml_transitional
|
53
|
+
text HTMLTags::XML_ENCODING
|
54
|
+
text HTMLTags::TRANSITIONAL
|
55
|
+
end
|
56
|
+
|
57
|
+
def xhtml_strict
|
58
|
+
text HTMLTags::XML_ENCODING
|
59
|
+
text HTMLTags::STRICT
|
60
|
+
end
|
61
|
+
|
62
|
+
def xhtml_frameset
|
63
|
+
text HTMLTags::XML_ENCODING
|
64
|
+
text HTMLTags::FRAMESET
|
65
|
+
end
|
66
|
+
|
67
|
+
####################
|
68
|
+
#
|
69
|
+
# Utilities
|
70
|
+
#
|
71
|
+
####################
|
72
|
+
|
73
|
+
def text(out)
|
74
|
+
@out << out
|
75
|
+
end
|
76
|
+
|
77
|
+
def clean_text(out)
|
78
|
+
text html_escape(out)
|
79
|
+
end
|
80
|
+
|
81
|
+
def html_escape(out)
|
82
|
+
out.to_s.to_xs
|
83
|
+
end
|
84
|
+
|
85
|
+
def to_s
|
86
|
+
@out
|
87
|
+
end
|
88
|
+
|
89
|
+
private
|
90
|
+
|
91
|
+
def __tag(tag_name, attributes={ }, &block)
|
92
|
+
text __opening_tag(tag_name, attributes)
|
93
|
+
__eval_block(&block)
|
94
|
+
text __closing_tag(tag_name)
|
95
|
+
end
|
96
|
+
|
97
|
+
def __eval_block(&block)
|
98
|
+
@out.indent if indented?
|
99
|
+
instance_eval(&block)
|
100
|
+
@out.outdent if indented?
|
101
|
+
end
|
102
|
+
|
103
|
+
def __opening_tag(tag_name, attributes={ })
|
104
|
+
if attributes.any?
|
105
|
+
"<#{tag_name} #{__expand_attributes(attributes)}>"
|
106
|
+
else
|
107
|
+
"<#{tag_name}>"
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
def __closing_tag(tag_name)
|
112
|
+
"</#{tag_name}>"
|
113
|
+
end
|
114
|
+
|
115
|
+
def __self_closing_tag(tag_name, attributes={ })
|
116
|
+
if attributes.any?
|
117
|
+
text "<#{tag_name} #{__expand_attributes(attributes)} />"
|
118
|
+
else
|
119
|
+
text "<#{tag_name} />"
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
def __expand_attributes(attributes)
|
124
|
+
attributes.map { |key, value| "#{key}=\"#{value}\""}.join(" ")
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
@@ -0,0 +1,126 @@
|
|
1
|
+
module Simply
|
2
|
+
module HTMLTags
|
3
|
+
unless defined?(XML_ENCODING)
|
4
|
+
XML_ENCODING = '<?xml version="1.0" encoding="UTF-8"?>'.freeze
|
5
|
+
|
6
|
+
STRICT = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'.freeze
|
7
|
+
TRANSITIONAL = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'.freeze
|
8
|
+
FRAMESET = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">'.freeze
|
9
|
+
|
10
|
+
SELF_CLOSING_TAGS = [
|
11
|
+
:base,
|
12
|
+
:meta,
|
13
|
+
:link,
|
14
|
+
:hr,
|
15
|
+
:br,
|
16
|
+
:param,
|
17
|
+
:img,
|
18
|
+
:area,
|
19
|
+
:input,
|
20
|
+
:col,
|
21
|
+
:frame
|
22
|
+
]
|
23
|
+
|
24
|
+
BLOCK_TAGS = [
|
25
|
+
:html,
|
26
|
+
:head,
|
27
|
+
:title,
|
28
|
+
:style,
|
29
|
+
:script,
|
30
|
+
:noscript,
|
31
|
+
:body,
|
32
|
+
:div,
|
33
|
+
:p,
|
34
|
+
:ul,
|
35
|
+
:ol,
|
36
|
+
:li,
|
37
|
+
:dl,
|
38
|
+
:dt,
|
39
|
+
:dd,
|
40
|
+
:address,
|
41
|
+
:pre,
|
42
|
+
:blockquote,
|
43
|
+
:ins,
|
44
|
+
:del,
|
45
|
+
:a,
|
46
|
+
:span,
|
47
|
+
:bdo,
|
48
|
+
:em,
|
49
|
+
:strong,
|
50
|
+
:dfn,
|
51
|
+
:code,
|
52
|
+
:samp,
|
53
|
+
:kbd,
|
54
|
+
:var,
|
55
|
+
:cite,
|
56
|
+
:abbr,
|
57
|
+
:acronym,
|
58
|
+
:q,
|
59
|
+
:sub,
|
60
|
+
:sup,
|
61
|
+
:tt,
|
62
|
+
:i,
|
63
|
+
:b,
|
64
|
+
:big,
|
65
|
+
:small,
|
66
|
+
:object,
|
67
|
+
:map,
|
68
|
+
:form,
|
69
|
+
:label,
|
70
|
+
:select,
|
71
|
+
:optgroup,
|
72
|
+
:option,
|
73
|
+
:textarea,
|
74
|
+
:fieldset,
|
75
|
+
:legend,
|
76
|
+
:button,
|
77
|
+
:table,
|
78
|
+
:caption,
|
79
|
+
:colgroup,
|
80
|
+
:thead,
|
81
|
+
:tfoot,
|
82
|
+
:tbody,
|
83
|
+
:tr,
|
84
|
+
:th,
|
85
|
+
:td,
|
86
|
+
:h1,
|
87
|
+
:h2,
|
88
|
+
:h3,
|
89
|
+
:h4,
|
90
|
+
:h5,
|
91
|
+
:h6,
|
92
|
+
:strike,
|
93
|
+
:center,
|
94
|
+
:dir,
|
95
|
+
:noframes,
|
96
|
+
:basefont,
|
97
|
+
:u,
|
98
|
+
:menu,
|
99
|
+
:iframe,
|
100
|
+
:font,
|
101
|
+
:s,
|
102
|
+
:applet,
|
103
|
+
:isindex,
|
104
|
+
:script,
|
105
|
+
:a,
|
106
|
+
:td,
|
107
|
+
:h5,
|
108
|
+
:h3,
|
109
|
+
:li,
|
110
|
+
:div,
|
111
|
+
:pre,
|
112
|
+
:body,
|
113
|
+
:ol,
|
114
|
+
:h4,
|
115
|
+
:h2,
|
116
|
+
:object,
|
117
|
+
:legend,
|
118
|
+
:dl,
|
119
|
+
:h6,
|
120
|
+
:ul,
|
121
|
+
:form,
|
122
|
+
:h1
|
123
|
+
]
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module Simply
|
2
|
+
module Indentation
|
3
|
+
class IndentationError < StandardError
|
4
|
+
def message
|
5
|
+
"You can't outdent before the beginning of the page"
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
unless defined?(DEFAULT_INDENTATION_LEVEL)
|
10
|
+
DEFAULT_NUM_SPACES_TO_INDENT = 2
|
11
|
+
DEFAULT_INDENTATION_LEVEL = 0
|
12
|
+
end
|
13
|
+
|
14
|
+
def <<(text)
|
15
|
+
out = empty? ? "" : "\n"
|
16
|
+
out << "#{" " * indentation_level}#{text}"
|
17
|
+
|
18
|
+
super(out)
|
19
|
+
end
|
20
|
+
|
21
|
+
def indent(number_of_spaces = DEFAULT_NUM_SPACES_TO_INDENT)
|
22
|
+
@indentation_level ||= DEFAULT_INDENTATION_LEVEL
|
23
|
+
@indentation_level += number_of_spaces
|
24
|
+
end
|
25
|
+
|
26
|
+
def outdent(number_of_spaces = DEFAULT_NUM_SPACES_TO_INDENT)
|
27
|
+
@indentation_level ||= DEFAULT_INDENTATION_LEVEL
|
28
|
+
@indentation_level -= number_of_spaces
|
29
|
+
raise IndentationError if @indentation_level < 0
|
30
|
+
@indentation_level
|
31
|
+
end
|
32
|
+
|
33
|
+
def indentation_level
|
34
|
+
@indentation_level ||= DEFAULT_INDENTATION_LEVEL
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Simply
|
2
|
+
module Locals
|
3
|
+
# Pass in a hash of local variables
|
4
|
+
# which will be available to the template:
|
5
|
+
#
|
6
|
+
# @builder.locals = { :foo => 1, :bar => 2, :baz => 3}
|
7
|
+
# @builder.ul do
|
8
|
+
# li foo
|
9
|
+
# li bar
|
10
|
+
# li baz
|
11
|
+
# end
|
12
|
+
#
|
13
|
+
# @builder.to_s #=> "<ul><li>1</li><li>2</li><li>3</li></ul>"
|
14
|
+
def locals=(hash={ })
|
15
|
+
def metaclass(&block)
|
16
|
+
meta = class << self; self; end
|
17
|
+
meta.instance_eval(&block)
|
18
|
+
end
|
19
|
+
|
20
|
+
hash.each do |key, value|
|
21
|
+
metaclass do
|
22
|
+
define_method key do
|
23
|
+
value
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module Simply
|
2
|
+
module Version
|
3
|
+
unless defined?(Simply::VERSION)
|
4
|
+
version_info = YAML.load(File.read(File.dirname(__FILE__) + "/../../VERSION.yml"))
|
5
|
+
|
6
|
+
MAJOR = version_info[:major]
|
7
|
+
MINOR = version_info[:minor]
|
8
|
+
TINY = version_info[:patch]
|
9
|
+
VERSION = "#{MAJOR}.#{MINOR}.#{TINY}"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
data/lib/simply.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
module Simply
|
2
|
+
dir = File.dirname(__FILE__) + "/simply"
|
3
|
+
|
4
|
+
require "#{dir}/version"
|
5
|
+
include Version
|
6
|
+
|
7
|
+
require "rubygems"
|
8
|
+
require "builder/xchar"
|
9
|
+
require "#{dir}/locals"
|
10
|
+
require "#{dir}/indentation"
|
11
|
+
require "#{dir}/html_tags"
|
12
|
+
require "#{dir}/html_builder"
|
13
|
+
end
|
14
|
+
|
15
|
+
def Simply(options={}, &block)
|
16
|
+
builder = Simply::HTMLBuilder.new(options, &block)
|
17
|
+
builder.to_s
|
18
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/../../spec_helper"
|
2
|
+
|
3
|
+
module Simply
|
4
|
+
describe HTMLBuilder, "with indentation" do
|
5
|
+
before(:each) do
|
6
|
+
@builder = HTMLBuilder.new(:indent => true)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should be indented when passed :indent => true" do
|
10
|
+
builder = HTMLBuilder.new(:indent => true)
|
11
|
+
builder.should be_indented
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should not be indented when passed :indent => false" do
|
15
|
+
builder = HTMLBuilder.new(:indent => false)
|
16
|
+
builder.should_not be_indented
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should not be indented by default" do
|
20
|
+
builder = HTMLBuilder.new
|
21
|
+
builder.should_not be_indented
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should indent html when indented = true, and given a block structure" do
|
25
|
+
@builder.html do
|
26
|
+
body do
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
@builder.to_s.should == (<<-HERE).chomp
|
31
|
+
<html>
|
32
|
+
<body>
|
33
|
+
</body>
|
34
|
+
</html>
|
35
|
+
HERE
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should not indent tags which are on the same block level" do
|
39
|
+
@builder.br
|
40
|
+
@builder.br
|
41
|
+
@builder.to_s.should == "<br />\n<br />"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/../../spec_helper"
|
2
|
+
|
3
|
+
module Simply
|
4
|
+
describe HTMLBuilder, "with locals" do
|
5
|
+
before(:each) do
|
6
|
+
@builder = HTMLBuilder.new
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should have access to a variables value" do
|
10
|
+
@builder.locals = { :foo => "bar" }
|
11
|
+
@builder.ul do
|
12
|
+
li foo
|
13
|
+
end
|
14
|
+
|
15
|
+
@builder.to_s.should == "<ul><li>bar</li></ul>"
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should convert integer keys to strings" do
|
19
|
+
@builder.locals = { :foo => 1 }
|
20
|
+
@builder.ul do
|
21
|
+
li foo
|
22
|
+
end
|
23
|
+
|
24
|
+
@builder.to_s.should == "<ul><li>1</li></ul>"
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should be able to use any number of keys" do
|
28
|
+
@builder.locals = { :foo => 1, :bar => 2, :baz => 3}
|
29
|
+
@builder.ul do
|
30
|
+
li foo
|
31
|
+
li bar
|
32
|
+
li baz
|
33
|
+
end
|
34
|
+
|
35
|
+
@builder.to_s.should == "<ul><li>1</li><li>2</li><li>3</li></ul>"
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should take locals in the constructor" do
|
39
|
+
builder = HTMLBuilder.new(:locals => { :foo => "bar", :bar => "baz" }) do
|
40
|
+
ul do
|
41
|
+
li foo
|
42
|
+
li bar
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
builder.to_s.should == "<ul><li>bar</li><li>baz</li></ul>"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,136 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/../spec_helper"
|
2
|
+
|
3
|
+
module Simply
|
4
|
+
describe HTMLBuilder do
|
5
|
+
before(:each) do
|
6
|
+
@builder = HTMLBuilder.new
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should create a self-closing hr tag" do
|
10
|
+
@builder.br
|
11
|
+
@builder.to_s.should == "<br />"
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should create a self closing hr tag" do
|
15
|
+
@builder.hr
|
16
|
+
@builder.to_s.should == "<hr />"
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should create two self-closing tags" do
|
20
|
+
@builder.br
|
21
|
+
@builder.hr
|
22
|
+
@builder.to_s.should == "<br /><hr />"
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should output text directly with the text method" do
|
26
|
+
@builder.text "foo"
|
27
|
+
@builder.to_s.should == "foo"
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should have a paragraph tag" do
|
31
|
+
@builder.p "foo"
|
32
|
+
@builder.to_s.should == "<p>foo</p>"
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should contain the text inside the paragraph tags" do
|
36
|
+
@builder.p "bar baz"
|
37
|
+
@builder.to_s.should == "<p>bar baz</p>"
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should take options with a string given" do
|
41
|
+
@builder.p "bar baz", :class => :something
|
42
|
+
|
43
|
+
@builder.to_s.should == "<p class=\"something\">bar baz</p>"
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should have a paragraph tag which takes a block" do
|
47
|
+
@builder.p do
|
48
|
+
text "foo"
|
49
|
+
end
|
50
|
+
|
51
|
+
@builder.to_s.should == "<p>foo</p>"
|
52
|
+
end
|
53
|
+
|
54
|
+
Simply::HTMLBuilder::SELF_CLOSING_TAGS.each do |tag|
|
55
|
+
it "should have the tag #{tag}" do
|
56
|
+
@builder.send(tag)
|
57
|
+
@builder.to_s.should == "<#{tag} />"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should set options given to a self-closing tag" do
|
62
|
+
@builder.img :src => "foo"
|
63
|
+
@builder.to_s.should == "<img src=\"foo\" />"
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should set two options to a self-closing tag" do
|
67
|
+
@builder.img :src => "foo", :alt => :some_text
|
68
|
+
out = @builder.to_s
|
69
|
+
out.should =~ /<img.*alt=\"some_text\".*\/>/
|
70
|
+
out.should =~ /<img.*src=\"foo\".*\/>/
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should set options on a non-self closing tag (like a p)" do
|
74
|
+
@builder.p :class => :foo, :name => :something do
|
75
|
+
text "foo"
|
76
|
+
end
|
77
|
+
|
78
|
+
@builder.to_s.should == '<p class="foo" name="something">foo</p>'
|
79
|
+
end
|
80
|
+
|
81
|
+
Simply::HTMLBuilder::BLOCK_TAGS.each do |tag|
|
82
|
+
it "should have the #{tag} tag" do
|
83
|
+
@builder.send(tag, "foo")
|
84
|
+
@builder.to_s.should == "<#{tag}>foo</#{tag}>"
|
85
|
+
end
|
86
|
+
|
87
|
+
it "should contain the text inside the #{tag} tags" do
|
88
|
+
@builder.send(tag, "bar baz")
|
89
|
+
@builder.to_s.should == "<#{tag}>bar baz</#{tag}>"
|
90
|
+
end
|
91
|
+
|
92
|
+
it "should take options with a string given" do
|
93
|
+
@builder.send(tag, "bar baz", :class => :something)
|
94
|
+
|
95
|
+
@builder.to_s.should == "<#{tag} class=\"something\">bar baz</#{tag}>"
|
96
|
+
end
|
97
|
+
|
98
|
+
it "should have a paragraph tag which takes a block" do
|
99
|
+
@builder.send(tag) do
|
100
|
+
text "foo"
|
101
|
+
end
|
102
|
+
|
103
|
+
@builder.to_s.should == "<#{tag}>foo</#{tag}>"
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
it "should raise an argument error when calling a closing tag with no options" do
|
108
|
+
lambda {
|
109
|
+
@builder.p
|
110
|
+
}.should raise_error(ArgumentError)
|
111
|
+
end
|
112
|
+
|
113
|
+
it "should escape special html values" do
|
114
|
+
@builder.h1 'Apples & Oranges'
|
115
|
+
@builder.to_s.should == "<h1>Apples & Oranges</h1>"
|
116
|
+
end
|
117
|
+
|
118
|
+
it "should be able to generate the xhtml-transitional dtd declaration" do
|
119
|
+
@builder.xhtml_transitional
|
120
|
+
@builder.to_s.should include('<?xml version="1.0" encoding="UTF-8"?>')
|
121
|
+
@builder.to_s.should include('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">')
|
122
|
+
end
|
123
|
+
|
124
|
+
it "should be able to generate the xhtml strict declaration" do
|
125
|
+
@builder.xhtml_strict
|
126
|
+
@builder.to_s.should include('<?xml version="1.0" encoding="UTF-8"?>')
|
127
|
+
@builder.to_s.should include('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">')
|
128
|
+
end
|
129
|
+
|
130
|
+
it "should be able to generate the xhtml frameset" do
|
131
|
+
@builder.xhtml_frameset
|
132
|
+
@builder.to_s.should include('<?xml version="1.0" encoding="UTF-8"?>')
|
133
|
+
@builder.to_s.should include('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">')
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/../spec_helper"
|
2
|
+
|
3
|
+
module Simply
|
4
|
+
describe Indentation do
|
5
|
+
before(:each) do
|
6
|
+
@stream = ""
|
7
|
+
@stream.extend Indentation
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should be able to add text to the buffer" do
|
11
|
+
@stream << "foo"
|
12
|
+
@stream.should == "foo"
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should indent by two after a call to indent" do
|
16
|
+
@stream.indent
|
17
|
+
@stream << "foo"
|
18
|
+
@stream.should == " foo"
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should indent multiple calls, each on new lines" do
|
22
|
+
@stream.indent
|
23
|
+
@stream << "foo"
|
24
|
+
@stream << "bar"
|
25
|
+
@stream.should == " foo\n bar"
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should indent by 4, when passed 4 explicitly" do
|
29
|
+
@stream.indent(4)
|
30
|
+
@stream << "foo"
|
31
|
+
@stream.should == " foo"
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should nest indentation" do
|
35
|
+
@stream << "foo do"
|
36
|
+
@stream.indent
|
37
|
+
@stream << "something do"
|
38
|
+
@stream.indent
|
39
|
+
@stream << "bar do"
|
40
|
+
|
41
|
+
@stream.should == "foo do\n something do\n bar do"
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should outdent" do
|
45
|
+
@stream.indent
|
46
|
+
@stream.outdent
|
47
|
+
@stream << "foo"
|
48
|
+
@stream.should == "foo"
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should outdent by the number of spaces given" do
|
52
|
+
@stream.indent(2)
|
53
|
+
@stream.indent(2)
|
54
|
+
@stream.outdent(4)
|
55
|
+
@stream << "foo"
|
56
|
+
@stream.should == "foo"
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should raise an error if outdenting too far" do
|
60
|
+
@stream.indent(2)
|
61
|
+
lambda {
|
62
|
+
@stream.outdent(4)
|
63
|
+
}.should raise_error(Indentation::IndentationError, "You can't outdent before the beginning of the page")
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should raise an indentation error if the page hasn't yet been indented" do
|
67
|
+
lambda {
|
68
|
+
@stream.outdent
|
69
|
+
}.should raise_error(Indentation::IndentationError)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/../spec_helper"
|
2
|
+
|
3
|
+
describe Simply, "the method" do
|
4
|
+
it "should take a block" do
|
5
|
+
Simply do
|
6
|
+
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should produce html with the block" do
|
11
|
+
s = Simply do
|
12
|
+
br
|
13
|
+
end
|
14
|
+
|
15
|
+
s.should == "<br />"
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should pass locals in" do
|
19
|
+
s = Simply :locals => {:foo => "foo"} do
|
20
|
+
text foo
|
21
|
+
end
|
22
|
+
|
23
|
+
s.should == "foo"
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should pass indentation in" do
|
27
|
+
s = Simply :indent => true do
|
28
|
+
html do
|
29
|
+
body do
|
30
|
+
p "foo"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
s.should == (<<-HERE).chomp
|
36
|
+
<html>
|
37
|
+
<body>
|
38
|
+
<p>
|
39
|
+
foo
|
40
|
+
</p>
|
41
|
+
</body>
|
42
|
+
</html>
|
43
|
+
HERE
|
44
|
+
end
|
45
|
+
end
|
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: smtlaissezfaire-simply
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Scott Taylor
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-05-07 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: TODO
|
17
|
+
email: scott@railsnewbie.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README
|
24
|
+
files:
|
25
|
+
- Rakefile
|
26
|
+
- VERSION.yml
|
27
|
+
- lib/simply.rb
|
28
|
+
- lib/simply/html_builder.rb
|
29
|
+
- lib/simply/html_tags.rb
|
30
|
+
- lib/simply/indentation.rb
|
31
|
+
- lib/simply/locals.rb
|
32
|
+
- lib/simply/version.rb
|
33
|
+
- spec/simply/html_builder/indentation_spec.rb
|
34
|
+
- spec/simply/html_builder/locals_spec.rb
|
35
|
+
- spec/simply/html_builder_spec.rb
|
36
|
+
- spec/simply/indentation_spec.rb
|
37
|
+
- spec/simply/simply_spec.rb
|
38
|
+
- spec/simply/version_spec.rb
|
39
|
+
- spec/spec.opts
|
40
|
+
- spec/spec_helper.rb
|
41
|
+
- README
|
42
|
+
has_rdoc: true
|
43
|
+
homepage: http://github.com/smtlaissezfaire/simply
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options:
|
46
|
+
- --charset=UTF-8
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: "0"
|
54
|
+
version:
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: "0"
|
60
|
+
version:
|
61
|
+
requirements: []
|
62
|
+
|
63
|
+
rubyforge_project:
|
64
|
+
rubygems_version: 1.2.0
|
65
|
+
signing_key:
|
66
|
+
specification_version: 2
|
67
|
+
summary: TODO
|
68
|
+
test_files:
|
69
|
+
- spec/simply/html_builder/indentation_spec.rb
|
70
|
+
- spec/simply/html_builder/locals_spec.rb
|
71
|
+
- spec/simply/html_builder_spec.rb
|
72
|
+
- spec/simply/indentation_spec.rb
|
73
|
+
- spec/simply/simply_spec.rb
|
74
|
+
- spec/simply/version_spec.rb
|
75
|
+
- spec/spec_helper.rb
|