haml-edge 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (180) hide show
  1. data/EDGE_GEM_VERSION +1 -0
  2. data/FAQ +138 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.rdoc +332 -0
  5. data/REVISION +1 -0
  6. data/Rakefile +226 -0
  7. data/VERSION +1 -0
  8. data/bin/css2sass +7 -0
  9. data/bin/haml +9 -0
  10. data/bin/html2haml +7 -0
  11. data/bin/sass +8 -0
  12. data/extra/edge_gem_watch.rb +13 -0
  13. data/extra/haml-mode.el +596 -0
  14. data/extra/sass-mode.el +200 -0
  15. data/init.rb +8 -0
  16. data/lib/haml/buffer.rb +255 -0
  17. data/lib/haml/engine.rb +268 -0
  18. data/lib/haml/error.rb +22 -0
  19. data/lib/haml/exec.rb +395 -0
  20. data/lib/haml/filters.rb +275 -0
  21. data/lib/haml/helpers/action_view_extensions.rb +45 -0
  22. data/lib/haml/helpers/action_view_mods.rb +181 -0
  23. data/lib/haml/helpers.rb +488 -0
  24. data/lib/haml/html.rb +222 -0
  25. data/lib/haml/precompiler.rb +904 -0
  26. data/lib/haml/shared.rb +45 -0
  27. data/lib/haml/template/patch.rb +58 -0
  28. data/lib/haml/template/plugin.rb +72 -0
  29. data/lib/haml/template.rb +42 -0
  30. data/lib/haml/util.rb +88 -0
  31. data/lib/haml/version.rb +47 -0
  32. data/lib/haml.rb +1044 -0
  33. data/lib/sass/css.rb +388 -0
  34. data/lib/sass/engine.rb +495 -0
  35. data/lib/sass/environment.rb +46 -0
  36. data/lib/sass/error.rb +35 -0
  37. data/lib/sass/plugin/merb.rb +56 -0
  38. data/lib/sass/plugin/rails.rb +24 -0
  39. data/lib/sass/plugin.rb +204 -0
  40. data/lib/sass/repl.rb +51 -0
  41. data/lib/sass/script/bool.rb +13 -0
  42. data/lib/sass/script/color.rb +97 -0
  43. data/lib/sass/script/funcall.rb +29 -0
  44. data/lib/sass/script/functions.rb +134 -0
  45. data/lib/sass/script/lexer.rb +148 -0
  46. data/lib/sass/script/literal.rb +82 -0
  47. data/lib/sass/script/number.rb +231 -0
  48. data/lib/sass/script/operation.rb +30 -0
  49. data/lib/sass/script/parser.rb +142 -0
  50. data/lib/sass/script/string.rb +9 -0
  51. data/lib/sass/script/unary_operation.rb +21 -0
  52. data/lib/sass/script/variable.rb +20 -0
  53. data/lib/sass/script.rb +38 -0
  54. data/lib/sass/tree/attr_node.rb +64 -0
  55. data/lib/sass/tree/comment_node.rb +30 -0
  56. data/lib/sass/tree/debug_node.rb +22 -0
  57. data/lib/sass/tree/directive_node.rb +50 -0
  58. data/lib/sass/tree/file_node.rb +27 -0
  59. data/lib/sass/tree/for_node.rb +29 -0
  60. data/lib/sass/tree/if_node.rb +27 -0
  61. data/lib/sass/tree/mixin_def_node.rb +18 -0
  62. data/lib/sass/tree/mixin_node.rb +35 -0
  63. data/lib/sass/tree/node.rb +99 -0
  64. data/lib/sass/tree/rule_node.rb +161 -0
  65. data/lib/sass/tree/variable_node.rb +24 -0
  66. data/lib/sass/tree/while_node.rb +21 -0
  67. data/lib/sass.rb +1062 -0
  68. data/rails/init.rb +1 -0
  69. data/test/benchmark.rb +99 -0
  70. data/test/haml/engine_test.rb +795 -0
  71. data/test/haml/helper_test.rb +228 -0
  72. data/test/haml/html2haml_test.rb +108 -0
  73. data/test/haml/markaby/standard.mab +52 -0
  74. data/test/haml/mocks/article.rb +6 -0
  75. data/test/haml/results/content_for_layout.xhtml +15 -0
  76. data/test/haml/results/eval_suppressed.xhtml +9 -0
  77. data/test/haml/results/filters.xhtml +62 -0
  78. data/test/haml/results/helpers.xhtml +93 -0
  79. data/test/haml/results/helpful.xhtml +10 -0
  80. data/test/haml/results/just_stuff.xhtml +68 -0
  81. data/test/haml/results/list.xhtml +12 -0
  82. data/test/haml/results/nuke_inner_whitespace.xhtml +40 -0
  83. data/test/haml/results/nuke_outer_whitespace.xhtml +148 -0
  84. data/test/haml/results/original_engine.xhtml +20 -0
  85. data/test/haml/results/partial_layout.xhtml +5 -0
  86. data/test/haml/results/partials.xhtml +21 -0
  87. data/test/haml/results/render_layout.xhtml +3 -0
  88. data/test/haml/results/silent_script.xhtml +74 -0
  89. data/test/haml/results/standard.xhtml +162 -0
  90. data/test/haml/results/tag_parsing.xhtml +23 -0
  91. data/test/haml/results/very_basic.xhtml +5 -0
  92. data/test/haml/results/whitespace_handling.xhtml +89 -0
  93. data/test/haml/rhtml/_av_partial_1.rhtml +12 -0
  94. data/test/haml/rhtml/_av_partial_2.rhtml +8 -0
  95. data/test/haml/rhtml/action_view.rhtml +62 -0
  96. data/test/haml/rhtml/standard.rhtml +54 -0
  97. data/test/haml/template_test.rb +204 -0
  98. data/test/haml/templates/_av_partial_1.haml +9 -0
  99. data/test/haml/templates/_av_partial_1_ugly.haml +9 -0
  100. data/test/haml/templates/_av_partial_2.haml +5 -0
  101. data/test/haml/templates/_av_partial_2_ugly.haml +5 -0
  102. data/test/haml/templates/_layout.erb +3 -0
  103. data/test/haml/templates/_layout_for_partial.haml +3 -0
  104. data/test/haml/templates/_partial.haml +8 -0
  105. data/test/haml/templates/_text_area.haml +3 -0
  106. data/test/haml/templates/action_view.haml +47 -0
  107. data/test/haml/templates/action_view_ugly.haml +47 -0
  108. data/test/haml/templates/breakage.haml +8 -0
  109. data/test/haml/templates/content_for_layout.haml +10 -0
  110. data/test/haml/templates/eval_suppressed.haml +11 -0
  111. data/test/haml/templates/filters.haml +66 -0
  112. data/test/haml/templates/helpers.haml +95 -0
  113. data/test/haml/templates/helpful.haml +11 -0
  114. data/test/haml/templates/just_stuff.haml +83 -0
  115. data/test/haml/templates/list.haml +12 -0
  116. data/test/haml/templates/nuke_inner_whitespace.haml +32 -0
  117. data/test/haml/templates/nuke_outer_whitespace.haml +144 -0
  118. data/test/haml/templates/original_engine.haml +17 -0
  119. data/test/haml/templates/partial_layout.haml +3 -0
  120. data/test/haml/templates/partialize.haml +1 -0
  121. data/test/haml/templates/partials.haml +12 -0
  122. data/test/haml/templates/render_layout.haml +2 -0
  123. data/test/haml/templates/silent_script.haml +40 -0
  124. data/test/haml/templates/standard.haml +42 -0
  125. data/test/haml/templates/standard_ugly.haml +42 -0
  126. data/test/haml/templates/tag_parsing.haml +21 -0
  127. data/test/haml/templates/very_basic.haml +4 -0
  128. data/test/haml/templates/whitespace_handling.haml +87 -0
  129. data/test/haml/util_test.rb +87 -0
  130. data/test/linked_rails.rb +12 -0
  131. data/test/sass/css2sass_test.rb +193 -0
  132. data/test/sass/engine_test.rb +709 -0
  133. data/test/sass/functions_test.rb +109 -0
  134. data/test/sass/more_results/more1.css +9 -0
  135. data/test/sass/more_results/more1_with_line_comments.css +26 -0
  136. data/test/sass/more_results/more_import.css +29 -0
  137. data/test/sass/more_templates/_more_partial.sass +2 -0
  138. data/test/sass/more_templates/more1.sass +23 -0
  139. data/test/sass/more_templates/more_import.sass +11 -0
  140. data/test/sass/plugin_test.rb +213 -0
  141. data/test/sass/results/alt.css +4 -0
  142. data/test/sass/results/basic.css +9 -0
  143. data/test/sass/results/compact.css +5 -0
  144. data/test/sass/results/complex.css +87 -0
  145. data/test/sass/results/compressed.css +1 -0
  146. data/test/sass/results/expanded.css +19 -0
  147. data/test/sass/results/import.css +29 -0
  148. data/test/sass/results/line_numbers.css +49 -0
  149. data/test/sass/results/mixins.css +95 -0
  150. data/test/sass/results/multiline.css +24 -0
  151. data/test/sass/results/nested.css +22 -0
  152. data/test/sass/results/parent_ref.css +13 -0
  153. data/test/sass/results/script.css +16 -0
  154. data/test/sass/results/subdir/nested_subdir/nested_subdir.css +1 -0
  155. data/test/sass/results/subdir/subdir.css +3 -0
  156. data/test/sass/results/units.css +11 -0
  157. data/test/sass/script_test.rb +250 -0
  158. data/test/sass/templates/_partial.sass +2 -0
  159. data/test/sass/templates/alt.sass +16 -0
  160. data/test/sass/templates/basic.sass +23 -0
  161. data/test/sass/templates/bork.sass +2 -0
  162. data/test/sass/templates/bork2.sass +2 -0
  163. data/test/sass/templates/compact.sass +17 -0
  164. data/test/sass/templates/complex.sass +309 -0
  165. data/test/sass/templates/compressed.sass +15 -0
  166. data/test/sass/templates/expanded.sass +17 -0
  167. data/test/sass/templates/import.sass +11 -0
  168. data/test/sass/templates/importee.sass +19 -0
  169. data/test/sass/templates/line_numbers.sass +13 -0
  170. data/test/sass/templates/mixins.sass +76 -0
  171. data/test/sass/templates/multiline.sass +20 -0
  172. data/test/sass/templates/nested.sass +25 -0
  173. data/test/sass/templates/parent_ref.sass +25 -0
  174. data/test/sass/templates/script.sass +101 -0
  175. data/test/sass/templates/subdir/nested_subdir/_nested_partial.sass +2 -0
  176. data/test/sass/templates/subdir/nested_subdir/nested_subdir.sass +3 -0
  177. data/test/sass/templates/subdir/subdir.sass +6 -0
  178. data/test/sass/templates/units.sass +11 -0
  179. data/test/test_helper.rb +21 -0
  180. metadata +278 -0
