haml-edge 2.1.21 → 2.1.22
Sign up to get free protection for your applications and to get access to all the features.
- data/EDGE_GEM_VERSION +1 -1
- data/FAQ.md +142 -0
- data/{README.rdoc → README.md} +141 -141
- data/Rakefile +29 -17
- data/VERSION +1 -1
- data/lib/haml/buffer.rb +63 -27
- data/lib/haml/engine.rb +103 -80
- data/lib/haml/error.rb +7 -7
- data/lib/haml/exec.rb +80 -26
- data/lib/haml/filters.rb +106 -40
- data/lib/haml/helpers/action_view_extensions.rb +34 -39
- data/lib/haml/helpers/action_view_mods.rb +132 -139
- data/lib/haml/helpers.rb +207 -153
- data/lib/haml/html.rb +40 -21
- data/lib/haml/precompiler.rb +2 -0
- data/lib/haml/shared.rb +34 -3
- data/lib/haml/template/patch.rb +1 -1
- data/lib/haml/template/plugin.rb +0 -2
- data/lib/haml/template.rb +5 -0
- data/lib/haml/util.rb +136 -1
- data/lib/haml/version.rb +16 -4
- data/lib/haml.rb +502 -481
- data/lib/sass/css.rb +106 -68
- data/lib/sass/engine.rb +55 -22
- data/lib/sass/environment.rb +52 -21
- data/lib/sass/error.rb +23 -12
- data/lib/sass/files.rb +27 -0
- data/lib/sass/plugin/merb.rb +2 -2
- data/lib/sass/plugin/rails.rb +0 -2
- data/lib/sass/plugin.rb +32 -23
- data/lib/sass/repl.rb +7 -0
- data/lib/sass/script/bool.rb +9 -5
- data/lib/sass/script/color.rb +87 -1
- data/lib/sass/script/funcall.rb +23 -2
- data/lib/sass/script/functions.rb +93 -44
- data/lib/sass/script/lexer.rb +33 -3
- data/lib/sass/script/literal.rb +93 -1
- data/lib/sass/script/node.rb +14 -0
- data/lib/sass/script/number.rb +128 -4
- data/lib/sass/script/operation.rb +16 -1
- data/lib/sass/script/parser.rb +51 -21
- data/lib/sass/script/string.rb +7 -4
- data/lib/sass/script/unary_operation.rb +14 -1
- data/lib/sass/script/variable.rb +12 -1
- data/lib/sass/script.rb +26 -5
- data/lib/sass/tree/attr_node.rb +46 -9
- data/lib/sass/tree/comment_node.rb +41 -1
- data/lib/sass/tree/debug_node.rb +8 -0
- data/lib/sass/tree/directive_node.rb +20 -0
- data/lib/sass/tree/file_node.rb +12 -0
- data/lib/sass/tree/for_node.rb +15 -0
- data/lib/sass/tree/if_node.rb +22 -0
- data/lib/sass/tree/mixin_def_node.rb +12 -1
- data/lib/sass/tree/mixin_node.rb +13 -0
- data/lib/sass/tree/node.rb +136 -6
- data/lib/sass/tree/rule_node.rb +66 -7
- data/lib/sass/tree/variable_node.rb +10 -0
- data/lib/sass/tree/while_node.rb +11 -1
- data/lib/sass.rb +544 -534
- metadata +7 -6
- data/FAQ +0 -138
data/lib/haml/helpers.rb
CHANGED
@@ -1,24 +1,31 @@
|
|
1
|
-
|
2
|
-
require 'haml/helpers/
|
1
|
+
if defined?(ActionView)
|
2
|
+
require 'haml/helpers/action_view_mods'
|
3
|
+
require 'haml/helpers/action_view_extensions'
|
4
|
+
end
|
3
5
|
|
4
6
|
module Haml
|
5
|
-
# This module contains various helpful methods to make it easier to do
|
6
|
-
#
|
7
|
+
# This module contains various helpful methods to make it easier to do various tasks.
|
8
|
+
# {Haml::Helpers} is automatically included in the context
|
7
9
|
# that a Haml template is parsed in, so all these methods are at your
|
8
10
|
# disposal from within the template.
|
9
11
|
module Helpers
|
10
|
-
# An object that raises an error when #
|
12
|
+
# An object that raises an error when \{#to\_s} is called.
|
11
13
|
# It's used to raise an error when the return value of a helper is used
|
12
14
|
# when it shouldn't be.
|
13
15
|
class ErrorReturn
|
16
|
+
# @param message [String] The error message to raise when \{#to\_s} is called
|
14
17
|
def initialize(message)
|
15
18
|
@message = message
|
16
19
|
end
|
17
20
|
|
21
|
+
# Raises an error.
|
22
|
+
#
|
23
|
+
# @raise [Haml::Error] The error
|
18
24
|
def to_s
|
19
25
|
raise Haml::Error.new(@message)
|
20
26
|
end
|
21
27
|
|
28
|
+
# @return [String] A human-readable string representation
|
22
29
|
def inspect
|
23
30
|
"Haml::Helpers::ErrorReturn(#{@message.inspect})"
|
24
31
|
end
|
@@ -29,45 +36,41 @@ module Haml
|
|
29
36
|
@@action_view_defined = defined?(ActionView)
|
30
37
|
@@force_no_action_view = false
|
31
38
|
|
32
|
-
#
|
39
|
+
# @return [Boolean] Whether or not ActionView is loaded
|
33
40
|
def self.action_view?
|
34
41
|
@@action_view_defined
|
35
42
|
end
|
36
43
|
|
37
|
-
# Note: this does
|
38
|
-
#
|
39
|
-
# in Rails.
|
44
|
+
# Note: this does **not** need to be called when using Haml helpers
|
45
|
+
# normally in Rails.
|
40
46
|
#
|
41
|
-
# Initializes the current object
|
42
|
-
# as
|
43
|
-
# as a normal ActionView rendering
|
44
|
-
# using Haml.
|
47
|
+
# Initializes the current object as though it were in the same context
|
48
|
+
# as a normal ActionView instance using Haml.
|
45
49
|
# This is useful if you want to use the helpers in a context
|
46
50
|
# other than the normal setup with ActionView.
|
47
51
|
# For example:
|
48
52
|
#
|
49
|
-
#
|
50
|
-
#
|
51
|
-
#
|
52
|
-
#
|
53
|
-
#
|
54
|
-
#
|
53
|
+
# context = Object.new
|
54
|
+
# class << context
|
55
|
+
# include Haml::Helpers
|
56
|
+
# end
|
57
|
+
# context.init_haml_helpers
|
58
|
+
# context.haml_tag :p, "Stuff"
|
55
59
|
#
|
56
60
|
def init_haml_helpers
|
57
61
|
@haml_buffer = Haml::Buffer.new(@haml_buffer, Haml::Engine.new('').send(:options_for_buffer))
|
58
62
|
nil
|
59
63
|
end
|
60
64
|
|
61
|
-
# call-seq:
|
62
|
-
# non_haml { ... }
|
63
|
-
#
|
64
65
|
# Runs a block of code in a non-Haml context
|
65
|
-
# (i.e. #
|
66
|
+
# (i.e. \{#is\_haml?} will return false).
|
66
67
|
#
|
67
68
|
# This is mainly useful for rendering sub-templates such as partials in a non-Haml language,
|
68
69
|
# particularly where helpers may behave differently when run from Haml.
|
69
70
|
#
|
70
71
|
# Note that this is automatically applied to Rails partials.
|
72
|
+
#
|
73
|
+
# @yield A block which won't register as Haml
|
71
74
|
def non_haml
|
72
75
|
was_active = @haml_buffer.active?
|
73
76
|
@haml_buffer.active = false
|
@@ -76,14 +79,19 @@ module Haml
|
|
76
79
|
@haml_buffer.active = was_active
|
77
80
|
end
|
78
81
|
|
79
|
-
#
|
80
|
-
# find_and_preserve(input, tags = haml_buffer.options[:preserve])
|
81
|
-
# find_and_preserve(tags = haml_buffer.options[:preserve]) {...}
|
82
|
-
#
|
83
|
-
# Uses preserve to convert any newlines inside whitespace-sensitive tags
|
82
|
+
# Uses \{#preserve} to convert any newlines inside whitespace-sensitive tags
|
84
83
|
# into the HTML entities for endlines.
|
85
|
-
#
|
86
|
-
#
|
84
|
+
#
|
85
|
+
# @param tags [Array<String>] Tags that should have newlines escaped
|
86
|
+
#
|
87
|
+
# @overload find_and_preserve(input, tags = haml_buffer.options[:preserve])
|
88
|
+
# Escapes newlines within a string.
|
89
|
+
#
|
90
|
+
# @param input [String] The string within which to escape newlines
|
91
|
+
# @overload find_and_preserve(tags = haml_buffer.options[:preserve])
|
92
|
+
# Escapes newlines within a block of Haml code.
|
93
|
+
#
|
94
|
+
# @yield The block within which to escape newlines
|
87
95
|
def find_and_preserve(input = nil, tags = haml_buffer.options[:preserve], &block)
|
88
96
|
return find_and_preserve(capture_haml(&block), input || tags) if block
|
89
97
|
|
@@ -93,55 +101,62 @@ module Haml
|
|
93
101
|
end
|
94
102
|
end
|
95
103
|
|
96
|
-
#
|
97
|
-
#
|
98
|
-
# preserve {...}
|
99
|
-
#
|
100
|
-
# Takes any string, finds all the endlines and converts them to
|
101
|
-
# HTML entities for endlines so they'll render correctly in
|
104
|
+
# Takes any string, finds all the newlines, and converts them to
|
105
|
+
# HTML entities so they'll render correctly in
|
102
106
|
# whitespace-sensitive tags without screwing up the indentation.
|
107
|
+
#
|
108
|
+
# @overload perserve(input)
|
109
|
+
# Escapes newlines within a string.
|
110
|
+
#
|
111
|
+
# @param input [String] The string within which to escape all newlines
|
112
|
+
# @overload perserve
|
113
|
+
# Escapes newlines within a block of Haml code.
|
114
|
+
#
|
115
|
+
# @yield The block within which to escape newlines
|
103
116
|
def preserve(input = '', &block)
|
104
117
|
return preserve(capture_haml(&block)) if block
|
105
118
|
|
106
119
|
input.chomp("\n").gsub(/\n/, '
').gsub(/\r/, '')
|
107
120
|
end
|
108
|
-
|
109
121
|
alias_method :flatten, :preserve
|
110
122
|
|
111
|
-
# Takes an Enumerable object and a block
|
112
|
-
# and iterates over the
|
123
|
+
# Takes an `Enumerable` object and a block
|
124
|
+
# and iterates over the enum,
|
113
125
|
# yielding each element to a Haml block
|
114
|
-
# and putting the result into
|
126
|
+
# and putting the result into `<li>` elements.
|
115
127
|
# This creates a list of the results of the block.
|
116
128
|
# For example:
|
117
129
|
#
|
118
|
-
#
|
119
|
-
#
|
130
|
+
# = list_of([['hello'], ['yall']]) do |i|
|
131
|
+
# = i[0]
|
120
132
|
#
|
121
133
|
# Produces:
|
122
134
|
#
|
123
|
-
#
|
124
|
-
#
|
135
|
+
# <li>hello</li>
|
136
|
+
# <li>yall</li>
|
125
137
|
#
|
126
138
|
# And
|
127
139
|
#
|
128
|
-
#
|
129
|
-
#
|
130
|
-
#
|
140
|
+
# = list_of({:title => 'All the stuff', :description => 'A book about all the stuff.'}) do |key, val|
|
141
|
+
# %h3= key.humanize
|
142
|
+
# %p= val
|
131
143
|
#
|
132
144
|
# Produces:
|
133
145
|
#
|
134
|
-
#
|
135
|
-
#
|
136
|
-
#
|
137
|
-
#
|
138
|
-
#
|
139
|
-
#
|
140
|
-
#
|
141
|
-
#
|
142
|
-
#
|
143
|
-
|
144
|
-
|
146
|
+
# <li>
|
147
|
+
# <h3>Title</h3>
|
148
|
+
# <p>All the stuff</p>
|
149
|
+
# </li>
|
150
|
+
# <li>
|
151
|
+
# <h3>Description</h3>
|
152
|
+
# <p>A book about all the stuff.</p>
|
153
|
+
# </li>
|
154
|
+
#
|
155
|
+
# @param enum [Enumerable] The list of objects to iterate over
|
156
|
+
# @yield [item] A block which contains Haml code that goes within list items
|
157
|
+
# @yieldparam item An element of `enum`
|
158
|
+
def list_of(enum, &block)
|
159
|
+
to_return = enum.collect do |i|
|
145
160
|
result = capture_haml(i, &block)
|
146
161
|
|
147
162
|
if result.count("\n") > 1
|
@@ -156,18 +171,18 @@ module Haml
|
|
156
171
|
to_return.join("\n")
|
157
172
|
end
|
158
173
|
|
159
|
-
# Returns a hash containing default assignments for the xmlns and xml:lang
|
160
|
-
# attributes of the
|
161
|
-
# It also takes an optional argument for the value of xml:lang and lang,
|
162
|
-
# which defaults to 'en-US'.
|
174
|
+
# Returns a hash containing default assignments for the `xmlns`, `lang`, and `xml:lang`
|
175
|
+
# attributes of the `html` HTML element.
|
163
176
|
# For example,
|
164
177
|
#
|
165
|
-
#
|
178
|
+
# %html{html_attrs}
|
166
179
|
#
|
167
180
|
# becomes
|
168
181
|
#
|
169
|
-
#
|
182
|
+
# <html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en-US' lang='en-US'>
|
170
183
|
#
|
184
|
+
# @param lang [String] The value of `xml:lang` and `lang`
|
185
|
+
# @return [Hash<#to_s, String>] The attribute hash
|
171
186
|
def html_attrs(lang = 'en-US')
|
172
187
|
{:xmlns => "http://www.w3.org/1999/xhtml", 'xml:lang' => lang, :lang => lang}
|
173
188
|
end
|
@@ -176,18 +191,20 @@ module Haml
|
|
176
191
|
# to the lines of the template.
|
177
192
|
# For example:
|
178
193
|
#
|
179
|
-
#
|
180
|
-
#
|
181
|
-
#
|
182
|
-
#
|
183
|
-
#
|
194
|
+
# %h1 foo
|
195
|
+
# - tab_up
|
196
|
+
# %p bar
|
197
|
+
# - tab_down
|
198
|
+
# %strong baz
|
184
199
|
#
|
185
200
|
# Produces:
|
186
201
|
#
|
187
|
-
#
|
188
|
-
#
|
189
|
-
#
|
202
|
+
# <h1>foo</h1>
|
203
|
+
# <p>bar</p>
|
204
|
+
# <strong>baz</strong>
|
190
205
|
#
|
206
|
+
# @param i [Fixnum] The number of tabs by which to increase the indentation
|
207
|
+
# @see #tab_down
|
191
208
|
def tab_up(i = 1)
|
192
209
|
haml_buffer.tabulation += i
|
193
210
|
end
|
@@ -195,81 +212,91 @@ module Haml
|
|
195
212
|
# Decrements the number of tabs the buffer automatically adds
|
196
213
|
# to the lines of the template.
|
197
214
|
#
|
198
|
-
#
|
215
|
+
# @param i [Fixnum] The number of tabs by which to decrease the indentation
|
216
|
+
# @see #tab_up
|
199
217
|
def tab_down(i = 1)
|
200
218
|
haml_buffer.tabulation -= i
|
201
219
|
end
|
202
220
|
|
203
|
-
# Surrounds
|
221
|
+
# Surrounds a block of Haml code with strings,
|
204
222
|
# with no whitespace in between.
|
205
223
|
# For example:
|
206
224
|
#
|
207
|
-
#
|
208
|
-
#
|
225
|
+
# = surround '(', ')' do
|
226
|
+
# %a{:href => "food"} chicken
|
209
227
|
#
|
210
228
|
# Produces:
|
211
229
|
#
|
212
|
-
#
|
230
|
+
# (<a href='food'>chicken</a>)
|
213
231
|
#
|
214
232
|
# and
|
215
233
|
#
|
216
|
-
#
|
217
|
-
#
|
234
|
+
# = surround '*' do
|
235
|
+
# %strong angry
|
218
236
|
#
|
219
237
|
# Produces:
|
220
238
|
#
|
221
|
-
#
|
239
|
+
# *<strong>angry</strong>*
|
222
240
|
#
|
223
|
-
|
224
|
-
|
241
|
+
# @param front [String] The string to add before the Haml
|
242
|
+
# @param back [String] The string to add after the Haml
|
243
|
+
# @yield A block of Haml to surround
|
244
|
+
def surround(front, back = front, &block)
|
225
245
|
output = capture_haml(&block)
|
226
246
|
|
227
247
|
"#{front}#{output.chomp}#{back}\n"
|
228
248
|
end
|
229
249
|
|
230
|
-
# Prepends
|
250
|
+
# Prepends a string to the beginning of a Haml block,
|
231
251
|
# with no whitespace between.
|
232
252
|
# For example:
|
233
253
|
#
|
234
|
-
#
|
235
|
-
#
|
254
|
+
# = precede '*' do
|
255
|
+
# %span.small Not really
|
236
256
|
#
|
237
257
|
# Produces:
|
238
258
|
#
|
239
|
-
#
|
259
|
+
# *<span class='small'>Not really</span>
|
240
260
|
#
|
241
|
-
|
242
|
-
|
261
|
+
# @param str [String] The string to add before the Haml
|
262
|
+
# @yield A block of Haml to prepend to
|
263
|
+
def precede(str, &block)
|
264
|
+
"#{str}#{capture_haml(&block).chomp}\n"
|
243
265
|
end
|
244
266
|
|
245
|
-
# Appends
|
267
|
+
# Appends a string to the end of a Haml block,
|
246
268
|
# with no whitespace between.
|
247
269
|
# For example:
|
248
270
|
#
|
249
|
-
#
|
250
|
-
#
|
251
|
-
#
|
271
|
+
# click
|
272
|
+
# = succeed '.' do
|
273
|
+
# %a{:href=>"thing"} here
|
252
274
|
#
|
253
275
|
# Produces:
|
254
276
|
#
|
255
|
-
#
|
256
|
-
#
|
277
|
+
# click
|
278
|
+
# <a href='thing'>here</a>.
|
257
279
|
#
|
258
|
-
|
259
|
-
|
280
|
+
# @param str [String] The string to add after the Haml
|
281
|
+
# @yield A block of Haml to append to
|
282
|
+
def succeed(str, &block)
|
283
|
+
"#{capture_haml(&block).chomp}#{str}\n"
|
260
284
|
end
|
261
285
|
|
262
|
-
# Captures the result of
|
286
|
+
# Captures the result of a block of Haml code,
|
263
287
|
# gets rid of the excess indentation,
|
264
288
|
# and returns it as a string.
|
265
289
|
# For example, after the following,
|
266
290
|
#
|
267
|
-
#
|
268
|
-
#
|
269
|
-
#
|
291
|
+
# .foo
|
292
|
+
# - foo = capture_haml(13) do |a|
|
293
|
+
# %p= a
|
270
294
|
#
|
271
|
-
# the local variable
|
295
|
+
# the local variable `foo` would be assigned to `"<p>13</p>\n"`.
|
272
296
|
#
|
297
|
+
# @param args [Array] Arguments to pass into the block
|
298
|
+
# @yield [args] A block of Haml code that will be converted to a string
|
299
|
+
# @yieldparam args [Array] `args`
|
273
300
|
def capture_haml(*args, &block)
|
274
301
|
buffer = eval('_hamlout', block.binding) rescue haml_buffer
|
275
302
|
with_haml_buffer(buffer) do
|
@@ -295,7 +322,9 @@ module Haml
|
|
295
322
|
haml_buffer.capture_position = nil
|
296
323
|
end
|
297
324
|
|
298
|
-
|
325
|
+
# @deprecated This will be removed in version 2.4.
|
326
|
+
# @see \{#haml\_concat}
|
327
|
+
def puts(*args)
|
299
328
|
warn <<END
|
300
329
|
DEPRECATION WARNING:
|
301
330
|
The Haml #puts helper is deprecated and will be removed in version 2.4.
|
@@ -304,67 +333,76 @@ END
|
|
304
333
|
haml_concat(*args)
|
305
334
|
end
|
306
335
|
|
307
|
-
# Outputs text directly to the Haml buffer, with the proper
|
336
|
+
# Outputs text directly to the Haml buffer, with the proper indentation.
|
337
|
+
#
|
338
|
+
# @param text [#to_s] The text to output
|
308
339
|
def haml_concat(text = "")
|
309
340
|
haml_buffer.buffer << haml_indent << text.to_s << "\n"
|
310
341
|
nil
|
311
342
|
end
|
312
343
|
|
313
|
-
#
|
344
|
+
# @return [String] The indentation string for the current line
|
314
345
|
def haml_indent
|
315
346
|
' ' * haml_buffer.tabulation
|
316
347
|
end
|
317
348
|
|
318
|
-
#
|
319
|
-
# call-seq:
|
320
|
-
# haml_tag(name, *flags, attributes = {}) {...}
|
321
|
-
# haml_tag(name, text, *flags, attributes = {}) {...}
|
322
|
-
#
|
323
349
|
# Creates an HTML tag with the given name and optionally text and attributes.
|
324
|
-
# Can take a block that will
|
325
|
-
#
|
326
|
-
# If the block is a Haml block or outputs text using haml_concat,
|
350
|
+
# Can take a block that will run between the opening and closing tags.
|
351
|
+
# If the block is a Haml block or outputs text using \{#haml\_concat},
|
327
352
|
# the text will be properly indented.
|
328
353
|
#
|
329
|
-
#
|
354
|
+
# `flags` is a list of symbol flags
|
330
355
|
# like those that can be put at the end of a Haml tag
|
331
|
-
# (
|
332
|
-
# Currently, only
|
356
|
+
# (`:/`, `:<`, and `:>`).
|
357
|
+
# Currently, only `:/` and `:<` are supported.
|
358
|
+
#
|
359
|
+
# `haml_tag` outputs directly to the buffer;
|
360
|
+
# its return value should not be used.
|
361
|
+
# If you need to get the results as a string,
|
362
|
+
# use \{#capture\_haml\}.
|
333
363
|
#
|
334
364
|
# For example,
|
335
365
|
#
|
336
|
-
#
|
337
|
-
#
|
338
|
-
#
|
339
|
-
#
|
340
|
-
#
|
341
|
-
#
|
342
|
-
#
|
343
|
-
#
|
366
|
+
# haml_tag :table do
|
367
|
+
# haml_tag :tr do
|
368
|
+
# haml_tag :td, {:class => 'cell'} do
|
369
|
+
# haml_tag :strong, "strong!"
|
370
|
+
# haml_concat "data"
|
371
|
+
# end
|
372
|
+
# haml_tag :td do
|
373
|
+
# haml_concat "more_data"
|
374
|
+
# end
|
344
375
|
# end
|
345
376
|
# end
|
346
|
-
# end
|
347
377
|
#
|
348
378
|
# outputs
|
349
379
|
#
|
350
|
-
#
|
351
|
-
#
|
352
|
-
#
|
353
|
-
#
|
354
|
-
#
|
355
|
-
#
|
356
|
-
#
|
357
|
-
#
|
358
|
-
#
|
359
|
-
#
|
360
|
-
#
|
361
|
-
#
|
362
|
-
#
|
363
|
-
#
|
380
|
+
# <table>
|
381
|
+
# <tr>
|
382
|
+
# <td class='cell'>
|
383
|
+
# <strong>
|
384
|
+
# strong!
|
385
|
+
# </strong>
|
386
|
+
# data
|
387
|
+
# </td>
|
388
|
+
# <td>
|
389
|
+
# more_data
|
390
|
+
# </td>
|
391
|
+
# </tr>
|
392
|
+
# </table>
|
393
|
+
#
|
394
|
+
# @param name [#to_s] The name of the tag
|
395
|
+
# @param flags [Array<Symbol>] Haml end-of-tag flags
|
396
|
+
#
|
397
|
+
# @overload haml_tag(name, *flags, attributes = {})
|
398
|
+
# @yield The block of Haml code within the tag
|
399
|
+
# @overload haml_tag(name, text, *flags, attributes = {})
|
400
|
+
# @param text [#to_s] The text within the tag
|
364
401
|
def haml_tag(name, *rest, &block)
|
365
402
|
ret = ErrorReturn.new(<<MESSAGE)
|
366
403
|
haml_tag outputs directly to the Haml template.
|
367
|
-
Disregard its return value and use the - operator
|
404
|
+
Disregard its return value and use the - operator,
|
405
|
+
or use capture_haml to get the value as a String.
|
368
406
|
MESSAGE
|
369
407
|
|
370
408
|
name = name.to_s
|
@@ -414,28 +452,39 @@ MESSAGE
|
|
414
452
|
# Characters that need to be escaped to HTML entities from user input
|
415
453
|
HTML_ESCAPE = { '&'=>'&', '<'=>'<', '>'=>'>', '"'=>'"', "'"=>''', }
|
416
454
|
|
417
|
-
# Returns a copy of
|
455
|
+
# Returns a copy of `text` with ampersands, angle brackets and quotes
|
418
456
|
# escaped into HTML entities.
|
457
|
+
#
|
458
|
+
# @param text [String] The string to sanitize
|
459
|
+
# @return [String] The sanitized string
|
419
460
|
def html_escape(text)
|
420
461
|
text.to_s.gsub(/[\"><&]/) { |s| HTML_ESCAPE[s] }
|
421
462
|
end
|
422
463
|
|
423
|
-
# Escapes HTML entities in
|
464
|
+
# Escapes HTML entities in `text`, but without escaping an ampersand
|
424
465
|
# that is already part of an escaped entity.
|
466
|
+
#
|
467
|
+
# @param text [String] The string to sanitize
|
468
|
+
# @return [String] The sanitized string
|
425
469
|
def escape_once(text)
|
426
470
|
text.to_s.gsub(/[\"><]|&(?!([a-zA-Z]+|(#\d+));)/) { |s| HTML_ESCAPE[s] }
|
427
471
|
end
|
428
472
|
|
429
473
|
# Returns whether or not the current template is a Haml template.
|
430
474
|
#
|
431
|
-
# This function, unlike other Haml::Helpers functions,
|
432
|
-
# also works in other ActionView templates,
|
475
|
+
# This function, unlike other {Haml::Helpers} functions,
|
476
|
+
# also works in other `ActionView` templates,
|
433
477
|
# where it will always return false.
|
478
|
+
#
|
479
|
+
# @return [Boolean] Whether or not the current template is a Haml template
|
434
480
|
def is_haml?
|
435
481
|
!@haml_buffer.nil? && @haml_buffer.active?
|
436
482
|
end
|
437
483
|
|
438
|
-
# Returns whether or not
|
484
|
+
# Returns whether or not `block` is defined directly in a Haml template.
|
485
|
+
#
|
486
|
+
# @param block [Proc] A Ruby block
|
487
|
+
# @return [Boolean] Whether or not `block` is defined directly in a Haml template
|
439
488
|
def block_is_haml?(block)
|
440
489
|
eval('_hamlout', block.binding)
|
441
490
|
true
|
@@ -445,10 +494,10 @@ MESSAGE
|
|
445
494
|
|
446
495
|
private
|
447
496
|
|
448
|
-
#
|
449
|
-
# with_haml_buffer(buffer) {...}
|
497
|
+
# Runs a block of code with the given buffer as the currently active buffer.
|
450
498
|
#
|
451
|
-
#
|
499
|
+
# @param buffer [Haml::Buffer] The Haml buffer to use temporarily
|
500
|
+
# @yield A block in which the given buffer should be used
|
452
501
|
def with_haml_buffer(buffer)
|
453
502
|
@haml_buffer, old_buffer = buffer, @haml_buffer
|
454
503
|
old_buffer.active, was_active = false, old_buffer.active? if old_buffer
|
@@ -460,13 +509,18 @@ MESSAGE
|
|
460
509
|
@haml_buffer = old_buffer
|
461
510
|
end
|
462
511
|
|
463
|
-
#
|
512
|
+
# The current {Haml::Buffer} object.
|
513
|
+
#
|
514
|
+
# @return [Haml::Buffer]
|
464
515
|
def haml_buffer
|
465
516
|
@haml_buffer
|
466
517
|
end
|
467
518
|
|
468
|
-
# Gives a proc the same local
|
519
|
+
# Gives a proc the same local `_hamlout` and `_erbout` variables
|
469
520
|
# that the current template has.
|
521
|
+
#
|
522
|
+
# @param proc [#call] The proc to bind
|
523
|
+
# @return [Proc] A new proc with the new variables bound
|
470
524
|
def haml_bind_proc(&proc)
|
471
525
|
_hamlout = haml_buffer
|
472
526
|
_erbout = _hamlout.buffer
|
@@ -478,12 +532,12 @@ MESSAGE
|
|
478
532
|
end
|
479
533
|
|
480
534
|
class Object
|
481
|
-
# Haml overrides various ActionView helpers,
|
482
|
-
# which call an #
|
535
|
+
# Haml overrides various `ActionView` helpers,
|
536
|
+
# which call an \{#is\_haml?} method
|
483
537
|
# to determine whether or not the current context object
|
484
538
|
# is a proper Haml context.
|
485
|
-
# Because ActionView helpers may be included in non
|
486
|
-
# it's a good idea to define
|
539
|
+
# Because `ActionView` helpers may be included in non-`ActionView::Base` classes,
|
540
|
+
# it's a good idea to define \{#is\_haml?} for all objects.
|
487
541
|
def is_haml?
|
488
542
|
false
|
489
543
|
end
|