tagz 8.2.0 → 9.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/lib/tagz.rb +84 -55
  2. data/tagz.gemspec +1 -1
  3. data/test/tagz_test.rb +23 -34
  4. metadata +4 -4
@@ -4,7 +4,7 @@ unless defined? Tagz
4
4
  #
5
5
  module Tagz
6
6
  def Tagz.version()
7
- '8.2.0'
7
+ '9.0.0'
8
8
  end
9
9
 
10
10
  def Tagz.description
@@ -55,8 +55,8 @@ unless defined? Tagz
55
55
  unless options.empty?
56
56
  attributes = ' ' <<
57
57
  options.map do |key, value|
58
- key = Tagz.escape_attribute(key)
59
- value = Tagz.escape_attribute(value)
58
+ key = Tagz.escape_key(key)
59
+ value = Tagz.escape_value(value)
60
60
  if value =~ %r/"/
61
61
  raise ArgumentError, value if value =~ %r/'/
62
62
  value = "'#{ value }'"
@@ -125,7 +125,7 @@ unless defined? Tagz
125
125
  nil
126
126
  end
127
127
 
128
- if(strategy.nil? or (tagz.nil? and not Tagz.globally===self))
128
+ if(strategy.nil? or (tagz.nil? and Tagz.privately===self))
129
129
  begin
130
130
  super
131
131
  ensure
@@ -190,7 +190,20 @@ unless defined? Tagz
190
190
  }
191
191
 
192
192
  module HtmlSafe
193
- attr_accessor :html_safe
193
+ def html_safe() @html_safe ||= true end
194
+ def html_safe?() html_safe end
195
+ def html_safe=(value) @html_safe = !!value end
196
+ end
197
+
198
+ def Tagz.html_safe(*args, &block)
199
+ if args.empty? and block.nil?
200
+ Tagz.namespace(:HtmlSafe)
201
+ else
202
+ string = args.join
203
+ string += block.call.to_s if block
204
+ string.extend(HtmlSafe)
205
+ string
206
+ end
194
207
  end
195
208
 
196
209
  class Document < ::String
@@ -223,7 +236,6 @@ unless defined? Tagz
223
236
  def concat(string)
224
237
  self << string
225
238
  end
226
- #alias_method 'concat', '<<'
227
239
 
228
240
  def escape(string)
229
241
  return string if string.respond_to?(:html_safe)
@@ -271,8 +283,8 @@ unless defined? Tagz
271
283
  unless options.empty?
272
284
  ' ' <<
273
285
  options.map do |key, value|
274
- key = Tagz.escape_attribute(key)
275
- value = Tagz.escape_attribute(value)
286
+ key = Tagz.escape_key(key)
287
+ value = Tagz.escape_value(value)
276
288
  if value =~ %r/"/
277
289
  raise ArgumentError, value if value =~ %r/'/
278
290
  value = "'#{ value }'"
@@ -376,11 +388,20 @@ unless defined? Tagz
376
388
  end
377
389
  Tagz.singleton_class{ define_method(:xchar){ Tagz.namespace(:XChar) } }
378
390
 
379
- NoEscapeProc = lambda{|*values| values.join}
380
- Tagz.singleton_class{ define_method(:no_escape_proc){ Tagz.namespace(:NoEscapeProc) } }
391
+ NoEscapeContentProc = lambda{|*contents| contents.join}
392
+ Tagz.singleton_class{ define_method(:no_escape_content_proc){ Tagz.namespace(:NoEscapeContentProc) } }
393
+ EscapeContentProc = lambda{|*contents| Tagz.xchar.escape(contents.join)}
394
+ Tagz.singleton_class{ define_method(:escape_content_proc){ Tagz.namespace(:EscapeContentProc) } }
381
395
 
