tagz 0.0.2 → 0.0.4

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 (5) hide show
  1. data/lib/tagz.rb +99 -115
  2. data/test/tagz.rb +5 -6
  3. metadata +41 -36
  4. data/a.rb +0 -12
  5. data/tidy +0 -3
data/lib/tagz.rb CHANGED
@@ -1,20 +1,19 @@
1
1
  module Tagz
2
- Tagz::VERSION = '0.0.1' unless defined? Tagz::VERSION
2
+ Tagz::VERSION = '0.0.4' unless defined? Tagz::VERSION
3
3
  def self.version() VERSION end
4
4
 
5
5
  class NotOpen < StandardError; end
6
6
  class StillOpen < StandardError; end
7
7
 
8
8
  class << self
9
- #--{{{
10
9
  def default_non_container_tag_list
10
+ # http://www.w3schools.com/xhtml/xhtml_ref_byfunc.asp
11
11
  %w[
12
12
  br hr input img area base basefont
13
13
  ]
14
14
  end
15
15
 
16
16
  def non_container_tag_list *argv
17
- #--{{{
18
17
  if argv.first
19
18
  @non_container_tag_list = [argv.first].flatten.uniq.map{|arg| arg.to_s}
20
19
  @non_container_tags = @non_container_tag_list.inject(Hash.new){|h,k| h.update k => true}
@@ -27,35 +26,34 @@ module Tagz
27
26
  @non_container_tag_list
28
27
  end
29
28
  end
30
- #--}}}
31
29
  end
30
+
32
31
  def non_container_tags
33
- #--{{{
34
32
  if defined?(@non_container_tags) and @non_container_tags
35
33
  @non_container_tags
36
34
  else
37
35
  non_container_tag_list
38
36
  @non_container_tags
39
37
  end
40
- #--}}}
41
38
  end
39
+
42
40
  def container? tag
43
41
  not(non_container_tags[tag.to_s] or false)
44
42
  end
43
+
45
44
  def emtpy? tag
46
45
  not container?(tag)
47
46
  end
48
- #--}}}
49
47
  end
50
48
 
51
- module Fragment #--{{{
49
+ module Fragment
52
50
  attr_accessor 'tag'
53
51
  attr_accessor 'open'
54
52
  attr_accessor 'added'
55
53
  attr_accessor 'created_at'
56
54
  alias_method 'open?', 'open'
57
55
 
58
- def self.new str='', tag='', options = {} #--{{{
56
+ def self.new str='', tag='', options = {}
59
57
  return str if Fragment === str
60
58
  str.extend Fragment
61
59
  str.tag = tag.to_s
@@ -65,13 +63,7 @@ module Tagz
65
63
  options.each{|k,v| str.send "#{ k }=", v}
66
64
  str.created_at = caller
67
65
  str
68
- end #--}}}
69
-
70
- =begin
71
- def tag
72
- stack.last
73
66
  end
74
- =end
75
67
 
76
68
  def stack
77
69
  @stack ||= []
@@ -87,31 +79,86 @@ module Tagz
87
79
  stack.pop
88
80
  end
89
81
 
90
- def closes? other #--{{{
82
+ def closes? other
91
83
  tag.to_s == other.tag.to_s
92
- end #--}}}
84
+ end
93
85
 
94
- def << content #--{{{
86
+ def << content
95
87
  super content.to_s
96
- end #--}}}
88
+ end
97
89
 
98
- def add fragment #--{{{
90
+ def add fragment
99
91
  push fragment
100
92
  oid = fragment.object_id
101
93
  self << fragment unless added[oid]
102
94
  self
103
95
  ensure
104
96
  added[oid] = true
105
- end #--}}}
106
- end #--}}}
97
+ end
98
+ end
99
+
107
100
 
101
+ module Abilities
102
+ private
103
+ def tagz *argv, &block
104
+ stack = __tag_stack__
105
+ top = stack.last
106
+ size = stack.size
108
107
 
109
- module Abilities #--{{{
110
- def __tag_stack__ #--{{{
108
+ unless block
109
+ top ||= Fragment()
110
+ if argv.empty?
111
+ top
112
+ else
113
+ string = argv.join
114
+ top.add string
115
+ string
116
+ end
117
+ else
118
+ obj = Tagz === self ? self : clone.instance_eval{ extend Tagz; self }
119
+ stack << (top=Fragment())
120
+ top = stack.last
121
+ tid = top.object_id
122
+
123
+ string = obj.instance_eval(&block)
124
+
125
+ until stack.last.object_id == tid
126
+ pop = stack.pop
127
+ last = stack.last
128
+ last.add pop
129
+ end
130
+
131
+ top = stack.pop
132
+
133
+ content_was_added = top.size > 0
134
+ top.add string unless content_was_added
135
+
136
+ top
137
+ end
138
+ end
139
+
140
+ def method_missing m, *a, &b
141
+ case m.to_s
142
+ when %r/^(.*[^_])_(!)?$/o
143
+ m, bang = $1, $2
144
+ unless bang
145
+ __tag_start__ m, *a, &b
146
+ else
147
+ __tag_start__(m, *a){}
148
+ end
149
+ when %r/^_([^_].*)$/o
150
+ m = $1
151
+ __tag_stop__ m, *a, &b
152
+ else
153
+ super
154
+ end
155
+ end
156
+
157
+ def __tag_stack__
111
158
  @__tag_stack__ ||= []
