haml 2.0.10 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of haml might be problematic. Click here for more details.

Files changed (107) hide show
  1. data/.yardopts +5 -0
  2. data/MIT-LICENSE +1 -1
  3. data/README.md +347 -0
  4. data/Rakefile +124 -19
  5. data/VERSION +1 -1
  6. data/VERSION_NAME +1 -0
  7. data/extra/haml-mode.el +397 -78
  8. data/extra/sass-mode.el +148 -36
  9. data/extra/update_watch.rb +13 -0
  10. data/lib/haml.rb +15 -993
  11. data/lib/haml/buffer.rb +131 -84
  12. data/lib/haml/engine.rb +129 -97
  13. data/lib/haml/error.rb +7 -7
  14. data/lib/haml/exec.rb +127 -42
  15. data/lib/haml/filters.rb +107 -42
  16. data/lib/haml/helpers.rb +210 -156
  17. data/lib/haml/helpers/action_view_extensions.rb +34 -39
  18. data/lib/haml/helpers/action_view_mods.rb +132 -139
  19. data/lib/haml/html.rb +77 -65
  20. data/lib/haml/precompiler.rb +404 -213
  21. data/lib/haml/shared.rb +78 -0
  22. data/lib/haml/template.rb +14 -14
  23. data/lib/haml/template/patch.rb +2 -2
  24. data/lib/haml/template/plugin.rb +2 -3
  25. data/lib/haml/util.rb +211 -6
  26. data/lib/haml/version.rb +30 -13
  27. data/lib/sass.rb +7 -856
  28. data/lib/sass/css.rb +169 -161
  29. data/lib/sass/engine.rb +344 -328
  30. data/lib/sass/environment.rb +79 -0
  31. data/lib/sass/error.rb +33 -11
  32. data/lib/sass/files.rb +139 -0
  33. data/lib/sass/plugin.rb +160 -117
  34. data/lib/sass/plugin/merb.rb +7 -6
  35. data/lib/sass/plugin/rails.rb +5 -6
  36. data/lib/sass/repl.rb +58 -0
  37. data/lib/sass/script.rb +59 -0
  38. data/lib/sass/script/bool.rb +17 -0
  39. data/lib/sass/script/color.rb +183 -0
  40. data/lib/sass/script/funcall.rb +50 -0
  41. data/lib/sass/script/functions.rb +198 -0
  42. data/lib/sass/script/lexer.rb +178 -0
  43. data/lib/sass/script/literal.rb +177 -0
  44. data/lib/sass/script/node.rb +14 -0
  45. data/lib/sass/script/number.rb +381 -0
  46. data/lib/sass/script/operation.rb +45 -0
  47. data/lib/sass/script/parser.rb +172 -0
  48. data/lib/sass/script/string.rb +12 -0
  49. data/lib/sass/script/unary_operation.rb +34 -0
  50. data/lib/sass/script/variable.rb +31 -0
  51. data/lib/sass/tree/comment_node.rb +73 -10
  52. data/lib/sass/tree/debug_node.rb +30 -0
  53. data/lib/sass/tree/directive_node.rb +42 -17
  54. data/lib/sass/tree/file_node.rb +41 -0
  55. data/lib/sass/tree/for_node.rb +48 -0
  56. data/lib/sass/tree/if_node.rb +54 -0
  57. data/lib/sass/tree/mixin_def_node.rb +29 -0
  58. data/lib/sass/tree/mixin_node.rb +48 -0
  59. data/lib/sass/tree/node.rb +214 -11
  60. data/lib/sass/tree/prop_node.rb +109 -0
  61. data/lib/sass/tree/rule_node.rb +178 -51
  62. data/lib/sass/tree/variable_node.rb +34 -0
  63. data/lib/sass/tree/while_node.rb +31 -0
  64. data/test/haml/engine_test.rb +331 -36
  65. data/test/haml/helper_test.rb +12 -1
  66. data/test/haml/results/content_for_layout.xhtml +0 -3
  67. data/test/haml/results/filters.xhtml +2 -0
  68. data/test/haml/results/list.xhtml +1 -1
  69. data/test/haml/template_test.rb +7 -2
  70. data/test/haml/templates/content_for_layout.haml +0 -2
  71. data/test/haml/templates/list.haml +1 -1
  72. data/test/haml/util_test.rb +92 -0
  73. data/test/sass/css2sass_test.rb +69 -24
  74. data/test/sass/engine_test.rb +586 -64
  75. data/test/sass/functions_test.rb +125 -0
  76. data/test/sass/more_results/more1.css +9 -0
  77. data/test/sass/more_results/more1_with_line_comments.css +26 -0
  78. data/test/sass/more_results/more_import.css +29 -0
  79. data/test/sass/more_templates/_more_partial.sass +2 -0
  80. data/test/sass/more_templates/more1.sass +23 -0
  81. data/test/sass/more_templates/more_import.sass +11 -0
  82. data/test/sass/plugin_test.rb +81 -28
  83. data/test/sass/results/line_numbers.css +49 -0
  84. data/test/sass/results/{constants.css → script.css} +4 -4
  85. data/test/sass/results/subdir/subdir.css +2 -0
  86. data/test/sass/results/units.css +11 -0
  87. data/test/sass/script_test.rb +258 -0
  88. data/test/sass/templates/import.sass +1 -1
  89. data/test/sass/templates/importee.sass +7 -2
  90. data/test/sass/templates/line_numbers.sass +13 -0
  91. data/test/sass/templates/{constants.sass → script.sass} +11 -10
  92. data/test/sass/templates/subdir/nested_subdir/_nested_partial.sass +2 -0
  93. data/test/sass/templates/subdir/subdir.sass +2 -2
  94. data/test/sass/templates/units.sass +11 -0
  95. data/test/test_helper.rb +14 -0
  96. metadata +77 -19
  97. data/FAQ +0 -138
  98. data/README.rdoc +0 -319
  99. data/lib/sass/constant.rb +0 -216
  100. data/lib/sass/constant/color.rb +0 -101
  101. data/lib/sass/constant/literal.rb +0 -54
  102. data/lib/sass/constant/nil.rb +0 -9
  103. data/lib/sass/constant/number.rb +0 -87
  104. data/lib/sass/constant/operation.rb +0 -30
  105. data/lib/sass/constant/string.rb +0 -22
  106. data/lib/sass/tree/attr_node.rb +0 -57
  107. data/lib/sass/tree/value_node.rb +0 -20
