Markaby 0.6.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. data/.gitignore +4 -0
  2. data/CHANGELOG.rdoc +43 -0
  3. data/Markaby.gemspec +100 -0
  4. data/README.rdoc +262 -0
  5. data/Rakefile +48 -0
  6. data/VERSION +1 -0
  7. data/garlic.rb +29 -0
  8. data/init.rb +6 -0
  9. data/lib/markaby/builder.rb +275 -0
  10. data/lib/markaby/builder_tags.rb +64 -0
  11. data/lib/markaby/cssproxy.rb +55 -0
  12. data/lib/markaby/kernel_method.rb +7 -0
  13. data/lib/markaby/rails/current.rb +41 -0
  14. data/lib/markaby/rails/deprecated.rb +122 -0
  15. data/lib/markaby/rails.rb +68 -0
  16. data/lib/markaby/tags.rb +193 -0
  17. data/lib/markaby/tilt.rb +21 -0
  18. data/lib/markaby.rb +30 -0
  19. data/spec/markaby/builder_spec.rb +40 -0
  20. data/spec/markaby/css_proxy_spec.rb +44 -0
  21. data/spec/markaby/fragment_spec.rb +7 -0
  22. data/spec/markaby/markaby_other_static.mab +1 -0
  23. data/spec/markaby/markaby_spec.rb +207 -0
  24. data/spec/markaby/rails/spec_helper.rb +20 -0
  25. data/spec/markaby/rails/views/markaby/_a_partial.mab +3 -0
  26. data/spec/markaby/rails/views/markaby/_partial_child_with_locals.mab +1 -0
  27. data/spec/markaby/rails/views/markaby/access_to_helpers.mab +1 -0
  28. data/spec/markaby/rails/views/markaby/broken.mab +7 -0
  29. data/spec/markaby/rails/views/markaby/correct_template_values.mab +5 -0
  30. data/spec/markaby/rails/views/markaby/no_values_passed.mab +3 -0
  31. data/spec/markaby/rails/views/markaby/partial_parent.mab +1 -0
  32. data/spec/markaby/rails/views/markaby/partial_parent_with_locals.mab +7 -0
  33. data/spec/markaby/rails/views/markaby/render_erb_without_explicit_render_call.erb +1 -0
  34. data/spec/markaby/rails/views/markaby/render_explicit_but_empty_markaby_layout.mab +0 -0
  35. data/spec/markaby/rails/views/markaby/render_mab_without_explicit_render_call.mab +3 -0
  36. data/spec/markaby/rails/views/markaby/render_with_ivar.mab +3 -0
  37. data/spec/markaby/rails/views/markaby/renders_erb.rhtml +1 -0
  38. data/spec/markaby/rails_spec.rb +190 -0
  39. data/spec/markaby/rails_version_spec.rb +37 -0
  40. data/spec/markaby/tilt/erb.erb +1 -0
  41. data/spec/markaby/tilt/locals.mab +1 -0
  42. data/spec/markaby/tilt/markaby.mab +1 -0
  43. data/spec/markaby/tilt/markaby_other_static.mab +1 -0
  44. data/spec/markaby/tilt/render_twice.mab +1 -0
  45. data/spec/markaby/tilt/scope.mab +1 -0
  46. data/spec/markaby/tilt/yielding.mab +2 -0
  47. data/spec/markaby/tilt_spec.rb +86 -0
  48. data/spec/markaby/xml_markup_spec.rb +9 -0
  49. data/spec/spec.opts +2 -0
  50. data/spec/spec_helper.rb +39 -0
  51. metadata +126 -0
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ doc
2
+ coverage
3
+ .garlic
4
+ pkg
data/CHANGELOG.rdoc ADDED
@@ -0,0 +1,43 @@
1
+ = 0.6
2
+ === 23 August, 2009
3
+
4
+ * Canonical repo changed to http://github.com/joho/markaby
5
+ * Gem moved to Github (using jeweler)
6
+ * Rails init process changed to work with rails > 2.1
7
+ * Default attributes on the root (<html>) element can now be overidden
8
+ * Reworked CssProxy, allowing attributes on hr and br
9
+ * Added Kernel#mab convenience method (require 'markaby/kernel_method')
10
+ * WhenOnRails: Can now use :locals with render_markaby
11
+ * WhenOnRails: Template errors now report line number
12
+
13
+ = 0.5
14
+ === 03 October, 2006
15
+
16
+ * XHTML Validation built in. So, if you have an invalid tag: error. Invalid attribute: error.
17
+ And two identical IDs in the same document: error. Optional, of course. But handy!
18
+ * New Markaby::Fragment class adds much flexibility. If it discovers you are using a tag as a string,
19
+ the tag is removed from the stream. (<tt>div { strong("Real") + " Giraffes" }</tt>)
20
+ * The prevailing rule now is: if you want it escaped, pass it as an arg. If not, pass it to a block.
21
+ * Again, escaped: <tt>h1("Me & You Have a Giraffe")</tt>
22
+ * And, not escaped: <tt>h1 { "<a href='/'>Home</a>" }</tt>
23
+ * Less method_missing, meaning: faster calls all around. Tag methods generated based on doctype.
24
+ * 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.
25
+ * The <tt>img</tt> method doesn't try to inject an empty alt tag and a zero border. No more of that.
26
+
27
+ = 0.3
28
+ === 02nd February, 2006
29
+
30
+ * Allow Markaby::Builder.new without args.
31
+ * Rails helper method render_markaby.
32
+
33
+ = 0.2
34
+ === 17th January, 2006
35
+
36
+ * Public announcement.
37
+ * DOCTYPES, head tags.
38
+ * Works with Rails helpers.
39
+
40
+ = 0.1
41
+ === 05th January, 2006
42
+
43
+ * Initial import.
data/Markaby.gemspec ADDED
@@ -0,0 +1,100 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{Markaby}
8
+ s.version = "0.6.3"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["_why", "Tim Fletcher", "John Barton", "spox", "smtlaissezfaire"]
12
+ s.date = %q{2009-10-10}
13
+ s.description = %q{Tim Fletcher and _why's ruby driven HTML templating system}
14
+ s.email = %q{jrbarton@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "README.rdoc"
17
+ ]
18
+ s.files = [
19
+ ".gitignore",
20
+ "CHANGELOG.rdoc",
21
+ "Markaby.gemspec",
22
+ "README.rdoc",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "garlic.rb",
26
+ "init.rb",
27
+ "lib/markaby.rb",
28
+ "lib/markaby/builder.rb",
29
+ "lib/markaby/builder_tags.rb",
30
+ "lib/markaby/cssproxy.rb",
31
+ "lib/markaby/kernel_method.rb",
32
+ "lib/markaby/rails.rb",
33
+ "lib/markaby/rails/current.rb",
34
+ "lib/markaby/rails/deprecated.rb",
35
+ "lib/markaby/tags.rb",
36
+ "lib/markaby/tilt.rb",
37
+ "spec/markaby/builder_spec.rb",
38
+ "spec/markaby/css_proxy_spec.rb",
39
+ "spec/markaby/fragment_spec.rb",
40
+ "spec/markaby/markaby_other_static.mab",
41
+ "spec/markaby/markaby_spec.rb",
42
+ "spec/markaby/rails/spec_helper.rb",
43
+ "spec/markaby/rails/views/markaby/_a_partial.mab",
44
+ "spec/markaby/rails/views/markaby/_partial_child_with_locals.mab",
45
+ "spec/markaby/rails/views/markaby/access_to_helpers.mab",
46
+ "spec/markaby/rails/views/markaby/broken.mab",
47
+ "spec/markaby/rails/views/markaby/correct_template_values.mab",
48
+ "spec/markaby/rails/views/markaby/no_values_passed.mab",
49
+ "spec/markaby/rails/views/markaby/partial_parent.mab",
50
+ "spec/markaby/rails/views/markaby/partial_parent_with_locals.mab",
51
+ "spec/markaby/rails/views/markaby/render_erb_without_explicit_render_call.erb",
52
+ "spec/markaby/rails/views/markaby/render_explicit_but_empty_markaby_layout.mab",
53
+ "spec/markaby/rails/views/markaby/render_mab_without_explicit_render_call.mab",
54
+ "spec/markaby/rails/views/markaby/render_with_ivar.mab",
55
+ "spec/markaby/rails/views/markaby/renders_erb.rhtml",
56
+ "spec/markaby/rails_spec.rb",
57
+ "spec/markaby/rails_version_spec.rb",
58
+ "spec/markaby/tilt/erb.erb",
59
+ "spec/markaby/tilt/locals.mab",
60
+ "spec/markaby/tilt/markaby.mab",
61
+ "spec/markaby/tilt/markaby_other_static.mab",
62
+ "spec/markaby/tilt/render_twice.mab",
63
+ "spec/markaby/tilt/scope.mab",
64
+ "spec/markaby/tilt/yielding.mab",
65
+ "spec/markaby/tilt_spec.rb",
66
+ "spec/markaby/xml_markup_spec.rb",
67
+ "spec/spec.opts",
68
+ "spec/spec_helper.rb"
69
+ ]
70
+ s.homepage = %q{http://joho.github.com/markaby/}
71
+ s.rdoc_options = ["--charset=UTF-8"]
72
+ s.require_paths = ["lib"]
73
+ s.rubygems_version = %q{1.3.5}
74
+ s.summary = %q{Markup as Ruby, write HTML in your native Ruby tongue}
75
+ s.test_files = [
76
+ "spec/markaby/builder_spec.rb",
77
+ "spec/markaby/css_proxy_spec.rb",
78
+ "spec/markaby/fragment_spec.rb",
79
+ "spec/markaby/markaby_spec.rb",
80
+ "spec/markaby/rails/spec_helper.rb",
81
+ "spec/markaby/rails_spec.rb",
82
+ "spec/markaby/rails_version_spec.rb",
83
+ "spec/markaby/tilt_spec.rb",
84
+ "spec/markaby/xml_markup_spec.rb",
85
+ "spec/spec_helper.rb"
86
+ ]
87
+
88
+ if s.respond_to? :specification_version then
89
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
90
+ s.specification_version = 3
91
+
92
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
93
+ s.add_runtime_dependency(%q<builder>, [">= 2.0.0"])
94
+ else
95
+ s.add_dependency(%q<builder>, [">= 2.0.0"])
96
+ end
97
+ else
98
+ s.add_dependency(%q<builder>, [">= 2.0.0"])
99
+ end
100
+ end
data/README.rdoc ADDED
@@ -0,0 +1,262 @@
1
+ = Markaby (Markup as Ruby)
2
+
3
+ Markaby is a very short bit of code for writing HTML pages in pure Ruby.
4
+ It is an alternative to ERb which weaves the two languages together.
5
+ Also a replacement for templating languages which use primitive languages
6
+ that blend with HTML.
7
+
8
+ == Using Markaby as a Rails plugin
9
+
10
+ Write Rails templates in pure Ruby. Example layout:
11
+
12
+ html do
13
+ head do
14
+ title 'Products: ' + action_name
15
+ stylesheet_link_tag 'scaffold'
16
+ end
17
+
18
+ body do
19
+ p flash[:notice], :style => "color: green"
20
+
21
+ self << content_for_layout
22
+ end
23
+ end
24
+
25
+ Markaby supports many versions of rails:
26
+
27
+ 1.2.2, 1.2.3, 1.2.4, 1.2.5, 1.2.6, 2.1.0, 2.1.1, 2.1.2, 2.2.0,
28
+ 2.2.1, 2.2.2, 2.2.3, 2.3.1, 2.3.2, 2.3.2.1, 2.3.3, 2.3.3.1, 2.3.4
29
+
30
+ == Using Markaby as a Ruby class
31
+
32
+ Markaby is flaming easy to call from your Ruby classes.
33
+
34
+ require 'markaby'
35
+
36
+ mab = Markaby::Builder.new
37
+ mab.html do
38
+ head { title "Boats.com" }
39
+ body do
40
+ h1 "Boats.com has great deals"
41
+ ul do
42
+ li "$49 for a canoe"
43
+ li "$39 for a raft"
44
+ li "$29 for a huge boot that floats and can fit 5 people"
45
+ end
46
+ end
47
+ end
48
+ puts mab.to_s
49
+
50
+ Markaby::Builder.new does take two arguments for passing in variables and
51
+ a helper object. You can also affix the block right on to the class.
52
+
53
+ See Markaby::Builder for all of that.
54
+
55
+ = A Note About <tt>instance_eval</tt>
56
+
57
+ The Markaby::Builder class is different from the normal Builder class,
58
+ since it uses <tt>instance_eval</tt> when running blocks. This cleans
59
+ up the appearance of the Markaby code you write. If <tt>instance_eval</tt>
60
+ was not used, the code would look like this:
61
+
62
+ mab = Markaby::Builder.new
63
+ mab.html do
64
+ mab.head { mab.title "Boats.com" }
65
+ mab.body do
66
+ mab.h1 "Boats.com has great deals"
67
+ end
68
+ end
69
+ puts mab.to_s
70
+
71
+ So, the advantage is the cleanliness of your code. The disadvantage is that
72
+ the block will run inside the Markaby::Builder object's scope. This means
73
+ that inside these blocks, <tt>self</tt> will be your Markaby::Builder object.
74
+ When you use instance variables in these blocks, they will be instance variables
75
+ of the Markaby::Builder object.
76
+
77
+ This doesn't affect Rails users, but when used in regular Ruby code, it can
78
+ be a bit disorienting. You are recommended to put your Markaby code in a
79
+ module where it won't mix with anything.
80
+
81
+ = The Six Steps of Markaby
82
+
83
+ If you dive right into Markaby, it'll probably make good sense, but you're
84
+ likely to run into a few kinks. Why not review these six steps and commit
85
+ them memory so you can really *know* what you're doing?
86
+
87
+ == 1. Element Classes
88
+
89
+ Element classes may be added by hooking methods onto container elements:
90
+
91
+ div.entry do
92
+ h2.entryTitle 'Son of WebPage'
93
+ div.entrySection %{by Anthony}
94
+ div.entryContent 'Okay, once again, the idea here is ...'
95
+ end
96
+
97
+ Which results in:
98
+
99
+ <div class="entry">
100
+ <h2 class="entryTitle">Son of WebPage</h2>
101
+ <div class="entrySection">by Anthony</div>
102
+ <div class="entryContent">Okay, once again, the idea here is ...</div>
103
+ </div>
104
+
105
+ == 2. Element IDs
106
+
107
+ IDs may be added by the use of bang methods:
108
+
109
+ div.page! {
110
+ div.content! {
111
+ h1 "A Short Short Saintly Dog"
112
+ }
113
+ }
114
+
115
+ Which results in:
116
+
117
+ <div id="page">
118
+ <div id="content">
119
+ <h1>A Short Short Saintly Dog</h1>
120
+ </div>
121
+ </div>
122
+
123
+ == 3. Validate Your XHTML 1.0 Output
124
+
125
+ If you'd like Markaby to help you assemble valid XHTML documents,
126
+ you can use the <tt>xhtml_transitional</tt> or <tt>xhtml_strict</tt>
127
+ methods in place of the normal <tt>html</tt> tag.
128
+
129
+ xhtml_strict do
130
+ head { ... }
131
+ body { ... }
132
+ end
133
+
134
+ This will add the XML instruction and the doctype tag to your document.
135
+ Also, a character set meta tag will be placed inside your <tt>head</tt>
136
+ tag.
137
+
138
+ Now, since Markaby knows which doctype you're using, it checks a big
139
+ list of valid tags and attributes before printing anything.
140
+
141
+ >> div :styl => "padding: 10px" do
142
+ >> img :src => "samorost.jpg"
143
+ >> end
144
+ InvalidHtmlError: no such attribute `styl'
145
+
146
+ Markaby will also make sure you don't use the same element ID twice!
147
+
148
+ == 4. Escape or No Escape?
149
+
150
+ Markaby uses a simple convention for escaping stuff: if a string
151
+ is an argument, it gets escaped. If the string is in a block, it
152
+ doesn't.
153
+
154
+ This is handy if you're using something like RedCloth or
155
+ RDoc inside an element. Pass the string back through the block
156
+ and it'll skip out of escaping.
157
+
158
+ div.comment { RedCloth.new(str).to_html }
159
+
160
+ But, if we have some raw text that needs escaping, pass it in
161
+ as an argument:
162
+
163
+ div.comment raw_str
164
+
165
+ One caveat: if you have other tags inside a block, the string
166
+ passed back will be ignored.
167
+
168
+ div.comment {
169
+ div.author "_why"
170
+ div.says "Torpedoooooes!"
171
+ "<div>Silence.</div>"
172
+ }
173
+
174
+ The final div above won't appear in the output. You can't mix
175
+ tag modes like that, friend.
176
+
177
+ == 5. Auto-stringification
178
+
179
+ If you end up using any of your Markaby "tags" as a string, the
180
+ tag won't be output. It'll be up to you to add the new string
181
+ back into the HTML output.
182
+
183
+ This means if you call <tt>to_s</tt>, you'll get a string back.
184
+
185
+ div.title { "Rock Bottom" + span(" by Robert Wyatt").to_s }
186
+
187
+ But, when you're adding strings in Ruby, <tt>to_s</tt> happens automatically.
188
+
189
+ div.title { "Rock Bottom" + span(" by Robert Wyatt") }
190
+
191
+ Interpolation works fine.
192
+
193
+ div.title { "Rock Bottom #{span(" by Robert Wyatt")}" }
194
+
195
+ And any other operation you might perform on a string.
196
+
197
+ div.menu! \
198
+ ['5.gets', 'bits', 'cult', 'inspect', '-h'].map do |category|
199
+ link_to category
200
+ end.
201
+ join( " | " )
202
+
203
+ == 6. The <tt>tag!</tt> Method
204
+
205
+ If you need to force a tag at any time, call <tt>tag!</tt> with the
206
+ tag name followed by the possible arguments and block. The CssProxy
207
+ won't work with this technique.
208
+
209
+ tag! :select, :id => "country_list" do
210
+ countries.each do |country|
211
+ tag! :option, country
212
+ end
213
+ end
214
+
215
+ = A Note About Rails Helpers
216
+
217
+ When used in Rails templates, the Rails helper object is passed into
218
+ Markaby::Builder. When you call helper methods inside Markaby, the output
219
+ from those methods will be output to the stream. This is incredibly
220
+ handy, since most Rails helpers output HTML tags.
221
+
222
+ head do
223
+ javascript_include_tag 'prototype'
224
+ autodiscovery_link_tag
225
+ end
226
+
227
+ However, some methods are designed to give back a String which you can use
228
+ elsewhere. That's okay! Every method returns a Fragment object, which can
229
+ be used as a string.
230
+
231
+ p { "Total is: #{number_to_human_size @file_bytes}" }
232
+
233
+ Also see the Quick Tour above, specifically the stuff about auto-stringification.
234
+
235
+ If for any reason you have trouble with fragments, you can just
236
+ call the <tt>@helpers</tt> object with the method and you'll get
237
+ the String back and nothing will be output.
238
+
239
+ p { "Total is: #{@helpers.number_to_human_size @file_bytes}" }
240
+
241
+ Conversely, you may call instance variables from your controller by using
242
+ a method and its value will be returned, nothing will be output.
243
+
244
+ # Inside imaginary ProductController
245
+ def list
246
+ @products = Product.find :all
247
+ end
248
+
249
+ # Inside app/views/product/list.mab
250
+ products.each do |product|
251
+ p product.title
252
+ end
253
+
254
+ = Credits
255
+
256
+ Markaby is a work of immense hope by Tim Fletcher and why the lucky stiff.
257
+ It is maintained by joho, spox, and smtlaissezfaire.
258
+ Thankyou for giving it a whirl.
259
+
260
+ Markaby is inspired by the HTML library within cgi.rb. Hopefully it will
261
+ turn around and take some cues.
262
+
data/Rakefile ADDED
@@ -0,0 +1,48 @@
1
+ require 'rake'
2
+ require 'spec/rake/spectask'
3
+ require 'rake/clean'
4
+ require 'rake/rdoctask'
5
+
6
+ task :default => :spec
7
+
8
+ desc 'Run the specs'
9
+ Spec::Rake::SpecTask.new do |t|
10
+ t.warning = false
11
+ t.spec_opts = ["--color"]
12
+ end
13
+
14
+ Rake::RDocTask.new do |rdoc|
15
+ rdoc.rdoc_dir = 'doc/rdoc'
16
+ rdoc.options << '--line-numbers'
17
+ rdoc.rdoc_files.add(['README.rdoc', 'CHANGELOG.rdoc', 'lib/**/*.rb'])
18
+ end
19
+
20
+ begin
21
+ require 'jeweler'
22
+
23
+ Jeweler::Tasks.new do |gemspec|
24
+ gemspec.name = "Markaby"
25
+ gemspec.summary = "Markup as Ruby, write HTML in your native Ruby tongue"
26
+ gemspec.description = "Tim Fletcher and _why's ruby driven HTML templating system"
27
+ gemspec.email = "jrbarton@gmail.com"
28
+ gemspec.homepage = "http://joho.github.com/markaby/"
29
+ gemspec.authors = ["_why", "Tim Fletcher", "John Barton", "spox", "smtlaissezfaire"]
30
+ gemspec.add_dependency 'builder', '>=2.0.0'
31
+ end
32
+ rescue LoadError
33
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
34
+ end
35
+
36
+ desc "List any Markaby specific warnings"
37
+ task :warnings do
38
+ `ruby -w test/test_markaby.rb 2>&1`.split(/\n/).each do |line|
39
+ next unless line =~ /warning:/
40
+ next if line =~ /builder-/
41
+ puts line
42
+ end
43
+ end
44
+
45
+ desc "Start a Markaby-aware IRB session"
46
+ task :irb do
47
+ sh 'irb -I lib -r markaby -r markaby/kernel_method'
48
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.6.3
data/garlic.rb ADDED
@@ -0,0 +1,29 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/lib/markaby/rails")
2
+
3
+ garlic do
4
+ # this plugin
5
+ repo "markaby", :path => '.'
6
+
7
+ # other repos
8
+ repo "rails", :url => "git://github.com/rails/rails"
9
+
10
+ # target railses
11
+ RAILS_TAREGETS = Markaby::Rails::SUPPORTED_RAILS_VERSIONS.map do |version|
12
+ "v#{version}"
13
+ end
14
+
15
+ RAILS_TAREGETS.each do |rails|
16
+ # declare how to prepare, and run each CI target
17
+ target "Rails: #{rails}", :tree_ish => rails do
18
+ prepare do
19
+ plugin "markaby", :clone => true # so we can work in targets
20
+ end
21
+
22
+ run do
23
+ cd "vendor/plugins/markaby" do
24
+ sh "rake"
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
data/init.rb ADDED
@@ -0,0 +1,6 @@
1
+ $:.unshift File.expand_path(File.join(File.dirname(__FILE__), 'lib'))
2
+
3
+ require 'markaby'
4
+ require 'markaby/rails'
5
+
6
+ Markaby::Rails.load