• Stars
    star
    362
  • Rank 117,671 (Top 3 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 16 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Provide a simple way to create XML markup and data structures.

Project: Builder

Goal

Provide a simple way to create XML markup and data structures.

Classes

Builder::XmlMarkup:: Generate XML markup notation Builder::XmlEvents:: Generate XML events (i.e. SAX-like)

Notes:

  • An Builder::XmlTree class to generate XML tree (i.e. DOM-like) structures is also planned, but not yet implemented. Also, the events builder is currently lagging the markup builder in features.

Usage

  require 'rubygems'
  require_gem 'builder', '~> 2.0'

  builder = Builder::XmlMarkup.new
  xml = builder.person { |b| b.name("Jim"); b.phone("555-1234") }
  xml #=> <person><name>Jim</name><phone>555-1234</phone></person>

or

  require 'rubygems'
  require_gem 'builder'

  builder = Builder::XmlMarkup.new(:target=>STDOUT, :indent=>2)
  builder.person { |b| b.name("Jim"); b.phone("555-1234") }
  #
  # Prints:
  # <person>
  #   <name>Jim</name>
  #   <phone>555-1234</phone>
  # </person>

Compatibility

Version 2.0.0 Compatibility Changes

Version 2.0.0 introduces automatically escaped attribute values for the first time. Versions prior to 2.0.0 did not insert escape characters into attribute values in the XML markup. This allowed attribute values to explicitly reference entities, which was occasionally used by a small number of developers. Since strings could always be explicitly escaped by hand, this was not a major restriction in functionality.

However, it did surprise most users of builder. Since the body text is normally escaped, everybody expected the attribute values to be escaped as well. Escaped attribute values were the number one support request on the 1.x Builder series.

Starting with Builder version 2.0.0, all attribute values expressed as strings will be processed and the appropriate characters will be escaped (e.g. "&" will be translated to "&amp;"). Attribute values that are expressed as Symbol values will not be processed for escaped characters and will be unchanged in output. (Yes, this probably counts as Symbol abuse, but the convention is convenient and flexible).

Example:

  xml = Builder::XmlMarkup.new
  xml.sample(:escaped=>"This&That", :unescaped=>:"Here&amp;There")
  xml.target!  =>
    <sample escaped="This&amp;That" unescaped="Here&amp;There"/>

Version 1.0.0 Compatibility Changes

Version 1.0.0 introduces some changes that are not backwards compatible with earlier releases of builder. The main areas of incompatibility are:

  • Keyword based arguments to +new+ (rather than positional based). It was found that a developer would often like to specify indentation without providing an explicit target, or specify a target without indentation. Keyword based arguments handle this situation nicely.

  • Builder must now be an explicit target for markup tags. Instead of writing

    xml_markup = Builder::XmlMarkup.new
    xml_markup.div { strong("text") }

you need to write

    xml_markup = Builder::XmlMarkup.new
    xml_markup.div { xml_markup.strong("text") }
  • The builder object is passed as a parameter to all nested markup blocks. This allows you to create a short alias for the builder object that can be used within the block. For example, the previous example can be written as:
    xml_markup = Builder::XmlMarkup.new
    xml_markup.div { |xml| xml.strong("text") }
  • If you have both a pre-1.0 and a post-1.0 gem of builder installed, you can choose which version to use through the RubyGems +require_gem+ facility.
    require_gem 'builder', "~> 0.0"   # Gets the old version
    require_gem 'builder', "~> 1.0"   # Gets the new version

Features

  • XML Comments are supported ...
    xml_markup.comment! "This is a comment"
      #=>  <!-- This is a comment -->
  • XML processing instructions are supported ...
    xml_markup.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8"
      #=>  <?xml version="1.0" encoding="UTF-8"?>

If the processing instruction is omitted, it defaults to "xml". When the processing instruction is "xml", the defaults attributes are:

version: 1.0 encoding: "UTF-8"

(NOTE: if the encoding is set to "UTF-8" and $KCODE is set to "UTF8", then Builder will emit UTF-8 encoded strings rather than encoding non-ASCII characters as entities.)

  • XML entity declarations are now supported to a small degree.
    xml_markup.declare! :DOCTYPE, :chapter, :SYSTEM, "../dtds/chapter.dtd"
      #=>  <!DOCTYPE chapter SYSTEM "../dtds/chapter.dtd">

The parameters to a declare! method must be either symbols or strings. Symbols are inserted without quotes, and strings are inserted with double quotes. Attribute-like arguments in hashes are not allowed.

If you need to have an argument to declare! be inserted without quotes, but the argument does not conform to the typical Ruby syntax for symbols, then use the :"string" form to specify a symbol.

For example:

    xml_markup.declare! :ELEMENT, :chapter, :"(title,para+)"
      #=>  <!ELEMENT chapter (title,para+)>

Nested entity declarations are allowed. For example:

    @xml_markup.declare! :DOCTYPE, :chapter do |x|
      x.declare! :ELEMENT, :chapter, :"(title,para+)"
      x.declare! :ELEMENT, :title, :"(#PCDATA)"
      x.declare! :ELEMENT, :para, :"(#PCDATA)"
    end

    #=>

    <!DOCTYPE chapter [
      <!ELEMENT chapter (title,para+)>
      <!ELEMENT title (#PCDATA)>
      <!ELEMENT para (#PCDATA)>
    ]>
  • Some support for XML namespaces is now available. If the first argument to a tag call is a symbol, it will be joined to the tag to produce a namespace:tag combination. It is easier to show this than describe it.
   xml.SOAP :Envelope do ... end

Just put a space before the colon in a namespace to produce the right form for builder (e.g. "SOAP:Envelope" => "xml.SOAP :Envelope")

  • String attribute values are now escaped by default by Builder (NOTE: this is new behavior as of version 2.0).

    However, occasionally you need to use entities in attribute values. Using a symbol (rather than a string) for an attribute value will cause Builder to not run its quoting/escaping algorithm on that particular value.

    (Note: The +escape_attrs+ option for builder is now obsolete).

    Example:

    xml = Builder::XmlMarkup.new
    xml.sample(:escaped=>"This&That", :unescaped=>:"Here&amp;There")
    xml.target!  =>
      <sample escaped="This&amp;That" unescaped="Here&amp;There"/>
  • UTF-8 Support

    Builder correctly translates UTF-8 characters into valid XML. (New in version 2.0.0). Thanks to Sam Ruby for the translation code.

    You can get UTF-8 encoded output by making sure that the XML encoding is set to "UTF-8" and that the $KCODE variable is set to "UTF8".

    $KCODE = 'UTF8'
    xml = Builder::Markup.new
    xml.instruct!(:xml, :encoding => "UTF-8")
    xml.sample("Iñtërnâtiônàl")
    xml.target!  =>
      "<sample>Iñtërnâtiônàl</sample>"

Links

Description Link
Documents http://builder.rubyforge.org/
Github Clone git://github.com/jimweirich/builder.git
Issue / Bug Reports https://github.com/jimweirich/builder/issues?state=open

Contact

Description Value
Author Jim Weirich
Email [email protected]
Home Page http://onestepback.org
License MIT Licence (http://www.opensource.org/licenses/mit-license.html)

More Repositories

1

rspec-given

Given/When/Then keywords for RSpec Specifications
Ruby
652
star
2

wyriki

Experimental Rails application to explore decoupling app logic from Rails.
CSS
272
star
3

gilded_rose_kata

The Gilded Rose Code Cata
Ruby
202
star
4

re

Regular Expression Construction
Ruby
181
star
5

argus

Ruby API for controlling a Parrot AR Drone
Ruby
117
star
6

swimlanes

Draw git repositories in swim lane notation
JavaScript
113
star
7

sorcerer

Generate Ruby source from a Ripper style AST
Ruby
99
star
8

flexmock

Flexible mocking for Ruby testing
Ruby
93
star
9

sicp-study

Study Group Worked Exercises from "The Structure and Interpretation of Computer Programs"
Scheme
86
star
10

presentation_solid_ruby

SOLID Ruby Design Principles Presentation
Ruby
74
star
11

Given

A Given/When/Then Specification Framework
Ruby
60
star
12

emacs-setup

Emacs Setup and Customization
Emacs Lisp
59
star
13

presentation_connascence

The Grand Unifying Theory of Software Development: Connascence
Ruby
49
star
14

lambda_fizz

The Classic FizzBuzz program implemented in pure Ruby-Flavored Lambda Calculus
Ruby
47
star
15

emacs-setup-esk

My Emacs Setup based on the Emacs Starter Kit (ESK)
Emacs Lisp
45
star
16

bnr-ios-rubymotion

Big Nerd Ranch Guide to iOS Programming Examples in RubyMotion
Ruby
41
star
17

texp

Temporal Expressions for Ruby
Ruby
40
star
18

emacs-starter-kit

A Starter Kit for Rubyists wanting to use Emacs
Emacs Lisp
29
star
19

presentation_source_control

Source Control for People Who Don't Like Source Control
25
star
20

dim

DIM - Dependency Injection - Minimal
Ruby
25
star
21

sudoku

A Simple Sudoku Solver
Ruby
22
star
22

irb-setup

My setup and initialization files for irb
Ruby
18
star
23

presentation_enterprise_mom

What the Enterprise Can Learn From Your Mom presentation for erubycon 2008. (Aka "What? Threads are Hard?")
Ruby
18
star
24

presentation_10papers

10 Papers -- Really Fast
Ruby
17
star
25

presentation_writing_solid_ruby_code

How to Write Robust Ruby Programs
Ruby
16
star
26

partially_valid

A Rails plugin that allows validation on partially completed Active Record models (useful in wizards that incrementally build a model).
Ruby
16
star
27

pair_programming_bot

Pair Programming Bot iPhone Application
Ruby
16
star
28

presentation_ynot

Keynote and practice files for the Y-Not Talk (deriving the y-combinator from first principles)
15
star
29

rava

Ruby Code for Java Developers
Ruby
14
star
30

gotags

Simple TAGS file generator written in go (compare to ctags or exuberant_ctags)
Go
13
star
31

presentation_agile_engineering_practices

Agile Engineering Practices Overview
13
star
32

presentation_testing_why_dont_we_do_it_like_this

A presentation on ways to improve the way we do testing in an agile process.
Ruby
13
star
33

presentation_kata_and_analysis

A Presentation on a simple code kata and an analysis of the decisions made throughout the coding session.
Ruby
11
star
34

beer_song

Beer Song Kata (courtesy of Sandi Metz)
9
star
35

dudley

Techniques for Decoupling your application logic from Rails (or any web framework for that matter).
Ruby
9
star
36

BankOcrKata

Ruby solution to the Bank OCR Kata described at http://www.codingdojo.org/cgi-bin/wiki.pl?KataBankOCR
Ruby
9
star
37

presentation-connascence-examined

Connascence Examined Presentation
Java
9
star
38

polite_programmer_presentation

The Polite Programmer Presentation
Ruby
9
star
39

presentation_parenthetically_speaking

Keynote Presentation on SICP
Ruby
8
star
40

present_code

Tools for autoupdating Keynote presentations from a live code base.
Ruby
8
star
41

presentation_playing_it_safe

Presentation on Writing Good Library Code in Ruby
JavaScript
7
star
42

presentation_event-vs-cells

Presentation given at Big Ruby on Evented vs Celluloid
Ruby
7
star
43

presentation_to_infinity

Mountain Ruby Keynote - Don't be afraid to pioneer your ideas
6
star
44

RakePresentations

Rake Boot Camp and Power Rake Presentations
Ruby
6
star
45

polite_programmer_blog

The Blog of the Polite Programmers
6
star
46

sample_friends_app

This is a sample Rails app where I play around with some queries.
Ruby
5
star
47

presentation_flying_robots

Presentation on Controlling AR Drone with Ruby
4
star
48

Personography

Personal Project for Jenny
Ruby
4
star
49

project_euler_solutions

My solutions for the Project Euler problem set.
Ruby
4
star
50

rakedocs

Documents for the Rake Build System
CSS
3
star
51

jsblogger_sample

Sample Implementation of JS Blogger
Ruby
3
star
52

presentation-given

RSpec Given/When/Then Presentation
Ruby
2
star
53

protection_proxy

A proxy that protects against updates of selected fields
Ruby
2
star
54

example_blogger_with_seo

This is a version of the JumpStart blogger example with SEO url mapping
Ruby
1
star
55

travis_ci_flexmock_debug

A Project using FlexMock that can be deployed onto Travis-CI to see why flexmock isn't picked up.
Ruby
1
star