joho-Markaby 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG 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,69 @@
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.0"
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-08-23}
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"
17
+ ]
18
+ s.files = [
19
+ "CHANGELOG",
20
+ "Markaby.gemspec",
21
+ "README",
22
+ "Rakefile",
23
+ "VERSION",
24
+ "init.rb",
25
+ "lib/markaby.rb",
26
+ "lib/markaby/builder.rb",
27
+ "lib/markaby/cssproxy.rb",
28
+ "lib/markaby/kernel_method.rb",
29
+ "lib/markaby/metaid.rb",
30
+ "lib/markaby/rails.rb",
31
+ "lib/markaby/tags.rb",
32
+ "lib/markaby/template.rb",
33
+ "setup.rb",
34
+ "test/rails/markaby/_monkeys.mab",
35
+ "test/rails/markaby/broken.mab",
36
+ "test/rails/markaby/create.mab",
37
+ "test/rails/markaby/index.mab",
38
+ "test/rails/monkeys.html",
39
+ "test/rails/test_helper.rb",
40
+ "test/rails/test_preamble.rb",
41
+ "test/rails_test.rb",
42
+ "test/test_markaby.rb",
43
+ "tools/rakehelp.rb"
44
+ ]
45
+ s.homepage = %q{http://joho.github.com/markaby/}
46
+ s.rdoc_options = ["--charset=UTF-8"]
47
+ s.require_paths = ["lib"]
48
+ s.rubygems_version = %q{1.3.4}
49
+ s.summary = %q{Markup as Ruby, write HTML in your native Ruby tongue}
50
+ s.test_files = [
51
+ "test/rails/test_helper.rb",
52
+ "test/rails/test_preamble.rb",
53
+ "test/rails_test.rb",
54
+ "test/test_markaby.rb"
55
+ ]
56
+
57
+ if s.respond_to? :specification_version then
58
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
59
+ s.specification_version = 3
60
+
61
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
62
+ s.add_runtime_dependency(%q<builder>, [">= 2.0.0"])
63
+ else
64
+ s.add_dependency(%q<builder>, [">= 2.0.0"])
65
+ end
66
+ else
67
+ s.add_dependency(%q<builder>, [">= 2.0.0"])
68
+ end
69
+ end
data/README ADDED
@@ -0,0 +1,256 @@
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
+ == Using Markaby as a Ruby class
26
+
27
+ Markaby is flaming easy to call from your Ruby classes.
28
+
29
+ require 'markaby'
30
+
31
+ mab = Markaby::Builder.new
32
+ mab.html do
33
+ head { title "Boats.com" }
34
+ body do
35
+ h1 "Boats.com has great deals"
36
+ ul do
37
+ li "$49 for a canoe"
38
+ li "$39 for a raft"
39
+ li "$29 for a huge boot that floats and can fit 5 people"
40
+ end
41
+ end
42
+ end
43
+ puts mab.to_s
44
+
45
+ Markaby::Builder.new does take two arguments for passing in variables and
46
+ a helper object. You can also affix the block right on to the class.
47
+
48
+ See Markaby::Builder for all of that.
49
+
50
+ = A Note About <tt>instance_eval</tt>
51
+
52
+ The Markaby::Builder class is different from the normal Builder class,
53
+ since it uses <tt>instance_eval</tt> when running blocks. This cleans
54
+ up the appearance of the Markaby code you write. If <tt>instance_eval</tt>
55
+ was not used, the code would look like this:
56
+
57
+ mab = Markaby::Builder.new
58
+ mab.html do
59
+ mab.head { mab.title "Boats.com" }
60
+ mab.body do
61
+ mab.h1 "Boats.com has great deals"
62
+ end
63
+ end
64
+ puts mab.to_s
65
+
66
+ So, the advantage is the cleanliness of your code. The disadvantage is that
67
+ the block will run inside the Markaby::Builder object's scope. This means
68
+ that inside these blocks, <tt>self</tt> will be your Markaby::Builder object.
69
+ When you use instance variables in these blocks, they will be instance variables
70
+ of the Markaby::Builder object.
71
+
72
+ This doesn't affect Rails users, but when used in regular Ruby code, it can
73
+ be a bit disorienting. You are recommended to put your Markaby code in a
74
+ module where it won't mix with anything.
75
+
76
+ = The Six Steps of Markaby
77
+
78
+ If you dive right into Markaby, it'll probably make good sense, but you're
79
+ likely to run into a few kinks. Why not review these six steps and commit
80
+ them memory so you can really *know* what you're doing?
81
+
82
+ == 1. Element Classes
83
+
84
+ Element classes may be added by hooking methods onto container elements:
85
+
86
+ div.entry do
87
+ h2.entryTitle 'Son of WebPage'
88
+ div.entrySection %{by Anthony}
89
+ div.entryContent 'Okay, once again, the idea here is ...'
90
+ end
91
+
92
+ Which results in:
93
+
94
+ <div class="entry">
95
+ <h2 class="entryTitle">Son of WebPage</h2>
96
+ <div class="entrySection">by Anthony</div>
97
+ <div class="entryContent">Okay, once again, the idea here is ...</div>
98
+ </div>
99
+
100
+ == 2. Element IDs
101
+
102
+ IDs may be added by the use of bang methods:
103
+
104
+ div.page! {
105
+ div.content! {
106
+ h1 "A Short Short Saintly Dog"
107
+ }
108
+ }
109
+
110
+ Which results in:
111
+
112
+ <div id="page">
113
+ <div id="content">
114
+ <h1>A Short Short Saintly Dog</h1>
115
+ </div>
116
+ </div>
117
+
118
+ == 3. Validate Your XHTML 1.0 Output
119
+
120
+ If you'd like Markaby to help you assemble valid XHTML documents,
121
+ you can use the <tt>xhtml_transitional</tt> or <tt>xhtml_strict</tt>
122
+ methods in place of the normal <tt>html</tt> tag.
123
+
124
+ xhtml_strict do
125
+ head { ... }
126
+ body { ... }
127
+ end
128
+
129
+ This will add the XML instruction and the doctype tag to your document.
130
+ Also, a character set meta tag will be placed inside your <tt>head</tt>
131
+ tag.
132
+
133
+ Now, since Markaby knows which doctype you're using, it checks a big
134
+ list of valid tags and attributes before printing anything.
135
+
136
+ >> div :styl => "padding: 10px" do
137
+ >> img :src => "samorost.jpg"
138
+ >> end
139
+ InvalidHtmlError: no such attribute `styl'
140
+
141
+ Markaby will also make sure you don't use the same element ID twice!
142
+
143
+ == 4. Escape or No Escape?
144
+
145
+ Markaby uses a simple convention for escaping stuff: if a string
146
+ is an argument, it gets escaped. If the string is in a block, it
147
+ doesn't.
148
+
149
+ This is handy if you're using something like RedCloth or
150
+ RDoc inside an element. Pass the string back through the block
151
+ and it'll skip out of escaping.
152
+
153
+ div.comment { RedCloth.new(str).to_html }
154
+
155
+ But, if we have some raw text that needs escaping, pass it in
156
+ as an argument:
157
+
158
+ div.comment raw_str
159
+
160
+ One caveat: if you have other tags inside a block, the string
161
+ passed back will be ignored.
162
+
163
+ div.comment {
164
+ div.author "_why"
165
+ div.says "Torpedoooooes!"
166
+ "<div>Silence.</div>"
167
+ }
168
+
169
+ The final div above won't appear in the output. You can't mix
170
+ tag modes like that, friend.
171
+
172
+ == 5. Auto-stringification
173
+
174
+ If you end up using any of your Markaby "tags" as a string, the
175
+ tag won't be output. It'll be up to you to add the new string
176
+ back into the HTML output.
177
+
178
+ This means if you call <tt>to_s</tt>, you'll get a string back.
179
+
180
+ div.title { "Rock Bottom" + span(" by Robert Wyatt").to_s }
181
+
182
+ But, when you're adding strings in Ruby, <tt>to_s</tt> happens automatically.
183
+
184
+ div.title { "Rock Bottom" + span(" by Robert Wyatt") }
185
+
186
+ Interpolation works fine.
187
+
188
+ div.title { "Rock Bottom #{span(" by Robert Wyatt")}" }
189
+
190
+ And any other operation you might perform on a string.
191
+
192
+ div.menu! \
193
+ ['5.gets', 'bits', 'cult', 'inspect', '-h'].map do |category|
194
+ link_to category
195
+ end.
196
+ join( " | " )
197
+
198
+ == 6. The <tt>tag!</tt> Method
199
+
200
+ If you need to force a tag at any time, call <tt>tag!</tt> with the
201
+ tag name followed by the possible arguments and block. The CssProxy
202
+ won't work with this technique.
203
+
204
+ tag! :select, :id => "country_list" do
205
+ countries.each do |country|
206
+ tag! :option, country
207
+ end
208
+ end
209
+
210
+ = A Note About Rails Helpers
211
+
212
+ When used in Rails templates, the Rails helper object is passed into
213
+ Markaby::Builder. When you call helper methods inside Markaby, the output
214
+ from those methods will be output to the stream. This is incredibly
215
+ handy, since most Rails helpers output HTML tags.
216
+
217
+ head do
218
+ javascript_include_tag 'prototype'
219
+ autodiscovery_link_tag
220
+ end
221
+
222
+ However, some methods are designed to give back a String which you can use
223
+ elsewhere. That's okay! Every method returns a Fragment object, which can
224
+ be used as a string.
225
+
226
+ p { "Total is: #{number_to_human_size @file_bytes}" }
227
+
228
+ Also see the Quick Tour above, specifically the stuff about auto-stringification.
229
+
230
+ If for any reason you have trouble with fragments, you can just
231
+ call the <tt>@helpers</tt> object with the method and you'll get
232
+ the String back and nothing will be output.
233
+
234
+ p { "Total is: #{@helpers.number_to_human_size @file_bytes}" }
235
+
236
+ Conversely, you may call instance variables from your controller by using
237
+ a method and its value will be returned, nothing will be output.
238
+
239
+ # Inside imaginary ProductController
240
+ def list
241
+ @products = Product.find :all
242
+ end
243
+
244
+ # Inside app/views/product/list.mab
245
+ products.each do |product|
246
+ p product.title
247
+ end
248
+
249
+ = Credits
250
+
251
+ Markaby is a work of immense hope by Tim Fletcher and why the lucky stiff.
252
+ Thankyou for giving it a whirl.
253
+
254
+ Markaby is inspired by the HTML library within cgi.rb. Hopefully it will
255
+ turn around and take some cues.
256
+
data/Rakefile ADDED
@@ -0,0 +1,49 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/clean'
4
+ require 'rake/rdoctask'
5
+ require 'tools/rakehelp'
6
+ require 'fileutils'
7
+ include FileUtils
8
+
9
+ task :default => [:test]
10
+
11
+ setup_tests
12
+ setup_rdoc ['README', 'CHANGELOG', 'lib/**/*.rb']
13
+
14
+ begin
15
+ require 'jeweler'
16
+ Jeweler::Tasks.new do |gemspec|
17
+ gemspec.name = "Markaby"
18
+ gemspec.summary = "Markup as Ruby, write HTML in your native Ruby tongue"
19
+ gemspec.description = "Tim Fletcher and _why's ruby driven HTML templating system"
20
+ gemspec.email = "jrbarton@gmail.com"
21
+ gemspec.homepage = "http://joho.github.com/markaby/"
22
+ gemspec.authors = ["_why", "Tim Fletcher", "John Barton", "spox", "smtlaissezfaire"]
23
+ gemspec.add_dependency 'builder', '>=2.0.0'
24
+ end
25
+ rescue LoadError
26
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
27
+ end
28
+
29
+ desc "List any Markaby specific warnings"
30
+ task :warnings do
31
+ `ruby -w test/test_markaby.rb 2>&1`.split(/\n/).each do |line|
32
+ next unless line =~ /warning:/
33
+ next if line =~ /builder-/
34
+ puts line
35
+ end
36
+ end
37
+
38
+ desc "Start a Markaby-aware IRB session"
39
+ task :irb do
40
+ sh 'irb -I lib -r markaby -r markaby/kernel_method'
41
+ end
42
+
43
+ namespace :test do
44
+ desc ''
45
+ task :rails do
46
+ Dir.chdir '../../../'
47
+ sh 'rake test:plugins PLUGIN=markaby'
48
+ end
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.6.0
data/init.rb ADDED
@@ -0,0 +1,13 @@
1
+ $:.unshift File.expand_path(File.join(File.dirname(__FILE__), 'lib'))
2
+
3
+ require 'markaby'
4
+ require 'markaby/rails'
5
+
6
+
7
+ if defined? ActionView::Template and ActionView::Template.respond_to? :register_template_handler
8
+ ActionView::Template
9
+ else
10
+ ActionView::Base
11
+ end.register_template_handler(:mab, Markaby::Rails::ActionViewTemplateHandler)
12
+
13
+ ActionController::Base.send :include, Markaby::Rails::ActionControllerHelpers
data/lib/markaby.rb ADDED
@@ -0,0 +1,35 @@
1
+ # = About lib/markaby.rb
2
+ #
3
+ # By requiring <tt>lib/markaby</tt>, you can load Markaby's dependency (the Builder library,)
4
+ # as well as the full set of Markaby classes.
5
+ #
6
+ # For a full list of features and instructions, see the README.
7
+ $:.unshift File.expand_path(File.dirname(__FILE__))
8
+
9
+ # Markaby is a module containing all of the great Markaby classes that
10
+ # do such an excellent job.
11
+ #
12
+ # * Markaby::Builder: the class for actually calling the Ruby methods
13
+ # which write the HTML.
14
+ # * Markaby::CSSProxy: a class which adds element classes and IDs to
15
+ # elements when used within Markaby::Builder.
16
+ # * Markaby::MetAid: metaprogramming helper methods.
17
+ # * Markaby::Tags: lists the roles of various XHTML tags to help Builder
18
+ # use these tags as they are intended.
19
+ # * Markaby::Template: a class for hooking Markaby into Rails as a
20
+ # proper templating language.
21
+ module Markaby
22
+ VERSION = '0.5'
23
+
24
+ class InvalidXhtmlError < Exception; end
25
+ end
26
+
27
+ unless defined?(Builder)
28
+ require 'rubygems'
29
+ require 'builder'
30
+ end
31
+
32
+ require 'markaby/builder'
33
+ require 'markaby/cssproxy'
34
+ require 'markaby/metaid'
35
+ require 'markaby/template'