@@ -1,45 +1,40 @@
1
1
  require 'haml/helpers/action_view_mods'
2
2
 
3
- if defined?(ActionView)
4
- module Haml
5
- module Helpers
6
- # This module contains various useful helper methods
7
- # that either tie into ActionView or the rest of the ActionPack stack,
8
- # or are only useful in that context.
9
- # Thus, the methods defined here are only available
10
- # if ActionView is installed.
11
- module ActionViewExtensions
12
- # Returns a value for the "class" attribute
13
- # unique to this controller/action pair.
14
- # This can be used to target styles specifically at this action or controller.
15
- # For example, if the current action were EntryController#show,
16
- #
17
- # %div{:class => page_class} My Div
18
- #
19
- # would become
20
- #
21
- # <div class="entry show">My Div</div>
22
- #
23
- # Then, in a stylesheet
24
- # (shown here as Sass),
25
- # you could refer to this specific action:
26
- #
27
- # .entry.show
28
- # :font-weight bold
29
- #
30
- # or to all actions in the entry controller:
31
- #
32
- # .entry
33
- # :color #00f
34
- #
35
- def page_class
36
- controller.controller_name + " " + controller.action_name
37
- end
38
-
39
- # :stopdoc:
40
- alias_method :generate_content_class_names, :page_class
41
- # :startdoc:
3
+ module Haml
4
+ module Helpers
5
+ # This module contains various useful helper methods
6
+ # that either tie into ActionView or the rest of the ActionPack stack,
7
+ # or are only useful in that context.
8
+ # Thus, the methods defined here are only available
9
+ # if ActionView is installed.
10
+ module ActionViewExtensions
11
+ # Returns a value for the "class" attribute
12
+ # unique to this controller/action pair.
13
+ # This can be used to target styles specifically at this action or controller.
14
+ # For example, if the current action were `EntryController#show`,
15
+ #
16
+ # %div{:class => page_class} My Div
17
+ #
18
+ # would become
19
+ #
20
+ # <div class="entry show">My Div</div>
21
+ #
22
+ # Then, in a stylesheet (shown here as {Sass}),
23
+ # you could refer to this specific action:
24
+ #
25
+ # .entry.show
26
+ # font-weight: bold
27
+ #
28
+ # or to all actions in the entry controller:
29
+ #
30
+ # .entry
31
+ # color: #00f
32
+ #
33
+ # @return [String] The class name for the current page
34
+ def page_class
35
+ controller.controller_name + " " + controller.action_name
42
36
  end