382
- EscapeProc = lambda{|*values| Tagz.xchar.escape(values.join)}
383
- Tagz.singleton_class{ define_method(:escape_proc){ Tagz.namespace(:EscapeProc) } }
396
+ NoEscapeKeyProc = lambda{|*values| values.join}
397
+ Tagz.singleton_class{ define_method(:no_escape_key_proc){ Tagz.namespace(:NoEscapeKeyProc) } }
398
+ EscapeKeyProc = lambda{|*values| Tagz.xchar.escape(values.join).gsub(/_/, '-')}
399
+ Tagz.singleton_class{ define_method(:escape_key_proc){ Tagz.namespace(:EscapeKeyProc) } }
400
+
401
+ NoEscapeValueProc = lambda{|*values| values.join}
402
+ Tagz.singleton_class{ define_method(:no_escape_value_proc){ Tagz.namespace(:NoEscapeValueProc) } }
403
+ EscapeValueProc = lambda{|*values| Tagz.xchar.escape(values.join)}
404
+ Tagz.singleton_class{ define_method(:escape_value_proc){ Tagz.namespace(:EscapeValueProc) } }
384
405
 
385
406
  module Globally; include Tagz; end
386
407
  Tagz.singleton_class{ define_method(:globally){ Tagz.namespace(:Globally) } }
@@ -399,54 +420,60 @@ unless defined? Tagz
399
420
  Tagz.xchar.escape(strings.join)
400
421
  end
401
422
 
402
- # support for configuring attribute escaping
423
+ # raw utils
403
424
  #
404
- def Tagz.escape_attribute!(*args, &block)
405
- previous = @escape_attribute if defined?(@escape_attribute)
406
- unless args.empty? and block.nil?
407
- value = block ? block : args.shift
408
- value = Tagz.escape_proc if value==true
409
- value = Tagz.no_escape_proc if(value==false or value==nil)
410
- @escape_attribute = value.to_proc
411
- return previous
412
- end
413
- @escape_attribute
414
- end
415
- def Tagz.escape_attributes!(*args, &block)
416
- Tagz.escape_attribute!(*args, &block)
417
- end
418
- def Tagz.escape_attribute(value)
419
- @escape_attribute.call(value.to_s)
425
+ def Tagz.raw(*args, &block)
426
+ Tagz.html_safe(*args, &block)
420
427
  end
421
428
 
422
- # support for configuring content escaping
429
+ # generate code for escape configuration
423
430
  #
424
- def Tagz.escape_content!(*args, &block)
425
- previous = @escape_content if defined?(@escape_content)
426
- unless args.empty? and block.nil?
427
- value = block ? block : args.shift
428
- value = Tagz.escape_proc if value==true
429
- value = Tagz.no_escape_proc if(value==false or value==nil)
430
- @escape_content = value.to_proc
431
- return previous
432
- end
433
- @escape_content
434
- end
435
- def Tagz.escape_contents!(*args, &block)
436
- Tagz.escape_content!(*args, &block)
437
- end
438
- def Tagz.escape_content(value)
439
- @escape_content.call(value.to_s)
431
+ %w( key value content ).each do |type|
432
+
433
+ module_eval <<-__, __FILE__, __LINE__
434
+ def Tagz.escape_#{ type }!(*args, &block)
435
+ previous = @escape_#{ type } if defined?(@escape_#{ type })
436
+ unless args.empty?
437
+ value = args.shift
438
+ value = Tagz.escape_#{ type }_proc if value==true
439
+ value = Tagz.no_escape_#{ type }_proc if(value==false or value==nil)
440
+ @escape_#{ type } = value.to_proc
441
+ if block
442
+ begin
443
+ return block.call()
444
+ ensure
445
+ @escape_#{ type } = previous
446
+ end
447
+ else
448
+ return previous
449
+ end
450
+ end
451
+ @escape_#{ type }
452
+ end
453
+
454
+ def Tagz.escape_#{ type }s!(*args, &block)
455
+ Tagz.escape_#{ type }!(*args, &block)
456
+ end
457
+
458
+ def Tagz.escape_#{ type }(value)
459
+ @escape_#{ type }.call(value.to_s)
460
+ end
461
+ __
462
+
440
463
  end
441
464
 
442
465
  # configure tagz escaping
443
466
  #