@@ -0,0 +1,200 @@
1
+ ;;; sass-mode.el --- Major mode for editing Sass files
2
+
3
+ ;; Copyright (c) 2007, 2008 Nathan Weizenbaum
4
+
5
+ ;; Author: Nathan Weizenbaum
6
+ ;; URL: http://github.com/nex3/haml/tree/master
7
+ ;; Version: 1.0
8
+ ;; Keywords: markup, language
9
+
10
+ ;;; Commentary:
11
+
12
+ ;; Because Sass's indentation schema is similar
13
+ ;; to that of YAML and Python, many indentation-related
14
+ ;; functions are similar to those in yaml-mode and python-mode.
15
+
16
+ ;; To install, save this on your load path and add the following to
17
+ ;; your .emacs file:
18
+ ;;
19
+ ;; (require 'sass-mode)
20
+
21
+ ;;; Code:
22
+
23
+ (require 'haml-mode)
24
+
25
+ ;; User definable variables
26
+
27
+ (defgroup sass nil
28
+ "Support for the Sass template language."
29
+ :group 'languages
30
+ :prefix "sass-")
31
+
32
+ (defcustom sass-mode-hook nil
33
+ "Hook run when entering Sass mode."
34
+ :type 'hook
35
+ :group 'sass)
36
+
37
+ (defcustom sass-indent-offset 2
38
+ "Amount of offset per level of indentation."
39
+ :type 'integer
40
+ :group 'sass)
41
+
42
+ (defvar sass-non-block-openers
43
+ '("^ *:[^ \t]+[ \t]+[^ \t]"
44
+ "^ *[^ \t:]+[ \t]*[=:][ \t]*[^ \t]")
45
+ "A list of regexps that match lines of Sass that couldn't have
46
+ text nested beneath them.")
47
+
48
+ ;; Font lock
49
+
50
+ (defconst sass-selector-font-lock-keywords
51
+ '(;; Attribute selectors (e.g. p[foo=bar])
52
+ ("\\[\\([^]=]+\\)" (1 font-lock-variable-name-face)
53
+ ("[~|$^*]?=\\([^]=]+\\)" nil nil (1 font-lock-string-face)))
54
+ ("&" 0 font-lock-constant-face)
55
+ ("\\.\\w+" 0 font-lock-type-face)
56
+ ("#\\w+" 0 font-lock-keyword-face)
57
+ ;; Pseudo-selectors, optionally with arguments (e.g. :first, :nth-child(12))
58
+ ("\\(::?\\w+\\)" (1 font-lock-function-name-face)
59
+ ("(\\([^)]+\\))" nil nil (1 font-lock-string-face)))))
60
+
61
+ (defconst sass-script-font-lock-keywords
62
+ `(("\"\\([^\"\\\\]\\|\\\\.\\)*\"" 0 font-lock-string-face)
63
+ ("!\\w+" 0 font-lock-variable-name-face)
64
+ ("#[0-9a-fA-F]\\{0,6\\}" 0 font-lock-preprocessor-face)
65
+ (,(regexp-opt
66
+ '("true" "false" "black" "silver" "gray" "white" "maroon" "red"
67
+ "purple" "fuchsia" "green" "lime" "olive" "yellow" "navy"
68
+ "blue" "teal" "aqua"))
69
+ 0 font-lock-constant-face)
70
+ (,(regexp-opt '("and" "or" "not")) 0 font-lock-keyword-face)))
71
+
72
+ (defconst sass-syntax-table
73
+ (let ((st (make-syntax-table)))
74
+ (modify-syntax-entry ?- "w" st)
75
+ st))
76
+
77
+ (defconst sass-script-syntax-table
78
+ (let ((st (make-syntax-table sass-syntax-table)))
79
+ (modify-syntax-entry ?- "." st)
80
+ st))
81
+
82
+ (defconst sass-font-lock-keywords
83
+ '((sass-highlight-line 1 nil nil t)))
84
+
85
+ (defconst sass-line-keywords
86
+ '(("@\\(\\w+\\)" 0 font-lock-keyword-face sass-highlight-directive)
87
+ ("/[/*].*" 0 font-lock-comment-face)
88
+ ("[=+]\\w+" 0 font-lock-function-name-face sass-highlight-script-after-match)
89
+ ("!\\w+" 0 font-lock-variable-name-face sass-highlight-script-after-match)
90
+ (":\\w+" 0 font-lock-variable-name-face)
91
+ ("\\w+\s*:" 0 font-lock-variable-name-face)
92
+ ("\\(\\w+\\)\s*=" 1 font-lock-variable-name-face sass-highlight-script-after-match)
93
+ ("\\(:\\w+\\)\s*=" 1 font-lock-variable-name-face sass-highlight-script-after-match)
94
+ (".*" sass-highlight-selector))
95
+ "A list of full-line Sass syntax to highlight,
96
+ used by `sass-highlight-line'.
97
+
98
+ Each item is either of the form (REGEXP SUBEXP FACE), (REGEXP FN),
99
+ or (REGEXP SUBEXP FACE FN). Each REGEXP is run successively on the
100
+ beginning of non-whitespace on the current line until one matches.
101
+ If it has SUBEXP and FACE, then SUBEXP is highlighted using FACE.
102
+ If it has FN, FN is run.")
103
+
104
+ (defun sass-highlight-line (limit)
105
+ "Highlight a single line using some Sass single-line syntax,
106
+ taken from `sass-line-keywords'."
107
+ (save-match-data
108
+ (when (re-search-forward "^ *\\(.+\\)$" limit t)
109
+ (goto-char (match-beginning 1))
110
+ (dolist (keyword sass-line-keywords)
111
+ (destructuring-bind (keyword subexp-or-fn &optional face fn) keyword
112
+ (when (looking-at keyword)
113
+ (if (integerp subexp-or-fn)
114
+ (put-text-property (match-beginning subexp-or-fn)
115
+ (match-end subexp-or-fn)
116
+ 'face face)
117
+ (setq fn subexp-or-fn))
118
+ (when fn (funcall fn))
119
+ (end-of-line)
120
+ (return t)))))))
121
+
122
+ (defun sass-highlight-selector ()
123
+ "Highlight a CSS selector starting at `point'
124
+ and ending at `end-of-line'."
125
+ (let ((font-lock-keywords sass-selector-font-lock-keywords)
126
+ font-lock-multiline)
127
+ (font-lock-fontify-region
128
+ (point) (progn (end-of-line) (point))))
129
+ t)
130
+
131
+ (defun sass-highlight-script (beg end)
132
+ "Highlight a section of SassScript between BEG and END."
133
+ (save-match-data
134
+ (with-syntax-table sass-script-syntax-table
135
+ (let ((font-lock-keywords sass-script-font-lock-keywords)
136
+ font-lock-syntax-table
137
+ font-lock-extend-region-functions)
138
+ (font-lock-fontify-region beg end)))))
139
+
140
+ (defun sass-highlight-script-after-match ()
141
+ (end-of-line)
142
+ (sass-highlight-script (match-end 0) (point)))
143
+
144
+ (defun sass-highlight-directive ()
145
+ (goto-char (match-end 0))
146
+ (block nil
147
+ (case (intern (match-string 1))
148
+ (for
149
+ (unless (looking-at " +!\\w+") (return))
150
+ (put-text-property (match-beginning 0) (match-end 0)
151
+ 'face font-lock-variable-name-face)
152
+ (goto-char (match-end 0))
153
+ (unless (looking-at " +from") (return))
154
+ (put-text-property (match-beginning 0) (match-end 0)
155
+ 'face font-lock-keyword-face)
156
+ (goto-char (match-end 0))
157
+ (when (looking-at " +\\(.+?\\) +\\(to\\|through\\)")
158
+ (sass-highlight-script (match-beginning 1) (match-end 1))
159
+ (put-text-property (match-beginning 2) (match-end 2)
160
+ 'face font-lock-keyword-face))
161
+ (sass-highlight-script-after-match))
162
+
163
+ (else
164
+ (unless (looking-at " +if") (return))
165
+ (put-text-property (match-beginning 0) (match-end 0)
166
+ 'face font-lock-keyword-face)
167
+ (sass-highlight-script-after-match))
168
+
169
+ ((if while debug) (sass-highlight-script-after-match)))))
170
+
171
+ ;; Constants
172
+
173
+ ;; Mode setup
174
+
175
+ ;;;###autoload
176
+ (define-derived-mode sass-mode haml-mode "Sass"
177
+ "Major mode for editing Sass files."
178
+ (set-syntax-table sass-syntax-table)
179
+ (setq font-lock-extend-region-functions
180
+ '(font-lock-extend-region-wholelines font-lock-extend-region-multiline))
181
+ (setq font-lock-multiline nil)
182
+ (setq comment-start "/*")
183
+ (set (make-local-variable 'haml-indent-function) 'sass-indent-p)
184
+ (set (make-local-variable 'haml-indent-offset) sass-indent-offset)
185
+ (setq font-lock-defaults '(sass-font-lock-keywords t t)))
186
+
187
+ ;; Indentation
188
+
189
+ (defun sass-indent-p ()
190
+ "Returns t if the current line can have lines nested beneath it."
191
+ (loop for opener in sass-non-block-openers
192
+ unless (looking-at opener) return t
193
+ return nil))
194
+
195
+ ;;;###autoload
196
+ (add-to-list 'auto-mode-alist '("\\.sass$" . sass-mode))
197
+
198
+ ;; Setup/Activation
199
+ (provide 'sass-mode)
200
+ ;;; sass-mode.el ends here
data/init.rb ADDED
@@ -0,0 +1,8 @@
1
+ begin
2
+ require File.join(File.dirname(__FILE__), 'lib', 'haml') # From here
3
+ rescue LoadError
4
+ require 'haml' # From gem
5
+ end
6
+
7
+ # Load Haml and Sass
8
+ Haml.init_rails(binding)
@@ -0,0 +1,255 @@
1
+ module Haml
2
+ # This class is used only internally. It holds the buffer of XHTML that
3
+ # is eventually output by Haml::Engine's to_html method. It's called
4
+ # from within the precompiled code, and helps reduce the amount of
5
+ # processing done within instance_eval'd code.
6
+ class Buffer
7
+ include Haml::Helpers
8
+ include Haml::Util
9
+
10
+ # The string that holds the compiled XHTML. This is aliased as
11
+ # _erbout for compatibility with ERB-specific code.
12
+ attr_accessor :buffer
13
+
14
+ # The options hash passed in from Haml::Engine.
15
+ attr_accessor :options
16
+
17
+ # The Buffer for the enclosing Haml document.
18
+ # This is set for partials and similar sorts of nested templates.
19
+ # It's nil at the top level (see #toplevel?).
20
+ attr_accessor :upper
21
+
22
+ # See #active?
23
+ attr_writer :active
24
+
25
+ # True if the format is XHTML
26
+ def xhtml?
27
+ not html?
28
+ end
29
+
30
+ # True if the format is any flavor of HTML
31
+ def html?
32
+ html4? or html5?
33
+ end
34
+
35
+ # True if the format is HTML4
36
+ def html4?
37
+ @options[:format] == :html4
38
+ end
39
+
40
+ # True if the format is HTML5
41
+ def html5?
42
+ @options[:format] == :html5
43
+ end
44
+
45
+ # True if this buffer is a top-level template,
46
+ # as opposed to a nested partial.
47
+ def toplevel?
48
+ upper.nil?
49
+ end
50
+
51
+ # True if this buffer is currently being used to render a Haml template.
52
+ # However, this returns false if a subtemplate is being rendered,
53
+ # even if it's a subtemplate of this buffer's template.
54
+ def active?
55
+ @active
56
+ end
57
+
58
+ # Gets the current tabulation of the document.
59
+ def tabulation
60
+ @real_tabs + @tabulation
61
+ end
62
+
63
+ # Sets the current tabulation of the document.
64
+ def tabulation=(val)
65
+ val = val - @real_tabs
66
+ @tabulation = val > -1 ? val : 0
67
+ end
68
+
69
+ # Creates a new buffer.
70
+ def initialize(upper = nil, options = {})
71
+ @active = true
72
+ @upper = upper
73
+ @options = {
74
+ :attr_wrapper => "'",
75
+ :ugly => false,
76
+ :format => :xhtml
77
+ }.merge options
78
+ @buffer = ""
79
+ @tabulation = 0
80
+
81
+ # The number of tabs that Engine thinks we should have
82
+ # @real_tabs + @tabulation is the number of tabs actually output
83
+ @real_tabs = 0
84
+ end
85
+
86
+ def push_text(text, tab_change, dont_tab_up)
87
+ if @tabulation > 0
88
+ # Have to push every line in by the extra user set tabulation.
89
+ # Don't push lines with just whitespace, though,
90
+ # because that screws up precompiled indentation.
91
+ text.gsub!(/^(?!\s+$)/m, tabs)
92
+ text.sub!(tabs, '') if dont_tab_up
93
+ end
94
+
95
+ @buffer << text
96
+ @real_tabs += tab_change
97
+ end
98
+
99
+ def adjust_tabs(tab_change)
100
+ @real_tabs += tab_change
101
+ end
102
+
103
+ Haml::Util.def_static_method(self, :format_script, [:result],
104
+ :preserve_script, :in_tag, :preserve_tag, :escape_html,
105
+ :nuke_inner_whitespace, :interpolated, :ugly, <<RUBY)
106
+ <% unless ugly %>
107
+ # If we're interpolated,
108
+ # then the custom tabulation is handled in #push_text.
109
+ # The easiest way to avoid it here is to reset @tabulation.
110
+ <% if interpolated %>
111
+ old_tabulation = @tabulation
112
+ @tabulation = 0
113
+ <% end %>
114
+
115
+ tabulation = @real_tabs
116
+ result = result.to_s.<% if nuke_inner_whitespace %>strip<% else %>rstrip<% end %>
117
+ <% else %>
118
+ result = result.to_s<% if nuke_inner_whitespace %>.strip<% end %>
119
+ <% end %>
120
+
121
+ <% if escape_html %> result = html_escape(result) <% end %>
122
+
123
+ <% if preserve_tag %>
124
+ result = Haml::Helpers.preserve(result)
125
+ <% elsif preserve_script %>
126
+ result = Haml::Helpers.find_and_preserve(result, options[:preserve])
127
+ <% end %>
128
+
129
+ <% if ugly %>
130
+ return result
131
+ <% else %>
132
+
133
+ has_newline = result.include?("\\n")
134
+ <% if in_tag && !nuke_inner_whitespace %>
135
+ <% unless preserve_tag %> if !has_newline <% end %>
136
+ @real_tabs -= 1
137
+ <% if interpolated %> @tabulation = old_tabulation <% end %>
138
+ return result
139
+ <% unless preserve_tag %> end <% end %>
140
+ <% end %>
141
+
142
+ # Precompiled tabulation may be wrong
143
+ <% if !interpolated && !in_tag %>
144
+ result = tabs + result if @tabulation > 0
145
+ <% end %>
146
+
147
+ if has_newline
148
+ result = result.gsub "\\n", "\\n" + tabs(tabulation)
149
+
150
+ # Add tabulation if it wasn't precompiled
151
+ <% if in_tag && !nuke_inner_whitespace %> result = tabs(tabulation) + result <% end %>
152
+ end
153
+
154
+ <% if in_tag && !nuke_inner_whitespace %>
155
+ result = "\\n\#{result}\\n\#{tabs(tabulation-1)}"
156
+ @real_tabs -= 1
157
+ <% end %>
158
+ <% if interpolated %> @tabulation = old_tabulation <% end %>
159
+ result
160
+ <% end %>
161
+ RUBY
162
+
163
+ # Takes the various information about the opening tag for an
164
+ # element, formats it, and adds it to the buffer.
165
+ def open_tag(name, self_closing, try_one_line, preserve_tag, escape_html, class_id,
166
+ nuke_outer_whitespace, nuke_inner_whitespace, obj_ref, content, *attributes_hashes)
167
+ tabulation = @real_tabs
168
+
169
+ attributes = class_id
170
+ attributes_hashes.each do |old|
171
+ self.class.merge_attrs(attributes, to_hash(old.map {|k, v| [k.to_s, v]}))
172
+ end
173
+ self.class.merge_attrs(attributes, parse_object_ref(obj_ref)) if obj_ref
174
+
175
+ if self_closing && xhtml?
176
+ str = " />" + (nuke_outer_whitespace ? "" : "\n")
177
+ else
178
+ str = ">" + ((if self_closing && html?
179
+ nuke_outer_whitespace
180
+ else
181
+ try_one_line || preserve_tag || nuke_inner_whitespace
182
+ end) ? "" : "\n")
183
+ end
184
+
185
+ attributes = Precompiler.build_attributes(html?, @options[:attr_wrapper], attributes)
186
+ @buffer << "#{nuke_outer_whitespace || @options[:ugly] ? '' : tabs(tabulation)}<#{name}#{attributes}#{str}"
187
+
188
+ if content
189
+ @buffer << "#{content}</#{name}>" << (nuke_outer_whitespace ? "" : "\n")
190
+ return
191
+ end
192
+
193
+ @real_tabs += 1 unless self_closing || nuke_inner_whitespace
194
+ end
195
+
196
+ def self.merge_attrs(to, from)
197
+ if to['id'] && from['id']
198
+ to['id'] << '_' << from.delete('id')
199
+ elsif to['id'] || from['id']
200
+ from['id'] ||= to['id']
201
+ end
202
+
203
+ if to['class'] && from['class']
204
+ # Make sure we don't duplicate class names
205
+ from['class'] = (from['class'].split(' ') | to['class'].split(' ')).join(' ')
206
+ elsif to['class'] || from['class']
207
+ from['class'] ||= to['class']
208
+ end
209
+
210
+ to.merge!(from)
211
+ end
212
+
213
+ private
214
+
215
+ # Some of these methods are exposed as public class methods
216
+ # so they can be re-used in helpers.
217
+
218
+ @@tab_cache = {}
219
+ # Gets <tt>count</tt> tabs. Mostly for internal use.
220
+ def tabs(count = 0)
221
+ tabs = [count + @tabulation, 0].max
222
+ @@tab_cache[tabs] ||= ' ' * tabs
223
+ end
224
+
225
+ # Takes an array of objects and uses the class and id of the first
226
+ # one to create an attributes hash.
227
+ # The second object, if present, is used as a prefix,
228
+ # just like you can do with dom_id() and dom_class() in Rails
229
+ def parse_object_ref(ref)
230
+ prefix = ref[1]
231
+ ref = ref[0]
232
+ # Let's make sure the value isn't nil. If it is, return the default Hash.
233
+ return {} if ref.nil?
234
+ class_name = underscore(ref.class)
235
+ id = "#{class_name}_#{ref.id || 'new'}"
236
+ if prefix
237
+ class_name = "#{ prefix }_#{ class_name}"
238
+ id = "#{ prefix }_#{ id }"
239
+ end
240
+
241
+ {'id' => id, 'class' => class_name}
242
+ end
243
+
244
+ # Changes a word from camel case to underscores.
245
+ # Based on the method of the same name in Rails' Inflector,
246
+ # but copied here so it'll run properly without Rails.
247
+ def underscore(camel_cased_word)
248
+ camel_cased_word.to_s.gsub(/::/, '_').
249
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
250
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
251
+ tr("-", "_").
252
+ downcase
253
+ end
254
+ end
255
+ end