37
+ alias_method :generate_content_class_names, :page_class
43
38
  end
44
39
  end
45
40
  end
@@ -1,166 +1,139 @@
1
- if defined?(ActionView) and not defined?(Merb::Plugins)
2
- module ActionView
3
- class Base # :nodoc:
4
- def render_with_haml(*args, &block)
5
- options = args.first
6
-
7
- # If render :layout is used with a block,
8
- # it concats rather than returning a string
9
- # so we need it to keep thinking it's Haml
10
- # until it hits the sub-render
11
- if is_haml? && !(options.is_a?(Hash) && options[:layout] && block_given?)
12
- return non_haml { render_without_haml(*args, &block) }
13
- end
14
- render_without_haml(*args, &block)
1
+ module ActionView
2
+ class Base
3
+ def render_with_haml(*args, &block)
4
+ options = args.first
5
+
6
+ # If render :layout is used with a block,
7
+ # it concats rather than returning a string
8
+ # so we need it to keep thinking it's Haml
9
+ # until it hits the sub-render
10
+ if is_haml? && !(options.is_a?(Hash) && options[:layout] && block_given?)
11
+ return non_haml { render_without_haml(*args, &block) }
12
+ end
13
+ render_without_haml(*args, &block)
14
+ end
15
+ alias_method :render_without_haml, :render
16
+ alias_method :render, :render_with_haml
17
+
18
+ # Rails >2.1
19
+ if Haml::Util.has?(:instance_method, self, :output_buffer)
20
+ def output_buffer_with_haml
21
+ return haml_buffer.buffer if is_haml?
22
+ output_buffer_without_haml
15
23
  end
16
- alias_method :render_without_haml, :render
17
- alias_method :render, :render_with_haml
18
-
19
- # Rails >2.1
20
- if Haml::Util.has?(:instance_method, self, :output_buffer)
21
- def output_buffer_with_haml
22
- return haml_buffer.buffer if is_haml?
23
- output_buffer_without_haml
24
+ alias_method :output_buffer_without_haml, :output_buffer
25
+ alias_method :output_buffer, :output_buffer_with_haml
26
+
27
+ def set_output_buffer_with_haml(new)
28
+ if is_haml?
29
+ haml_buffer.buffer = new
30
+ else
31
+ set_output_buffer_without_haml new
24
32
  end
25
- alias_method :output_buffer_without_haml, :output_buffer
26
- alias_method :output_buffer, :output_buffer_with_haml
33
+ end
34
+ alias_method :set_output_buffer_without_haml, :output_buffer=
35
+ alias_method :output_buffer=, :set_output_buffer_with_haml
36
+ end
37
+ end
27
38
 
28
- def set_output_buffer_with_haml(new)
29
- if is_haml?
30
- haml_buffer.buffer = new
39
+ module Helpers
40
+ # In Rails <=2.1, we've got to override considerable capturing infrastructure.
41
+ # In Rails >2.1, we can make do with only overriding #capture
42
+ # (which no longer behaves differently in helper contexts).
43
+ unless Haml::Util.has?(:instance_method, ActionView::Base, :output_buffer)
44
+ module CaptureHelper
45
+ def capture_with_haml(*args, &block)
46
+ # Rails' #capture helper will just return the value of the block
47
+ # if it's not actually in the template context,
48
+ # as detected by the existance of an _erbout variable.
49
+ # We've got to do the same thing for compatibility.
50
+
51
+ if is_haml? && block_is_haml?(block)
52
+ capture_haml(*args, &block)
31
53
  else
