GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Rubygem
Fork of nex3/haml
Description: HTML Abstraction Markup Language - A Markup Haiku
Homepage: http://haml.hamptoncatlin.com
Clone URL: git://github.com/chriseppstein/haml.git
Changing the HTML-generation option back to :output, and documenting it.
nex3 (author)
Tue Feb 26 10:57:45 -0800 2008
commit  9996603e31669498125a01ff6edc11dca6bbd579
tree    6bddb32b242ed9f3a35c09eb72f5902edd27d695
parent  eb0ff140ea03d5f1255411022382460691825ecf
...
754
755
756
757
 
 
 
 
 
 
 
758
759
760
...
783
784
785
786
 
787
788
789
...
754
755
756
 
757
758
759
760
761
762
763
764
765
766
...
789
790
791
 
792
793
794
795
0
@@ -754,7 +754,13 @@ $LOAD_PATH << dir unless $LOAD_PATH.include?(dir)
0
 # from <tt>environment.rb</tt> in Rails,
0
 # or by passing an options hash to Haml::Engine.
0
 # Available options are:
0
-#
0
+#
0
+# [<tt>:output</tt>] Determines the output format. The default is :xhtml.
0
+# Currently, the only other option is :html4,
0
+# which is identical to :xhtml except that tags
0
+# that are normally auto-closed aren't,
0
+# and HTML4 doctypes are generated rather than XHTML.
0
+#
0
 # [<tt>:suppress_eval</tt>] Whether or not attribute hashes and Ruby scripts
0
 # designated by <tt>=</tt> or <tt>~</tt> should be
0
 # evaluated. If this is true, said scripts are
0
@@ -783,7 +789,7 @@ $LOAD_PATH << dir unless $LOAD_PATH.include?(dir)
0
 #
0
 # [<tt>:autoclose</tt>] A list of tag names that should be automatically self-closed
0
 # if they have no content.
0
-# Defaults to <tt>['meta', 'img', 'link', 'br', 'hr', 'input', 'area']</tt>.
0
+# Defaults to <tt>['meta', 'img', 'link', 'br', 'hr', 'input', 'area', 'param', 'col', 'base']</tt>.
0
 #
0
 module Haml
0
   # This method is called by init.rb,
...
24
25
26
 
 
 
 
 
 
 
 
 
 
27
28
29
...
37
38
39
 
 
40
41
42
...
48
49
50
51
 
52
53
54
 
 
 
 
55
56
57
...
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
...
47
48
49
50
51
52
53
54
...
60
61
62
 
63
64
65
66
67
68
69
70
71
72
73
0
@@ -24,6 +24,16 @@ module Haml
0
     # to produce the Haml document.
0
     attr :precompiled, true
0
 
0
+ # Returns whether or not this Engine instance is generating XHTML output.
0
+ def xhtml?
0
+ not html4?
0
+ end
0
+
0
+ # Returns whether or not this Engine instance is generating HTML4 output.
0
+ def html4?
0
+ @options[:output] == :html4
0
+ end
0
+
0
     # Creates a new instace of Haml::Engine that will compile the given
0
     # template string when <tt>render</tt> is called.
0
     # See README for available options.
0
@@ -37,6 +47,8 @@ module Haml
0
       @options = {
0
         :suppress_eval => false,
0
         :attr_wrapper => "'",
0
+
0
+ # Don't forget to update the docs in lib/haml.rb if you update these
0
         :autoclose => %w(meta img link br hr input area param col base),
0
         :filters => {
0
           'sass' => Haml::Filters::Sass,
0
@@ -48,10 +60,14 @@ module Haml
0
           'markdown' => Haml::Filters::Markdown },
0
         :filename => '(haml)',
0
         :ugly => false,
0
- :html4 => false
0
+ :output => :xhtml
0
       }
0
       @options.rec_merge! options
0
 
0
+ unless [:xhtml, :html4].include?(@options[:output])
0
+ raise Haml::Error, "Invalid output format #{@options[:output].inspect}"
0
+ end
0
+
0
       unless @options[:suppress_eval]
