tagz 1.0.0 → 4.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -1,240 +1,215 @@
1
1
  NAME
2
+
2
3
  tagz.rb
3
4
 
4
5
  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
6
 
23
- require 'tagz'
7
+ require Tagz
24
8
 
25
- class Table < ::Array
26
- include Tagz
9
+ include Tagz.globally
27
10
 
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
11
+ a_(:href => "/foo"){ "bar" } #=> <a href="/foo">bar</a>
44
12
 
45
13
  DESCRIPTION
46
- tagz.rb offers a mixin module which adds four private methods to the calling
47
- object:
48
14
 
49
- tagz
50
- tagz__
51
- __tagz
52
- method_missing
15
+ tagz.rb is generates html, xml, or any sgml variant like a small ninja
16
+ running across the backs of a herd of giraffes swatting of heads like a
17
+ mark-up weedwacker. weighing in at less than 200 lines of code tagz.rb adds
18
+ an html syntax to ruby that is both unobtrusive, safe, and available
19
+ globally to objects without the need for any builder or superfluous objects.
20
+ tagz.rb is designed for applications that generate html to be able to do so
21
+ easily in any context without heavyweight syntax or scoping issues, like a
22
+ ninja sword through butter.
53
23
 
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
24
+ INSTALL
57
25
 
58
- the method_missing tagz.rb adds *only* works from inside a Tagz{}/tagz{}
59
- block, for instance
26
+ gem install tagz
60
27
 
61
- include Tagz
28
+ HISTORY
62
29
 
63
- tagz{ h1_{ 'this works' } }
30
+ 4.2.0
31
+ - general lib cleanup
32
+ - introduction of dual-mixin technique (Tagz.globally)
33
+ - few small bug fixes
34
+ - ninja tales
64
35
 
65
- h1_{ 'this throws NameError' }
36
+ SAMPLES
66
37
 
67
- tagz.rb is very non-restrictive, allowing you to generate invalid html,
68
- html4, xml, whatever, all using the same simple interface
38
+ <========< samples/a.rb >========>
69
39
 
70
- for example
40
+ ~ > cat samples/a.rb
71
41
 
42
+ #
43
+ # in the simplest case tagz generates html using a syntax which safely mixes
44
+ # in to any object
45
+ #
46
+
72
47
  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
- }
48
+ include Tagz.globally
49
+
50
+ class GiraffeModel
51
+ def link
52
+ a_(:href => "/giraffe/neck/42"){ "whack!" }
53
+ end
54
+ end
55
+
56
+ puts GiraffeModel.new.link
96
57
 
97
- #=> <br>
98
- #=> <br />
99
- #=> <br />
58
+ ~ > ruby samples/a.rb
100
59
 
101
-
102
- tagz{
103
- span_('content', :style => 'color:mauve')
104
- }
60
+ <a href="/giraffe/neck/42">whack!</a>
105
61
 
106
- #=> <span style="color:mauve">content</span>
107
62
 
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
- }
63
+ <========< samples/b.rb >========>
123
64
 
124
- #=> <div class="container"><span style="color:mauve">content</span></div>
65
+ ~ > cat samples/b.rb
125
66
 
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
67
+ #
68
+ # tagz.rb mixes quite easily with your favourite templating engine, avoiding
69
+ # the need for '<% rows.each do |row| %> ... <% row.each do |cell| %> '
70
+ # madness and other types of logic to be coded in the templating language,
71
+ # leaving templating to template engines and logic and looping to ruby -
72
+ # unencumbered by extra funky syntax
73
+ #
74
+
75
+ require 'tagz'
76
+ include Tagz.globally
77
+
78
+ require 'erb'
79
+
80
+ rows = %w( a b c ), %w( 1 2 3 )
81
+
82
+ template = ERB.new <<-ERB
83
+ <html>
84
+ <body>
85
+ <%=
86
+
87
+ if rows
88
+
89
+ table_{
90
+ rows.each do |row|
91
+ tr_{
92
+ row.each do |cell|
93
+ td_{ cell }
94
+ end
95
+ }
96
+ end
97
+ }
98
+
99
+ end
100
+
101
+ %>
102
+ </body>
103
+ </html>
104
+ ERB
105
+
106
+ puts template.result(binding)
107
+
108
+
109
+ ~ > ruby samples/b.rb
110
+
111
+ <html>
112
+ <body>
113
+ <table><tr><td>a</td><td>b</td><td>c</td></tr><tr><td>1</td><td>2</td><td>3</td></tr></table>
114
+ </body>
115
+ </html>
116
+
117
+
118
+ <========< samples/c.rb >========>
119
+
120
+ ~ > cat samples/c.rb
121
+
122
+ #
123
+ # once you've learned to generate html using tagz you're primed to generate
124
+ # xml too
125
+ #
126
+
127
+ require 'tagz'
128
+ include Tagz.globally
129
+
130
+ doc =
131
+ xml_{
132
+ giraffe_{ 'large' }
133
+ ninja_{ 'small' }
134
134
  }
135
- }
135
+
136
+ puts doc
136
137
 
137
- #=> <table width="42"><tr><td>1</td><td>2</td><td>3</td></tr></table>
138
+ ~ > ruby samples/c.rb
138
139
 
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
140
+ <xml><giraffe>large</giraffe><ninja>small</ninja></xml>
143
141
 
