README 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. = Project: Builder
  2. == Goal
  3. Provide a simple way to create XML markup and data structures.
  4. == Classes
  5. Builder::XmlMarkup:: Generate XML markup notiation
  6. Builder::XmlEvents:: Generate XML events (i.e. SAX-like)
  7. <b>Notes</b>::
  8. * An <tt>Builder::XmlTree</tt> class to generate XML tree
  9. (i.e. DOM-like) structures is also planned, but not yet implemented.
  10. Also, the events builder is currently lagging the markup builder in
  11. features.
  12. == Usage
  13. require 'rubygems'
  14. require_gem 'builder', '~> 3.0'
  15. builder = Builder::XmlMarkup.new
  16. xml = builder.person { |b| b.name("Jim"); b.phone("555-1234") }
  17. xml #=> <person><name>Jim</name><phone>555-1234</phone></person>
  18. or
  19. require 'rubygems'
  20. require_gem 'builder'
  21. builder = Builder::XmlMarkup.new(:target=>STDOUT, :indent=>2)
  22. builder.person { |b| b.name("Jim"); b.phone("555-1234") }
  23. #
  24. # Prints:
  25. # <person>
  26. # <name>Jim</name>
  27. # <phone>555-1234</phone>
  28. # </person>
  29. == Compatibility
  30. === Version 3.0.0
  31. Version 3 adds Ruby 1.9.2 compatibility.
  32. === Version 2.0.0 Compatibility Changes
  33. Version 2.0.0 introduces automatically escaped attribute values for
  34. the first time. Versions prior to 2.0.0 did not insert escape
  35. characters into attribute values in the XML markup. This allowed
  36. attribute values to explicitly reference entities, which was
  37. occasionally used by a small number of developers. Since strings
  38. could always be explicitly escaped by hand, this was not a major
  39. restriction in functionality.
  40. However, it did suprise most users of builder. Since the body text is
  41. normally escaped, everybody expected the attribute values to be
  42. escaped as well. Escaped attribute values were the number one support
  43. request on the 1.x Builder series.
  44. Starting with Builder version 2.0.0, all attribute values expressed as
  45. strings will be processed and the appropriate characters will be
  46. escaped (e.g. "&" will be tranlated to "&amp;"). Attribute values
  47. that are expressed as Symbol values will not be processed for escaped
  48. characters and will be unchanged in output. (Yes, this probably counts
  49. as Symbol abuse, but the convention is convenient and flexible).
  50. Example:
  51. xml = Builder::XmlMarkup.new
  52. xml.sample(:escaped=>"This&That", :unescaped=>:"Here&amp;There")
  53. xml.target! =>
  54. <sample escaped="This&amp;That" unescaped="Here&amp;There"/>
  55. === Version 1.0.0 Compatibility Changes
  56. Version 1.0.0 introduces some changes that are not backwards
  57. compatible with earlier releases of builder. The main areas of
  58. incompatibility are:
  59. * Keyword based arguments to +new+ (rather than positional based). It
  60. was found that a developer would often like to specify indentation
  61. without providing an explicit target, or specify a target without
  62. indentation. Keyword based arguments handle this situation nicely.
  63. * Builder must now be an explicit target for markup tags. Instead of
  64. writing
  65. xml_markup = Builder::XmlMarkup.new
  66. xml_markup.div { strong("text") }
  67. you need to write
  68. xml_markup = Builder::XmlMarkup.new
  69. xml_markup.div { xml_markup.strong("text") }
  70. * The builder object is passed as a parameter to all nested markup
  71. blocks. This allows you to create a short alias for the builder
  72. object that can be used within the block. For example, the previous
  73. example can be written as:
  74. xml_markup = Builder::XmlMarkup.new
  75. xml_markup.div { |xml| xml.strong("text") }
  76. * If you have both a pre-1.0 and a post-1.0 gem of builder installed,
  77. you can choose which version to use through the RubyGems
  78. +require_gem+ facility.
  79. require_gem 'builder', "~> 0.0" # Gets the old version
  80. require_gem 'builder', "~> 1.0" # Gets the new version
  81. == Features
  82. * XML Comments are supported ...
  83. xml_markup.comment! "This is a comment"
  84. #=> <!-- This is a comment -->
  85. * XML processing instructions are supported ...
  86. xml_markup.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8"
  87. #=> <?xml version="1.0" encoding="UTF-8"?>
  88. If the processing instruction is omitted, it defaults to "xml".
  89. When the processing instruction is "xml", the defaults attributes
  90. are:
  91. <b>version</b>:: 1.0
  92. <b>encoding</b>:: "UTF-8"
  93. (NOTE: if the encoding is set to "UTF-8" and $KCODE is set to
  94. "UTF8", then Builder will emit UTF-8 encoded strings rather than
  95. encoding non-ASCII characters as entities.)
  96. * XML entity declarations are now supported to a small degree.
  97. xml_markup.declare! :DOCTYPE, :chapter, :SYSTEM, "../dtds/chapter.dtd"
  98. #=> <!DOCTYPE chapter SYSTEM "../dtds/chapter.dtd">
  99. The parameters to a declare! method must be either symbols or
  100. strings. Symbols are inserted without quotes, and strings are
  101. inserted with double quotes. Attribute-like arguments in hashes are
  102. not allowed.
  103. If you need to have an argument to declare! be inserted without
  104. quotes, but the arguement does not conform to the typical Ruby
  105. syntax for symbols, then use the :"string" form to specify a symbol.
  106. For example:
  107. xml_markup.declare! :ELEMENT, :chapter, :"(title,para+)"
  108. #=> <!ELEMENT chapter (title,para+)>
  109. Nested entity declarations are allowed. For example:
  110. @xml_markup.declare! :DOCTYPE, :chapter do |x|
  111. x.declare! :ELEMENT, :chapter, :"(title,para+)"
  112. x.declare! :ELEMENT, :title, :"(#PCDATA)"
  113. x.declare! :ELEMENT, :para, :"(#PCDATA)"
  114. end
  115. #=>
  116. <!DOCTYPE chapter [
  117. <!ELEMENT chapter (title,para+)>
  118. <!ELEMENT title (#PCDATA)>
  119. <!ELEMENT para (#PCDATA)>
  120. ]>
  121. * Some support for XML namespaces is now available. If the first
  122. argument to a tag call is a symbol, it will be joined to the tag to
  123. produce a namespace:tag combination. It is easier to show this than
  124. describe it.
  125. xml.SOAP :Envelope do ... end
  126. Just put a space before the colon in a namespace to produce the
  127. right form for builder (e.g. "<tt>SOAP:Envelope</tt>" =>
  128. "<tt>xml.SOAP :Envelope</tt>")
  129. * String attribute values are <em>now</em> escaped by default by
  130. Builder (<b>NOTE:</b> this is _new_ behavior as of version 2.0).
  131. However, occasionally you need to use entities in attribute values.
  132. Using a symbols (rather than a string) for an attribute value will
  133. cause Builder to not run its quoting/escaping algorithm on that
  134. particular value.
  135. (<b>Note:</b> The +escape_attrs+ option for builder is now
  136. obsolete).
  137. Example:
  138. xml = Builder::XmlMarkup.new
  139. xml.sample(:escaped=>"This&That", :unescaped=>:"Here&amp;There")
  140. xml.target! =>
  141. <sample escaped="This&amp;That" unescaped="Here&amp;There"/>
  142. * UTF-8 Support
  143. Builder correctly translates UTF-8 characters into valid XML. (New
  144. in version 2.0.0). Thanks to Sam Ruby for the translation code.
  145. Example:
  146. xml = Builder::Markup.new
  147. xml.sample("Iñtërnâtiônàl")
  148. xml.target! =>
  149. "<sample>I&#241;t&#235;rn&#226;ti&#244;n&#224;l</sample>"
  150. You can get UTF-8 encoded output by making sure that the XML
  151. encoding is set to "UTF-8" and that the $KCODE variable is set to
  152. "UTF8".
  153. $KCODE = 'UTF8'
  154. xml = Builder::Markup.new
  155. xml.instruct!(:xml, :encoding => "UTF-8")
  156. xml.sample("Iñtërnâtiônàl")
  157. xml.target! =>
  158. "<sample>Iñtërnâtiônàl</sample>"
  159. == Contact
  160. Author:: Jim Weirich
  161. Email:: jim@weirichhouse.org
  162. Home Page:: http://onestepback.org
  163. License:: MIT Licence (http://www.opensource.org/licenses/mit-license.html)