Parent

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 { xmm.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 { 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. out must respond to the << operator. The default 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=><b>OBSOLETE</em>

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
def initialize(options={})
  indent = options[:indent] || 0
  margin = options[:margin] || 0
  super(indent, margin)
  @target = options[:target] || ""
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 259
def cdata!(text)
  _ensure_no_block block_given?
  _special("<![CDATA[", "]]>", text, nil)
end
comment!(comment_text) click to toggle source
# File lib/builder/xmlmarkup.rb, line 197
def comment!(comment_text)
  _ensure_no_block block_given?
  _special("<!-- ", " -->", comment_text, nil)
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
def declare!(inst, *args, &block)
  _indent
  @target << "<!#{inst}"
  args.each do |arg|
    case arg
    when String
      @target << %{ "#{arg}"} # " WART
    when Symbol
      @target << " #{arg}"
    end
  end
  if block_given?
    @target << " ["
    _newline
    _nested_structures(block)
    @target << "]"
  end
  @target << ">"
  _newline
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"?>
# File lib/builder/xmlmarkup.rb, line 238
def instruct!(directive_tag=:xml, attrs={})
  _ensure_no_block block_given?
  if directive_tag == :xml
    a = { :version=>"1.0", :encoding=>"UTF-8" }
    attrs = a.merge attrs
  end
  _special(
  "<?#{directive_tag}",
  "?>",
  nil,
  attrs,
  [:version, :encoding, :standalone])
end
target!() click to toggle source

Return the target of the builder.

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

[Validate]

Generated with the Darkfish Rdoc Generator 2.