joho-Markaby 0.6.0
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/CHANGELOG +43 -0
- data/Markaby.gemspec +69 -0
- data/README +256 -0
- data/Rakefile +49 -0
- data/VERSION +1 -0
- data/init.rb +13 -0
- data/lib/markaby.rb +35 -0
- data/lib/markaby/builder.rb +289 -0
- data/lib/markaby/cssproxy.rb +48 -0
- data/lib/markaby/kernel_method.rb +7 -0
- data/lib/markaby/metaid.rb +16 -0
- data/lib/markaby/rails.rb +90 -0
- data/lib/markaby/tags.rb +179 -0
- data/lib/markaby/template.rb +31 -0
- data/setup.rb +1551 -0
- data/test/rails/markaby/_monkeys.mab +12 -0
- data/test/rails/markaby/broken.mab +7 -0
- data/test/rails/markaby/create.mab +9 -0
- data/test/rails/markaby/index.mab +7 -0
- data/test/rails/monkeys.html +13 -0
- data/test/rails/test_helper.rb +7 -0
- data/test/rails/test_preamble.rb +30 -0
- data/test/rails_test.rb +99 -0
- data/test/test_markaby.rb +126 -0
- data/tools/rakehelp.rb +106 -0
- metadata +94 -0
@@ -0,0 +1,289 @@
|
|
1
|
+
require 'markaby/tags'
|
2
|
+
|
3
|
+
module Markaby
|
4
|
+
# The Markaby::Builder class is the central gear in the system. When using
|
5
|
+
# from Ruby code, this is the only class you need to instantiate directly.
|
6
|
+
#
|
7
|
+
# mab = Markaby::Builder.new
|
8
|
+
# mab.html do
|
9
|
+
# head { title "Boats.com" }
|
10
|
+
# body do
|
11
|
+
# h1 "Boats.com has great deals"
|
12
|
+
# ul do
|
13
|
+
# li "$49 for a canoe"
|
14
|
+
# li "$39 for a raft"
|
15
|
+
# li "$29 for a huge boot that floats and can fit 5 people"
|
16
|
+
# end
|
17
|
+
# end
|
18
|
+
# end
|
19
|
+
# puts mab.to_s
|
20
|
+
#
|
21
|
+
class Builder
|
22
|
+
|
23
|
+
@@default = {
|
24
|
+
:indent => 0,
|
25
|
+
:output_helpers => true,
|
26
|
+
:output_xml_instruction => true,
|
27
|
+
:output_meta_tag => true,
|
28
|
+
:auto_validation => true,
|
29
|
+
:tagset => Markaby::XHTMLTransitional,
|
30
|
+
:root_attributes => {
|
31
|
+
:xmlns => 'http://www.w3.org/1999/xhtml', :'xml:lang' => 'en', :lang => 'en'
|
32
|
+
}
|
33
|
+
}
|
34
|
+
|
35
|
+
def self.set(option, value)
|
36
|
+
@@default[option] = value
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.ignored_helpers
|
40
|
+
@@ignored_helpers ||= []
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.ignore_helpers(*helpers)
|
44
|
+
ignored_helpers.concat helpers
|
45
|
+
end
|
46
|
+
|
47
|
+
attr_accessor :output_helpers, :tagset
|
48
|
+
|
49
|
+
# Create a Markaby builder object. Pass in a hash of variable assignments to
|
50
|
+
# +assigns+ which will be available as instance variables inside tag construction
|
51
|
+
# blocks. If an object is passed in to +helpers+, its methods will be available
|
52
|
+
# from those same blocks.
|
53
|
+
#
|
54
|
+
# Pass in a +block+ to new and the block will be evaluated.
|
55
|
+
#
|
56
|
+
# mab = Markaby::Builder.new {
|
57
|
+
# html do
|
58
|
+
# body do
|
59
|
+
# h1 "Matching Mole"
|
60
|
+
# end
|
61
|
+
# end
|
62
|
+
# }
|
63
|
+
#
|
64
|
+
def initialize(assigns = {}, helpers = nil, &block)
|
65
|
+
@streams = [[]]
|
66
|
+
@assigns = assigns.dup
|
67
|
+
@helpers = helpers
|
68
|
+
@elements = {}
|
69
|
+
|
70
|
+
@@default.each do |k, v|
|
71
|
+
instance_variable_set("@#{k}", @assigns.delete(k) || v)
|
72
|
+
end
|
73
|
+
|
74
|
+
@assigns.each do |k, v|
|
75
|
+
instance_variable_set("@#{k}", v)
|
76
|
+
end
|
77
|
+
|
78
|
+
@builder = XmlMarkup.new(:indent => @indent, :target => @streams.last)
|
79
|
+
|
80
|
+
text(capture(&block)) if block
|
81
|
+
end
|
82
|
+
|
83
|
+
# Returns a string containing the HTML stream. Internally, the stream is stored as an Array.
|
84
|
+
def to_s
|
85
|
+
@streams.last.to_s
|
86
|
+
end
|
87
|
+
|
88
|
+
# Write a +string+ to the HTML stream without escaping it.
|
89
|
+
def text(string)
|
90
|
+
@builder << string.to_s
|
91
|
+
nil
|
92
|
+
end
|
93
|
+
alias_method :<<, :text
|
94
|
+
alias_method :concat, :text
|
95
|
+
|
96
|
+
# Captures the HTML code built inside the +block+. This is done by creating a new
|
97
|
+
# stream for the builder object, running the block and passing back its stream as a string.
|
98
|
+
#
|
99
|
+
# >> Markaby::Builder.new.capture { h1 "TEST"; h2 "CAPTURE ME" }
|
100
|
+
# => "<h1>TITLE</h1>\n<h2>CAPTURE ME</h2>\n"
|
101
|
+
#
|
102
|
+
def capture(&block)
|
103
|
+
@streams.push(@builder.target = [])
|
104
|
+
@builder.level += 1
|
105
|
+
str = instance_eval(&block)
|
106
|
+
str = @streams.last.join if @streams.last.any?
|
107
|
+
@streams.pop
|
108
|
+
@builder.level -= 1
|
109
|
+
@builder.target = @streams.last
|
110
|
+
str
|
111
|
+
end
|
112
|
+
|
113
|
+
# Create a tag named +tag+. Other than the first argument which is the tag name,
|
114
|
+
# the arguments are the same as the tags implemented via method_missing.
|
115
|
+
def tag!(tag, *args, &block)
|
116
|
+
ele_id = nil
|
117
|
+
if @auto_validation and @tagset
|
118
|
+
if !@tagset.tagset.has_key?(tag)
|
119
|
+
raise InvalidXhtmlError, "no element `#{tag}' for #{tagset.doctype}"
|
120
|
+
elsif args.last.respond_to?(:to_hash)
|
121
|
+
attrs = args.last.to_hash
|
122
|
+
|
123
|
+
if @tagset.forms.include?(tag) and attrs[:id]
|
124
|
+
attrs[:name] ||= attrs[:id]
|
125
|
+
end
|
126
|
+
|
127
|
+
attrs.each do |k, v|
|
128
|
+
atname = k.to_s.downcase.intern
|
129
|
+
unless k =~ /:/ or @tagset.tagset[tag].include? atname
|
130
|
+
raise InvalidXhtmlError, "no attribute `#{k}' on #{tag} elements"
|
131
|
+
end
|
132
|
+
if atname == :id
|
133
|
+
ele_id = v.to_s
|
134
|
+
if @elements.has_key? ele_id
|
135
|
+
raise InvalidXhtmlError, "id `#{ele_id}' already used (id's must be unique)."
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
141
|
+
if block
|
142
|
+
str = capture(&block)
|
143
|
+
block = proc { text(str) }
|
144
|
+
end
|
145
|
+
|
146
|
+
f = fragment { @builder.method_missing(tag, *args, &block) }
|
147
|
+
@elements[ele_id] = f if ele_id
|
148
|
+
f
|
149
|
+
end
|
150
|
+
|
151
|
+
# This method is used to intercept calls to helper methods and instance
|
152
|
+
# variables. Here is the order of interception:
|
153
|
+
#
|
154
|
+
# * If +sym+ is a helper method, the helper method is called
|
155
|
+
# and output to the stream.
|
156
|
+
# * If +sym+ is a Builder::XmlMarkup method, it is passed on to the builder object.
|
157
|
+
# * If +sym+ is also the name of an instance variable, the
|
158
|
+
# value of the instance variable is returned.
|
159
|
+
# * If +sym+ has come this far and no +tagset+ is found, +sym+ and its arguments are passed to tag!
|
160
|
+
# * If a tagset is found, though, +NoMethodError+ is raised.
|
161
|
+
#
|
162
|
+
# method_missing used to be the lynchpin in Markaby, but it's no longer used to handle
|
163
|
+
# HTML tags. See html_tag for that.
|
164
|
+
def method_missing(sym, *args, &block)
|
165
|
+
if @helpers.respond_to?(sym, true) && !self.class.ignored_helpers.include?(sym)
|
166
|
+
r = @helpers.send(sym, *args, &block)
|
167
|
+
if @output_helpers and r.respond_to? :to_str
|
168
|
+
fragment { @builder << r }
|
169
|
+
else
|
170
|
+
r
|
171
|
+
end
|
172
|
+
elsif @assigns.has_key?(sym)
|
173
|
+
@assigns[sym]
|
174
|
+
elsif @assigns.has_key?(stringy_key = sym.to_s)
|
175
|
+
# Rails' ActionView assigns hash has string keys for
|
176
|
+
# instance variables that are defined in the controller.
|
177
|
+
@assigns[stringy_key]
|
178
|
+
elsif instance_variables.include?(ivar = "@#{sym}")
|
179
|
+
instance_variable_get(ivar)
|
180
|
+
elsif !@helpers.nil? && @helpers.instance_variables.include?(ivar)
|
181
|
+
@helpers.instance_variable_get(ivar)
|
182
|
+
elsif ::Builder::XmlMarkup.instance_methods.include?(sym.to_s)
|
183
|
+
@builder.__send__(sym, *args, &block)
|
184
|
+
elsif @tagset.nil?
|
185
|
+
tag!(sym, *args, &block)
|
186
|
+
else
|
187
|
+
raise NoMethodError, "no such method `#{sym}'"
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
# Every HTML tag method goes through an html_tag call. So, calling <tt>div</tt> is equivalent
|
192
|
+
# to calling <tt>html_tag(:div)</tt>. All HTML tags in Markaby's list are given generated wrappers
|
193
|
+
# for this method.
|
194
|
+
#
|
195
|
+
# If the @auto_validation setting is on, this method will check for many common mistakes which
|
196
|
+
# could lead to invalid XHTML.
|
197
|
+
def html_tag(sym, *args, &block)
|
198
|
+
if @auto_validation and @tagset.self_closing.include?(sym) and block
|
199
|
+
raise InvalidXhtmlError, "the `#{sym}' element is self-closing, please remove the block"
|
200
|
+
elsif args.empty? and block.nil?
|
201
|
+
CssProxy.new(self, @streams.last, sym)
|
202
|
+
else
|
203
|
+
tag!(sym, *args, &block)
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
XHTMLTransitional.tags.each do |k|
|
208
|
+
class_eval %{
|
209
|
+
def #{k}(*args, &block)
|
210
|
+
html_tag(#{k.inspect}, *args, &block)
|
211
|
+
end
|
212
|
+
}
|
213
|
+
end
|
214
|
+
|
215
|
+
remove_method :head
|
216
|
+
|
217
|
+
# Builds a head tag. Adds a <tt>meta</tt> tag inside with Content-Type
|
218
|
+
# set to <tt>text/html; charset=utf-8</tt>.
|
219
|
+
def head(*args, &block)
|
220
|
+
tag!(:head, *args) do
|
221
|
+
tag!(:meta, "http-equiv" => "Content-Type", "content" => "text/html; charset=utf-8") if @output_meta_tag
|
222
|
+
instance_eval(&block)
|
223
|
+
end
|
224
|
+
end
|
225
|
+
|
226
|
+
# Builds an html tag. An XML 1.0 instruction and an XHTML 1.0 Transitional doctype
|
227
|
+
# are prepended. Also assumes <tt>:xmlns => "http://www.w3.org/1999/xhtml",
|
228
|
+
# :lang => "en"</tt>.
|
229
|
+
def xhtml_transitional(attrs = {}, &block)
|
230
|
+
self.tagset = Markaby::XHTMLTransitional
|
231
|
+
xhtml_html(attrs, &block)
|
232
|
+
end
|
233
|
+
|
234
|
+
# Builds an html tag with XHTML 1.0 Strict doctype instead.
|
235
|
+
def xhtml_strict(attrs = {}, &block)
|
236
|
+
self.tagset = Markaby::XHTMLStrict
|
237
|
+
xhtml_html(attrs, &block)
|
238
|
+
end
|
239
|
+
|
240
|
+
# Builds an html tag with XHTML 1.0 Frameset doctype instead.
|
241
|
+
def xhtml_frameset(attrs = {}, &block)
|
242
|
+
self.tagset = Markaby::XHTMLFrameset
|
243
|
+
xhtml_html(attrs, &block)
|
244
|
+
end
|
245
|
+
|
246
|
+
private
|
247
|
+
|
248
|
+
def xhtml_html(attrs = {}, &block)
|
249
|
+
instruct! if @output_xml_instruction
|
250
|
+
declare!(:DOCTYPE, :html, :PUBLIC, *tagset.doctype)
|
251
|
+
tag!(:html, @root_attributes.merge(attrs), &block)
|
252
|
+
end
|
253
|
+
|
254
|
+
def fragment
|
255
|
+
stream = @streams.last
|
256
|
+
start = stream.length
|
257
|
+
yield
|
258
|
+
length = stream.length - start
|
259
|
+
Fragment.new(stream, start, length)
|
260
|
+
end
|
261
|
+
|
262
|
+
end
|
263
|
+
|
264
|
+
# Every tag method in Markaby returns a Fragment. If any method gets called on the Fragment,
|
265
|
+
# the tag is removed from the Markaby stream and given back as a string. Usually the fragment
|
266
|
+
# is never used, though, and the stream stays intact.
|
267
|
+
#
|
268
|
+
# For a more practical explanation, check out the README.
|
269
|
+
class Fragment < ::Builder::BlankSlate
|
270
|
+
def initialize(*args)
|
271
|
+
@stream, @start, @length = args
|
272
|
+
end
|
273
|
+
def method_missing(*args, &block)
|
274
|
+
# We can't do @stream.slice!(@start, @length),
|
275
|
+
# as it would invalidate the @starts and @lengths of other Fragment instances.
|
276
|
+
@str = @stream[@start, @length].to_s
|
277
|
+
@stream[@start, @length] = [nil] * @length
|
278
|
+
def self.method_missing(*args, &block)
|
279
|
+
@str.send(*args, &block)
|
280
|
+
end
|
281
|
+
@str.send(*args, &block)
|
282
|
+
end
|
283
|
+
end
|
284
|
+
|
285
|
+
class XmlMarkup < ::Builder::XmlMarkup
|
286
|
+
attr_accessor :target, :level
|
287
|
+
end
|
288
|
+
|
289
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module Markaby
|
2
|
+
# Class used by Markaby::Builder to store element options. Methods called
|
3
|
+
# against the CssProxy object are added as element classes or IDs.
|
4
|
+
#
|
5
|
+
# See the README for examples.
|
6
|
+
class CssProxy
|
7
|
+
|
8
|
+
# Creates a CssProxy object.
|
9
|
+
def initialize(builder, stream, sym)
|
10
|
+
@builder, @stream, @sym, @attrs = builder, stream, sym, {}
|
11
|
+
|
12
|
+
@original_stream_length = @stream.length
|
13
|
+
|
14
|
+
@builder.tag! @sym
|
15
|
+
end
|
16
|
+
|
17
|
+
# Adds attributes to an element. Bang methods set the :id attribute.
|
18
|
+
# Other methods add to the :class attribute.
|
19
|
+
def method_missing(id_or_class, *args, &block)
|
20
|
+
if (idc = id_or_class.to_s) =~ /!$/
|
21
|
+
@attrs[:id] = $`
|
22
|
+
else
|
23
|
+
@attrs[:class] = @attrs[:class].nil? ? idc : "#{@attrs[:class]} #{idc}".strip
|
24
|
+
end
|
25
|
+
|
26
|
+
unless args.empty?
|
27
|
+
if args.last.respond_to? :to_hash
|
28
|
+
@attrs.merge! args.pop.to_hash
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
args.push(@attrs)
|
33
|
+
|
34
|
+
while @stream.length > @original_stream_length
|
35
|
+
@stream.pop
|
36
|
+
end
|
37
|
+
|
38
|
+
if block
|
39
|
+
@builder.tag! @sym, *args, &block
|
40
|
+
else
|
41
|
+
@builder.tag! @sym, *args
|
42
|
+
end
|
43
|
+
|
44
|
+
return self
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# metaprogramming assistant -- metaid.rb
|
2
|
+
class Object # :nodoc:
|
3
|
+
# The hidden singleton lurks behind everyone
|
4
|
+
def metaclass; class << self; self; end; end
|
5
|
+
def meta_eval(&blk); metaclass.instance_eval(&blk); end
|
6
|
+
|
7
|
+
# Adds methods to a metaclass
|
8
|
+
def meta_def(name, &blk)
|
9
|
+
meta_eval { define_method(name, &blk) }
|
10
|
+
end
|
11
|
+
|
12
|
+
# Defines an instance method within a class
|
13
|
+
def class_def(name, &blk)
|
14
|
+
class_eval { define_method(name, &blk) }
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
module ActionView # :nodoc:
|
2
|
+
class Base # :nodoc:
|
3
|
+
def render_template(template_extension, template, file_path = nil, local_assigns = {})
|
4
|
+
if handler = @@template_handlers[template_extension]
|
5
|
+
template ||= read_template_file(file_path, template_extension)
|
6
|
+
handler.new(self).render(template, local_assigns, file_path)
|
7
|
+
else
|
8
|
+
compile_and_render_template(template_extension, template, file_path, local_assigns)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
module Markaby
|
15
|
+
module Rails
|
16
|
+
# Markaby helpers for Rails.
|
17
|
+
module ActionControllerHelpers
|
18
|
+
# Returns a string of HTML built from the attached +block+. Any +options+ are
|
19
|
+
# passed into the render method.
|
20
|
+
#
|
21
|
+
# Use this method in your controllers to output Markaby directly from inside.
|
22
|
+
def render_markaby(options = {}, &block)
|
23
|
+
render options.merge({ :text => Builder.new(options[:locals], self, &block).to_s })
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class ActionViewTemplateHandler # :nodoc:
|
28
|
+
def initialize(action_view)
|
29
|
+
@action_view = action_view
|
30
|
+
end
|
31
|
+
def render(template, local_assigns, file_path)
|
32
|
+
template = Template.new(template)
|
33
|
+
template.path = file_path
|
34
|
+
template.render(@action_view.assigns.merge(local_assigns), @action_view)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
class Builder < Markaby::Builder # :nodoc:
|
39
|
+
def initialize(*args, &block)
|
40
|
+
super *args, &block
|
41
|
+
|
42
|
+
@assigns.each { |k, v| @helpers.instance_variable_set("@#{k}", v) }
|
43
|
+
end
|
44
|
+
|
45
|
+
def flash(*args)
|
46
|
+
@helpers.controller.send(:flash, *args)
|
47
|
+
end
|
48
|
+
|
49
|
+
# Emulate ERB to satisfy helpers like <tt>form_for</tt>.
|
50
|
+
def _erbout
|
51
|
+
@_erbout ||= FauxErbout.new(self)
|
52
|
+
end
|
53
|
+
|
54
|
+
# Content_for will store the given block in an instance variable for later use
|
55
|
+
# in another template or in the layout.
|
56
|
+
#
|
57
|
+
# The name of the instance variable is content_for_<name> to stay consistent
|
58
|
+
# with @content_for_layout which is used by ActionView's layouts.
|
59
|
+
#
|
60
|
+
# Example:
|
61
|
+
#
|
62
|
+
# content_for("header") do
|
63
|
+
# h1 "Half Shark and Half Lion"
|
64
|
+
# end
|
65
|
+
#
|
66
|
+
# If used several times, the variable will contain all the parts concatenated.
|
67
|
+
def content_for(name, &block)
|
68
|
+
@helpers.assigns["content_for_#{name}"] =
|
69
|
+
eval("@content_for_#{name} = (@content_for_#{name} || '') + capture(&block)")
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
Template.builder_class = Builder
|
76
|
+
|
77
|
+
class FauxErbout < ::Builder::BlankSlate # :nodoc:
|
78
|
+
def initialize(builder)
|
79
|
+
@builder = builder
|
80
|
+
end
|
81
|
+
def nil? # see ActionView::Helpers::CaptureHelper#capture
|
82
|
+
true
|
83
|
+
end
|
84
|
+
def method_missing(*args, &block)
|
85
|
+
@builder.send *args, &block
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
end
|