tagz 7.0.0 → 7.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.
Files changed (4) hide show
  1. data/lib/tagz.rb +214 -204
  2. data/tagz.gemspec +1 -1
  3. data/test/tagz.rb +19 -3
  4. metadata +2 -2
data/lib/tagz.rb CHANGED
@@ -1,237 +1,166 @@
1
1
  unless defined? Tagz
2
2
 
3
+ # core tagz functions
4
+ #
3
5
  module Tagz
4
- def Tagz.version() '7.0.0' end
6
+ def Tagz.version() '7.1.0' end
5
7
 
6
8
  private
9
+ # access tagz doc and enclose tagz operations
10
+ #
11
+ def tagz document = nil, &block
12
+ @tagz ||= nil ## shut wornings up
13
+ previous = @tagz
7
14
 
8
- # open_tag
9
- #
10
- def tagz__ name, *argv, &block
11
- options = argv.last.is_a?(Hash) ? argv.pop : {}
12
- content = argv
13
-
14
- unless options.empty?
15
- attributes = ' ' <<
16
- options.map do |key, value|
17
- key = Tagz.escape_attribute(key)
18
- value = Tagz.escape_attribute(value)
19
- if value =~ %r/"/
20
- raise ArgumentError, value if value =~ %r/'/
21
- value = "'#{ value }'"
22
- else
23
- raise ArgumentError, value if value =~ %r/"/
24
- value = "\"#{ value }\""
25
- end
26
- [key, value].join('=')
27
- end.join(' ')
28
- else
29
- attributes = ''
15
+ if block
16
+ @tagz ||= (Tagz.document.for(document) || Tagz.document.new)
17
+ begin
18
+ size = @tagz.size
19
+ value = instance_eval(&block)
20
+ @tagz << value unless(@tagz.size > size)
21
+ @tagz
22
+ ensure
23
+ @tagz = previous
24
+ end
25
+ else
26
+ document ? Tagz.document.for(document) : @tagz
27
+ end
30
28
  end
31
29
 
32
- tagz.push "<#{ name }#{ attributes }>"
30
+ # open_tag
31
+ #
32
+ def tagz__ name, *argv, &block
33
+ options = argv.last.is_a?(Hash) ? argv.pop : {}
34
+ content = argv
35
+
36
+ unless options.empty?
37
+ attributes = ' ' <<
38
+ options.map do |key, value|
39
+ key = Tagz.escape_attribute(key)
40
+ value = Tagz.escape_attribute(value)
41
+ if value =~ %r/"/
42
+ raise ArgumentError, value if value =~ %r/'/
43
+ value = "'#{ value }'"
44
+ else
45
+ raise ArgumentError, value if value =~ %r/"/
46
+ value = "\"#{ value }\""
47
+ end
48
+ [key, value].join('=')
49
+ end.join(' ')
50
+ else
51
+ attributes = ''
52
+ end
33
53
 
34
- if content.empty?
35
- if block
36
- size = tagz.size
37
- value = block.call(tagz)
54
+ tagz.push "<#{ name }#{ attributes }>"
38
55
 
39
- if value.nil?
40
- unless(tagz.size > size)
41
- tagz[-1] = "/>"
56
+ if content.empty?
57
+ if block
58
+ size = tagz.size
59
+ value = block.call(tagz)
60
+
61
+ if value.nil?
62
+ unless(tagz.size > size)
63
+ tagz[-1] = "/>"
64
+ else
65
+ tagz.push "</#{ name }>"
66
+ end
42
67
  else
68
+ tagz << value.to_s unless(tagz.size > size)
43
69
  tagz.push "</#{ name }>"
44
70
  end
45
- else
71
+
72
+ end
73
+ else
74
+ tagz << content.join
75
+ if block
76
+ size = tagz.size
77
+ value = block.call(tagz)
46
78
  tagz << value.to_s unless(tagz.size > size)
47
- tagz.push "</#{ name }>"
48
79
  end
49
-
80
+ tagz.push "</#{ name }>"
50
81
  end
51
- else
52
- tagz << content.join
53
- if block
54
- size = tagz.size
55
- value = block.call(tagz)
56
- tagz << value.to_s unless(tagz.size > size)
57
- end
58
- tagz.push "</#{ name }>"
59
- end
60
82
 
61
- tagz
62
- end
63
-
64
- # close_tag
65
- #
66
- def __tagz tag, *a, &b
67
- tagz.push "</#{ tag }>"
68
- end
83
+ tagz
84
+ end
69
85
 