444
467
  def Tagz.escape!(options = {})
445
- options = {:attributes => options, :content => options} unless options.is_a?(Hash)
446
- escape_attributes = options[:attributes]||options['attributes']
447
- escape_content = options[:content]||options['content']
448
- Tagz.escape_attributes!(!!escape_attributes)
449
- Tagz.escape_content!(!!escape_content)
468
+ options = {:keys => options, :values => options, :content => options} unless options.is_a?(Hash)
469
+
470
+ escape_keys = options[:keys]||options['keys']||options[:key]||options['key']
471
+ escape_values = options[:values]||options['values']||options[:value]||options['value']
472
+ escape_contents = options[:contents]||options['contents']||options[:content]||options['content']
473
+
474
+ Tagz.escape_keys!(!!escape_keys)
475
+ Tagz.escape_values!(!!escape_values)
476
+ Tagz.escape_contents!(!!escape_contents)
450
477
  end
451
478
  def Tagz.i_know_what_the_hell_i_am_doing!
452
479
  escape!(false)
@@ -456,13 +483,15 @@ unless defined? Tagz
456
483
  end
457
484
  def Tagz.xml_mode!
458
485
  Tagz.escape!(
459
- :attributes => true,
460
- :content => true
486
+ :keys => true,
487
+ :values => true,
488
+ :contents => true
461
489
  )
462
490
  end
463
491
  def Tagz.html_mode!
464
492
  Tagz.escape!(
465
- :attributes => true,
493
+ :keys => true,
494
+ :values => false,
466
495
  :content => false
467
496
  )
468
497
  end
@@ -476,5 +505,5 @@ unless defined? Tagz
476
505
  (argv.empty? and block.nil?) ? ::Tagz : Tagz.tagz(*argv, &block)
477
506
  end
478
507
 
479
- Tagz.xml_mode!
508
+ Tagz.escape!(true)
480
509
  end
@@ -3,7 +3,7 @@
3
3
 
4
4
  Gem::Specification::new do |spec|
5
5
  spec.name = "tagz"
6
- spec.version = "8.2.0"
6
+ spec.version = "9.0.0"
7
7
  spec.platform = Gem::Platform::RUBY
8
8
  spec.summary = "tagz"
9
9
  spec.description = "\n tagz.rb is generates html, xml, or any sgml variant like a small ninja\n running across the backs of a herd of giraffes swatting of heads like\n a mark-up weedwacker. weighing in at less than 300 lines of code\n tagz.rb adds an html/xml/sgml syntax to ruby that is both unobtrusive,\n safe, and available globally to objects without the need for any\n builder or superfluous objects. tagz.rb is designed for applications\n that generate html to be able to do so easily in any context without\n heavyweight syntax or scoping issues, like a ninja sword through\n butter.\n\n"
@@ -10,8 +10,6 @@ $:.unshift '.'
10
10
 
11
11
  require 'tagz'
12
12
 
13
- Tagz.html_mode!
14
-
15
13
  class TagzTest < Test::Unit::TestCase
16
14
  include Tagz
17
15
 
@@ -476,7 +474,7 @@ class TagzTest < Test::Unit::TestCase
476
474
  end
477
475
 
478
476
  def test_390
479
- expected = '<div class="bar&amp;foo&gt;">foo&bar></div>'
477
+ expected = '<div class="bar&amp;foo&gt;">foo&amp;bar&gt;</div>'
480
478
  actual = tagz{ div_(:class => 'bar&foo>'){ 'foo&bar>' } }
481
479
  assert_equal expected, actual
482
480
 
@@ -486,7 +484,7 @@ class TagzTest < Test::Unit::TestCase
486
484
  end
487
485
 
488
486
  def test_400
489
- expected = '<div><span>foo&bar</span></div>'
487
+ expected = '<div><span>foo&amp;bar</span></div>'
490
488
  actual = tagz{ div_{ span_{ 'foo&bar' } } }
491
489
  assert_equal expected, actual
492
490
  end
@@ -525,7 +523,7 @@ class TagzTest < Test::Unit::TestCase
525
523
 
