README.rdoc 7.4 KB

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