70
- # access tagz doc and enclose tagz operations
71
- #
72
- def tagz document = nil, &block
73
- @tagz ||= nil ## shut wornings up
74
- previous = @tagz
75
-
76
- if block
77
- @tagz ||= (Document.for(document) || Document.new)
78
- begin
79
- size = @tagz.size
80
- value = instance_eval(&block)
81
- @tagz << value unless(@tagz.size > size)
82
- @tagz
83
- ensure
84
- @tagz = previous
85
- end
86
- else
87
- document ? Document.for(document) : @tagz
86
+ # close_tag
87
+ #
88
+ def __tagz tag, *a, &b
89
+ tagz.push "</#{ tag }>"
88
90
  end
89
- end
90
91
 
91
- # catch special tagz methods
92
- #
93
- def method_missing m, *a, &b
94
- strategy =
95
- case m.to_s
96
- when %r/^(.*[^_])_(!)?$/o
97
- :open_tag
98
- when %r/^_([^_].*)$/o
99
- :close_tag
100
- when 'e'
101
- :element
102
- when '__', '___'
103
- :puts
104
- else
105
- nil
106
- end
92
+ # catch special tagz methods
93
+ #
94
+ def method_missing m, *a, &b
95
+ strategy =
96
+ case m.to_s
97
+ when %r/^(.*[^_])_(!)?$/o
98
+ :open_tag
99
+ when %r/^_([^_].*)$/o
100
+ :close_tag
101
+ when 'e'
102
+ :element
103
+ when '__', '___'
104
+ :puts
105
+ else
106
+ nil
107
+ end
107
108
 
108
- if(strategy.nil? or (tagz.nil? and not Globally===self))
109
- begin
110
- super
111
- ensure
112
- $!.set_backtrace caller(skip=1) if $!
109
+ if(strategy.nil? or (tagz.nil? and not Tagz.globally===self))
110
+ begin
111
+ super
112
+ ensure
113
+ $!.set_backtrace caller(skip=1) if $!
114
+ end
113
115
  end
114
- end
115
-
116
- case strategy
117
- when :open_tag
118
- m, bang = $1, $2
119
- b ||= lambda{} if bang
120
- tagz{ tagz__(m, *a, &b) }
121
- when :close_tag
122
- m = $1
123
- tagz{ __tagz(m, *a, &b) }
124
- when :element
125
- Element.new(*a, &b)
126
- when :puts
127
- tagz do
128
- tagz.push("\n")
129
- unless a.empty?
130
- tagz.push(a.join)
116
+
117
+ case strategy
118
+ when :open_tag
119
+ m, bang = $1, $2
120
+ b ||= lambda{} if bang
121
+ tagz{ tagz__(m, *a, &b) }
122
+ when :close_tag
123
+ m = $1
124
+ tagz{ __tagz(m, *a, &b) }
125
+ when :element
126
+ Tagz.element.new(*a, &b)
127
+ when :puts
128
+ tagz do
131
129
  tagz.push("\n")
130
+ unless a.empty?
131
+ tagz.push(a.join)
132
+ tagz.push("\n")
133
+ end
132
134
  end
133
- end
134
- end
135
- end
136
-
137
- # escape utils
138
- #
139
- def Tagz.escapeHTML(*strings)
140
- XChar.escape(strings.join)
141
- end
142
- def Tagz.escape(*strings)
143
- XChar.escape(strings.join)
144
- end
145
-
146
- # support for configuring attribute escaping
147
- #
148
- def Tagz.escape_attribute!(*args, &block)
149
- previous = @escape_attribute if defined?(@escape_attribute)
150
- unless args.empty? and block.nil?
151
- value = block ? block : args.shift
152
- value = Escape if value==true
153
- value = NoEscape if(value==false or value==nil)
154
- @escape_attribute = value.to_proc
155
- return previous
135
+ end
156
136
  end
157
- @escape_attribute
158
- end
159
- def Tagz.escape_attributes!(*args, &block)
160
- Tagz.escape_attribute!(*args, &block)
161
- end
162
- def Tagz.escape_attribute(value)
163
- @escape_attribute.call(value.to_s)
164
- end
137
+ end
165
138
 
166
- # support for configuring content escaping
167
- #
168
- def Tagz.escape_content!(*args, &block)
169
- previous = @escape_content if defined?(@escape_content)
170
- unless args.empty? and block.nil?
171
- value = block ? block : args.shift
172
- value = Escape if value==true
173
- value = NoEscape if(value==false or value==nil)
174
- @escape_content = value.to_proc
175
- return previous
176
- end
177
- @escape_content
178
- end
179
- def Tagz.escape_contents!(*args, &block)
180
- Tagz.escape_content!(*args, &block)
181
- end
182
- def Tagz.escape_content(value)
183
- @escape_content.call(value.to_s)
184
- end
185
139
 