526
524
  def test_440
527
525
  c = Class.new{
528
- include Tagz
526
+ include Tagz.privately
529
527
  def foobar() tagz{ div_{ 'foobar' } } end
530
528
  def barfoo() div_{ 'barfoo' } end
531
529
  }.new
@@ -565,33 +563,24 @@ class TagzTest < Test::Unit::TestCase
565
563
  expected = %(<div a&gt;b="a&gt;b">content</div>)
566
564
  assert_equal expected, actual
567
565
 
568
- original = Tagz.escape_attribute! false
569
- assert original
570
- actual = nil
571
- assert_nothing_raised{ actual=c.a}
572
- expected = %(<div a>b="a>b">content</div>)
573
- assert_equal expected, actual
574
-
575
- Tagz.escape_attribute! original
576
- actual = nil
577
- assert_nothing_raised{ actual=c.a}
578
- expected = %(<div a&gt;b="a&gt;b">content</div>)
579
- assert_equal expected, actual
580
-
581
- upcased = Tagz.escape_attribute! lambda{|value| original.call(value).upcase}
582
- assert upcased
583
- actual = nil
584
- assert_nothing_raised{ actual=c.a}
585
- expected = %(<div A&GT;B="A&GT;B">content</div>)
586
- assert_equal expected, actual
566
+ Tagz.escape_keys!(false) do
567
+ Tagz.escape_values!(false) do
568
+ actual = nil
569
+ assert_nothing_raised{ actual=c.a}
570
+ expected = %(<div a>b="a>b">content</div>)
571
+ assert_equal expected, actual
572
+ end
573
+ end
587
574
 
588
- Tagz.escape_attributes! lambda{|value| upcased.call(value).downcase}
589
- actual = nil
590
- assert_nothing_raised{ actual=c.a}
591
- expected = %(<div a&gt;b="a&gt;b">content</div>)
592
- assert_equal expected, actual
593
- ensure
594
- Tagz.escape_attributes!(original)
575
+ upcased = lambda{|value| value.to_s.upcase}
576
+ Tagz.escape_key!(upcased) do
577
+ Tagz.escape_value!(upcased) do
578
+ actual = nil
579
+ assert_nothing_raised{ actual=c.a}
580
+ expected = %(<div A>B="A>B">content</div>)
581
+ assert_equal expected, actual
582
+ end
583
+ end
595
584
  end
596
585
 
597
586
  def test_470
@@ -604,7 +593,7 @@ class TagzTest < Test::Unit::TestCase
604
593
 
605
594
  actual = nil
606
595
  assert_nothing_raised{ actual=c.a}
607
- expected = %(<div>a>b</div>)
596
+ expected = %(<div>a&gt;b</div>)
608
597
  assert_equal expected, actual
609
598
 
610
599
  original = Tagz.escape_content!(true)
@@ -618,13 +607,13 @@ class TagzTest < Test::Unit::TestCase
618
607
  assert upcased
619
608
  actual = nil
620
609
  assert_nothing_raised{ actual=c.a}
621
- expected = %(<div>A>B</div>)
610
+ expected = %(<div>A&GT;B</div>)
622
611
  assert_equal expected, actual
623
612
 
624
613
  Tagz.escape_content!(original)
625
614
  actual = nil
626
615
  assert_nothing_raised{ actual=c.a}
627
- expected = %(<div>a>b</div>)
616
+ expected = %(<div>a&gt;b</div>)
628
617
  assert_equal expected, actual
629
618
  ensure
630
619
  Tagz.escape_content!(original)
metadata CHANGED
@@ -4,10 +4,10 @@ version: !ruby/object:Gem::Version
4
4
  hash: 87
5
5
  prerelease:
6
6
  segments:
7
- - 8
8
- - 2
7
+ - 9
9
8
  - 0
10
- version: 8.2.0
9
+ - 0
10
+ version: 9.0.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ara T. Howard
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-02-18 00:00:00 -07:00
18
+ date: 2011-03-01 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies: []
21
21