tagz 1.0.0 → 4.2.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/tagz.rb CHANGED
@@ -5,8 +5,8 @@ unless defined? Tagz
5
5
  module Tagz
6
6
  unless defined?(Tagz::VERSION)
7
7
  Tagz::VERSION = [
8
- Tagz::VERSION_MAJOR = 1,
9
- Tagz::VERSION_MINOR = 0,
8
+ Tagz::VERSION_MAJOR = 4,
9
+ Tagz::VERSION_MINOR = 2,
10
10
  Tagz::VERSION_TEENY = 0
11
11
  ].join('.')
12
12
  def Tagz.version() Tagz::VERSION end
@@ -83,27 +83,20 @@ unless defined? Tagz
83
83
  self
84
84
  end
85
85
 
86
- private
87
-
88
- def tagz &block
89
- if block
90
- if not defined?(@tagz) or @tagz.nil?
91
- @tagz = Fragment.new
92
- top = true
93
- end
94
- begin
95
- size = @tagz.size
96
- value = instance_eval(&block)
97
- @tagz << value unless(Fragment === value or @tagz.size > size)
98
- @tagz
99
- ensure
100
- @tagz = nil if top
101
- end
102
- else
103
- @tagz ||= Fragment.new
86
+ module Globally
87
+ private
88
+ include Tagz
89
+ def method_missing m, *a, &b
90
+ tagz{ super }
104
91
  end
105
92
  end
106
93
 
94
+ def Tagz.globally
95
+ Globally
96
+ end
97
+
98
+ private
99
+
107
100
  def tagz__ name, *argv, &block
108
101
  options = {}
109
102
  content = []
@@ -165,25 +158,54 @@ unless defined? Tagz
165
158
  tagz << "</#{ tag }>"
166
159
  end
167
160
 
161
+ def tagz &block
162
+ if block
163
+ if not defined?(@tagz) or @tagz.nil?
164
+ @tagz = Fragment.new
165
+ top = true
166
+ end
167
+ begin
168
+ size = @tagz.size
169
+ value = instance_eval(&block)
170
+ @tagz << value unless(Fragment === value or @tagz.size > size)
171
+ @tagz.to_s
172
+ ensure
173
+ @tagz = nil if top
174
+ end
175
+ else
176
+ @tagz if defined? @tagz
177
+ end
178
+ end
179
+
168
180
  def method_missing m, *a, &b
169
- return super unless @tagz
181
+ if not Globally === self
182
+ unless defined?(@tagz) and @tagz
183
+ begin
184
+ super
185
+ ensure
186
+ $!.set_backtrace caller(skip=1) if $!
187
+ end
188
+ end
189
+ end
190
+
170
191
  case m.to_s
171
192
  when %r/^(.*[^_])_(!)?$/o
172
193
  m, bang = $1, $2
173
- unless bang
174
- tagz__(m, *a, &b)
175
- else
176
- tagz__(m, *a){}
177
- end
194
+ b ||= lambda{} if bang
195
+ tagz{ tagz__(m, *a, &b) }
178
196
  when %r/^_([^_].*)$/o
179
197
  m = $1
180
- __tagz(m, *a, &b)
198
+ tagz{ __tagz(m, *a, &b) }
181
199
  when 'e'
182
200
  Element.new(*a, &b)
183
201
  when '__'
184
- tagz << "\n"
202
+ tagz{ tagz << "\n" }
185
203
  else
186
- super
204
+ begin
205
+ super
206
+ ensure
207
+ $!.set_backtrace caller(skip=1) if $!
208
+ end
187
209
  end
188
210
  end
189
211
 
