class Builder::XmlMarkup
}
xml.stong(“text”)
xml_builder.div { |xml|
xml_builder = Builder::XmlMarkup.new
For example:
within the block.
object sent as an argument, allowing you to use a shorter alias
little less cumbersome, the markup block now gets the markup
block was found to be prone to error. To make this change a
Although more verbose, the subtle change in semantics within the
xml.div { xml.strong(“text”) }
you need to write:
xml.div { strong(“text”) }
For instance, instead of
markup methods must now be explicitly send to the xml builder.
block calls to execute the markup block. This means that all
the message receiver as self is now obsolete. We now use normal
* The instance_eval implementation which forces self to refer to
# </name>
# <last>Weirich</last>
# <first>Jim</first>
# <name>
# prints:
builder.name { |b| b.first(“Jim”); b.last(“Weirich) }
builder = Builder::XmlMarkup.new(:target=>$stdout, :indent=>2)
# spaces per indent and an over all indentation level of 4.
# xm will produce nicely formatted and indented XML with 2
xm = Builder.new(:indent=>2, :margin=>4)
# xm will produce nicely formatted and indented XML.
xm = Builder.new(:indent=>2)
Example:
Initial indentation may be specified using a third parameter.
indent for each level as a second argument to XmlBuilder.new.
* Indentation is enabled by providing the number of spaces to
# Markup written to x2
will be send to xm
.
x2 = Builder::XmlMarkup.new(:target=>xm)
xm = Builder::XmlMarkup.new
# The markup is written to STDOUT (using <<)
xm = Builder::XmlMarkup.new(STDOUT)
# The markup is appended to buffer (using <<)
xm = Builder::XmlMarkup.new(buffer)
buffer = ”“
# result is a string containing the markup.
result = xm.title(”yada“)
xm = Builder::XmlMarkup.new
Examples:
then XmlMarkup defaults to a string target.
that accepts the <<
method. If no target is given,
* XmlMarkup builds the markup in any object (called a target)
”xml.SOAP :Envelope
“)
right form for builder (e.g. ”SOAP:Envelope
“ =>
Just put a space before the colon in a namespace to produce the
xml.SOAP :Envelope do … end
show this than describe it.
the tag to produce a namespace:tag combination. It is easier to
first argument to a tag call is a symbol, it will be joined to
* Direct support for XML namespaces is now available. If the
bullet item for a better way to handle XML namespaces).
the tag name) like normal markup methods. (But see the nexttag!
will also take text and attribute arguments (after
<SOAP:Envelope> … </SOAP:Envelope>”
will produce …
xml.tag!(“SOAP:Envelope”) { … }
Example:
cases.
identifiers. Use the tag!
method to handle these
* Sometimes tags use special characters not allowed in ruby
insert text without modification.
> and & automatically. Use the <<
operation to
* The special XML characters <, >, and & are converted to <,
} # </div>
xm.text! “another line”; xmbr # another line<br/>
xm.text! “line”; xm.br # line<br/>
xm.div { # <div>
Example:
the text!
method to accomplish this.
* Sometimes you wish to insert text without enclosing tags. Use
undefined.
* The order that attributes are inserted in markup tags is
== Notes:
} # </html>
} # </body>
xm.p(“paragraph”) # <p>paragraph</p>
xm.h1(“Header”) # <h1>Header</h1>
xm.comment! “HI” # <!– HI –>
xm.body { # <body>
} # </head>
xm.title(“History”) # <title>History</title>
xm.head { # <head>
xm.html { # <html>
xm.instruct! # <?xml version=“1.0” encoding=“UTF-8”?>
# NOTE: order of attributes is not specified.
# => <target option=“fast” name=“compile”>
xm.target(“name”=>“compile”, “option”=>“fast”)
xm.div { xm.br } # => <div><br/></div>
# => <a href=“onestepback.org”>A Link</a>
xm.a(“A Link”, “href”=>“onestepback.org”)
xm.em { xm.b(“emp & bold”) } # => emph & bold
xm.em(“emphasized”) # => emphasized
following, xm
is an XmlMarkup
object.
Examples will demonstrate this easier than words. In the
tag with nested markup in the block.
markup. Any method with a block will be treated as an XML markup
an XmlMarkup object will be translated to the equivalent XML
Create XML markup easily. All (well, almost all) methods sent to
def _attr_value(value)
def _attr_value(value) case value when ::Symbol value.to_s else _escape_attribute(value.to_s) end end
def _end_tag(sym)
def _end_tag(sym) @target << "</#{sym}>" end
def _ensure_no_block(got_block)
def _ensure_no_block(got_block) if got_block ::Kernel::raise IllegalBlockError.new( "Blocks are not allowed on XML instructions" ) end end
def _insert_attributes(attrs, order=[])
def _insert_attributes(attrs, order=[]) return if attrs.nil? order.each do |k| v = attrs[k] @target << %{ #{k}="#{_attr_value(v)}"} if v # " WART end attrs.each do |k, v| @target << %{ #{k}="#{_attr_value(v)}"} unless order.member?(k) # " WART end end
def _special(open, close, data=nil, attrs=nil, order=[])
def _special(open, close, data=nil, attrs=nil, order=[]) _indent @target << open @target << data if data _insert_attributes(attrs, order) if attrs @target << close _newline end
def _start_tag(sym, attrs, end_too=false)
Start an XML tag. If end_too is true, then the start
def _start_tag(sym, attrs, end_too=false) @target << "<#{sym}" _insert_attributes(attrs) @target << "/" if end_too @target << ">" end
def _text(text)
def _text(text) @target << text end
def cdata!(text)
#=>
xml.cdata!("text to be included in cdata")
For example:
Insert a CDATA section into the XML markup.
def cdata!(text) _ensure_no_block ::Kernel::block_given? _special("<![CDATA[", "]]>", text.gsub(']]>', ']]]]><![CDATA[>'), nil) end
def comment!(comment_text)
def comment!(comment_text) _ensure_no_block ::Kernel::block_given? _special("<!-- ", " -->", comment_text, nil) end
def declare!(inst, *args, &block)
xml.declare! :ELEMENT, :blah, "yada"
For example:
Insert an XML declaration into the XML markup.
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 ::Kernel::block_given? @target << " [" _newline _nested_structures(block) @target << "]" end @target << ">" _newline end
def initialize(options={})
finer control over escaping attribute values.
values), then give the value as a Symbol. This allows much
values (perhaps you are using entities in the attribute
now automatically escaped. If you need unescaped attribute
(and will be quietly ignored). String attribute values are
The :escape_attrs option is no longer supported by builder
:escape_attrs=>OBSOLETE::
spaces).
Amount of initial indentation (specified in levels, not
:margin=>initial_indentation_level::
indentation and no line breaks.
Number of spaces used for indentation. The default is no
:indent=>indentation::
itself. The default target is a plain string target.
the <<(a_string) operator and return
Object receiving the markup. +target_object+ must respond to
:target=>target_object::
option hash.
Create an XML markup builder. Parameters are specified by an
def initialize(options={}) indent = options[:indent] || 0 margin = options[:margin] || 0 super(indent, margin) @target = options[:target] || "" end
def instruct!(directive_tag=:xml, attrs={})
$KCODE is "UTF8", then builder will emit UTF-8 encoded strings
Note: If the encoding is setup to "UTF-8" and the value of
#=>
xml.instruct! :aaa, :bbb=>"ccc"
#=>
xml.instruct!
For example:
Insert a processing instruction into the XML markup. E.g.
def instruct!(directive_tag=:xml, attrs={}) _ensure_no_block ::Kernel::block_given? if directive_tag == :xml a = { :version=>"1.0", :encoding=>"UTF-8" } attrs = a.merge attrs coding = attrs[:encoding].downcase end _special( "<?#{directive_tag}", "?>", nil, attrs, [:version, :encoding, :standalone]) end
def target!
def target! @target end