186
- # configure tagz escaping
140
+ # supporting code
141
+ #
142
+ module Tagz
143
+ # singleton_class access for ad-hoc method adding from inside namespace
187
144
  #
188
- def Tagz.escape!(options = {})
189
- options = {:attributes => options, :content => options} unless options.is_a?(Hash)
190
- escape_attributes = options[:attributes]||options['attributes']
191
- escape_content = options[:content]||options['content']
192
- Tagz.escape_attributes!(!!escape_attributes)
193
- Tagz.escape_content!(!!escape_content)
194
- end
195
- def Tagz.i_know_what_the_hell_i_am_doing!
196
- escape!(false)
197
- end
198
- def Tagz.i_do_not_know_what_the_hell_i_am_doing!
199
- escape!(true)
200
- end
201
- def Tagz.xml_mode!
202
- Tagz.escape!(
203
- :attributes => true,
204
- :content => true
205
- )
206
- end
207
- def Tagz.html_mode!
208
- Tagz.escape!(
209
- :attributes => true,
210
- :content => false
145
+ def Tagz.singleton_class(&block)
146
+ @singleton_class ||= (
147
+ class << Tagz
148
+ self
149
+ end
211
150
  )
212
- end
213
-
214
-
215
-
216
- # module shortcuts - namespace preserving
217
- #
218
- def Tagz.globally
219
- Globally
220
- end
221
- def Tagz.privately
222
- Privately
151
+ block ? @singleton_class.module_eval(&block) : @singleton_class
223
152
  end
224
153
 
225
154
  # hide away our own shit to minimize pollution
226
155
  #
227
- module TagzConstants
156
+ module Namespace
228
157
  class Document < ::String
229
158
  def Document.for other
230
159
  Document === other ? other : Document.new(other.to_s)
231
160
  end
232
161
 
233
162
  def element
234
- Element.new(*a, &b)
163
+ Tagz.element.new(*a, &b)
235
164
  end
236
165
  alias_method 'e', 'element'
237
166
 
@@ -253,7 +182,7 @@ unless defined? Tagz
253
182
  #alias_method 'concat', '<<'
254
183
 
255
184
  def escape(*strings)
256
- XChar.escape(strings.join)
185
+ Tagz.xchar.escape(strings.join)
257
186
  end
258
187
  alias_method 'h', 'escape'
259
188
 
@@ -278,6 +207,7 @@ unless defined? Tagz
278
207
  self
279
208
  end
280
209
  end
210
+ Tagz.singleton_class{ define_method(:document){ Document } }
281
211
 
282
212
  class Element < ::String
283
213
  def Element.attributes options
@@ -327,6 +257,7 @@ unless defined? Tagz
327
257
  end
328
258
  end
329
259
  end
260
+ Tagz.singleton_class{ define_method(:element){ Element } }
330
261
 
331
262
  module XChar
332
263
  # http://intertwingly.net/stories/2004/04/14/i18n.html#CleaningWindows
@@ -385,19 +316,98 @@ unless defined? Tagz
385
316
  ))
386
317
  end
387
318
  end
319
+ Tagz.singleton_class{ define_method(:xchar){ XChar } }
320
+
321
+ NoEscapeProc = lambda{|*values| values.join}
322
+ Tagz.singleton_class{ define_method(:no_escape_proc){ NoEscapeProc } }
323
+
324
+ EscapeProc = lambda{|*values| Tagz.xchar.escape(values.join)}
325
+ Tagz.singleton_class{ define_method(:escape_proc){ EscapeProc } }
326
+
327
+ module Globally; include Tagz; end
328
+ Tagz.singleton_class{ define_method(:globally){ Globally } }
388
329
 
389
- NoEscape = lambda{|*values| values.join}
390
- Escape = lambda{|*values| XChar.escape(values.join)}
330
+ module Privately; include Tagz; end
331
+ Tagz.singleton_class{ define_method(:privately){ Privately } }
332
+ end
333
+
334
+ remove_const(:Namespace)
391
335
 
392
- module Globally; include ::Tagz; end
393
- module Privately; include ::Tagz; end
336
+ # escape utils
337
+ #
338
+ def Tagz.escapeHTML(*strings)
339
+ Tagz.xchar.escape(strings.join)
340
+ end
341
+ def Tagz.escape(*strings)
342
+ Tagz.xchar.escape(strings.join)
394
343
  end
395
344
 
396
- # const_missing support
345
+ # support for configuring attribute escaping
397
346
  #