32
- set_output_buffer_without_haml new
54
+ capture_without_haml(*args, &block)
33
55
  end
34
56
  end
35
- alias_method :set_output_buffer_without_haml, :output_buffer=
36
- alias_method :output_buffer=, :set_output_buffer_with_haml
37
- end
38
- end
57
+ alias_method :capture_without_haml, :capture
58
+ alias_method :capture, :capture_with_haml
39
59
 
40
- # This overrides various helpers in ActionView
41
- # to make them work more effectively with Haml.
42
- module Helpers
43
- # :stopdoc:
44
- # In Rails <=2.1, we've got to override considerable capturing infrastructure.
45
- # In Rails >2.1, we can make do with only overriding #capture
46
- # (which no longer behaves differently in helper contexts).
47
- unless Haml::Util.has?(:instance_method, ActionView::Base, :output_buffer)
48
- module CaptureHelper
49
- def capture_with_haml(*args, &block)
50
- # Rails' #capture helper will just return the value of the block
51
- # if it's not actually in the template context,
52
- # as detected by the existance of an _erbout variable.
53
- # We've got to do the same thing for compatibility.
54
-
55
- if is_haml? && block_is_haml?(block)
56
- capture_haml(*args, &block)
57
- else
58
- capture_without_haml(*args, &block)
59
- end
60
- end
61
- alias_method :capture_without_haml, :capture
62
- alias_method :capture, :capture_with_haml
63
-
64
- def capture_erb_with_buffer_with_haml(buffer, *args, &block)
65
- if is_haml?
66
- capture_haml(*args, &block)
67
- else
68
- capture_erb_with_buffer_without_haml(buffer, *args, &block)
69
- end
60
+ def capture_erb_with_buffer_with_haml(buffer, *args, &block)
61
+ if is_haml?
62
+ capture_haml(*args, &block)
63
+ else
64
+ capture_erb_with_buffer_without_haml(buffer, *args, &block)
70
65
  end
71
- alias_method :capture_erb_with_buffer_without_haml, :capture_erb_with_buffer
72
- alias_method :capture_erb_with_buffer, :capture_erb_with_buffer_with_haml
73
66
  end
67
+ alias_method :capture_erb_with_buffer_without_haml, :capture_erb_with_buffer
68
+ alias_method :capture_erb_with_buffer, :capture_erb_with_buffer_with_haml
69
+ end
74
70
 
75
- module TextHelper
76
- def concat_with_haml(string, binding = nil)
77
- if is_haml?
78
- haml_buffer.buffer.concat(string)
79
- else
80
- concat_without_haml(string, binding)
81
- end
71
+ module TextHelper
72
+ def concat_with_haml(string, binding = nil)
73
+ if is_haml?
74
+ haml_buffer.buffer.concat(string)
75
+ else
76
+ concat_without_haml(string, binding)
82
77
  end
83
- alias_method :concat_without_haml, :concat
84
- alias_method :concat, :concat_with_haml
85
78
  end
86
- else
87
- module CaptureHelper
88
- def capture_with_haml(*args, &block)
89
- if Haml::Helpers.block_is_haml?(block)
90
- capture_haml(*args, &block)
91
- else
92
- capture_without_haml(*args, &block)
93
- end
79
+ alias_method :concat_without_haml, :concat
80
+ alias_method :concat, :concat_with_haml
81
+ end
82
+ else
83
+ module CaptureHelper
84
+ def capture_with_haml(*args, &block)
85
+ if Haml::Helpers.block_is_haml?(block)
86
+ capture_haml(*args, &block)
87
+ else
88
+ capture_without_haml(*args, &block)
94
89
  end
95
- alias_method :capture_without_haml, :capture
96
- alias_method :capture, :capture_with_haml
97
90
  end
