xx 2.0.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/xx.rb CHANGED
@@ -1,12 +1,17 @@
1
- unless defined? $__xx_rb__
1
+ unless(defined?($__xx_rb__) or defined?(XX))
2
+
2
3
  require "rexml/document"
3
4
 
4
5
  module XX
5
6
  #--{{{
6
- VERSION = "2.0.0"
7
-
7
+ VERSION = "2.1.0"
8
8
  def self.version() VERSION end
9
9
 
10
+ LIBDIR = File.dirname(File.expand_path(__FILE__)) << File::SEPARATOR
11
+ def self.libdir() LIBDIR end
12
+
13
+ require libdir + "binding_of_caller"
14
+
10
15
  %w(
11
16
  CRAZY_LIKE_A_HELL
12
17
  PERMISSIVE
@@ -59,7 +64,7 @@ module XX
59
64
  alias_method "to_s", "to_str"
60
65
  def pretty port = ""
61
66
  #--{{{
62
- @doc.write port, indent=2, transitive=false, ie_hack=true
67
+ @doc.write port, indent=0, transitive=false, ie_hack=true
63
68
  port
64
69
  #--}}}
65
70
  end
@@ -281,6 +286,7 @@ module XX
281
286
  #--}}}
282
287
  end
283
288
  alias_method "t__", "xx_text_"
289
+ alias_method "text__", "xx_text_"
284
290
  def xx_markup_ *objects, &b
285
291
  #--{{{
286
292
  doc = xx_doc
@@ -299,6 +305,7 @@ module XX
299
305
  #--}}}
300
306
  end
301
307
  alias_method "x__", "xx_markup_"
308
+ alias_method "markup__", "xx_markup_"
302
309
  def xx_any_ *objects, &b
303
310
  #--{{{
304
311
  doc = xx_doc
@@ -316,6 +323,7 @@ module XX
316
323
  doc.create text, &b
317
324
  #--}}}
318
325
  end
326
+ alias_method "any__", "xx_any_"
319
327
  alias_method "h__", "xx_any_"
320
328
  alias_method "x__", "xx_any_" # supplant for now
321
329
  def xx_cdata_ *objects, &b
@@ -331,6 +339,7 @@ module XX
331
339
  doc.create cdata, &b
332
340
  #--}}}
333
341
  end
342
+ alias_method "cdata__", "xx_cdata_"
334
343
  alias_method "c__", "xx_cdata_"
335
344
  def xx_parse_attributes string
336
345
  #--{{{
@@ -637,6 +646,212 @@ module XX
637
646
  alias_method "xml__", "xml_"
638
647
  #--}}}
639
648
  end
649
+
650
+ module Template
651
+ #--{{{
652
+ class Basic
653
+ #--{{{
654
+ %w( path inline type object template port pretty ).each{|a| attr_accessor a}
655
+ def initialize opts = {}, &b
656
+ #--{{{
657
+ @path = opts['path'] || opts[:path]
658
+ @inline = opts['inline'] || opts[:inline]
659
+ @type = opts['type'] || opts[:type]
660
+ @object = opts['object'] || opts[:object]
661
+ @pretty = opts['pretty'] || opts[:pretty]
662
+ @port = opts['port'] || opts[:port] || STDOUT
663
+
664
+ bool = lambda{|value| value ? true : false}
665
+ raise ArgumentError unless(bool[@path] ^ bool[@inline])
666
+
667
+ path_init(&b) if @path
668
+ inline_init(&b) if @inline
669
+
670
+ @type =
671
+ case @type.to_s.downcase.strip
672
+ when /^xhtml$/
673
+ XHTML
674
+ when /^xml$/
675
+ XML
676
+ when /^html4$/
677
+ HTML4
678
+ else
679
+ XHTML
680
+ end
681
+ #--}}}
682
+ end
683
+ def path_init &b
684
+ #--{{{
685
+ @template = IO.read @path
686
+ @type = @path[%r/\.[^\.]+$/o] || XHTML unless @type
687
+ #--}}}
688
+ end
689
+ def inline_init &b
690
+ #--{{{
691
+ @template = (@inline || b.call).to_s
692
+ @type ||= XHTML
693
+ #--}}}
694
+ end
695
+ def expand binding, opts = {}
696
+ #--{{{
697
+ port = opts['port'] || opts[:port] || STDOUT
698
+ pretty = opts['pretty'] || opts[:pretty]
699
+
700
+ object = eval 'self', binding
701
+
702
+ unless type === object
703
+ __m = type
704
+ klass = object.class
705
+ klass.module_eval{ include __m }
706
+ end
707
+
708
+ doc = eval @template, binding
709
+
710
+ display = pretty ? 'pretty' : 'to_str'
711
+
712
+ doc.send display, port
713
+ #--}}}
714
+ end
715
+ alias_method "result", "expand"
716
+ #--}}}
717
+ end
718
+
719
+ class File < Basic
720
+ #--{{{
721
+ def initialize *argv, &b
722
+ #--{{{
723
+ opts, argv = argv.partition{|arg| Hash === arg}
724
+ opts = opts.inject{|a,b| a.update b}
725
+ path = argv.shift
726
+ raise ArgumentError, "no path" unless path
727
+ raise ArgumentError, "bad opts" unless Hash === opts or opts.nil?
728
+ opts ||= {}
729
+ opts['path'] = opts[:path] = path.to_s
730
+ super opts, &b
731
+ #--}}}
732
+ end
733
+ #--}}}
734
+ end
735
+
736
+ class Inline < Basic
737
+ #--{{{
738
+ def initialize *argv, &b
739
+ #--{{{
740
+ opts, argv = argv.partition{|arg| Hash === arg}
741
+ opts = opts.inject{|a,b| a.update b}
742
+ inline = argv.shift || b.call
743
+ raise ArgumentError, "no inline" unless inline
744
+ raise ArgumentError, "bad opts" unless Hash === opts or opts.nil?
745
+ opts ||= {}
746
+ opts['inline'] = opts[:inline] = inline.to_s
747
+ super opts, &b
748
+ #--}}}
749
+ end
750
+ #--}}}
751
+ end
752
+ #--}}}
753
+ end
754
+
755
+ module Expandable
756
+ #--{{{
757
+ module InstanceMethods
758
+ #--{{{
759
+ def xx_template_file *a, &b
760
+ #--{{{
761
+ template = XX::Template::File.new *a, &b
762
+ template.object ||= self
763
+ template
764
+ #--}}}
765
+ end
766
+ def xx_template_inline *a, &b
767
+ #--{{{
768
+ template = XX::Template::Inline.new *a, &b
769
+ template.object ||= self
770
+ template
771
+ #--}}}
772
+ end
773
+ def xx_expand template, opts = {}
774
+ #--{{{
775
+ port = opts['port'] || opts[:port] || STDOUT
776
+ pretty = opts['pretty'] || opts[:pretty]
777
+ binding = opts['binding'] || opts[:binding]
778
+
779
+ type = template.type
780
+
781
+ unless type === self
782
+ klass = self.class
783
+ klass.module_eval{ include type }
784
+ end
785
+
786
+ display = pretty ? 'pretty' : 'to_str'
787
+
788
+ Binding.of_caller do |scope|
789
+ binding ||= eval('binding', scope)
790
+ doc = eval template.template, binding
791
+ doc.send display, port
792
+ end
793
+ #--}}}
794
+ end
795
+ def xx_expand_file *a, &b
796
+ #--{{{
797
+ template = xx_template_file *a, &b
798
+
799
+ type = template.type
800
+ pretty = template.pretty
801
+ port = template.port
802
+
803
+ unless type === self
804
+ klass = self.class
805
+ klass.module_eval{ include type }
806
+ end
807
+
808
+ display = pretty ? 'pretty' : 'to_str'
809
+
810
+ Binding.of_caller do |scope|
811
+ binding ||= eval('binding', scope)
812
+ doc = eval template.template, binding
813
+ doc.send display, port
814
+ end
815
+ #--}}}
816
+ end
817
+ alias_method "xx_expand_path", "xx_expand_file"
818
+ alias_method "xx_expand_template", "xx_expand_file"
819
+ def xx_expand_inline *a, &b
820
+ #--{{{
821
+ template = xx_template_inline *a, &b
822
+
823
+ type = template.type
824
+ pretty = template.pretty
825
+ port = template.port
826
+
827
+ unless type === self
828
+ klass = self.class
829
+ klass.module_eval{ include type }
830
+ end
831
+
832
+ display = pretty ? 'pretty' : 'to_str'
833
+
834
+ Binding.of_caller do |scope|
835
+ binding ||= eval('binding', scope)
836
+ doc = eval template.template, binding
837
+ doc.send display, port
838
+ end
839
+ #--}}}
840
+ end
841
+ alias_method "xx_expand_string", "xx_expand_inline"
842
+ #--}}}
843
+ end
844
+ module ClassMethods
845
+ end
846
+ def self.included other
847
+ #--{{{
848
+ other.instance_eval{ include InstanceMethods }
849
+ other.extend ClassMethods
850
+ #--}}}
851
+ end
852
+ #--}}}
853
+ end
854
+
640
855
  #--}}}
641
856
  end
642
857
 
@@ -1,10 +1,10 @@
1
- unless defined? $__xx_rb__
1
+ unless(defined?($__xx_rb__) or defined?(XX))
2
+
2
3
  require "rexml/document"
3
4
 
4
5
  module XX
5
6
  #--{{{
6
- VERSION = "2.0.0"
7
-
7
+ VERSION = "2.1.0"
8
8
  def self.version() VERSION end
9
9
 
10
10
  %w(
@@ -637,6 +637,126 @@ module XX
637
637
  alias_method "xml__", "xml_"
638
638
  #--}}}
639
639
  end
640
+
641
+ module Template
642
+
643
+ module InstanceMethods
644
+ def xx_template_file *a, &b
645
+ XX::Template::File.new *a, &b
646
+ end
647
+ def xx_template_inline *a, &b
648
+ XX::Template::Inline.new *a, &b
649
+ end
650
+ end
651
+ module ClassMethods
652
+ end
653
+
654
+ def self.included other
655
+ other.instance_eval{ include InstanceMethods }
656
+ other.extend ClassMethods
657
+ end
658
+
659
+ class Basic
660
+ #--{{{
661
+ %w(
662
+ path
663
+ inline
664
+ type
665
+ ).each{|a| attr_accessor a}
666
+
667
+ def initialize opts = {}, &b
668
+ #--{{{
669
+ @path = opts['path'] || opts[:path]
670
+ @inline = opts['inline'] || opts[:inline]
671
+ @type = opts['type'] || opts[:type]
672
+
673
+ bool = lambda{|value| value ? true : false}
674
+ raise ArgumentError unless(bool[@path] ^ bool[@inline])
675
+
676
+ path_init(&b) if @path
677
+ inline_init(&b) if @inline
678
+
679
+ @type =
680
+ case @type.to_s.downcase.strip
681
+ when /^xhtml$/
682
+ XHTML
683
+ when /^xml$/
684
+ XML
685
+ when /^html4$/
686
+ HTML4
687
+ else
688
+ XHTML
689
+ end
690
+ #--}}}
691
+ end
692
+
693
+ def path_init &b
694
+ #--{{{
695
+ @template = IO.read @path
696
+ @type = @path[%r/\.[^\.]+$/o] || XHTML unless @type
697
+ #--}}}
698
+ end
699
+
700
+ def inline_init &b
701
+ #--{{{
702
+ @template = (@inline || b.call).to_s
703
+ @type ||= XHTML
704
+ #--}}}
705
+ end
706
+
707
+ require 'binding_of_caller'
708
+ def expand opts = {}
709
+ #--{{{
710
+ port = opts['port'] || opts[:port] || STDOUT
711
+ pretty = opts['pretty'] || opts[:pretty]
712
+
713
+ Binding.of_caller do |binding_of_caller|
714
+ this = eval 'self', binding_of_caller
715
+ unless @type === this
716
+ klass = this.class
717
+ m = @type
718
+ klass.module_eval{ m }
719
+ end
720
+
721
+ doc = eval @template, binding_of_caller
722
+ msg = pretty ? 'pretty' : 'to_str'
723
+ doc.send msg, port
724
+ end
725
+ #--}}}
726
+ end
727
+
728
+ #--}}}
729
+ end
730
+
731
+ class File < Basic
732
+
733
+ def initialize *a, &b
734
+ path, opts, ignored = *a
735
+ raise ArgumentError unless path
736
+ raise ArgumentError unless Hash === opts or opts.nil?
737
+ opts ||= {}
738
+ opts['path'] = opts[:path] = path
739
+ super opts, &b
740
+
741
+ end
742
+
743
+ end
744
+
745
+ class Inline < Basic
746
+
747
+ def initialize *a, &b
748
+
749
+ inline, opts, ignored = *a
750
+ raise ArgumentError unless inline or b
751
+ raise ArgumentError unless Hash === opts or opts.nil?
752
+ opts ||= {}
753
+ opts['inline'] = opts[:inline] = inline
754
+ super opts, &b
755
+
756
+ end
757
+
758
+ end
759
+ end
640
760
  #--}}}
641
761
  end
642
762
 
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0
3
3
  specification_version: 1
4
4
  name: xx
5
5
  version: !ruby/object:Gem::Version
6
- version: 2.0.0
7
- date: 2007-02-16 00:00:00 -07:00
6
+ version: 2.1.0
7
+ date: 2007-02-24 00:00:00 -07:00
8
8
  summary: xx
9
9
  require_paths:
10
10
  - lib
@@ -29,7 +29,9 @@ post_install_message:
29
29
  authors:
30
30
  - Ara T. Howard
31
31
  files:
32
- - lib/xx-2.0.0.rb
32
+ - lib/xx.rb.bak
33
+ - lib/xx-2.1.0.rb
34
+ - lib/binding_of_caller.rb
33
35
  - lib/xx.rb
34
36
  test_files:
35
37
  - test/xx.rb