398
- def Tagz.const_missing(const)
399
- super unless TagzConstants.const_defined?(const)
400
- TagzConstants.const_get(const)
347
+ def Tagz.escape_attribute!(*args, &block)
348
+ previous = @escape_attribute if defined?(@escape_attribute)
349
+ unless args.empty? and block.nil?
350
+ value = block ? block : args.shift
351
+ value = Tagz.escape_proc if value==true
352
+ value = Tagz.no_escape_proc if(value==false or value==nil)
353
+ @escape_attribute = value.to_proc
354
+ return previous
355
+ end
356
+ @escape_attribute
357
+ end
358
+ def Tagz.escape_attributes!(*args, &block)
359
+ Tagz.escape_attribute!(*args, &block)
360
+ end
361
+ def Tagz.escape_attribute(value)
362
+ @escape_attribute.call(value.to_s)
363
+ end
364
+
365
+ # support for configuring content escaping
366
+ #
367
+ def Tagz.escape_content!(*args, &block)
368
+ previous = @escape_content if defined?(@escape_content)
369
+ unless args.empty? and block.nil?
370
+ value = block ? block : args.shift
371
+ value = Tagz.escape_proc if value==true
372
+ value = Tagz.no_escape_proc if(value==false or value==nil)
373
+ @escape_content = value.to_proc
374
+ return previous
375
+ end
376
+ @escape_content
377
+ end
378
+ def Tagz.escape_contents!(*args, &block)
379
+ Tagz.escape_content!(*args, &block)
380
+ end
381
+ def Tagz.escape_content(value)
382
+ @escape_content.call(value.to_s)
383
+ end
384
+
385
+ # configure tagz escaping
386
+ #
387
+ def Tagz.escape!(options = {})
388
+ options = {:attributes => options, :content => options} unless options.is_a?(Hash)
389
+ escape_attributes = options[:attributes]||options['attributes']
390
+ escape_content = options[:content]||options['content']
391
+ Tagz.escape_attributes!(!!escape_attributes)
392
+ Tagz.escape_content!(!!escape_content)
393
+ end
394
+ def Tagz.i_know_what_the_hell_i_am_doing!
395
+ escape!(false)
396
+ end
397
+ def Tagz.i_do_not_know_what_the_hell_i_am_doing!
398
+ escape!(true)
399
+ end
400
+ def Tagz.xml_mode!
401
+ Tagz.escape!(
402
+ :attributes => true,
403
+ :content => true
404
+ )
405
+ end
406
+ def Tagz.html_mode!
407
+ Tagz.escape!(
408
+ :attributes => true,
409
+ :content => false
410
+ )
401
411
  end
402
412
 
403
413
  # allow access to instance methods via module handle
data/tagz.gemspec CHANGED
@@ -3,7 +3,7 @@
3
3
 
4
4
  Gem::Specification::new do |spec|
5
5
  spec.name = "tagz"
6
- spec.version = "7.0.0"
6
+ spec.version = "7.1.0"
7
7
  spec.platform = Gem::Platform::RUBY
8
8
  spec.summary = "tagz"
9
9
 
data/test/tagz.rb CHANGED
@@ -674,7 +674,7 @@ class TagzTest < Test::Unit::TestCase
674
674
  before = constants
675
675
  include Tagz
676
676
  after = constants
677
- expected = %w[ TagzConstants ]
677
+ expected = []
678
678
  actual = after - before
679
679
  end
680
680
  assert_equal expected, actual
@@ -686,7 +686,7 @@ class TagzTest < Test::Unit::TestCase
686
686
  before = constants
687
687
  include Tagz.globally
688
688
  after = constants
689
- expected = %w[ TagzConstants ]
689
+ expected = []
690
690
  actual = after - before
691
691
  end
692
692
  assert_equal expected, actual
@@ -698,9 +698,25 @@ class TagzTest < Test::Unit::TestCase
698
698
  before = constants
699
699
  include Tagz.privately
700
700
  after = constants
701
- expected = %w[ TagzConstants ]
701
+ expected = []
702
702
  actual = after - before
703
703
  end
704
704
  assert_equal expected, actual
705
705
  end
706
+
707
+ def test_530
708
+ assert_nothing_raised{
709
+ code = <<-__
710
+ class C
711
+ Element=NoEscape=Document=XChar=Privately=Escape=Globally=42
712
+ include Tagz.globally
713
+ def a() tagz{ 42 } end
714
+ end
715
+ C.new.a()
716
+ __
717
+ assert_nothing_raised do
718
+ assert eval(code), '42'
719
+ end
720
+ }
721
+ end
706
722
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tagz
3
3
  version: !ruby/object:Gem::Version
4
- version: 7.0.0
4
+ version: 7.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ara T. Howard
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-07-26 00:00:00 -06:00
12
+ date: 2009-07-28 00:00:00 -06:00
13
13
  default_executable:
14
14
  dependencies: []
15
15