0
         @options[:filters].merge!({
0
           'erb' => Haml::Filters::ERB,
...
475
476
477
478
 
479
480
481
...
503
504
505
506
 
507
508
509
...
601
602
603
604
 
605
606
607
608
609
610
611
 
612
613
614
...
475
476
477
 
478
479
480
481
...
503
504
505
 
506
507
508
509
...
601
602
603
 
604
605
606
607
608
609
610
 
611
612
613
614
0
@@ -475,7 +475,7 @@ END
0
 
0
     def prerender_tag(name, self_close, attributes)
0
       attributes_string = Precompiler.build_attributes(@options[:attr_wrapper], attributes)
0
- "<#{name}#{attributes_string}#{self_close && !@options[:html4] ? ' /' : ''}>"
0
+ "<#{name}#{attributes_string}#{self_close && xhtml? ? ' /' : ''}>"
0
     end
0
     
0
     # Parses a line into tag_name, attributes, attributes_hash, object_ref, action, value
0
@@ -503,7 +503,7 @@ END
0
       raise SyntaxError.new("Illegal element: classes and ids must have values.") if attributes =~ /[\.#](\.|#|\z)/
0
 
0
       case action
0
- when '/'; atomic = !@options[:html4]
0
+ when '/'; atomic = xhtml?
0
       when '~'; parse = flattened = true
0
       when '='
0
         parse = true
0
@@ -601,14 +601,14 @@ END
0
     def text_for_doctype(text)
0
       text = text[3..-1].lstrip.downcase
0
       if text[0...3] == "xml"
0
- return nil if @options[:html4]
0
+ return nil if html4?
0
         wrapper = @options[:attr_wrapper]
0
         return "<?xml version=#{wrapper}1.0#{wrapper} encoding=#{wrapper}#{text.split(' ')[1] || "utf-8"}#{wrapper} ?>"
0
       end
0
 
0
       version, type = text.scan(DOCTYPE_REGEX)[0]
0
       
0
- unless @options[:html4]
0
+ unless html4?
0
         if version == "1.1"
0
           return '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">'
0
         end
...
395
396
397
 
 
 
 
 
 
 
 
 
398
399
400
401
402
 
 
403
404
405
406
 
407
408
409
410
 
411
412
413
414
 
415
416
417
418
419
 
420
421
...
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
 
 
410
411
412
413
414
 
415
416
417
418
 
419
420
421
422
 
423
424
425
426
427
 
428
429
430
0
@@ -395,27 +395,36 @@ class EngineTest < Test::Unit::TestCase
0
                  render("%p= 's' * 75", :ugly => true))
0
   end
0
 
0
+ def test_xhtml_output_option
0
+ assert_equal "<p>\n <br />\n</p>\n", render("%p\n %br", :output => :xhtml)
0
+ assert_equal "<a />\n", render("%a/", :output => :xhtml)
0
+ end
0
+
0
+ def test_arbitrary_output_option
0
+ assert_raise(Haml::Error, "Invalid output format :html1") { Haml::Engine.new("%br", :output => :html1) }
0
+ end
0
+
0
   # HTML 4.0
0
 
0
   def test_html_has_no_self_closing_tags
0
- assert_equal "<p>\n <br>\n</p>\n", render("%p\n %br", :html4 => :true)
0
- assert_equal "<br>\n", render("%br/", :html4 => :true)
0
+ assert_equal "<p>\n <br>\n</p>\n", render("%p\n %br", :output => :html4)
0
+ assert_equal "<br>\n", render("%br/", :output => :html4)
0
   end
0
 
0
   def test_html_renders_empty_node_with_closing_tag
0
- assert_equal %{<div class='foo'>\n</div>\n}, render(".foo", :html4 => :true)
0
+ assert_equal %{<div class='foo'>\n</div>\n}, render(".foo", :output => :html4)
0
   end
0
 
0
   def test_html_ignores_explicit_self_closing_declaration
0
- assert_equal "<a>\n</a>\n", render("%a/", :html4 => :true)
0
+ assert_equal "<a>\n</a>\n", render("%a/", :output => :html4)
0
   end
0
 
0
   def test_html_ignores_xml_prolog_declaration
0
- assert_equal "\n", render('!!! XML', :html4 => :true)
0
+ assert_equal "\n", render('!!! XML', :output => :html4)
0
   end
0
 
0
   def test_html_has_different_doctype
0
     assert_equal %{<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">\n},
0
- render('!!!', :html4 => :true)
0
+ render('!!!', :output => :html4)
0
   end
0
 end

Comments

    No one has commented yet.