91
+ alias_method :capture_without_haml, :capture
92
+ alias_method :capture, :capture_with_haml
98
93
  end
94
+ end
99
95
 
100
- module TagHelper
101
- def content_tag_with_haml(name, *args, &block)
102
- return content_tag_without_haml(name, *args, &block) unless is_haml?
96
+ module TagHelper
97
+ def content_tag_with_haml(name, *args, &block)
98
+ return content_tag_without_haml(name, *args, &block) unless is_haml?
103
99
 
104
- preserve = haml_buffer.options[:preserve].include?(name.to_s)
100
+ preserve = haml_buffer.options[:preserve].include?(name.to_s)
105
101
 
106
- if block_given? && block_is_haml?(block) && preserve
107
- return content_tag_without_haml(name, *args) {preserve(&block)}
108
- end
109
-
110
- returning content_tag_without_haml(name, *args, &block) do |content|
111
- return Haml::Helpers.preserve(content) if preserve && content
112
- end
102
+ if block_given? && block_is_haml?(block) && preserve
103
+ return content_tag_without_haml(name, *args) {preserve(&block)}
113
104
  end
114
105
 
115
- alias_method :content_tag_without_haml, :content_tag
116
- alias_method :content_tag, :content_tag_with_haml
106
+ returning content_tag_without_haml(name, *args, &block) do |content|
107
+ return Haml::Helpers.preserve(content) if preserve && content
108
+ end
117
109
  end
118
110
 
119
- class InstanceTag
120
- # Includes TagHelper
111
+ alias_method :content_tag_without_haml, :content_tag
112
+ alias_method :content_tag, :content_tag_with_haml
113
+ end
121
114
 
122
- def haml_buffer
123
- @template_object.send :haml_buffer
124
- end
115
+ class InstanceTag
116
+ # Includes TagHelper
125
117
 
126
- def is_haml?
127
- @template_object.send :is_haml?
128
- end
129
-
130
- alias_method :content_tag_without_haml, :content_tag
131
- alias_method :content_tag, :content_tag_with_haml
118
+ def haml_buffer
119
+ @template_object.send :haml_buffer
132
120
  end
133
121
 
134
- module FormTagHelper
135
- def form_tag_with_haml(url_for_options = {}, options = {}, *parameters_for_url, &proc)
136
- if is_haml?
137
- if block_given?
138
- oldproc = proc
139
- proc = haml_bind_proc do |*args|
140
- concat "\n"
141
- tab_up
142
- oldproc.call(*args)
143
- tab_down
144
- concat haml_indent
145
- end
146
- concat haml_indent
147
- end
148
- res = form_tag_without_haml(url_for_options, options, *parameters_for_url, &proc) + "\n"
149
- concat "\n" if block_given?
150
- res
151
- else
152
- form_tag_without_haml(url_for_options, options, *parameters_for_url, &proc)
153
- end
154
- end
155
- alias_method :form_tag_without_haml, :form_tag
156
- alias_method :form_tag, :form_tag_with_haml
122
+ def is_haml?
123
+ @template_object.send :is_haml?
157
124
  end
158
125
 
159
- module FormHelper
160
- def form_for_with_haml(object_name, *args, &proc)
161
- if block_given? && is_haml?
126
+ alias_method :content_tag_without_haml, :content_tag
127
+ alias_method :content_tag, :content_tag_with_haml
128
+ end
129
+
130
+ module FormTagHelper
131
+ def form_tag_with_haml(url_for_options = {}, options = {}, *parameters_for_url, &proc)
132
+ if is_haml?
133
+ if block_given?
162
134
  oldproc = proc
163
135
  proc = haml_bind_proc do |*args|
136
+ concat "\n"
164
137
  tab_up
165
138
  oldproc.call(*args)
166
139
  tab_down
@@ -168,14 +141,34 @@ if defined?(ActionView) and not defined?(Merb::Plugins)
168
141
  end
169
142
  concat haml_indent
170
143
  end