data/samples/a.rb ADDED
@@ -0,0 +1,15 @@
1
+ #
2
+ # in the simplest case tagz generates html using a syntax which safely mixes
3
+ # in to any object
4
+ #
5
+
6
+ require 'tagz'
7
+ include Tagz.globally
8
+
9
+ class GiraffeModel
10
+ def link
11
+ a_(:href => "/giraffe/neck/42"){ "whack!" }
12
+ end
13
+ end
14
+
15
+ puts GiraffeModel.new.link
data/samples/b.rb ADDED
@@ -0,0 +1,41 @@
1
+ #
2
+ # tagz.rb mixes quite easily with your favourite templating engine, avoiding
3
+ # the need for '<% rows.each do |row| %> ... <% row.each do |cell| %> '
4
+ # madness and other types of logic to be coded in the templating language,
5
+ # leaving templating to template engines and logic and looping to ruby -
6
+ # unencumbered by extra funky syntax
7
+ #
8
+
9
+ require 'tagz'
10
+ include Tagz.globally
11
+
12
+ require 'erb'
13
+
14
+ rows = %w( a b c ), %w( 1 2 3 )
15
+
16
+ template = ERB.new <<-ERB
17
+ <html>
18
+ <body>
19
+ <%=
20
+
21
+ if rows
22
+
23
+ table_{
24
+ rows.each do |row|
25
+ tr_{
26
+ row.each do |cell|
27
+ td_{ cell }
28
+ end
29
+ }
30
+ end
31
+ }
32
+
33
+ end
34
+
35
+ %>
36
+ </body>
37
+ </html>
38
+ ERB
39
+
40
+ puts template.result(binding)
41
+
data/samples/c.rb ADDED
@@ -0,0 +1,15 @@
1
+ #
2
+ # once you've learned to generate html using tagz you're primed to generate
3
+ # xml too
4
+ #
5
+
6
+ require 'tagz'
7
+ include Tagz.globally
8
+
9
+ doc =
10
+ xml_{
11
+ giraffe_{ 'large' }
12
+ ninja_{ 'small' }
13
+ }
14
+
15
+ puts doc
data/samples/d.rb ADDED
@@ -0,0 +1,28 @@
1
+ #
2
+ # tagz.rb doesn't cramp your style, allowing even invalid html to be
3
+ # generated. note the use of the 'tagz' method, which can be used both to
4
+ # capture output and to append content to the top of the stack.
5
+ #
6
+
7
+ require 'tagz'
8
+ include Tagz.globally
9
+
10
+ def header
11
+ tagz{
12
+ html_
13
+ body_(:class => 'ninja-like', :id => 'giraffe-slayer')
14
+
15
+ tagz << "\n<!-- this is the header -->\n"
16
+ }
17
+ end
18
+
19
+ def footer
20
+ tagz{
21
+ tagz << "\n<!-- this is the footer -->\n"
22
+
23
+ body_
24
+ html_
25
+ }
26
+ end
27
+
28
+ puts header, footer
data/samples/e.rb ADDED
@@ -0,0 +1,21 @@
1
+ #
2
+ # tagz.rb allows a safer method of mixin which requires any tagz methods to be
3
+ # insider a tagz block - tagz generating methods outside a tagz block with
4
+ # raise an error if tagz is included this way. also notice that the error is
5
+ # reported from where it was raised - not from the bowels of the the tagz.rb
6
+ # lib.
7
+ #
8
+
9
+ require 'tagz'
10
+ include Tagz
11
+
12
+ puts tagz{
13
+ html_{ 'works only in here' }
14
+ }
15
+
16
+ begin
17
+ html_{ 'not out here' }
18
+ rescue Object => e
19
+ p :backtrace => e.backtrace
20
+ end
21
+
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: 1.0.0
4
+ version: 4.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ara T. Howard
@@ -9,12 +9,12 @@ autorequire: tagz
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-02-14 00:00:00 -07:00
12
+ date: 2008-05-08 00:00:00 -06:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
16
16
  description:
17
- email: ara.t.howard@noaa.gov
17
+ email: ara.t.howard@gmail.com
18
18
  executables: []
19
19
 
20
20
  extensions: []
@@ -22,17 +22,19 @@ extensions: []
22
22
  extra_rdoc_files: []
23
23
 
24
24
  files:
25
+ - a.rb
25
26
  - gemspec.rb
26
27
  - gen_readme.rb
27
28
  - install.rb
28
29
  - lib
29
30
  - lib/tagz.rb
30
31
  - README
31
- - README.tmpl
32
- - sample
33
- - sample/a.rb
34
- - test
35
- - test/tagz.rb
32
+ - samples
33
+ - samples/a.rb
34
+ - samples/b.rb
35
+ - samples/c.rb
36
+ - samples/d.rb
37
+ - samples/e.rb
36
38
  has_rdoc: false
37
39
  homepage: http://codeforpeople.com/lib/ruby/tagz/
38
40
  post_install_message:
@@ -59,5 +61,5 @@ rubygems_version: 1.0.1
59
61
  signing_key:
60
62
  specification_version: 2
61
63
  summary: tagz