144
-
145
- tagz{
146
- div_{
147
- div_{ 'content' }
148
- 'this is ignored'
149
- }
150
- }
151
142
 
152
- #=> <div><div>content</div></div>
143
+ <========< samples/d.rb >========>
153
144
 
154
- # this is a side effect of the above rule
145
+ ~ > cat samples/d.rb
155
146
 
156
-
157
- tagz{
158
- div_{
159
- 'this is not ignored, see above rule'
147
+ #
148
+ # tagz.rb doesn't cramp your style, allowing even invalid html to be
149
+ # generated. note the use of the 'tagz' method, which can be used both to
150
+ # capture output and to append content to the top of the stack.
151
+ #
152
+
153
+ require 'tagz'
154
+ include Tagz.globally
155
+
156
+ def header
157
+ tagz{
158
+ html_
159
+ body_(:class => 'ninja-like', :id => 'giraffe-slayer')
160
+
161
+ tagz << "\n<!-- this is the header -->\n"
160
162
  }
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'
163
+ end
164
+
165
+ def footer
166
+ tagz{
167
+ tagz << "\n<!-- this is the footer -->\n"
168
+
169
+ body_
170
+ html_
170
171
  }
171
- }
172
+ end
173
+
174
+ puts header, footer
172
175
 
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>
176
+ ~ > ruby samples/d.rb
188
177
 
189
-
190
- tagz{
191
- __
192
- a_{ 'content' }
178
+ <html><body class="ninja-like" id="giraffe-slayer">
179
+ <!-- this is the header -->
180
+
181
+ <!-- this is the footer -->
182
+ <body><html>
193
183
 
194
- __ __
195
- b_{ 'content' }
196
-
197
- __ __ __
198
- c_{ 'content' }
199
- }
200
184
 
201
- #=> \n<a>content</a>\n\n<b>content</b>\n\n\n<c>content</c>
185
+ <========< samples/e.rb >========>
202
186
 
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
187
+ ~ > cat samples/e.rb
206
188
 
207
-
208
- tagz{
209
- link = e(:a, :href => 'foo.com'){ 'link text' }
210
-
211
- div_{ link }
189
+ #
190
+ # tagz.rb allows a safer method of mixin which requires any tagz methods to be
191
+ # insider a tagz block - tagz generating methods outside a tagz block with
192
+ # raise an error if tagz is included this way. also notice that the error is
193
+ # reported from where it was raised - not from the bowels of the the tagz.rb
194
+ # lib.
195
+ #
196
+
197
+ require 'tagz'
198
+ include Tagz
199
+
200
+ puts tagz{
201
+ html_{ 'works only in here' }
212
202
  }
203
+
204
+ begin
205
+ html_{ 'not out here' }
206
+ rescue Object => e
207
+ puts e.backtrace
208
+ end
209
+
213
210
 
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
211
+ ~ > ruby samples/e.rb
235
212
 
236
- URI
237
- http://rubyforge.org/projects/codeforpeople
213
+ <html>works only in here</html>
214
+ samples/e.rb:17
238
215
 
239
- AUTHOR
240
- ara.t.howard@gmail.com
data/a.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'tagz'
2
+
3
+ include Tagz
4
+
5
+ p a_{ 'foo' }
6
+ p a_{ 'foo' }
7
+
8
+ p tagz
data/gemspec.rb CHANGED
@@ -1,27 +1,35 @@
1
-
2
1
  lib, version = File::basename(File::dirname(File::expand_path(__FILE__))).split %r/-/, 2
3
2
 
4
3
  require 'rubygems'
5
4
 
6
5
  Gem::Specification::new do |spec|
7
6
  $VERBOSE = nil
7
+
8
+ shiteless = lambda do |list|
9
+ list.delete_if do |file|
10
+ file =~ %r/\.svn/ or
11
+ file =~ %r/\.tmp/
12
+ end
13
+ end
14
+
8
15
  spec.name = lib
9
16
  spec.version = version
10
17
  spec.platform = Gem::Platform::RUBY
11
18
  spec.summary = lib
12
19
 
13
- spec.files = Dir::glob "**/**"
14
- spec.executables = Dir::glob("bin/*").map{|exe| File::basename exe}
20
+ spec.files = shiteless[Dir::glob("**/**")]
21
+ spec.executables = shiteless[Dir::glob("bin/*")].map{|exe| File::basename exe}
15
22
 
16
23
  spec.require_path = "lib"
17
24
  spec.autorequire = lib
18
25
 
19
26
  spec.has_rdoc = File::exist? "doc"
20
27
  spec.test_suite_file = "test/#{ lib }.rb" if File::directory? "test"
28
+ #spec.add_dependency 'lib', '>= version'
21
29
 
22
30
  spec.extensions << "extconf.rb" if File::exists? "extconf.rb"
23
31
 
24
32
  spec.author = "Ara T. Howard"
25
- spec.email = "ara.t.howard@noaa.gov"
33
+ spec.email = "ara.t.howard@gmail.com"
26
34
  spec.homepage = "http://codeforpeople.com/lib/ruby/#{ lib }/"
27
35
  end