171
- form_for_without_haml(object_name, *args, &proc)
172
- concat "\n" if block_given? && is_haml?
144
+ res = form_tag_without_haml(url_for_options, options, *parameters_for_url, &proc) + "\n"
145
+ concat "\n" if block_given?
146
+ res
147
+ else
148
+ form_tag_without_haml(url_for_options, options, *parameters_for_url, &proc)
173
149
  end
174
- alias_method :form_for_without_haml, :form_for
175
- alias_method :form_for, :form_for_with_haml
176
150
  end
177
- # :startdoc:
151
+ alias_method :form_tag_without_haml, :form_tag
152
+ alias_method :form_tag, :form_tag_with_haml
153
+ end
154
+
155
+ module FormHelper
156
+ def form_for_with_haml(object_name, *args, &proc)
157
+ if block_given? && is_haml?
158
+ oldproc = proc
159
+ proc = haml_bind_proc do |*args|
160
+ tab_up
161
+ oldproc.call(*args)
162
+ tab_down
163
+ concat haml_indent
164
+ end
165
+ concat haml_indent
166
+ end
167
+ form_for_without_haml(object_name, *args, &proc)
168
+ concat "\n" if block_given? && is_haml?
169
+ end
170
+ alias_method :form_for_without_haml, :form_for
171
+ alias_method :form_for, :form_for_with_haml
178
172
  end
179
173
  end
180
174
  end
181
-
@@ -6,15 +6,21 @@ require 'hpricot'
6
6
  require 'cgi'
7
7
 
8
8
  module Haml
9
- # This class contains the functionality used in the +html2haml+ utility,
10
- # namely converting HTML documents to Haml templates.
11
- # It depends on Hpricot for HTML parsing (http://code.whytheluckystiff.net/hpricot/).
9
+ # Converts HTML documents into Haml templates.
10
+ # Depends on [Hpricot](http://code.whytheluckystiff.net/hpricot/) for HTML parsing.
11
+ #
12
+ # Example usage:
13
+ #
14
+ # Haml::Engine.new("<a href='http://google.com'>Blat</a>").render
15
+ # #=> "%a{:href => 'http://google.com'} Blat"
12
16
  class HTML
13
- # Creates a new instance of Haml::HTML that will compile the given template,
14
- # which can either be a string containing HTML or an Hpricot node,
15
- # to a Haml string when +render+ is called.
17
+ # @param template [String, Hpricot::Node] The HTML template to convert
18
+ # @option options :rhtml [Boolean] (false) Whether or not to parse
19
+ # ERB's `<%= %>` and `<% %>` into Haml's `=` and `-`
20
+ # @option options :xhtml [Boolean] (false) Whether or not to parse
21
+ # the HTML strictly as XHTML
16
22
  def initialize(template, options = {})
17
- @@options = options
23
+ @options = options
18
24
 
19
25
  if template.is_a? Hpricot::Node
20
26
  @template = template
@@ -23,12 +29,12 @@ module Haml
23
29
  template = template.read
24
30
  end
25
31
 
26
- if @@options[:rhtml]
32
+ if @options[:rhtml]
27
33
  match_to_html(template, /<%=(.*?)-?%>/m, 'loud')
28
34
  match_to_html(template, /<%-?(.*?)-?%>/m, 'silent')
29
35
  end
30
36
 
31
- method = @@options[:xhtml] ? Hpricot.method(:XML) : method(:Hpricot)
37
+ method = @options[:xhtml] ? Hpricot.method(:XML) : method(:Hpricot)
32
38
  @template = method.call(template.gsub('&', '&amp;'))
33
39
  end
34
40
  end
@@ -36,14 +42,18 @@ module Haml
36
42
  # Processes the document and returns the result as a string
37
43
  # containing the Haml template.
38
44
  def render
39
- @template.to_haml(0)
45
+ @template.to_haml(0, @options)
40
46
  end
41
47
  alias_method :to_haml, :render
42
48
 
49
+ # Haml monkeypatches various Hpricot classes
50
+ # to add methods for conversion to Haml.
43
51
  module ::Hpricot::Node