62
- test_files:
63
- - test/tagz.rb
64
+ test_files: []
65
+
data/README.tmpl DELETED
@@ -1,240 +0,0 @@
1
- NAME
2
- tagz.rb
3
-
4
- SYNOPSIS
5
- object mixin for squeaky clean and super speedy generation of any sgml
6
- (html/xml) tags using a private dsl
7
-
8
- + module level interface
9
-
10
- require 'tagz'
11
-
12
- html =
13
- Tagz{
14
- html_{
15
- body_(:class => 'container'){
16
- div_{ 'content' }
17
- }
18
- }
19
- }
20
-
21
- + private mixin interface
22
-
23
- require 'tagz'
24
-
25
- class Table < ::Array
26
- include Tagz
27
-
28
- def initialize width, height
29
- @width, @height = width, height
30
- end
31
-
32
- def to_row
33
- Tagz{
34
- table_(:width => @width, :height => @height){
35
- each do |row|
36
- tr_{
37
- row.each{|cell| td_{ cell }}
38
- }
39
- end
40
- }
41
- }
42
- end
43
- end
44
-
45
- DESCRIPTION
46
- tagz.rb offers a mixin module which adds four private methods to the calling
47
- object:
48
-
49
- tagz
50
- tagz__
51
- __tagz
52
- method_missing
53
-
54
- because the mixing adds only private methods it's safe to use, for instance,
55
- in the controller of a web framework which exposes methods to the world as
56
- http actions
57
-
58
- the method_missing tagz.rb adds *only* works from inside a Tagz{}/tagz{}
59
- block, for instance
60
-
61
- include Tagz
62
-
63
- tagz{ h1_{ 'this works' } }
64
-
65
- h1_{ 'this throws NameError' }
66
-
67
- tagz.rb is very non-restrictive, allowing you to generate invalid html,
68
- html4, xml, whatever, all using the same simple interface
69
-
70
- for example
71
-
72
- require 'tagz'
73
- include Tagz
74
-
75
- tagz{
76
- div_(:class => 'pinky'){ 'content' }
77
- }
78
-
79
- #=> <div class="pinky">content</div>
80
-
81
-
82
- tagz{
83
- img_(:src => 'foo.png')
84
- img_(:src => 'foo.png'){}
85
- }
86
-
87
- #=> <img src="foo.png">
88
- #=> <img src="foo.png" />
89
-
90
-
91
- tagz{
92
- br_
93
- br_!
94
- br_{}
95
- }
96
-
97
- #=> <br>
98
- #=> <br />
99
- #=> <br />
100
-
101
-
102
- tagz{
103
- span_('content', :style => 'color:mauve')
104
- }
105
-
106
- #=> <span style="color:mauve">content</span>
107
-
108
-
109
- tagz{
110
- div_(:class => 'container'){
111
- span_('content', :style => 'color:mauve')
112
- }
113
- }
114
-
115
- #=> <div class="container"><span style="color:mauve">content</span></div>
116
-
117
-
118
- tagz{
119
- div_(:class => 'container')
120
- span_('content', :style => 'color:mauve')
121
- _div
122
- }
123
-
124
- #=> <div class="container"><span style="color:mauve">content</span></div>
125
-
126
-
127
- tagz{
128
- table_(:width => 42{
129
- %w( 1 2 3 ).each do |row|
130
- tr_{
131
- row.each{|cell| td_{ cell }}
132
- }
133
- end
134
- }
135
- }
136
-
137
- #=> <table width="42"><tr><td>1</td><td>2</td><td>3</td></tr></table>
138
-
139
- # note that the return value of the table block ( the array %w( 1 2 3 )
140
- # is *NOT* added to the content. the rule is: add the return value of
141
- # the block if, and only if, the block did not add any content itself
142
- # during evaluation
143
-
144
-
145
- tagz{
146
- div_{
147
- div_{ 'content' }
148
- 'this is ignored'
149
- }
150
- }
151
-
152
- #=> <div><div>content</div></div>
153
-
154
- # this is a side effect of the above rule
155
-
156
-
157
- tagz{
158
- div_{
159
- 'this is not ignored, see above rule'
160
- }
161
- }
162
-
163
- #=> <div>this is not ignored, see above rule</div>
164
-
165
-
166
- tagz{
167
- div_{
168
- div_{ 'content' }
169
- tagz << 'this is appended' << ' and so is this'
170
- }
171
- }
172
-
173
- #=> <div><div>content</div>this is appended and so is this</div>
174
-
175
-
176
- tagz{
177
- a_
178
- b_
179
- c_
180
- tagz << 'valid html'
181
- _a
182
- _b
183
- _c
184
- _invalid
185
- }
186
-
187
- #=> <a><b><c>valid html</a></b></c></invalid>
188
-
189
-
190
- tagz{
191
- __
192
- a_{ 'content' }
193
-
194
- __ __
195
- b_{ 'content' }
196
-
197
- __ __ __
198
- c_{ 'content' }
199
- }
200
-
201
- #=> \n<a>content</a>\n\n<b>content</b>\n\n\n<c>content</c>
202
-
203
- # note that \n is newline. the html tagz generates is quite compact,
204
- # you can do 'tagz << "\n"' or just use the '__' method to output some
205
- # linebreaks
206
-
207
-
208
- tagz{
209
- link = e(:a, :href => 'foo.com'){ 'link text' }
210
-
211
- div_{ link }
212
- }
213
-
214
- #=> <div><a href="foo.com">link text</a>
215
-
216
-
217
- a couple of notes: *none* of the method_missing magic is available outside
218
- the 'tagz' block and, even inside, it supers-up when the missing method does
219
- not look like a tag method. you can use Tagz{} as a module method but to
220
- have access to @ivars you'll want to mixin the module to your own class and
221
- use tagz{}. no public methods are added to your object, only four private
222
- ones.
223
-
224
-
225
- HISTORY
226
- 1.0.0
227
- totally reworked tagz, dropping 300 loc - only 170 loc now. this release
228
- is *NOT* backward compatible with other tagz versions, though the api is
229
- very very close. the rework makes tagz safer to mixin, faster, and
230
- produces nicer looking html. this version also marks ramaze template
231
- support.
232
-
233
- INSTALL
234
- gem install tagz
235
-
236
- URI
237
- http://rubyforge.org/projects/codeforpeople
238
-
239
- AUTHOR
240
- ara.t.howard@gmail.com