···
On Tue, 14 Feb 2006, Ian Whitney wrote:
#
# xx modules extend the current object to allow natural document markup
#
doc = xhtml_{
html_{
head_{ title_{ " go xx! " } }
body_{ " one more and it would be illegal " }
}
}
puts doc.pretty
~ > ruby sample/a.rb
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang='en' xml:lang='en' xmlns='http://www.w3.org/1999/xhtml'>
<head>
<title> go xx! </title>
</head>
<body> one more and it would be illegal </body>
</html>
<========< sample/b.rb >========>
~ > cat sample/b.rb
require "xx"
#
# xml is as easy as html. xx extends your object very carefully, adding an
# one method that is not prefaced with 'xx_' : 'method_missing'. the
# method_missing defined is conservatively, recognizing only methods that end
# with underscore ('_') as 'tag' methods intended to generate markup. as with
# html, attributes may be passed to any tag method as either symbol or string.
#
class Table < ::Array
include XX::XML
attr "fields"
def initialize *a, &b
@fields = a.shift
replace a
end
def self:: *a, &b
new *a, &b
end
def to_xml
xml_{
table_{
each do |row|
row_{
fields.zip(row) do |field, value|
field_(:name => field, 'width' => value.size){ value }
end
}
end
}
}
end
end
table = Table[
%w( first_name last_name ssn ),
%w( jane doe 424-24-2424 ),
%w( john buck 574-86-4242 ),
]
puts table.to_xml.pretty
~ > ruby sample/b.rb
<?xml version='1.0'?>
<table>
<row>
<field name='first_name' width='4'>jane</field>
<field name='last_name' width='3'>doe</field>
<field name='ssn' width='11'>424-24-2424</field>
</row>
<row>
<field name='first_name' width='4'>john</field>
<field name='last_name' width='4'>buck</field>
<field name='ssn' width='11'>574-86-4242</field>
</row>
</table>
<========< sample/c.rb >========>
~ > cat sample/c.rb
require "xx"
#
# xx makes it impossible to generate invalid (syntactically) invalid documents
# - unless to instruct it in insert raw html or xml using the 'h_' or 'x_'
# methods. text inserted with 't_' is automatically escaped. like all xx
# methods these can have one or more underscores after them in case there is a
# collision with another method or the tag 'h', 'x', or 't' needs to be
# generated.
#
include XX::XML
doc = xml_{
root_{
div_{ t_ "this is escaped < > & text" }
div_{ h_ "this is raw <html>. & is not escaped" }
div_{ x_ "<raw> xml </raw>" }
div_{ x_{ even_{ entire_{ documents_{ "nest" } } } } }
}
}
puts doc.pretty
~ > ruby sample/c.rb
<?xml version='1.0'?>
<root>
<div>this is escaped < > & text</div>
<div>this is raw <html>. & is not escaped</div>
<div><raw> xml </raw></div>
<div><even><entire><documents>nest</documents></entire></even></div>
</root>
<========< sample/d.rb >========>
~ > cat sample/d.rb
require "xx"
#
# xx has only a few methods which end in '_'. these methods, therefore, cannot
# be used in conjuction with method_missing to auto-generate tags. for those
# methods a tag of the same method can be generated using and escaped form,
# namely two or more underscores always mean 'generate a tag'. those methods
# are:
#
# - g_
# - text_
# - t_
# - h_
# - x_
# - c_
# - at_
# - att_
# - yat_
#
include XX::XML
doc = xml_{
root_{
t_{ "this is a text element" }
t__{ "this is not text, but a __tag__ called t" }
x_{ "this un-escaped & < > stuff" }
x__{ "this is not un-escaped & < > stuff but a tag called x" }
}
}
puts doc.pretty
~ > ruby sample/d.rb
<?xml version='1.0'?>
<root>this is a text element<t>this is not text, but a __tag__ called t</t>this un-escaped & < > stuff<x>this is not un-escaped & < > stuff but a tag called x</x>
</root>
HISTORY
0.1.0:
- added the "g_" method, which generates any tag
^
g_("anytag", "key" => "value"){ b_{ "bold" } }
- added at_ and att_ methods to parse yaml and k=v strings as hashes.
at_("src : image.jpg, width : 100%")
#=> {"src"=>"image.jpg", "width"=> "100%"}
0.0.0:
- initial version
AUTHORS
dan fitzpatrick <dan@eparklabs.com>
ara.t.howard <ara.t.howard@eparklabs.com>
BUGS
please send bug reports to /dev/null. patches to addresses above. 
LICENSE
ePark Labs Public License version 1 Copyright (c) 2005, ePark Labs, Inc. and
contributors All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of ePark Labs nor the names of its contributors may be
used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-a
--
judge your success by what you had to give up in order to get it.
- h.h. the 14th dali lama