44
- # Returns the Haml representation of the given node,
45
- # at the given tabulation.
46
- def to_haml(tabs = 0)
52
+ # Returns the Haml representation of the given node.
53
+ #
54
+ # @param tabs [Fixnum] The indentation level of the resulting Haml.
55
+ # @option options (see Haml::HTML#initialize)
56
+ def to_haml(tabs, options)
47
57
  parse_text(self.to_s, tabs)
48
58
  end
49
59
 
@@ -68,34 +78,36 @@ module Haml
68
78
  end
69
79
  end
70
80
 
71
- # :stopdoc:
72
-
73
- def self.options
74
- @@options
75
- end
76
-
77
81
  TEXT_REGEXP = /^(\s*).*$/
78
82
 
83
+ # @see Hpricot::Node
79
84
  class ::Hpricot::Doc
80
- def to_haml(tabs = 0)
81
- (children || []).inject('') {|s, c| s << c.to_haml(0)}
85
+ # @see Hpricot::Node#to_haml
86
+ def to_haml(tabs, options)
87
+ (children || []).inject('') {|s, c| s << c.to_haml(0, options)}
82
88
  end
83
89
  end
84
90
 
91
+ # @see Hpricot::Node
85
92
  class ::Hpricot::XMLDecl
86
- def to_haml(tabs = 0)
93
+ # @see Hpricot::Node#to_haml
94
+ def to_haml(tabs, options)
87
95
  "#{tabulate(tabs)}!!! XML\n"
88
96
  end
89
97
  end
90
98
 
99
+ # @see Hpricot::Node
91
100
  class ::Hpricot::CData
92
- def to_haml(tabs = 0)
101
+ # @see Hpricot::Node#to_haml
102
+ def to_haml(tabs, options)
93
103
  "#{tabulate(tabs)}:cdata\n#{parse_text(self.content, tabs + 1)}"
94
104
  end
95
105
  end
96
106
 
107
+ # @see Hpricot::Node
97
108
  class ::Hpricot::DocType
