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
# File lib/markaby/builder.rb, line 51 51: def self.get(option) 52: @@options[option] 53: end
# File lib/markaby/builder.rb, line 59 59: def self.ignore_helpers(*helpers) 60: ignored_helpers.concat helpers 61: end
# File lib/markaby/builder.rb, line 55 55: def self.ignored_helpers 56: @@ignored_helpers ||= [] 57: end
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 }
# 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
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>"
# 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
# File lib/markaby/builder.rb, line 105 105: def helper=(helper) 106: @_helper = helper 107: end
# 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
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.
# 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: if AttrsBoolean.include? atname 182: if v 183: attrs[k] = atname.to_s 184: else 185: attrs.delete k 186: end 187: end 188: end 189: end 190: end 191: 192: if block 193: str = capture(&block) 194: block = proc { text(str) } 195: end 196: 197: f = fragment { @builder.tag!(tag, *args, &block) } 198: @used_ids[ele_id] = f if ele_id 199: f 200: end
# File lib/markaby/builder.rb, line 262 262: def fragment 263: stream = @streams.last 264: start = stream.length 265: yield 266: length = stream.length - start 267: Fragment.new(stream, start, length) 268: end
# File lib/markaby/builder.rb, line 257 257: def instance_methods_for(obj) 258: obj.instance_methods.map { |m| m.to_sym } 259: end
# File lib/markaby/builder.rb, line 249 249: def instance_methods_for(obj) 250: obj.instance_methods 251: end
# File lib/markaby/builder.rb, line 253 253: def instance_variables_for(obj) 254: obj.instance_variables.map { |var| var.to_sym } 255: end
# File lib/markaby/builder.rb, line 245 245: def instance_variables_for(obj) 246: obj.instance_variables 247: end
# File lib/markaby/builder.rb, line 109 109: def metaclass(&block) 110: metaclass = class << self; self; end 111: metaclass.class_eval(&block) 112: end
This method is used to intercept calls to helper methods and instance variables. Here is the order of interception:
If sym is a helper method, the helper method is called and output to the stream.
If sym is a Builder::XmlMarkup method, it is passed on to the builder object.
If sym is also the name of an instance variable, the value of the instance variable is returned.
If sym has come this far and no tagset is found, sym and its arguments are passed to tag!
If a tagset is found, though, NoMethodError is raised.
method_missing used to be the lynchpin in Markaby, but it’s no longer used to handle HTML tags. See html_tag for that.
# File lib/markaby/builder.rb, line 217 217: def method_missing(sym, *args, &block) 218: if @_helper.respond_to?(sym, true) && !self.class.ignored_helpers.include?(sym) 219: r = @_helper.send(sym, *args, &block) 220: if @output_helpers && r.respond_to?(:to_str) 221: fragment { @builder << r } 222: else 223: r 224: end 225: elsif @assigns.has_key?(sym) 226: @assigns[sym] 227: elsif @assigns.has_key?(stringy_key = sym.to_s) 228: # Rails' ActionView assigns hash has string keys for 229: # instance variables that are defined in the controller. 230: @assigns[stringy_key] 231: elsif instance_variables_for(self).include?(ivar = "@#{sym}".to_sym) 232: instance_variable_get(ivar) 233: elsif @_helper && instance_variables_for(@_helper).include?(ivar) 234: @_helper.instance_variable_get(ivar) 235: elsif instance_methods_for(::Builder::XmlMarkup).include?(sym) 236: @builder.__send__(sym, *args, &block) 237: elsif !@tagset 238: tag!(sym, *args, &block) 239: else 240: super 241: end 242: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.