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

Return the target of the builder.

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

Private Instance Methods

_attr_value(value) click to toggle source
    # File lib/builder/xmlmarkup.rb
326 def _attr_value(value)
327   case value
328   when ::Symbol
329     value.to_s
330   else
331     _escape_attribute(value.to_s)
332   end
333 end
_end_tag(sym) click to toggle source

Insert an ending tag.

    # File lib/builder/xmlmarkup.rb
310 def _end_tag(sym)
311   @target << "</#{sym}>"
312 end
_ensure_no_block(got_block) click to toggle source
    # File lib/builder/xmlmarkup.rb
335 def _ensure_no_block(got_block)
336   if got_block
337     ::Kernel::raise IllegalBlockError.new(
338       "Blocks are not allowed on XML instructions"
339     )
340   end
341 end
_insert_attributes(attrs, order=[]) click to toggle source

Insert the attributes (given in the hash).

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

Insert special instruction.

    # File lib/builder/xmlmarkup.rb
291 def _special(open, close, data=nil, attrs=nil, order=[])
292   _indent
293   @target << open
294   @target << data if data
295   _insert_attributes(attrs, order) if attrs
296   @target << close
297   _newline
298 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
302 def _start_tag(sym, attrs, end_too=false)
303   @target << "<#{sym}"
304   _insert_attributes(attrs)
305   @target << "/" if end_too
306   @target << ">"
307 end
_text(text) click to toggle source

Insert text directly in to the builder's target.

    # File lib/builder/xmlmarkup.rb
286 def _text(text)
287   @target << text
288 end