98
- def to_haml(tabs = 0)
109
+ # @see Hpricot::Node#to_haml
110
+ def to_haml(tabs, options)
99
111
  attrs = public_id.scan(/DTD\s+([^\s]+)\s*([^\s]*)\s*([^\s]*)\s*\/\//)[0]
100
112
  if attrs == nil
101
113
  raise Exception.new("Invalid doctype")
@@ -125,35 +137,40 @@ module Haml
125
137
  end
126
138
  end
127
139
 
140
+ # @see Hpricot::Node
128
141
  class ::Hpricot::Comment
129
- def to_haml(tabs = 0)
142
+ # @see Hpricot::Node#to_haml
143
+ def to_haml(tabs, options)
130
144
  "#{tabulate(tabs)}/\n#{parse_text(self.content, tabs + 1)}"
131
145
  end
132
146
  end
133
147
 
148
+ # @see Hpricot::Node
134
149
  class ::Hpricot::Elem
135
- def to_haml(tabs = 0)
150
+ # @see Hpricot::Node#to_haml
151
+ def to_haml(tabs, options)
136
152
  output = "#{tabulate(tabs)}"
137
- if HTML.options[:rhtml] && name[0...5] == 'haml:'
138
- return output + HTML.send("haml_tag_#{name[5..-1]}", CGI.unescapeHTML(self.inner_text))
153
+ if options[:rhtml] && name[0...5] == 'haml:'
154
+ return output + send("haml_tag_#{name[5..-1]}", CGI.unescapeHTML(self.inner_text))
139
155
  end
140
156
 
141
- output += "%#{name}" unless name == 'div' && (static_id? || static_classname?)
157
+ output += "%#{name}" unless name == 'div' &&
158
+ (static_id?(options) || static_classname?(options))
142
159
 
143
160
  if attributes
144
- if static_id?
161
+ if static_id?(options)
145
162
  output += "##{attributes['id']}"
146
163
  remove_attribute('id')
147
164
  end
148
- if static_classname?
165
+ if static_classname?(options)
149
166
  attributes['class'].split(' ').each { |c| output += ".#{c}" }
150
167
  remove_attribute('class')
151
168
  end
152
- output += haml_attributes if attributes.length > 0
169
+ output += haml_attributes(options) if attributes.length > 0
153
170
  end
154
171
 
155
172
  (self.children || []).inject(output + "\n") do |output, child|
156
- output + child.to_haml(tabs + 1)
173
+ output + child.to_haml(tabs + 1, options)
157
174
  end
158
175
  end
159
176
 
@@ -161,44 +178,48 @@ module Haml
161
178
 
162
179
  def dynamic_attributes
163
180
  @dynamic_attributes ||= begin
164
- attributes.inject({}) do |dynamic, pair|
165
- name, value = pair
166
- unless value.empty?
167
- full_match = nil
168
- ruby_value = value.gsub(%r{<haml:loud>\s*(.+?)\s*</haml:loud>}) do
169
- full_match = $`.empty? && $'.empty?
170
- full_match ? $1: "\#{#{$1}}"
171
- end
172
- unless ruby_value == value
173
- dynamic[name] = full_match ? ruby_value : %("#{ruby_value}")
174
- end
181
+ Haml::Util.map_hash(attributes) do |name, value|
182
+ next if value.empty?
183
+ full_match = nil
184
+ ruby_value = value.gsub(%r{<haml:loud>\s*(.+?)\s*</haml:loud>}) do
185
+ full_match = $`.empty? && $'.empty?
186
+ full_match ? $1: "\#{#{$1}}"
175
187
  end
176
- dynamic
188
+ next if ruby_value == value
189
+ [name, full_match ? ruby_value : %("#{ruby_value}")]
177
190
  end
178
191
  end
179
192
  end
180
-
181
- def static_attribute?(name)
182
- attributes[name] and !dynamic_attribute?(name)
193
+
194
+ def haml_tag_loud(text)
195
+ "= #{text.gsub(/\n\s*/, ' ').strip}\n"
196
+ end
197
+
198
+ def haml_tag_silent(text)
199
+ text.split("\n").map { |line| "- #{line.strip}\n" }.join
200
+ end
201
+
202
+ def static_attribute?(name, options)
203
+ attributes[name] and !dynamic_attribute?(name, options)
183
204
  end
184
205
 
185
- def dynamic_attribute?(name)
186
- HTML.options[:rhtml] and dynamic_attributes.key?(name)
206
+ def dynamic_attribute?(name, options)
207
+ options[:rhtml] and dynamic_attributes.key?(name)
187
208
  end
188
209
 
189
- def static_id?
190
- static_attribute? 'id'
210
+ def static_id?(options)
211
+ static_attribute?('id', options)
191
212
  end
192
213
 
193
- def static_classname?
194
- static_attribute? 'class'
214
+ def static_classname?(options)
215
+ static_attribute?('class', options)
195
216
  end
196
217
 
197
218
  # Returns a string representation of an attributes hash
198
219
  # that's prettier than that produced by Hash#inspect
199
- def haml_attributes
220
+ def haml_attributes(options)
200
221
  attrs = attributes.map do |name, value|
201
- value = dynamic_attribute?(name) ? dynamic_attributes[name] : value.inspect
222
+ value = dynamic_attribute?(name, options) ? dynamic_attributes[name] : value.inspect
202
223
  name = name.index(/\W/) ? name.inspect : ":#{name}"
203
224
  "#{name} => #{value}"
204
225
  end
@@ -206,14 +227,6 @@ module Haml
206
227
  end
207
228
  end
208
229
 
209
- def self.haml_tag_loud(text)
210
- "= #{text.gsub(/\n\s*/, ' ').strip}\n"
211
- end
212
-
213
- def self.haml_tag_silent(text)
214
- text.split("\n").map { |line| "- #{line.strip}\n" }.join
215
- end
216
-
217
230
  private
218
231
 
219
232
  def match_to_html(string, regex, tag)
@@ -221,6 +234,5 @@ module Haml
221
234
  "<haml:#{tag}>#{CGI.escapeHTML($1)}</haml:#{tag}>"
222
235
  end
223
236
  end
224
- # :startdoc:
225
237
  end
226
238
  end