public
Description: The open source social networking platform in Ruby on Rails from the author of RailsSpace
Homepage: http://insoshi.com
Clone URL: git://github.com/insoshi/insoshi.git
Search Repo:
Added markaby
Michael Hartl (author)
Sat Mar 01 18:20:37 -0800 2008
commit  fa249934b244cf18d5a9b6647a5cc706afa31599
tree    8eb7ecfca187b8baac7a3e662edb9e36914bf5cb
parent  0308b543442e58a787b00f40bc10cea8f690355e
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
0
@@ -1 +1,41 @@
0
+= SVN
0
+===
0
+
0
+* Default attributes on the root (<html>) element can now be overidden
0
+* Reworked CssProxy, allowing attributes on hr and br
0
+* Added Kernel#mab convenience method (require 'markaby/kernel_method')
0
+* WhenOnRails: Can now use :locals with render_markaby
0
+* WhenOnRails: Template errors now report line number
0
+
0
+= 0.5
0
+=== 03 October, 2006
0
+
0
+* XHTML Validation built in. So, if you have an invalid tag: error. Invalid attribute: error.
0
+ And two identical IDs in the same document: error. Optional, of course. But handy!
0
+* New Markaby::Fragment class adds much flexibility. If it discovers you are using a tag as a string,
0
+ the tag is removed from the stream. (<tt>div { strong("Real") + " Giraffes" }</tt>)
0
+* The prevailing rule now is: if you want it escaped, pass it as an arg. If not, pass it to a block.
0
+* Again, escaped: <tt>h1("Me & You Have a Giraffe")</tt>
0
+* And, not escaped: <tt>h1 { "<a href='/'>Home</a>" }</tt>
0
+* Less method_missing, meaning: faster calls all around. Tag methods generated based on doctype.
0
+* The <tt>html</tt> method doesn't write the doctype tags and meta tags. You must use <tt>xhtml_transitional</tt> or <tt>xhtml_strict</tt> methods to do that.
0
+* The <tt>img</tt> method doesn't try to inject an empty alt tag and a zero border. No more of that.
0
+
0
+= 0.3
0
+=== 02nd February, 2006
0
+
0
+* Allow Markaby::Builder.new without args.
0
+* Rails helper method render_markaby.
0
+
0
+= 0.2
0
+=== 17th January, 2006
0
+
0
+* Public announcement.
0
+* DOCTYPES, head tags.
0
+* Works with Rails helpers.
0
+
0
+= 0.1
0
+=== 05th January, 2006
0
+
0
+* Initial import.
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
0
@@ -1 +1,256 @@
0
+= Markaby (Markup as Ruby)
0
+
0
+Markaby is a very short bit of code for writing HTML pages in pure Ruby.
0
+It is an alternative to ERb which weaves the two languages together.
0
+Also a replacement for templating languages which use primitive languages
0
+that blend with HTML.
0
+
0
+== Using Markaby as a Rails plugin
0
+
0
+Write Rails templates in pure Ruby. Example layout:
0
+
0
+ html do
0
+ head do
0
+ title 'Products: ' + action_name
0
+ stylesheet_link_tag 'scaffold'
0
+ end
0
+
0
+ body do
0
+ p flash[:notice], :style => "color: green"
0
+
0
+ self << content_for_layout
0
+ end
0
+ end
0
+
0
+== Using Markaby as a Ruby class
0
+
0
+Markaby is flaming easy to call from your Ruby classes.
0
+
0
+ require 'markaby'
0
+
0
+ mab = Markaby::Builder.new
0
+ mab.html do
0
+ head { title "Boats.com" }
0
+ body do
0
+ h1 "Boats.com has great deals"
0
+ ul do
0
+ li "$49 for a canoe"
0
+ li "$39 for a raft"
0
+ li "$29 for a huge boot that floats and can fit 5 people"
0
+ end
0
+ end
0
+ end
0
+ puts mab.to_s
0
+
0
+Markaby::Builder.new does take two arguments for passing in variables and
0
+a helper object. You can also affix the block right on to the class.
0
+
0
+See Markaby::Builder for all of that.
0
+
0
+= A Note About <tt>instance_eval</tt>
0
+
0
+The Markaby::Builder class is different from the normal Builder class,
0
+since it uses <tt>instance_eval</tt> when running blocks. This cleans
0
+up the appearance of the Markaby code you write. If <tt>instance_eval</tt>
0
+was not used, the code would look like this:
0
+
0
+ mab = Markaby::Builder.new
0
+ mab.html do
0
+ mab.head { mab.title "Boats.com" }
0
+ mab.body do
0
+ mab.h1 "Boats.com has great deals"
0
+ end
0
+ end
0
+ puts mab.to_s
0
+
0
+So, the advantage is the cleanliness of your code. The disadvantage is that
0
+the block will run inside the Markaby::Builder object's scope. This means
0
+that inside these blocks, <tt>self</tt> will be your Markaby::Builder object.
0
+When you use instance variables in these blocks, they will be instance variables
0
+of the Markaby::Builder object.
0
+
0
+This doesn't affect Rails users, but when used in regular Ruby code, it can
0
+be a bit disorienting. You are recommended to put your Markaby code in a
0
+module where it won't mix with anything.
0
+
0
+= The Six Steps of Markaby
0
+
0
+If you dive right into Markaby, it'll probably make good sense, but you're
0
+likely to run into a few kinks. Why not review these six steps and commit
0
+them memory so you can really *know* what you're doing?
0
+
0
+== 1. Element Classes
0
+
0
+Element classes may be added by hooking methods onto container elements:
0
+
0
+ div.entry do
0
+ h2.entryTitle 'Son of WebPage'
0
+ div.entrySection %{by Anthony}
0
+ div.entryContent 'Okay, once again, the idea here is ...'
0
+ end
0
+
0
+Which results in:
0
+
0
+ <div class="entry">
0
+ <h2 class="entryTitle">Son of WebPage</h2>
0
+ <div class="entrySection">by Anthony</div>
0
+ <div class="entryContent">Okay, once again, the idea here is ...</div>
0
+ </div>
0
+
0
+== 2. Element IDs
0
+
0
+IDs may be added by the use of bang methods:
0
+
0
+ div.page! {
0
+ div.content! {
0
+ h1 "A Short Short Saintly Dog"
0
+ }
0
+ }
0
+
0
+Which results in:
0
+
0
+ <div id="page">
0
+ <div id="content">
0
+ <h1>A Short Short Saintly Dog</h1>
0
+ </div>
0
+ </div>
0
+
0
+== 3. Validate Your XHTML 1.0 Output
0
+
0
+If you'd like Markaby to help you assemble valid XHTML documents,
0
+you can use the <tt>xhtml_transitional</tt> or <tt>xhtml_strict</tt>
0
+methods in place of the normal <tt>html</tt> tag.
0
+
0
+ xhtml_strict do
0
+ head { ... }
0
+ body { ... }
0
+ end
0
+
0
+This will add the XML instruction and the doctype tag to your document.
0
+Also, a character set meta tag will be placed inside your <tt>head</tt>
0
+tag.
0
+
0
+Now, since Markaby knows which doctype you're using, it checks a big
0
+list of valid tags and attributes before printing anything.
0
+
0
+ >> div :styl => "padding: 10px" do
0
+ >> img :src => "samorost.jpg"
0
+ >> end
0
+ InvalidHtmlError: no such attribute `styl'
0
+
0
+Markaby will also make sure you don't use the same element ID twice!
0
+
0
+== 4. Escape or No Escape?
0
+
0
+Markaby uses a simple convention for escaping stuff: if a string
0
+is an argument, it gets escaped. If the string is in a block, it
0
+doesn't.
0
+
0
+This is handy if you're using something like RedCloth or
0
+RDoc inside an element. Pass the string back through the block
0
+and it'll skip out of escaping.
0
+
0
+ div.comment { RedCloth.new(str).to_html }
0
+
0
+But, if we have some raw text that needs escaping, pass it in
0
+as an argument:
0
+
0
+ div.comment raw_str
0
+
0
+One caveat: if you have other tags inside a block, the string
0
+passed back will be ignored.
0
+
0
+ div.comment {
0
+ div.author "_why"
0
+ div.says "Torpedoooooes!"
0
+ "<div>Silence.</div>"
0
+ }
0
+
0
+The final div above won't appear in the output. You can't mix
0
+tag modes like that, friend.
0
+
0
+== 5. Auto-stringification
0
+
0
+If you end up using any of your Markaby "tags" as a string, the
0
+tag won't be output. It'll be up to you to add the new string
0
+back into the HTML output.
0
+
0
+This means if you call <tt>to_s</tt>, you'll get a string back.
0
+
0
+ div.title { "Rock Bottom" + span(" by Robert Wyatt").to_s }
0
+
0
+But, when you're adding strings in Ruby, <tt>to_s</tt> happens automatically.
0
+
0
+ div.title { "Rock Bottom" + span(" by Robert Wyatt") }
0
+
0
+Interpolation works fine.
0
+
0
+ div.title { "Rock Bottom #{span(" by Robert Wyatt")}" }
0
+
0
+And any other operation you might perform on a string.
0
+
0
+ div.menu! \
0
+ ['5.gets', 'bits', 'cult', 'inspect', '-h'].map do |category|
0
+ link_to category
0
+ end.
0
+ join( " | " )
0
+
0
+== 6. The <tt>tag!</tt> Method
0
+
0
+If you need to force a tag at any time, call <tt>tag!</tt> with the
0
+tag name followed by the possible arguments and block. The CssProxy
0
+won't work with this technique.
0
+
0
+ tag! :select, :id => "country_list" do
0
+ countries.each do |country|
0
+ tag! :option, country
0
+ end
0
+ end
0
+
0
+= A Note About Rails Helpers
0
+
0
+When used in Rails templates, the Rails helper object is passed into
0
+Markaby::Builder. When you call helper methods inside Markaby, the output
0
+from those methods will be output to the stream. This is incredibly
0
+handy, since most Rails helpers output HTML tags.
0
+
0
+ head do
0
+ javascript_include_tag 'prototype'
0
+ autodiscovery_link_tag
0
+ end
0
+
0
+However, some methods are designed to give back a String which you can use
0
+elsewhere. That's okay! Every method returns a Fragment object, which can
0
+be used as a string.
0
+
0
+ p { "Total is: #{number_to_human_size @file_bytes}" }
0
+
0
+Also see the Quick Tour above, specifically the stuff about auto-stringification.
0
+
0
+If for any reason you have trouble with fragments, you can just
0
+call the <tt>@helpers</tt> object with the method and you'll get
0
+the String back and nothing will be output.
0
+
0
+ p { "Total is: #{@helpers.number_to_human_size @file_bytes}" }
0
+
0
+Conversely, you may call instance variables from your controller by using
0
+a method and its value will be returned, nothing will be output.
0
+
0
+ # Inside imaginary ProductController
0
+ def list
0
+ @products = Product.find :all
0
+ end
0
+
0
+ # Inside app/views/product/list.mab
0
+ products.each do |product|
0
+ p product.title
0
+ end
0
+
0
+= Credits
0
+
0
+Markaby is a work of immense hope by Tim Fletcher and why the lucky stiff.
0
+Thankyou for giving it a whirl.
0
+
0
+Markaby is inspired by the HTML library within cgi.rb. Hopefully it will
0
+turn around and take some cues.
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
0
@@ -1 +1,43 @@
0
+require 'rake'
0
+require 'rake/testtask'
0
+require 'rake/clean'
0
+require 'rake/gempackagetask'
0
+require 'rake/rdoctask'
0
+require 'tools/rakehelp'
0
+require 'fileutils'
0
+include FileUtils
0
+
0
+REV = File.read(".svn/entries")[/committed-rev="(\d+)"/, 1] rescue nil
0
+VERS = ENV['VERSION'] || "0.5" + (REV ? ".#{REV}" : "")
0
+
0
+task :default => [:package]
0
+
0
+setup_tests
0
+setup_rdoc ['README', 'CHANGELOG', 'lib/**/*.rb']
0
+
0
+summary = "Markup as Ruby, write HTML in your native Ruby tongue"
0
+test_file = "test/test_markaby.rb"
0
+setup_gem("markaby", VERS, "Tim Fletcher and _why", summary, [['builder', '>=2.0.0']], test_file)
0
+
0
+desc "List any Markaby specific warnings"
0
+task :warnings do
0
+ `ruby -w test/test_markaby.rb 2>&1`.split(/\n/).each do |line|
0
+ next unless line =~ /warning:/
0
+ next if line =~ /builder-/
0
+ puts line
0
+ end
0
+end
0
+
0
+desc "Start a Markaby-aware IRB session"
0
+task :irb do
0
+ sh 'irb -I lib -r markaby -r markaby/kernel_method'
0
+end
0
+
0
+namespace :test do
0
+ desc ''
0
+ task :rails do
0
+ Dir.chdir '../../../'
0
+ sh 'rake test:plugins PLUGIN=markaby'
0
+ end
0
+end
...
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
0
@@ -1 +1,9 @@
0
+$:.unshift File.expand_path(File.join(File.dirname(__FILE__), 'lib'))
0
+
0
+require 'markaby'
0
+require 'markaby/rails'
0
+
0
+ActionView::Base::register_template_handler 'mab', Markaby::Rails::ActionViewTemplateHandler
0
+
0
+ActionController::Base.send :include, Markaby::Rails::ActionControllerHelpers
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
0
@@ -1 +1,36 @@
0
+# = About lib/markaby.rb
0
+#
0
+# By requiring <tt>lib/markaby</tt>, you can load Markaby's dependency (the Builder library,)
0
+# as well as the full set of Markaby classes.
0
+#
0
+# For a full list of features and instructions, see the README.
0
+$:.unshift File.expand_path(File.dirname(__FILE__))
0
+
0
+# Markaby is a module containing all of the great Markaby classes that
0
+# do such an excellent job.
0
+#
0
+# * Markaby::Builder: the class for actually calling the Ruby methods
0
+# which write the HTML.
0
+# * Markaby::CSSProxy: a class which adds element classes and IDs to
0
+# elements when used within Markaby::Builder.
0
+# * Markaby::MetAid: metaprogramming helper methods.
0
+# * Markaby::Tags: lists the roles of various XHTML tags to help Builder
0
+# use these tags as they are intended.
0
+# * Markaby::Template: a class for hooking Markaby into Rails as a
0
+# proper templating language.
0
+module Markaby
0
+ VERSION = '0.5'
0
+
0
+ class InvalidXhtmlError < Exception; end
0
+end
0
+
0
+unless defined?(Builder)
0
+ require 'rubygems'
0
+ require 'builder'
0
+end
0
+
0
+require 'markaby/builder'
0
+require 'markaby/cssproxy'
0
+require 'markaby/metaid'
0
+require 'markaby/template'
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
0
@@ -1 +1,284 @@
0
+require 'markaby/tags'
0
+
0
+module Markaby
0
+ # The Markaby::Builder class is the central gear in the system. When using
0
+ # from Ruby code, this is the only class you need to instantiate directly.
0
+ #
0
+ # mab = Markaby::Builder.new
0
+ # mab.html do
0
+ # head { title "Boats.com" }
0
+ # body do
0
+ # h1 "Boats.com has great deals"
0
+ # ul do
0
+ # li "$49 for a canoe"
0
+ # li "$39 for a raft"
0
+ # li "$29 for a huge boot that floats and can fit 5 people"
0
+ # end
0
+ # end
0
+ # end
0
+ # puts mab.to_s
0
+ #
0
+ class Builder
0
+
0
+ @@default = {
0
+ :indent => 0,
0
+ :output_helpers => true,
0
+ :output_xml_instruction => true,
0
+ :output_meta_tag => true,
0
+ :auto_validation => true,
0
+ :tagset => Markaby::XHTMLTransitional,
0
+ :root_attributes => {
0
+ :xmlns => 'http://www.w3.org/1999/xhtml', :'xml:lang' => 'en', :lang => 'en'
0
+ }
0
+ }
0
+
0
+ def self.set(option, value)
0
+ @@default[option] = value
0
+ end
0
+
0
+ def self.ignored_helpers
0
+ @@ignored_helpers ||= []
0
+ end
0
+
0
+ def self.ignore_helpers(*helpers)
0
+ ignored_helpers.concat helpers
0
+ end
0
+
0
+ attr_accessor :output_helpers, :tagset
0
+
0
+ # Create a Markaby builder object. Pass in a hash of variable assignments to
0
+ # +assigns+ which will be available as instance variables inside tag construction
0
+ # blocks. If an object is passed in to +helpers+, its methods will be available
0
+ # from those same blocks.
0
+ #
0
+ # Pass in a +block+ to new and the block will be evaluated.
0
+ #
0
+ # mab = Markaby::Builder.new {
0
+ # html do
0
+ # body do
0
+ # h1 "Matching Mole"
0
+ # end
0
+ # end
0
+ # }
0
+ #
0
+ def initialize(assigns = {}, helpers = nil, &block)
0
+ @streams = [[]]
0
+ @assigns = assigns.dup
0
+ @helpers = helpers
0
+ @elements = {}
0
+
0
+ @@default.each do |k, v|
0
+ instance_variable_set("@#{k}", @assigns.delete(k) || v)
0
+ end
0
+
0
+ @assigns.each do |k, v|
0
+ instance_variable_set("@#{k}", v)
0
+ end
0
+
0
+ @builder = XmlMarkup.new(:indent => @indent, :target => @streams.last)
0
+
0
+ text(capture(&block)) if block
0
+ end
0
+
0
+ # Returns a string containing the HTML stream. Internally, the stream is stored as an Array.
0
+ def to_s
0
+ @streams.last.to_s
0
+ end
0
+
0
+ # Write a +string+ to the HTML stream without escaping it.
0
+ def text(string)
0
+ @builder << string.to_s
0
+ nil
0
+ end
0
+ alias_method :<<, :text
0
+ alias_method :concat, :text
0
+
0
+ # Captures the HTML code built inside the +block+. This is done by creating a new
0
+ # stream for the builder object, running the block and passing back its stream as a string.
0
+ #
0
+ # >> Markaby::Builder.new.capture { h1 "TEST"; h2 "CAPTURE ME" }
0
+ # => "<h1>TITLE</h1>\n<h2>CAPTURE ME</h2>\n"
0
+ #
0
+ def capture(&block)
0
+ @streams.push(@builder.target = [])
0
+ @builder.level += 1
0
+ str = instance_eval(&block)
0
+ str = @streams.last.join if @streams.last.any?
0
+ @streams.pop
0
+ @builder.level -= 1
0
+ @builder.target = @streams.last
0
+ str
0
+ end
0
+
0
+ # Create a tag named +tag+. Other than the first argument which is the tag name,
0
+ # the arguments are the same as the tags implemented via method_missing.
0
+ def tag!(tag, *args, &block)
0
+ ele_id = nil
0
+ if @auto_validation and @tagset
0
+ if !@tagset.tagset.has_key?(tag)
0
+ raise InvalidXhtmlError, "no element `#{tag}' for #{tagset.doctype}"
0
+ elsif args.last.respond_to?(:to_hash)
0
+ attrs = args.last.to_hash
0
+
0
+ if @tagset.forms.include?(tag) and attrs[:id]
0
+ attrs[:name] ||= attrs[:id]
0
+ end
0
+
0
+ attrs.each do |k, v|
0
+ atname = k.to_s.downcase.intern
0
+ unless k =~ /:/ or @tagset.tagset[tag].include? atname
0
+ raise InvalidXhtmlError, "no attribute `#{k}' on #{tag} elements"
0
+ end
0
+ if atname == :id
0
+ ele_id = v.to_s
0
+ if @elements.has_key? ele_id
0
+ raise InvalidXhtmlError, "id `#{ele_id}' already used (id's must be unique)."
0
+ end
0
+ end
0
+ end
0
+ end
0
+ end
0
+ if block
0
+ str = capture(&block)
0
+ block = proc { text(str) }
0
+ end
0
+
0
+ f = fragment { @builder.method_missing(tag, *args, &block) }
0
+ @elements[ele_id] = f if ele_id
0
+ f
0
+ end
0
+
0
+ # This method is used to intercept calls to helper methods and instance
0
+ # variables. Here is the order of interception:
0
+ #
0
+ # * If +sym+ is a helper method, the helper method is called
0
+ # and output to the stream.
0
+ # * If +sym+ is a Builder::XmlMarkup method, it is passed on to the builder object.
0
+ # * If +sym+ is also the name of an instance variable, the
0
+ # value of the instance variable is returned.
0
+ # * If +sym+ has come this far and no +tagset+ is found, +sym+ and its arguments are passed to tag!
0
+ # * If a tagset is found, though, +NoMethodError+ is raised.
0
+ #
0
+ # method_missing used to be the lynchpin in Markaby, but it's no longer used to handle
0
+ # HTML tags. See html_tag for that.
0
+ def method_missing(sym, *args, &block)
0
+ if @helpers.respond_to?(sym, true) && !self.class.ignored_helpers.include?(sym)
0
+ r = @helpers.send(sym, *args, &block)
0
+ if @output_helpers and r.respond_to? :to_str
0
+ fragment { @builder << r }
0
+ else
0
+ r
0
+ end
0
+ elsif @assigns.has_key?(sym)
0
+ @assigns[sym]
0
+ elsif @assigns.has_key?(stringy_key = sym.to_s)
0
+ # Rails' ActionView assigns hash has string keys for
0
+ # instance variables that are defined in the controller.
0
+ @assigns[stringy_key]
0
+ elsif instance_variables.include?(ivar = "@#{sym}")
0
+ instance_variable_get(ivar)
0
+ elsif !@helpers.nil? && @helpers.instance_variables.include?(ivar)
0
+ @helpers.instance_variable_get(ivar)
0
+ elsif ::Builder::XmlMarkup.instance_methods.include?(sym.to_s)
0
+ @builder.__send__(sym, *args, &block)
0
+ elsif @tagset.nil?
0
+ tag!(sym, *args, &block)
0
+ else
0
+ raise NoMethodError, "no such method `#{sym}'"
0
+ end
0
+ end
0
+
0
+ # Every HTML tag method goes through an html_tag call. So, calling <tt>div</tt> is equivalent
0
+ # to calling <tt>html_tag(:div)</tt>. All HTML tags in Markaby's list are given generated wrappers
0
+ # for this method.
0
+ #
0
+ # If the @auto_validation setting is on, this method will check for many common mistakes which
0
+ # could lead to invalid XHTML.
0
+ def html_tag(sym, *args, &block)
0
+ if @auto_validation and @tagset.self_closing.include?(sym) and block
0
+ raise InvalidXhtmlError, "the `#{sym}' element is self-closing, please remove the block"
0
+ elsif args.empty? and block.nil?
0
+ CssProxy.new(self, @streams.last, sym)
0
+ else
0
+ tag!(sym, *args, &block)
0
+ end
0
+ end
0
+
0
+ XHTMLTransitional.tags.each do |k|
0
+ class_eval %{
0
+ def #{k}(*args, &block)
0
+ html_tag(#{k.inspect}, *args, &block)
0
+ end
0
+ }
0
+ end
0
+
0
+ remove_method :head
0
+
0
+ # Builds a head tag. Adds a <tt>meta</tt> tag inside with Content-Type
0
+ # set to <tt>text/html; charset=utf-8</tt>.
0
+ def head(*args, &block)
0
+ tag!(:head, *args) do
0
+ tag!(:meta, "http-equiv" => "Content-Type", "content" => "text/html; charset=utf-8") if @output_meta_tag
0
+ instance_eval(&block)
0
+ end
0
+ end
0
+
0
+ # Builds an html tag. An XML 1.0 instruction and an XHTML 1.0 Transitional doctype
0
+ # are prepended. Also assumes <tt>:xmlns => "http://www.w3.org/1999/xhtml",
0
+ # :lang => "en"</tt>.
0
+ def xhtml_transitional(attrs = {}, &block)
0
+ self.tagset = Markaby::XHTMLTransitional
0
+ xhtml_html(attrs, &block)
0
+ end
0
+
0
+ # Builds an html tag with XHTML 1.0 Strict doctype instead.
0
+ def xhtml_strict(attrs = {}, &block)
0
+ self.tagset = Markaby::XHTMLStrict
0
+ xhtml_html(attrs, &block)
0
+ end
0
+
0
+ private
0
+
0
+ def xhtml_html(attrs = {}, &block)
0
+ instruct! if @output_xml_instruction
0
+ declare!(:DOCTYPE, :html, :PUBLIC, *tagset.doctype)
0
+ tag!(:html, @root_attributes.merge(attrs), &block)
0
+ end
0
+
0
+ def fragment
0
+ stream = @streams.last
0
+ start = stream.length
0
+ yield
0
+ length = stream.length - start
0
+ Fragment.new(stream, start, length)
0
+ end
0
+
0
+ end
0
+
0
+ # Every tag method in Markaby returns a Fragment. If any method gets called on the Fragment,
0
+ # the tag is removed from the Markaby stream and given back as a string. Usually the fragment
0
+ # is never used, though, and the stream stays intact.
0
+ #
0
+ # For a more practical explanation, check out the README.
0
+ class Fragment < ::Builder::BlankSlate
0
+ def initialize(*args)
0
+ @stream, @start, @length = args
0
+ end
0
+ def method_missing(*args, &block)
0
+ # We can't do @stream.slice!(@start, @length),
0
+ # as it would invalidate the @starts and @lengths of other Fragment instances.
0
+ @str = @stream[@start, @length].to_s
0
+ @stream[@start, @length] = [nil] * @length
0
+ def self.method_missing(*args, &block)
0
+ @str.send(*args, &block)
0
+ end
0
+ @str.send(*args, &block)
0
+ end
0
+ end
0
+
0
+ class XmlMarkup < ::Builder::XmlMarkup
0
+ attr_accessor :target, :level
0
+ end
0
+
0
+end
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
0
@@ -1 +1,49 @@
0
+module Markaby
0
+ # Class used by Markaby::Builder to store element options. Methods called
0
+ # against the CssProxy object are added as element classes or IDs.
0
+ #
0
+ # See the README for examples.
0
+ class CssProxy
0
+
0
+ # Creates a CssProxy object.
0
+ def initialize(builder, stream, sym)
0
+ @builder, @stream, @sym, @attrs = builder, stream, sym, {}
0
+
0
+ @original_stream_length = @stream.length
0
+
0
+ @builder.tag! @sym
0
+ end
0
+
0
+ # Adds attributes to an element. Bang methods set the :id attribute.
0
+ # Other methods add to the :class attribute.
0
+ def method_missing(id_or_class, *args, &block)
0
+ if (idc = id_or_class.to_s) =~ /!$/
0
+ @attrs[:id] = $`
0
+ else
0
+ @attrs[:class] = @attrs[:class].nil? ? idc : "#{@attrs[:class]} #{idc}".strip
0
+ end
0
+
0
+ unless args.empty?
0
+ if args.last.respond_to? :to_hash
0
+ @attrs.merge! args.pop.to_hash