Builder::XmlMarkup

Create XML markup easily. All (well, almost all) methods sent to an XmlMarkup object will be translated to the equivalent XML markup. Any method with a block will be treated as an XML markup tag with nested markup in the block.

Examples will demonstrate this easier than words. In the following, xm is an XmlMarkup object.

  xm.em("emphasized")            # => <em>emphasized</em>
  xm.em { xm.b("emp & bold") }   # => <em><b>emph &amp; bold</b></em>
  xm.a("A Link", "href"=>"http://onestepback.org")
                                 # => <a href="http://onestepback.org">A Link</a>
  xm.div { xm.br }               # => <div><br/></div>
  xm.target("name"=>"compile", "option"=>"fast")
                                 # => <target option="fast" name="compile"\>
                                 # NOTE: order of attributes is not specified.

  xm.instruct!                   # <?xml version="1.0" encoding="UTF-8"?>
  xm.html {                      # <html>
    xm.head {                    #   <head>
      xm.title("History")        #     <title>History</title>
    }                            #   </head>
    xm.body {                    #   <body>
      xm.h1("Header")            #     <h1>Header</h1>
      xm.p("paragraph")          #     <p>paragraph</p>
    }                            #   </body>
  }                              # </html>

Notes:

 
    xm = Builder.new(:indent=>2, :margin=>4)
    # xm will produce nicely formatted and indented XML with 2
    # spaces per indent and an over all indentation level of 4.

    builder = Builder::XmlMarkup.new(:target=>$stdout, :indent=>2)
    builder.name { |b| b.first("Jim"); b.last("Weirich) }
    # prints:
    #     <name>
    #       <first>Jim</first>
    #       <last>Weirich</last>
    #     </name>

Public Class Methods

new(options={}) click to toggle source

Create an XML markup builder. Parameters are specified by an option hash.

:target=>target_object

Object receiving the markup. target_object must respond to the <<(a_string) operator and return itself. The default target is a plain string target.

:indent=>indentation

Number of spaces used for indentation. The default is no indentation and no line breaks.

:margin=>initial_indentation_level

Amount of initial indentation (specified in levels, not spaces).

:escape_attrs=>OBSOLETE

The :escape_attrs option is no longer supported by builder (and will be quietly ignored). String attribute values are now automatically escaped. If you need unescaped attribute values (perhaps you are using entities in the attribute values), then give the value as a Symbol. This allows much finer control over escaping attribute values.

     # File lib/builder/xmlmarkup.rb, line 185
185:     def initialize(options={})
186:       indent = options[:indent] || 0
187:       margin = options[:margin] || 0
188:       super(indent, margin)
189:       @target = options[:target] || ""
190:     end

Public Instance Methods

cdata!(text) click to toggle source

Insert a CDATA section into the XML markup.

For example:

   xml.cdata!("text to be included in cdata")
       #=> <![CDATA[text to be included in cdata]]>
     # File lib/builder/xmlmarkup.rb, line 263
263:     def cdata!(text)
264:       _ensure_no_block ::Kernel::block_given?
265:       _special("<![CDATA[", "]]>", text, nil)
266:     end
comment!(comment_text) click to toggle source
     # File lib/builder/xmlmarkup.rb, line 197
197:     def comment!(comment_text)
198:       _ensure_no_block ::Kernel::block_given?
199:       _special("<!-- ", " -->", comment_text, nil)
200:     end
declare!(inst, *args, &block) click to toggle source

Insert an XML declaration into the XML markup.

For example:

  xml.declare! :ELEMENT, :blah, "yada"
      # => <!ELEMENT blah "yada">
     # File lib/builder/xmlmarkup.rb, line 208
208:     def declare!(inst, *args, &block)
209:       _indent
210:       @target << "<!#{inst}"
211:       args.each do |arg|
212:         case arg
213:         when ::String
214:           @target << %{ "#{arg}"} # " WART
215:         when ::Symbol
216:           @target << " #{arg}"
217:         end
218:       end
219:       if ::Kernel::block_given?
220:         @target << " ["
221:         _newline
222:         _nested_structures(block)
223:         @target << "]"
224:       end
225:       @target << ">"
226:       _newline
227:     end
instruct!(directive_tag=:xml, attrs={}) click to toggle source

Insert a processing instruction into the XML markup. E.g.

For example:

   xml.instruct!
       #=> <?xml version="1.0" encoding="UTF-8"?>
   xml.instruct! :aaa, :bbb=>"ccc"
       #=> <?aaa bbb="ccc"?>

Note: If the encoding is setup to “UTF-8” and the value of $KCODE is “UTF8”, then builder will emit UTF-8 encoded strings rather than the entity encoding normally used.

     # File lib/builder/xmlmarkup.rb, line 241
241:     def instruct!(directive_tag=:xml, attrs={})
242:       _ensure_no_block ::Kernel::block_given?
243:       if directive_tag == :xml
244:         a = { :version=>"1.0", :encoding=>"UTF-8" }
245:         attrs = a.merge attrs
246:         @encoding = attrs[:encoding].downcase
247:       end
248:       _special(
249:         "<?#{directive_tag}",
250:         "?>",
251:         nil,
252:         attrs,
253:         [:version, :encoding, :standalone])
254:     end
target!() click to toggle source

Return the target of the builder.

     # File lib/builder/xmlmarkup.rb, line 193
193:     def target!
194:       @target
195:     end

Private Instance Methods

_attr_value(value) click to toggle source
     # File lib/builder/xmlmarkup.rb, line 314
314:     def _attr_value(value)
315:       case value
316:       when ::Symbol
317:         value.to_s
318:       else
319:         _escape_quote(value.to_s)
320:       end
321:     end
_end_tag(sym) click to toggle source

Insert an ending tag.

     # File lib/builder/xmlmarkup.rb, line 298
298:     def _end_tag(sym)
299:       @target << "</#{sym}>"
300:     end
_ensure_no_block(got_block) click to toggle source
     # File lib/builder/xmlmarkup.rb, line 323
323:     def _ensure_no_block(got_block)
324:       if got_block
325:         ::Kernel::raise IllegalBlockError.new(
326:           "Blocks are not allowed on XML instructions"
327:         )
328:       end
329:     end
_insert_attributes(attrs, order=[]) click to toggle source

Insert the attributes (given in the hash).

     # File lib/builder/xmlmarkup.rb, line 303
303:     def _insert_attributes(attrs, order=[])
304:       return if attrs.nil?
305:       order.each do |k|
306:         v = attrs[k]
307:         @target << %{ #{k}="#{_attr_value(v)}"} if v # " WART
308:       end
309:       attrs.each do |k, v|
310:         @target << %{ #{k}="#{_attr_value(v)}"} unless order.member?(k) # " WART
311:       end
312:     end
_special(open, close, data=nil, attrs=nil, order=[]) click to toggle source

Insert special instruction.

     # File lib/builder/xmlmarkup.rb, line 279
279:     def _special(open, close, data=nil, attrs=nil, order=[])
280:       _indent
281:       @target << open
282:       @target << data if data
283:       _insert_attributes(attrs, order) if attrs
284:       @target << close
285:       _newline
286:     end
_start_tag(sym, attrs, end_too=false) click to toggle source

Start an XML tag. If end_too is true, then the start tag is also the end tag (e.g.

     # File lib/builder/xmlmarkup.rb, line 290
290:     def _start_tag(sym, attrs, end_too=false)
291:       @target << "<#{sym}"
292:       _insert_attributes(attrs)
293:       @target << "/" if end_too
294:       @target << ">"
295:     end
_text(text) click to toggle source

Insert text directly in to the builder’s target.

     # File lib/builder/xmlmarkup.rb, line 274
274:     def _text(text)
275:       @target << text
276:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.