112
- end #--}}}
159
+ end
113
160
 
114
- def __tag_attrs__ attrs = {} #--{{{
161
+ def __tag_attrs__ attrs = {}
115
162
  return nil if attrs.empty?
116
163
  ( 42 - 10 ).chr <<
117
164
  case attrs
@@ -120,9 +167,9 @@ module Tagz
120
167
  else
121
168
  attrs.to_s
122
169
  end
123
- end #--}}}
170
+ end
124
171
 
125
- def __tag_start__ *argv, &block #--{{{
172
+ def __tag_start__ *argv, &block
126
173
  tag = argv.shift
127
174
 
128
175
  case argv.size
@@ -235,9 +282,9 @@ module Tagz
235
282
 
236
283
  content
237
284
  end
238
- end #--}}}
285
+ end
239
286
 
240
- def __tag_stop__ tag #--{{{
287
+ def __tag_stop__ tag
241
288
  stack = __tag_stack__
242
289
  top = stack.last
243
290
  stop = Fragment( "</#{ tag }>", tag, :open => false )
@@ -255,68 +302,10 @@ module Tagz
255
302
  else
256
303
  raise NotOpen, tag.to_s
257
304
  end
258
- end #--}}}
259
-
260
- def method_missing m, *a, &b #--{{{
261
- case m.to_s
262
- when %r/^(.*[^_])_(!)?$/o
263
- m, bang = $1, $2
264
- unless bang
265
- __tag_start__ m, *a, &b
266
- else
267
- __tag_start__(m, *a){}
268
- end
269
- when %r/^_([^_].*)$/o
270
- m = $1
271
- __tag_stop__ m, *a, &b
272
- else
273
- super
274
- end
275
- end #--}}}
276
-
277
- def tagz *argv, &block #--{{{
278
- stack = __tag_stack__
279
- top = stack.last
280
- size = stack.size
281
-
282
- unless block
283
- top ||= Fragment()
284
- if argv.empty?
285
- top
286
- else
287
- string = argv.join
288
- top.add string
289
- string
290
- end
291
- else
292
- obj = Tagz === self ? self : clone.instance_eval{ extend Tagz; self }
293
- stack << (top=Fragment())
294
- top = stack.last
295
- tid = top.object_id
296
-
297
- string = obj.instance_eval(&block)
298
-
299
- until stack.last.object_id == tid
300
- pop = stack.pop
301
- last = stack.last
302
- last.add pop
303
- end
304
-
305
- top = stack.pop
306
-
307
- content_was_added = top.size > 0
308
- top.add string unless content_was_added
309
-
310
- top
311
- end
312
- end #--}}}
313
- alias_method "markup", "tagz"
314
- alias_method "__", "tagz"
315
- alias_method "_", "tagz"
305
+ end
316
306
 
317
307
  def Fragment(*a, &b) Fragment.new(*a, &b) end
318
308
 
319
- ### TODO - i'd like to use 'tag' for these but action view has eaten it!
320
309
  def element tag, *a, &b
321
310
  __tag_stack__ << (top=Fragment())
322
311
  __tag_start__ tag, *a, &b
@@ -354,36 +343,15 @@ module Tagz
354
343
  'xml:lang' => 'en'
355
344
  decl << html_(attrs, &b)
356
345
  end
357
- end #--}}}
358
-
359
-
360
- ### OLD CRAP
361
- =begin
362
- def concat s #--{{{
363
- tagz << s.to_s
364
- self
365
- end #--}}}
366
- def tag *a, &b #--{{{
367
- __tag_start__ *a, &b
368
- end #--}}}
369
- def tag! *a, &b #--{{{
370
- __tag_start__(*a){}
371
- end #--}}}
372
- def tag_ *a, &b #--{{{
373
- __tag_start__ *a, &b
374
- end #--}}}
375
- def _tag *a, &b #--{{{
376
- __tag_stop__ *a, &b
377
- end #--}}}
378
- =end
346
+ end
379
347
 
380
- def self.included other #--{{{
348
+ def self.included other
381
349
  other.module_eval{ include Abilities }
382
- end #--}}}
350
+ end
383
351
 
384
- def self.extend_object other #--{{{
352
+ def self.extend_object other
385
353
  other.extend Abilities
386
- end #--}}}
354
+ end
387
355
 
388
356
  class ::String
389
357
  include Tagz
