From: "Andrew S. Townley" Date: 2009-03-28T03:05:55+09:00 Subject: [PATCH] tagz-5.0.0 -- processing instruction support Hi Ara, I'm sure you're sick of me by now, but I've added processing instruction support to tagz. It may not suit your style, but I've tried to keep it in the spirit of the library. excalibur$ diff -u /var/lib/gems/1.8/gems/tagz-5.0.0/lib/tagz.rb /tmp/tagz.rb --- /var/lib/gems/1.8/gems/tagz-5.0.0/lib/tagz.rb 2009-03-24 11:58:05.000000000 +0000 +++ /tmp/tagz.rb 2009-03-27 18:01:11.000000000 +0000 @@ -15,27 +15,7 @@ # open_tag # def tagz__ name, *argv, &block - options = argv.last.is_a?(Hash) ? argv.pop : {} - content = argv - - unless options.empty? - attributes = ' ' << - options.map do |key, value| - key = XChar.escape key.to_s - value = XChar.escape value.to_s - if value =~ %r/"/ - raise ArgumentError, value if value =~ %r/'/ - value = "'#{ value }'" - else - raise ArgumentError, value if value =~ %r/"/ - value = "\"#{ value }\"" - end - [key, value].join('=') - end.join(' ') - else - attributes = '' - end - + attributes, content = process_attributes *argv, &block tagz.concat "<#{ name }#{ attributes }>" if content.empty? @@ -68,6 +48,13 @@ tagz.concat "" end + # processing instruction + # + def tagz_? name, *argv, &block + attributes, content = process_attributes *argv, &block + tagz.concat "" + end + # access tagz doc and enclose tagz operations # def tagz document = nil, &block @@ -94,6 +81,8 @@ def method_missing m, *a, &b strategy = case m.to_s + when %r/^(.*[^?])_\?$/o + :processing_instruction when %r/^(.*[^_])_(!)?$/o :open_tag when %r/^_([^_].*)$/o @@ -119,6 +108,9 @@ m, bang = $1, $2 b ||= lambda{} if bang tagz{ tagz__(m, *a, &b) } + when :processing_instruction + m = $1 + tagz{ tagz_?(m, *a, &b) } when :close_tag m = $1 tagz{ __tagz(m, *a, &b) } @@ -135,6 +127,33 @@ end end + def process_attributes *argv, &block + options = argv.last.is_a?(Hash) ? argv.pop : {} + content = argv + + attributes = nil + + unless options.empty? + attributes = ' ' << + options.map do |key, value| + key = XChar.escape key.to_s + value = XChar.escape value.to_s + if value =~ %r/"/ + raise ArgumentError, value if value =~ %r/'/ + value = "'#{ value }'" + else + raise ArgumentError, value if value =~ %r/"/ + value = "\"#{ value }\"" + end + [key, value].join('=') + end.join(' ') + else + attributes = '' + end + + [ attributes, content ] + end + class Document < ::String def Document.for other Document === other ? other : Document.new(other.to_s) excalibur$ cat c.rb require 'tagz' include Tagz.globally doc = Tagz { xml_?(:version => "1.0") mypi_?(:arg1 => "foo") tagz_?("xml-stylesheet", :href => "classic.xsl", :type => "text/xml") test_! } puts doc excalibur$ ruby c.rb -- Andrew S. Townley http://atownley.org