class 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.comment! "HI"           #     <!-- HI -->
    xm.h1("Header")            #     <h1>Header</h1>
    xm.p("paragraph")          #     <p>paragraph</p>
  }                            #   </body>
}                              # </html>

Notes:

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).

:quote => :single

Use single quotes for attributes rather than double quotes.

: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.

Calls superclass method Builder::XmlBase::new
    # File lib/builder/xmlmarkup.rb
189 def initialize(options={})
190   indent = options[:indent] || 0
191   margin = options[:margin] || 0
192   @quote = (options[:quote] == :single) ? "'" : '"'
193   @explicit_nil_handling = options[:explicit_nil_handling]
194   super(indent, margin)
195   @target = options[:target] || ""
196 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
269 def cdata!(text)
270   _ensure_no_block ::Kernel::block_given?
271   _special("<![CDATA[", "]]>", text.gsub(']]>', ']]]]><![CDATA[>'), nil)
272 end
comment!(comment_text) click to toggle source
    # File lib/builder/xmlmarkup.rb
203 def comment!(comment_text)
204   _ensure_no_block ::Kernel::block_given?
205   _special("<!-- ", " -->", comment_text, nil)
206 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
214 def declare!(inst, *args, &block)
215   _indent
216   @target << "<!#{inst}"
217   args.each do |arg|
218     case arg
219     when ::String
220       @target << %{ "#{arg}"} # " WART
221     when ::Symbol
222       @target << " #{arg}"
223     end
224   end
225   if ::Kernel::block_given?
226     @target << " ["
227     _newline
228     _nested_structures(block)
229     @target << "]"
230   end
231   @target << ">"
232   _newline
233 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
247 def instruct!(directive_tag=:xml, attrs={})
248   _ensure_no_block ::Kernel::block_given?
249   if directive_tag == :xml
250     a = { :version=>"1.0", :encoding=>"UTF-8" }
251     attrs = a.merge attrs
252     @encoding = attrs[:encoding].downcase
253   end
254   _special(
255     "<?#{directive_tag}",
256     "?>",
257     nil,
258     attrs,
259     [:version, :encoding, :standalone])
260 end
target!() click to toggle source

Return the target of the builder.

    # File lib/builder/xmlmarkup.rb
199 def target!
200   @target
201 end

Private Instance Methods

_attr_value(value) click to toggle source
    # File lib/builder/xmlmarkup.rb
320 def _attr_value(value)
321   case value
322   when ::Symbol
323     value.to_s
324   else
325     _escape_attribute(value.to_s)
326   end
327 end
_end_tag(sym) click to toggle source

Insert an ending tag.

    # File lib/builder/xmlmarkup.rb
304 def _end_tag(sym)
305   @target << "</#{sym}>"
306 end
_ensure_no_block(got_block) click to toggle source
    # File lib/builder/xmlmarkup.rb
329 def _ensure_no_block(got_block)
330   if got_block
331     ::Kernel::raise IllegalBlockError.new(
332       "Blocks are not allowed on XML instructions"
333     )
334   end
335 end
_insert_attributes(attrs, order=[]) click to toggle source

Insert the attributes (given in the hash).

    # File lib/builder/xmlmarkup.rb
309 def _insert_attributes(attrs, order=[])
310   return if attrs.nil?
311   order.each do |k|
312     v = attrs[k]
313     @target << %{ #{k}=#{@quote}#{_attr_value(v)}#{@quote}} if v
314   end
315   attrs.each do |k, v|
316     @target << %{ #{k}=#{@quote}#{_attr_value(v)}#{@quote}} unless order.member?(k) # " WART
317   end
318 end
_special(open, close, data=nil, attrs=nil, order=[]) click to toggle source

Insert special instruction.

    # File lib/builder/xmlmarkup.rb
285 def _special(open, close, data=nil, attrs=nil, order=[])
286   _indent
287   @target << open
288   @target << data if data
289   _insert_attributes(attrs, order) if attrs
290   @target << close
291   _newline
292 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. <br/>

    # File lib/builder/xmlmarkup.rb
296 def _start_tag(sym, attrs, end_too=false)
297   @target << "<#{sym}"
298   _insert_attributes(attrs)
299   @target << "/" if end_too
300   @target << ">"
301 end
_text(text) click to toggle source

Insert text directly in to the builder's target.

    # File lib/builder/xmlmarkup.rb
280 def _text(text)
281   @target << text
282 end