@@ -416,7 +384,23 @@ module Tagz
416
384
  tagz{ __tag_start__(self, *a){} }
417
385
  end
418
386
  end
387
+ end
419
388
 
389
+ class Object
390
+ def Tagz &block
391
+ const = Object.const_get :Tagz
392
+ if block
393
+ if const === self
394
+ this = self
395
+ else
396
+ this = eval('self', block).dup
397
+ this.send :extend, Tagz
398
+ end
399
+ this.send :tagz, &block
400
+ else
401
+ const
402
+ end
403
+ end
420
404
  end
421
405
 
422
406
  require File.join(File.dirname(__FILE__), 'tagz', 'rails') if defined? Rails
data/test/tagz.rb CHANGED
@@ -301,7 +301,7 @@ class TagzTest < Test::Unit::TestCase
301
301
  html_{
302
302
  body_{
303
303
  div_ :k => :v
304
- _ "content"
304
+ tagz << "content"
305
305
  _div
306
306
  }
307
307
  }
@@ -331,16 +331,15 @@ class TagzTest < Test::Unit::TestCase
331
331
  def test_280 #--{{{
332
332
  expected = 'content'
333
333
  actual = (
334
- __ "content"
334
+ tagz << "content"
335
335
  )
336
336
  assert_equal expected, actual
337
337
  end #--}}}
338
338
  def test_290 #--{{{
339
339
  expected = 'foobar'
340
340
  actual = (
341
- _{
342
- __ 'foo'
343
- __ 'bar'
341
+ tagz {
342
+ tagz << 'foo' << 'bar'
344
343
  }
345
344
  )
346
345
  assert_equal expected, actual
@@ -348,7 +347,7 @@ class TagzTest < Test::Unit::TestCase
348
347
  def test_300 #--{{{
349
348
  expected = 'foobar'
350
349
  actual = (
351
- _{ __ 'foo', 'bar' }
350
+ tagz{ tagz 'foo', 'bar' }
352
351
  )
353
352
  assert_equal expected, actual
354
353
  end #--}}}
metadata CHANGED
@@ -1,35 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.9.2
3
- specification_version: 1
4
2
  name: tagz
5
3
  version: !ruby/object:Gem::Version
6
- version: 0.0.2
7
- date: 2007-07-18 00:00:00 -06:00
8
- summary: tagz
9
- require_paths:
10
- - lib
11
- email: ara.t.howard@noaa.gov
12
- homepage: http://codeforpeople.com/lib/ruby/tagz/
13
- rubyforge_project:
14
- description:
15
- autorequire: tagz
16
- default_executable:
17
- bindir: bin
18
- has_rdoc: false
19
- required_ruby_version: !ruby/object:Gem::Version::Requirement
20
- requirements:
21
- - - ">"
22
- - !ruby/object:Gem::Version
23
- version: 0.0.0
24
- version:
4
+ version: 0.0.4
25
5
  platform: ruby
26
- signing_key:
27
- cert_chain:
28
- post_install_message:
29
6
  authors:
30
7
  - Ara T. Howard
8
+ autorequire: tagz
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-02-14 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: ara.t.howard@noaa.gov
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
31
24
  files:
32
- - a.rb
33
25
  - gemspec.rb
34
26
  - gen_readme.rb
35
27
  - install.rb
@@ -144,18 +136,31 @@ files:
144
136
  - sample/a.rb
145
137
  - test
146
138
  - test/tagz.rb
147
- - tidy
148
- test_files:
149
- - test/tagz.rb
139
+ has_rdoc: false
140
+ homepage: http://codeforpeople.com/lib/ruby/tagz/
141
+ post_install_message:
150
142
  rdoc_options: []
151
143
 
152
- extra_rdoc_files: []
153
-
154
- executables: []
155
-
156
- extensions: []
157
-
144
+ require_paths:
145
+ - lib
146
+ required_ruby_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: "0"
151
+ version:
152
+ required_rubygems_version: !ruby/object:Gem::Requirement
153
+ requirements:
154
+ - - ">="
155
+ - !ruby/object:Gem::Version
156
+ version: "0"
157
+ version:
158
158
  requirements: []
159
159
 
160
- dependencies: []
161
-
160
+ rubyforge_project:
161
+ rubygems_version: 1.0.1
162
+ signing_key:
163
+ specification_version: 2
164
+ summary: tagz
165
+ test_files:
166
+ - test/tagz.rb
data/a.rb DELETED
@@ -1,12 +0,0 @@
1
- require './lib/tagz'
2
- include Tagz
3
-
4
- puts tagz {
5
- a_ 'a'
6
- b_ 'b'
7
- c_ 'c'
8
- foo_{}
9
- div_{}
10
- }
11
-
12
-
data/tidy DELETED
@@ -1,3 +0,0 @@
1
- require 'rubygems'
2
- require 'hpricot'
3
- puts Hpricot(ARGF)