[DRE-commits] r2166 - in packages: . libxtemplate-ruby1.8 libxtemplate-ruby1.8/branches libxtemplate-ruby1.8/branches/upstream libxtemplate-ruby1.8/branches/upstream/current libxtemplate-ruby1.8/branches/upstream/current/bin libxtemplate-ruby1.8/branches/upstream/current/ext libxtemplate-ruby1.8/branches/upstream/current/lib libxtemplate-ruby1.8/branches/upstream/current/samples
lucas at alioth.debian.org
lucas at alioth.debian.org
Sat Dec 8 12:05:10 UTC 2007
Author: lucas
Date: 2007-12-08 12:05:10 +0000 (Sat, 08 Dec 2007)
New Revision: 2166
Added:
packages/libxtemplate-ruby1.8/
packages/libxtemplate-ruby1.8/branches/
packages/libxtemplate-ruby1.8/branches/upstream/
packages/libxtemplate-ruby1.8/branches/upstream/current/
packages/libxtemplate-ruby1.8/branches/upstream/current/bin/
packages/libxtemplate-ruby1.8/branches/upstream/current/bin/xtemplate
packages/libxtemplate-ruby1.8/branches/upstream/current/ext/
packages/libxtemplate-ruby1.8/branches/upstream/current/ext/xt.c
packages/libxtemplate-ruby1.8/branches/upstream/current/lib/
packages/libxtemplate-ruby1.8/branches/upstream/current/lib/xtemplate.rb
packages/libxtemplate-ruby1.8/branches/upstream/current/samples/
packages/libxtemplate-ruby1.8/branches/upstream/current/samples/sample33.rb
packages/libxtemplate-ruby1.8/branches/upstream/current/samples/sample34.rb
Log:
[svn-inject] Installing original source of libxtemplate-ruby1.8
Added: packages/libxtemplate-ruby1.8/branches/upstream/current/bin/xtemplate
===================================================================
--- packages/libxtemplate-ruby1.8/branches/upstream/current/bin/xtemplate (rev 0)
+++ packages/libxtemplate-ruby1.8/branches/upstream/current/bin/xtemplate 2007-12-08 12:05:10 UTC (rev 2166)
@@ -0,0 +1,245 @@
+#!/usr/local/bin/ruby-1.8
+
+require 'xtemplate'
+
+info = {
+ 'template' => {'filename' => '', 'mtime' => ''},
+ 'data' => {'filename' => '', 'mtime' => ''},
+}
+
+$out = $stdout
+$filename = nil
+$data = {}
+xml_data = nil
+yaml_data= nil
+
+$debug = false
+$eruby = false
+$erb = false
+$show_time = false
+$dump = false
+$template = nil
+$plugin = nil
+
+# for amrita
+$use_amrita = false
+$use_compiler = false
+$hint = nil
+
+
+$eruby = Object.new
+def $eruby.print(*msg)
+ @str.concat(*msg)
+end
+def $eruby.run(code)
+ @str = ""
+ if( $eruby )
+ eval(ERuby::Compiler.new().compile_string(code))
+ elsif( $erb )
+ eval(ERB::Compiler.new(nil).compile(code))
+ end
+ $plugin ||= @plugin
+ @str
+end
+
+if( $eruby = ARGV.index("--with-eruby") )
+ require 'eruby'
+ ARGV.delete_at($eruby)
+elsif( $erb = ARGV.index("--with-erb") )
+ require 'erb'
+ ARGV.delete_at($erb)
+end
+if( $debug = ARGV.index("--debug") )
+ ARGV.delete_at($debug)
+end
+
+while ARGV[0]
+ case ARGV[0]
+ when "--use-amrita"
+ $use_amrita = true
+ when "--use-compiler"
+ $use_compiler = true
+ when "--hint"
+ ARGV.shift
+ $hint = ARGV[0]
+ $hint = eval($hint)
+ when "--show-time"
+ $show_time = true
+# when "--html"
+# $html = true
+ when "-x"
+ ARGV.shift
+ xml_data = File.open(ARGV[0])
+ if( $eruby || $erb )
+ xml_data = obj.run(xml_data.read)
+ if( $debug )
+ print(xml_data, "\n")
+ end
+ end
+ $data = XTemplate::XMLDocument.new(xml_data)
+ info['data']['filename'] = ARGV[0]
+ info['data']['mtime'] = File.mtime(ARGV[0]).to_s
+ when "-d"
+ ARGV.shift
+ $data = File.open(ARGV[0]){|f| f.read}
+ $data = eval($data)
+ info['data']['filename'] = ARGV[0]
+ info['data']['mtime'] = File.mtime(ARGV[0]).to_s
+ when "-y"
+ ARGV.shift
+ yaml_data = File.open(ARGV[0]){|f| f.read}
+ begin
+ require 'xtemplate/yaml'
+ rescue LoadError
+ $stderr.print("YAML library (http://yaml4r.sourceforge.net/) is not installed.\n")
+ exit(1)
+ end
+ $data = XTemplate::YAMLDocument.new(yaml_data)
+ info['data']['filename'] = ARGV[0]
+ info['data']['mtime'] = File.mtime(ARGV[0]).to_s
+ when "-D"
+ ARGV.shift
+ $data = eval(ARGV[0])
+ info['data']['filename'] = '-'
+ info['data']['mtime'] = Time.now.to_s
+ when "-c"
+ ARGV.shift
+ $filename = ARGV[0]
+ $dump = true
+ when "-T"
+ ARGV.shift
+ $filename = ARGV[0]
+ $template = File.open(ARGV[0]){|f| f.read}
+ when "-t"
+ ARGV.shift
+ $filename = ARGV[0]
+ if( $eruby || $erb )
+ $text = obj.run(File.open($filename){|f|f.read})
+ if( $debug )
+ print($text, "\n")
+ end
+ end
+ when "-o"
+ ARGV.shift
+ $out = File.open(ARGV[0],"w")
+ else
+ print("\n",
+ "xtemplate [-d <data-file>|-D <data-expr>|-x <xml-file>|-y <yaml-file>]\n",
+ " [-t <template-file> | -T <serialized-template>]\n",
+ " [-o <output-file>]\n",
+ " [--with-eruby|--with-erb]\n",
+ "xtemplate -c <template-file> [-o <output-file>]\n",
+ " (serialize the template-file.)\n",
+ "\n",
+ "--show-time print processing time\n",
+ "--use-amrita use Amrita instead of XTemplate\n",
+ "--use-amrita-xml use Amrita instead of XTemplate\n",
+ "\n")
+ exit(0)
+ end
+ ARGV.shift
+end
+
+def amrita_data(data)
+ case data
+ when Hash
+ newdata = {}
+ attrs = {}
+ nodes = {}
+ data.each{|key,val|
+ case key[0,1]
+ when "@"
+ attrs[key[1..-1].intern] = val
+ when "."
+ nodes = val
+ else
+ nodes[key.intern] = amrita_data(val)
+ end
+ }
+ if( attrs.size > 0 )
+ newdata = Amrita::a(attrs){ nodes }
+ else
+ data.each{|key, val|
+ newdata[key.intern] = amrita_data(val)
+ }
+ end
+ newdata
+ when Array
+ data.collect{|d| amrita_data(d)}
+ else
+ data
+ end
+end
+
+def time(msg)
+ t1 = Time.now
+ r = yield
+ t2 = Time.now
+ if( $show_time )
+ $stderr.print("#{msg}: #{t2 - t1}\n")
+ end
+ r
+end
+
+if( $filename )
+ info['template']['filename'] = $filename
+ info['template']['mtime'] = File.mtime($filename).to_s
+else
+ info['template']['filename'] = "-"
+ info['template']['mtime'] = Time.now.to_s
+end
+
+if( defined?($data.to_hash) )
+ $data = $data.to_hash
+end
+$data['xtemplate'] = info
+
+if( $use_amrita )
+ require 'amrita/template'
+ $data = amrita_data($data)
+ if( $filename )
+ t = time("Preparation"){ Amrita::TemplateFile.new($filename) }
+ else
+ t = time("Preparation"){ Amrita::TemplateText.new($text) }
+ end
+ if( $use_compiler )
+ t.use_compiler = true
+ end
+ if( $hint )
+ t.set_hint_by_sample_data(amrita_data($hint))
+ end
+ if( ! $html )
+ require 'amrita/xml'
+ t.xml = true
+ end
+ time("Expansion"){ t.expand($out, $data) }
+else
+ if( $template )
+ t = time("Preparation"){ XTemplate::XMLTemplate.load($template) }
+ else
+ if( $html )
+ require 'xtemplate/html'
+ if( $text )
+ t = time("Preparation"){ XTemplate::HTMLTemplate.new($text) }
+ elsif( $filename )
+ t = time("Preparation"){ XTemplate::HTMLTemplate.new(File.open($filename)) }
+ else
+ t = time("Preparation"){ XTemplate::HTMLTemplate.new($stdin) }
+ end
+ else
+ if( $text )
+ t = time("Preparation"){ XTemplate::XMLTemplate.new($text) }
+ elsif( $filename )
+ t = time("Preparation"){ XTemplate::XMLTemplate.new(File.open($filename)) }
+ else
+ t = time("Preparation"){ XTemplate::XMLTemplate.new($stdin) }
+ end
+ end
+ end
+ if( $dump )
+ $out.print(t.dump())
+ else
+ time("Expansion"){ t.expand2($out, $data, :plugin => $plugin ) }
+ end
+end
+$out << "\n"
Property changes on: packages/libxtemplate-ruby1.8/branches/upstream/current/bin/xtemplate
___________________________________________________________________
Name: svn:executable
+
Added: packages/libxtemplate-ruby1.8/branches/upstream/current/ext/xt.c
===================================================================
--- packages/libxtemplate-ruby1.8/branches/upstream/current/ext/xt.c (rev 0)
+++ packages/libxtemplate-ruby1.8/branches/upstream/current/ext/xt.c 2007-12-08 12:05:10 UTC (rev 2166)
@@ -0,0 +1,432 @@
+#include <ruby.h>
+#include <intern.h>
+
+static VALUE rb_mXTemplate;
+static VALUE rb_mXPath;
+static VALUE rb_cXNode;
+
+static VALUE rb_mUtil;
+static VALUE rb_cSanitizedString;
+static VALUE PredefinedEntity;
+static VALUE PredefinedStringsRegex;
+static VALUE PredefinedEntitiesRegex;
+static VALUE RevPredefinedEntity;
+static VALUE EntityAmp;
+static VALUE SanitizedAmp;
+static VALUE empty_str;
+static VALUE quot_regexp;
+
+static ID i_to_s, i_gsub, i_new, i_dup, i_gtgt, i_collect, i_strip;
+static ID ii_name, ii_attrs, ii_children, ii_data_path, ii_propagation;
+static ID ii_exname, ii_exattr, ii_expand, ii_option;
+
+
+static VALUE
+rb_xt_sanitize_i(VALUE obj)
+{
+ return rb_funcall(obj, i_gsub, 1, PredefinedStringsRegex);
+}
+
+static VALUE
+rb_xt_sanitize_ii(VALUE s, VALUE data)
+{
+ if( strcmp(RSTRING(s)->ptr, RSTRING(EntityAmp)->ptr) == 0 ){
+ return s;
+ }
+ else{
+ return rb_hash_aref(RevPredefinedEntity, s);
+ }
+}
+
+static VALUE
+rb_xt_sanitize(VALUE m, VALUE obj)
+{
+ VALUE str;
+ if( rb_obj_is_kind_of(obj, rb_cSanitizedString) ){
+ return obj;
+ }
+ else{
+ obj = rb_funcall(obj, i_to_s, 0);
+ obj = rb_funcall(obj, i_gsub, 2, EntityAmp, SanitizedAmp);
+ obj = rb_iterate(rb_xt_sanitize_i, obj, rb_xt_sanitize_ii, Qnil);
+ return rb_funcall(rb_cSanitizedString, i_new, 1, obj);
+ }
+}
+
+static VALUE
+rb_xt_unsanitize_i(VALUE obj)
+{
+ return rb_funcall(obj, i_gsub, 1, PredefinedEntitiesRegex);
+}
+
+static VALUE
+rb_xt_unsanitize_ii(VALUE s, VALUE data)
+{
+ return rb_hash_aref(PredefinedEntity, s);
+}
+
+static VALUE
+rb_xt_unsanitize(VALUE m, VALUE str)
+{
+ if( rb_obj_is_kind_of(str, rb_cSanitizedString) ){
+ str = rb_iterate(rb_xt_unsanitize_i, str, rb_xt_unsanitize_ii, Qnil);
+ return rb_funcall(rb_cString, i_new, 1, str);
+ }
+ else{
+ return str;
+ }
+}
+
+static VALUE
+rb_xt_path_split(VALUE m, VALUE path)
+{
+ char *str;
+ int len;
+ int i;
+ int l;
+ int s;
+ VALUE ids;
+
+ Check_Type(path, T_STRING);
+ len = RSTRING(path)->len;
+ str = ALLOCA_N(char,len+1);
+ memcpy(str,RSTRING(path)->ptr,len+1);
+ ids = rb_ary_new();
+ l = 0;
+ s = 0;
+ for( i=0; i < len; i++ ){
+ switch( str[i] ){
+ case '{':
+ case '[':
+ l++;
+ break;
+ case '}':
+ case ']':
+ l--;
+ break;
+ case '/':
+ if( l == 0 ){
+ rb_ary_push(ids, rb_tainted_str_new(str + s, i - s));
+ s = i + 1;
+ }
+ break;
+ }
+ }
+ rb_ary_push(ids, rb_tainted_str_new(str + s, i - s));
+ if( str[0] == '/' ){
+ rb_ary_store(ids, 0, rb_const_get(rb_mXPath, rb_intern("RootNode")));
+ }
+ return ids;
+}
+
+static VALUE
+rb_xt_args_split(VALUE m, VALUE path)
+{
+ char *str;
+ int len;
+ int i;
+ int l;
+ int s;
+ VALUE ids;
+ VALUE v;
+ int escape, inref;
+
+ Check_Type(path, T_STRING);
+ path = rb_xt_unsanitize(m,path);
+ len = RSTRING(path)->len;
+ if( len == 0 ){
+ return rb_ary_new();
+ }
+ str = ALLOCA_N(char,len+1);
+ memcpy(str,RSTRING(path)->ptr,len+1);
+ ids = rb_ary_new();
+ l = 0;
+ s = 0;
+ escape = 0;
+ inref = 0;
+ for( i=0; i < len; i++ ){
+ switch( str[i] ){
+ case '\'':
+ case '"':
+ if( escape ){
+ escape = 0;
+ }
+ else{
+ if( l ){
+ l = 0;
+ }
+ else{
+ l = 1;
+ }
+ }
+ break;
+ case '\\':
+ escape = 1;
+ break;
+ case ',':
+ if( !l ){
+ v = rb_tainted_str_new(str + s, i - s);
+ v = rb_funcall(v, i_strip, 0);
+ v = rb_funcall(v, i_gsub, 2, quot_regexp, empty_str);
+ if( RSTRING(v)->len > 0 )
+ rb_ary_push(ids, v);
+ s = i + 1;
+ }
+ break;
+ }
+ }
+ v = rb_tainted_str_new(str + s, i - s);
+ v = rb_funcall(v, i_strip, 0);
+ v = rb_funcall(v, i_gsub, 2, quot_regexp, empty_str);
+ if( RSTRING(v)->len > 0 )
+ rb_ary_push(ids, v);
+ return ids;
+}
+
+static VALUE
+rb_xt_cond_split(VALUE m, VALUE path)
+{
+ char *str;
+ int len;
+ int i;
+ int l;
+ int s;
+ VALUE xs;
+
+ Check_Type(path, T_STRING);
+ len = RSTRING(path)->len;
+ str = ALLOCA_N(char,len+1);
+ memcpy(str,RSTRING(path)->ptr,len+1);
+ xs = rb_ary_new();
+ l = 0;
+ s = 0;
+ for( i=0; i < len; i++ ){
+ switch( str[i] ){
+ case '{':
+ case '[':
+ if( l == 0 ){
+ if( i == 0 ){
+ rb_ary_push(xs, rb_str_new2(""));
+ }
+ else if( i != s ){
+ VALUE tmp = rb_tainted_str_new(str + s, i - s);
+ if( OBJ_TAINTED(str) ){ OBJ_TAINT(tmp); };
+ rb_ary_push(xs, tmp);
+ }
+ s = i;
+ }
+ l ++;
+ break;
+ case '}':
+ case ']':
+ l--;
+ if( l == 0 ){
+ VALUE tmp = rb_tainted_str_new(str + s, i - s + 1);
+ if( OBJ_TAINTED(str) ){ OBJ_TAINT(tmp); };
+ rb_ary_push(xs, tmp);
+ s = i + 1;
+ }
+ break;
+ }
+ }
+ if( s != i ){
+ VALUE tmp = rb_tainted_str_new(str + s, i - s);
+ if( OBJ_TAINTED(str) ){ OBJ_TAINT(tmp); };
+ rb_ary_push(xs, tmp);
+ }
+ return xs;
+}
+
+static VALUE
+rb_xt_xnode_deep_dup_i(VALUE children)
+{
+ return rb_funcall(children, i_collect, 0);
+}
+
+static VALUE
+rb_xt_xnode_deep_dup_ii(VALUE child, VALUE data)
+{
+ VALUE node = data;
+ static VALUE rb_xt_xnode_deep_dup(int, VALUE[], VALUE);
+
+ if( rb_obj_is_kind_of(child, rb_cXNode) ){
+ VALUE argv[1] = {node};
+ return rb_xt_xnode_deep_dup(1, argv, child);
+ }
+ else{
+ return child;
+ }
+}
+
+static VALUE
+rb_xt_xnode_deep_dup(int argc, VALUE argv[], VALUE xnode)
+{
+ VALUE newnode;
+ VALUE name, attrs, children, parent, data_path;
+ VALUE propagation, exname, exattr, expand, option;
+
+ rb_scan_args(argc, argv, "01", &parent);
+
+ name = rb_ivar_get(xnode, ii_name);
+ if( ! NIL_P(name) )
+ name = rb_funcall(name, i_dup, 0);
+
+ attrs = rb_ivar_get(xnode, ii_attrs);
+ if( ! NIL_P(name) )
+ attrs = rb_funcall(attrs, i_dup, 0);
+
+ children = Qnil;
+
+ data_path = rb_ivar_get(xnode, ii_data_path);
+ if( ! NIL_P(data_path) )
+ data_path = rb_funcall(data_path, i_dup, 0);
+
+ propagation = rb_ivar_get(xnode, ii_propagation);
+
+ exname = rb_ivar_get(xnode, ii_exname);
+ if( ! NIL_P(exname) )
+ exname = rb_funcall(exname, i_dup, 0);
+
+ exattr = rb_ivar_get(xnode, ii_exattr);
+ if( ! NIL_P(exattr) )
+ exattr = rb_funcall(exattr, i_dup, 0);
+
+ expand = rb_ivar_get(xnode, ii_expand);
+
+ option = rb_funcall(rb_ivar_get(xnode, ii_option), i_dup, 0);
+
+ newnode = rb_funcall(rb_cXNode, i_new, 10,
+ name, attrs, Qnil, parent, data_path, propagation,
+ exname, exattr, expand, option);
+
+ children = rb_iterate(rb_xt_xnode_deep_dup_i, rb_ivar_get(xnode, ii_children),
+ rb_xt_xnode_deep_dup_ii, newnode);
+
+ rb_ivar_set(newnode, ii_children, children);
+
+ return newnode;
+}
+
+static VALUE
+rb_xt_xnode_dump_i(VALUE attrs)
+{
+ return rb_funcall(attrs, i_collect, 0);
+}
+
+static VALUE
+rb_xt_xnode_dump_ii(VALUE ary, VALUE data)
+{
+ VALUE attr, val, str;
+ Check_Type(ary, T_ARRAY);
+ attr = RARRAY(ary)->ptr[0];
+ val = RARRAY(ary)->ptr[1];
+ str = rb_str_new2("");
+ rb_str_concat(str, attr);
+ rb_str_cat2(str, "=\"");
+ rb_str_concat(str, val);
+ rb_str_cat2(str, "\"");
+ return str;
+}
+
+static VALUE
+rb_xt_xnode_dump(VALUE xnode, VALUE io)
+{
+ VALUE name = rb_ivar_get(xnode,ii_name);
+ VALUE children = rb_ivar_get(xnode, ii_children);
+ int i, len;
+ VALUE child;
+ VALUE tag;
+
+ if( ! NIL_P(name) ){
+ VALUE attrs = rb_ivar_get(xnode, ii_attrs);
+ attrs = rb_iterate(rb_xt_xnode_dump_i, attrs, rb_xt_xnode_dump_ii, Qnil);
+ if( RARRAY(attrs)->len > 0 ){
+ attrs = rb_str_concat(rb_str_new2(" "),
+ rb_ary_join(attrs, rb_str_new2(" ")));
+ }
+ else{
+ attrs = rb_str_new2("");
+ }
+ if( rb_funcall(children, rb_intern("empty?"), 0) ){
+ tag = rb_str_new2("<");
+ rb_str_concat(tag, name);
+ rb_str_concat(tag, attrs);
+ rb_str_cat2(tag, " />");
+ rb_funcall(io, i_gtgt, 1, tag);
+ return io;
+ }
+ else{
+ tag = rb_str_new2("<");
+ rb_str_concat(tag, name);
+ rb_str_concat(tag, attrs);
+ rb_str_cat2(tag, ">");
+ rb_funcall(io, i_gtgt, 1, tag);
+ }
+ }
+ Check_Type(children,T_ARRAY);
+ len = RARRAY(children)->len;
+ for( i=0; i < len; i++ ){
+ child = RARRAY(children)->ptr[i];
+ if( rb_obj_is_kind_of(child, rb_cXNode) ){
+ rb_xt_xnode_dump(child, io);
+ }
+ else{
+ rb_funcall(io, i_gtgt, 1, child);
+ }
+ }
+ if( ! NIL_P(name) ){
+ tag = rb_tainted_str_new2("</");
+ rb_str_concat(tag, name);
+ rb_str_cat2(tag, ">");
+ rb_funcall(io, i_gtgt, 1, tag);
+ }
+ return io;
+}
+
+void
+Init_xtemplate_ext()
+{
+ i_to_s = rb_intern("to_s");
+ i_gsub = rb_intern("gsub");
+ i_new = rb_intern("new");
+ i_dup = rb_intern("dup");
+ i_gtgt = rb_intern("<<");
+ i_collect = rb_intern("collect");
+ i_strip = rb_intern("strip");
+ ii_name = rb_intern("@name");
+ ii_attrs = rb_intern("@attrs");
+ ii_children = rb_intern("@children");
+ ii_data_path = rb_intern("@data_path");
+ ii_propagation = rb_intern("@propagation");
+ ii_exname = rb_intern("@exname");
+ ii_exattr = rb_intern("@exattr");
+ ii_expand = rb_intern("@expand");
+ ii_option = rb_intern("@option");
+
+ rb_mXTemplate = rb_eval_string("::XTemplate");
+ rb_mXPath = rb_eval_string("::XTemplate::XPath");
+ rb_cXNode = rb_eval_string("::XTemplate::XNode");
+ rb_mUtil = rb_eval_string("::XTemplate::Util");
+ rb_cSanitizedString = rb_eval_string("::XTemplate::SanitizedString");
+
+ PredefinedStringsRegex = rb_eval_string("::XTemplate::Util::PredefinedStringsRegex");
+ PredefinedEntitiesRegex = rb_eval_string("::XTemplate::Util::PredefinedEntitiesRegex");
+ PredefinedEntity = rb_eval_string("::XTemplate::Util::PredefinedEntity");
+ RevPredefinedEntity = rb_eval_string("::XTemplate::Util::RevPredefinedEntity");
+ EntityAmp = rb_eval_string("::XTemplate::Util::EntityAmp");
+ SanitizedAmp = rb_eval_string("::XTemplate::Util::SanitizedAmp");
+ empty_str = rb_tainted_str_new2("");
+ rb_gc_register_address(&empty_str);
+ quot_regexp = rb_eval_string("/(\\A['\"])|([\"']\\z)/");
+ rb_gc_register_address("_regexp);
+
+ rb_define_module_function(rb_mXPath, "path_split", rb_xt_path_split, 1);
+ rb_define_module_function(rb_mXPath, "cond_split", rb_xt_cond_split, 1);
+ rb_define_module_function(rb_mXPath, "args_split", rb_xt_args_split, 1);
+
+ rb_define_module_function(rb_mUtil, "sanitize", rb_xt_sanitize, 1);
+ rb_define_module_function(rb_mUtil, "unsanitize", rb_xt_unsanitize, 1);
+
+ rb_define_method(rb_cXNode, "dump", rb_xt_xnode_dump, 1);
+ rb_define_method(rb_cXNode, "deep_dup", rb_xt_xnode_deep_dup, -1);
+}
Added: packages/libxtemplate-ruby1.8/branches/upstream/current/lib/xtemplate.rb
===================================================================
--- packages/libxtemplate-ruby1.8/branches/upstream/current/lib/xtemplate.rb (rev 0)
+++ packages/libxtemplate-ruby1.8/branches/upstream/current/lib/xtemplate.rb 2007-12-08 12:05:10 UTC (rev 2166)
@@ -0,0 +1,165 @@
+# -*- ruby -*-
+#
+# XTemplate
+#
+
+require 'xtemplate/util'
+require 'xtemplate/xpath'
+require 'xtemplate/node'
+require 'xtemplate/xml'
+begin
+ require 'xtemplate_ext'
+rescue LoadError
+end
+
+module XTemplate
+ def warn(*msg)
+ $stderr.puts(msg.join)
+ end
+
+ class XMLTemplate
+ attr_reader :node
+ protected :node
+
+ def initialize(doc, templates = nil)
+ case doc
+ when XNode
+ @node = doc
+ @templates = templates || {}
+ else
+ parser = XMLParser.new
+ @node = parser.parse(doc)
+ @templates = templates || {}
+ @node.prepare(nil, nil, @templates)
+ end
+ end
+
+ def dup()
+ self.class.new(@node.deep_dup, @templates)
+ end
+ alias clone dup
+
+ def [](name)
+ node = @templates[name]
+ if( node )
+ self.class.new(node)
+ else
+ nil
+ end
+ end
+
+ def []=(name, template)
+ node = @templates[name]
+ node.children = []
+ if( template )
+ [template.node.children].flatten.each{|child|
+ node.add_child(child)
+ }
+ end
+ node
+ end
+
+ def <<(template)
+ @node.add_child(template.node)
+ self
+ end
+
+ def to_s
+ @node.to_s
+ end
+
+ def dump()
+ [Marshal.dump(self)].pack("m")
+ end
+
+ def XMLTemplate.load(str)
+ if( str.respond_to?(:read) )
+ str = str.read
+ end
+ Marshal.load(str.unpack("m")[0])
+ end
+
+ DEFAULT_OPTS = {
+ :keep_template => true,
+ :keep_data => false,
+ :plugin => XPath::Action.new,
+ }
+
+ def expand(data, opts = nil)
+ s = SanitizedString['']
+ expand2(s, data, opts)
+ s
+ end
+
+ def expand2(io, data, opts = nil)
+ if( opts )
+ DEFAULT_OPTS.each{|key,val|
+ opts[key] ||= val
+ }
+ else
+ opts = DEFAULT_OPTS
+ end
+ if( opts[:keep_template] )
+ node = @node.deep_dup()
+ else
+ node = @node
+ end
+ if( opts[:keep_data] )
+ data = XTemplate::dup_data(data)
+ end
+ if( defined?(data.to_hash) )
+ data = data.to_hash
+ end
+ node.expand(data, nil, data, opts[:plugin]) # (1)current data (2)parent data (3)root data
+ node.dump(io)
+ end
+ end
+
+ module_function
+
+ def use_simple_expand()
+ XNode::use_simple_expand()
+ end
+
+ def use_default_expand()
+ XNode::use_default_expand()
+ end
+
+ def use_simple_xpath()
+ XPath::use_simple_xpath()
+ end
+
+ def use_default_xpath()
+ XPath::use_default_xpath()
+ end
+
+ def dup_data(data, oids=[])
+ if( oids.include?(data.id) )
+ data
+ else
+ oids.push(data.id)
+ case data
+ when Hash
+ data = data.dup
+ data.each{|key,val| data[key] = dup_data(val,oids)}
+ data
+ when Array
+ data = data.dup
+ data.collect!{|val| dup_data(val,oids) }
+ data
+# when Numeric, TrueClass, FalseClass # immutable objects
+# data
+ else
+ begin
+ data.dup
+ rescue TypeError
+ data
+ end
+ end
+ end
+ end
+
+ EscapedString = SanitizedString
+ Template = XMLTemplate
+ TextNode = XPath::TextNode
+end
Added: packages/libxtemplate-ruby1.8/branches/upstream/current/samples/sample33.rb
===================================================================
--- packages/libxtemplate-ruby1.8/branches/upstream/current/samples/sample33.rb (rev 0)
+++ packages/libxtemplate-ruby1.8/branches/upstream/current/samples/sample33.rb 2007-12-08 12:05:10 UTC (rev 2166)
@@ -0,0 +1,38 @@
+# -*- ruby -*-
+#
+# An explicit iteration using the attribute 'each'
+#
+
+require 'xtemplate'
+
+class MyEnumerable
+ include Enumerable
+
+ def each
+ [
+ {'url' => "url1", 'name' => "anchor1"},
+ {'url' => "url2", 'name' => "anchor2"},
+ {'url' => "url3", 'name' => "anchor3"},
+ ].each{|link| yield(link)}
+ end
+end
+
+data ={
+ 'links' => MyEnumerable.new()
+}
+
+text = <<EOF
+<?xml version="1.0" encoding="UTF-8"?>
+<html xmlns:xt="#{XTemplate::BIND_URI}">
+<body>
+ <ul>
+ <li xt:each="/links">
+ <a><xt:select id="url{@href}" /><xt:select id="name" /></a>
+ </li>
+ </ul>
+</body>
+</html>
+EOF
+
+t = XTemplate::XMLTemplate.new(text)
+print(t.expand(data))
Property changes on: packages/libxtemplate-ruby1.8/branches/upstream/current/samples/sample33.rb
___________________________________________________________________
Name: svn:executable
+
Added: packages/libxtemplate-ruby1.8/branches/upstream/current/samples/sample34.rb
===================================================================
--- packages/libxtemplate-ruby1.8/branches/upstream/current/samples/sample34.rb (rev 0)
+++ packages/libxtemplate-ruby1.8/branches/upstream/current/samples/sample34.rb 2007-12-08 12:05:10 UTC (rev 2166)
@@ -0,0 +1,38 @@
+# -*- ruby -*-
+#
+# An explicit iteration using the tag <xt:each>.
+#
+
+require 'xtemplate'
+
+class MyEnumerable
+ include Enumerable
+
+ def each
+ [
+ {'url' => "url1", 'name' => "anchor1"},
+ {'url' => "url2", 'name' => "anchor2"},
+ {'url' => "url3", 'name' => "anchor3"},
+ ].each{|link| yield(link)}
+ end
+end
+
+data ={
+ 'links' => MyEnumerable.new()
+}
+
+text = <<EOF
+<?xml version="1.0" encoding="UTF-8"?>
+<html xmlns:xt="#{XTemplate::BIND_URI}">
+<body>
+ <ul>
+ <xt:each id="/links">
+ <li><a><xt:select id="url{@href}" /><xt:select id="name" /></a></li>
+ </xt:each>
+ </ul>
+</body>
+</html>
+EOF
+
+t = XTemplate::XMLTemplate.new(text)
+print(t.expand(data))
Property changes on: packages/libxtemplate-ruby1.8/branches/upstream/current/samples/sample34.rb
___________________________________________________________________
Name: svn:executable
+
More information about the Pkg-ruby-extras-commits
mailing list