Changeset 55
- Timestamp:
- 05/22/2006 02:06:16 (3 years ago)
- Location:
- branches/xhtml-careful
- Files:
-
- 4 modified
-
README (modified) (2 diffs)
-
lib/markaby.rb (modified) (1 diff)
-
lib/markaby/builder.rb (modified) (1 diff)
-
test/test_markaby.rb (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/xhtml-careful/README
r20 r55 133 133 IDs may be added by the use of bang methods: 134 134 135 div.page! 136 div.content! 135 div.page! { 136 div.content! { 137 137 h1 "A Short Short Saintly Dog" 138 end139 end138 } 139 } 140 140 141 141 Which results in: … … 156 156 end 157 157 158 == The <tt>capture</tt> Method 159 160 Want to catch a block of HTML as a string and play with it a bit? 161 Use the <tt>capture</tt> method. 162 163 Commonly used to join HTML blocks together: 158 == Markaby prevents invalid HTML 159 160 Since Markaby knows which doctype you're using, it checks a big 161 list of valid tags and attributes before printing anything. 162 163 >> div :styl => "padding: 10px" do 164 >> img :src => "samorost.jpg" 165 >> end 166 InvalidHtmlError: no such attribute `styl' 167 168 == Auto-stringification 169 170 If you end up using any of your Markaby "tags" as a string, the 171 tag won't be output. It'll be up to you to add the new string 172 back into the HTML output. 173 174 This means if you call <tt>to_s</tt>, you'll get a string back. 175 176 div.title { "Rock Bottom" + span(" by Robert Wyatt").to_s } 177 178 But, when you're adding strings in Ruby, <tt>to_s</tt> happens automatically. 179 180 div.title { "Rock Bottom" + span(" by Robert Wyatt") } 181 182 Interpolation works fine. 183 184 div.title { "Rock Bottom #{span(" by Robert Wyatt")}" } 185 186 And any other operation you might perform on a string. 164 187 165 188 div.menu! \ 166 189 ['5.gets', 'bits', 'cult', 'inspect', '-h'].map do |category| 167 capture { link_to category }190 link_to category 168 191 end. 169 192 join( " | " ) -
branches/xhtml-careful/lib/markaby.rb
r30 r55 21 21 module Markaby 22 22 VERSION = '0.4' 23 24 class InvalidXhtmlError < Exception; end 23 25 end 24 26 -
branches/xhtml-careful/lib/markaby/builder.rb
r54 r55 145 145 # the arguments are the same as the tags implemented via method_missing. 146 146 def tag!(tag, *args, &block) 147 if @tagset 148 if !@tagset.has_key?(tag) 149 raise InvalidXhtmlError, "no element `#{tag}' for #{doctype}" 150 elsif args.last.respond_to?(:to_hash) 151 attrs = args.last.to_hash 152 attrs.each do |k, v| 153 unless @tagset[tag].include? k 154 raise InvalidXhtmlError, "no attribute `#{k}' on #{tag} elements" 155 end 156 end 157 end 158 end 147 159 if block 148 160 str = capture &block -
branches/xhtml-careful/test/test_markaby.rb
r52 r55 14 14 class MarkabyTest < Test::Unit::TestCase 15 15 16 def assert_exception(str, exclass, exmsg) 17 begin 18 mab(str) 19 rescue Exception => e 20 assert_equal e.class, exclass 21 assert_equal e.message[/^.+?: (.+)$/, 1], exmsg 22 else 23 assert nil, exclass 24 end 25 end 26 16 27 def mab(string, assigns = {}, helpers = nil) 17 28 Markaby::Template.new(string.to_s).render(assigns, helpers) … … 64 75 mab(%q{div { @a = small 'Miniature'; @b = strong 'Large'; h1 "Monkeys"; h2 { "Giraffes #{@a} and #{@b}" }; h3 "Donkeys"; h4 { "Parakeet #{@b} as well..." }}}) 65 76 end 77 78 def test_invalid_xhtml 79 assert_exception %{dav {}}, NoMethodError, "no such method `dav'" 80 assert_exception %{div(:styl => 'ok') {}}, Markaby::InvalidXhtmlError, "no attribute `styl' on div elements" 81 assert_exception %{tbody.okay {}}, Markaby::InvalidXhtmlError, "no attribute `class' on tbody elements" 82 end 66 83 end