Class Markaby::Builder

  1. lib/markaby/builder.rb
Parent: Object

The Markaby::Builder class is the central gear in the system. When using from Ruby code, this is the only class you need to instantiate directly.

mab = Markaby::Builder.new
mab.html do
  head { title "Boats.com" }
  body do
    h1 "Boats.com has great deals"
    ul do
      li "$49 for a canoe"
      li "$39 for a raft"
      li "$29 for a huge boot that floats and can fit 5 people"
    end
  end
end
puts mab.to_s

Included modules

  1. Markaby::BuilderTags

Constants

DEFAULT_OPTIONS = { :indent => 0, :output_helpers => true, :output_xml_instruction => true, :output_meta_tag => true, :auto_validation => true, :tagset => Markaby::XHTMLTransitional, :root_attributes => { :xmlns => 'http://www.w3.org/1999/xhtml', :'xml:lang' => 'en', :lang => 'en' } }

Attributes

output_helpers [RW]
tagset [RW]

Public class methods

get (option)
[show source]
    # File lib/markaby/builder.rb, line 51
51:     def self.get(option)
52:       @@options[option]
53:     end
ignore_helpers (*helpers)
[show source]
    # File lib/markaby/builder.rb, line 59
59:     def self.ignore_helpers(*helpers)
60:       ignored_helpers.concat helpers
61:     end
ignored_helpers ()
[show source]
    # File lib/markaby/builder.rb, line 55
55:     def self.ignored_helpers
56:       @@ignored_helpers ||= []
57:     end
new (assigns = {}, helper = nil, &block)

Create a Markaby builder object. Pass in a hash of variable assignments to assigns which will be available as instance variables inside tag construction blocks. If an object is passed in to helper, its methods will be available from those same blocks.

Pass in a block to new and the block will be evaluated.

mab = Markaby::Builder.new {
  html do
    body do
      h1 "Matching Mole"
    end
  end
}
[show source]
     # File lib/markaby/builder.rb, line 80
 80:     def initialize(assigns = {}, helper = nil, &block)
 81:       @streams = [Stream.new]
 82:       @assigns = assigns.dup
 83:       @_helper = helper
 84:       @used_ids = {}
 85: 
 86:       @@options.each do |k, v|
 87:         instance_variable_set("@#{k}", @assigns.delete(k) || v)
 88:       end
 89: 
 90:       @assigns.each do |k, v|
 91:         instance_variable_set("@#{k}", v)
 92:       end
 93: 
 94:       if helper
 95:         helper.instance_variables.each do |iv|
 96:           instance_variable_set(iv, helper.instance_variable_get(iv))
 97:         end
 98:       end
 99: 
100:       @builder = XmlMarkup.new(:indent => @indent, :target => @streams.last)
101: 
102:       text(capture(&block)) if block
103:     end
restore_defaults! ()
[show source]
    # File lib/markaby/builder.rb, line 43
43:     def self.restore_defaults!
44:       @@options = DEFAULT_OPTIONS.dup
45:     end
set (option, value)
[show source]
    # File lib/markaby/builder.rb, line 47
47:     def self.set(option, value)
48:       @@options[option] = value
49:     end

Public instance methods

<< (string)

Alias for text

capture (&block)

Captures the HTML code built inside the block. This is done by creating a new stream for the builder object, running the block and passing back its stream as a string.

>> Markaby::Builder.new.capture { h1 "TEST"; h2 "CAPTURE ME" }
=> "<h1>TEST</h1><h2>CAPTURE ME</h2>"
[show source]
     # File lib/markaby/builder.rb, line 145
145:     def capture(&block)
146:       @streams.push(@builder.target = Stream.new)
147:       @builder.level += 1
148:       str = instance_eval(&block)
149:       str = @streams.last.join if @streams.last.any?
150:       @streams.pop
151:       @builder.level -= 1
152:       @builder.target = @streams.last
153:       str
154:     end
concat (string)

Alias for text

helper= (helper)
[show source]
     # File lib/markaby/builder.rb, line 105
105:     def helper=(helper)
106:       @_helper = helper
107:     end
locals= (locals)
[show source]
     # File lib/markaby/builder.rb, line 116
116:     def locals=(locals)
117:       locals.each do |key, value|
118:         metaclass do
119:           define_method key do
120:             value
121:           end
122:         end
123:       end
124:     end
tag! (tag, *args, &block)

Create a tag named tag. Other than the first argument which is the tag name, the arguments are the same as the tags implemented via method_missing.

[show source]
     # File lib/markaby/builder.rb, line 158
158:     def tag!(tag, *args, &block)
159:       ele_id = nil
160:       if @auto_validation && @tagset
161:         if !@tagset.tagset.has_key?(tag)
162:           raise InvalidXhtmlError, "no element `#{tag}' for #{tagset.doctype}"
163:         elsif args.last.respond_to?(:to_hash)
164:           attrs = args.last.to_hash
165: 
166:           if @tagset.forms.include?(tag) && attrs[:id]
167:             attrs[:name] ||= attrs[:id]
168:           end
169: 
170:           attrs.each do |k, v|
171:             atname = k.to_s.downcase.intern
172:             unless k =~ /:/ or @tagset.tagset[tag].include? atname
173:               raise InvalidXhtmlError, "no attribute `#{k}' on #{tag} elements"
174:             end
175:             if atname == :id
176:               ele_id = v.to_s
177:               if @used_ids.has_key? ele_id
178:                 raise InvalidXhtmlError, "id `#{ele_id}' already used (id's must be unique)."
179:               end
180:             end
181:           end
182:         end
183:       end
184: 
185:       if block
186:         str = capture(&block)
187:         block = proc { text(str) }
188:       end
189: 
190:       f = fragment { @builder.method_missing(tag, *args, &block) }
191:       @used_ids[ele_id] = f if ele_id
192:       f
193:     end
text (string)

Write a string to the HTML stream without escaping it.

[show source]
     # File lib/markaby/builder.rb, line 132
132:     def text(string)
133:       @builder << string.to_s
134:       nil
135:     end
to_s ()

Returns a string containing the HTML stream. Internally, the stream is stored as an Array.

[show source]
     # File lib/markaby/builder.rb, line 127
127:     def to_s
128:       @streams.last.to_s
129:     end