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,22 +1,22 @@
1
1
  module Haml
2
2
  # An exception raised by Haml code.
3
3
  class Error < StandardError
4
- # :stopdoc:
5
-
6
- # By default, an error is taken to refer to the line of the template
7
- # that was being processed when the exception was raised.
8
- # However, if line is non-nil, it + 1 is used instead.
4
+ # The line of the template on which the error occurred.
5
+ #
6
+ # @return [Fixnum]
9
7
  attr_reader :line
10
8
 
9
+ # @param message [String] The error message
10
+ # @param line [Fixnum] See \{#line}
11
11
  def initialize(message = nil, line = nil)
12
12
  super(message)
13
13
  @line = line
14
14
  end
15
- # :startdoc:
16
15
  end
17
16
 
18
17
  # SyntaxError is the type of exception raised when Haml encounters an
19
18
  # ill-formatted document.
20
- # It's not particularly interesting, except in that it includes Haml::Error.
19
+ # It's not particularly interesting,
20
+ # except in that it's a subclass of {Haml::Error}.
21
21
  class SyntaxError < Haml::Error; end
22
22
  end
@@ -2,19 +2,18 @@ require 'optparse'
2
2
  require 'fileutils'
3
3
 
4
4
  module Haml
5
- # This module contains code for working with the
6
- # haml, sass, and haml2html executables,
7
- # such as command-line parsing stuff.
8
- # It shouldn't need to be invoked by client code.
9
- module Exec # :nodoc:
10
- # A class that encapsulates the executable code
11
- # for all three executables.
12
- class Generic # :nodoc:
5
+ # This module handles the various Haml executables (`haml`, `sass`, `css2sass`, etc).
6
+ module Exec
7
+ # An abstract class that encapsulates the executable code for all three executables.
8
+ class Generic
9
+ # @param args [Array<String>] The command-line arguments
13
10
  def initialize(args)
14
11
  @args = args
15
12
  @options = {}
16
13
  end
17
14
 
15
+ # Parses the command-line arguments and runs the executable.
16
+ # Calls `Kernel#exit` at the end, so it never returns.
18
17
  def parse!
19
18
  begin
20
19
  @opts = OptionParser.new(&method(:set_opts))
@@ -32,12 +31,18 @@ module Haml
32
31
  exit 0
33
32
  end
34
33
 
34
+ # @return [String] A description of the executable
35
35
  def to_s
36
36
  @opts.to_s
37
37
  end
38
38
 
39
39
  protected
40
40
 
41
+ # Finds the line of the source template
42
+ # on which an exception was raised.
43
+ #
44
+ # @param exception [Exception] The exception
45
+ # @return [String] The line number
41
46
  def get_line(exception)
42
47
  # SyntaxErrors have weird line reporting
43
48
  # when there's trailing whitespace,
@@ -46,8 +51,13 @@ module Haml
46
51
  exception.backtrace[0].scan(/:(\d+)/).first.first
47
52
  end
48
53
 
49
- private
50
-
54
+ # Tells optparse how to parse the arguments
55
+ # available for all executables.
56
+ #
57
+ # This is meant to be overridden by subclasses
58
+ # so they can add their own options.
59
+ #
60
+ # @param opts [OptionParser]
51
61
  def set_opts(opts)
52
62
  opts.on('-s', '--stdin', :NONE, 'Read input from standard input instead of an input file') do
53
63
  @options[:input] = $stdin
@@ -68,12 +78,19 @@ module Haml
68
78
  end
69
79
  end
70
80
 
81
+ # Processes the options set by the command-line arguments.
82
+ # In particular, sets `@options[:input]` and `@options[:output]`
83
+ # to appropriate IO streams.
84
+ #
85
+ # This is meant to be overridden by subclasses
86
+ # so they can run their respective programs.
71
87
  def process_result
72
88
  input, output = @options[:input], @options[:output]
73
89
  input_file, output_file = if input
74
- [nil, open_file(ARGV[0], 'w')]
90
+ [nil, open_file(@args[0], 'w')]
75
91
  else
76
- [open_file(ARGV[0]), open_file(ARGV[1], 'w')]
92
+ @options[:filename] = @args[0]
93
+ [open_file(@args[0]), open_file(@args[1], 'w')]
77
94
  end
78
95
 
79
96
  input ||= input_file
@@ -84,22 +101,32 @@ module Haml
84
101
  @options[:input], @options[:output] = input, output
85
102
  end
86
103
 
104
+ private
105
+
87
106
  def open_file(filename, flag = 'r')
88
107
  return if filename.nil?
89
108
  File.open(filename, flag)
90
109
  end
91
110
  end
92
111
 
93
- # A class encapsulating the executable functionality
94
- # specific to Haml and Sass.
95
- class HamlSass < Generic # :nodoc:
112
+ # An abstrac class that encapsulates the code
113
+ # specific to the `haml` and `sass` executables.
114
+ class HamlSass < Generic
115
+ # @param args [Array<String>] The command-line arguments
96
116
  def initialize(args)
97
117
  super
98
118
  @options[:for_engine] = {}
99
119
  end
100
120
 
101
- private
121
+ protected
102
122
 
123
+ # Tells optparse how to parse the arguments
124
+ # available for the `haml` and `sass` executables.
125
+ #
126
+ # This is meant to be overridden by subclasses
127
+ # so they can add their own options.
128
+ #
129
+ # @param opts [OptionParser]
103
130
  def set_opts(opts)
104
131
  opts.banner = <<END
105
132
  Usage: #{@name.downcase} [options] [INPUT] [OUTPUT]
@@ -154,20 +181,33 @@ END
154
181
  super
155
182
  end
156
183
 
184
+ # Processes the options set by the command-line arguments.
185
+ # In particular, sets `@options[:for_engine][:filename]` to the input filename
186
+ # and requires the appropriate file.
187
+ #
188
+ # This is meant to be overridden by subclasses
189
+ # so they can run their respective programs.
157
190
  def process_result
158
191
  super
192
+ @options[:for_engine][:filename] = @options[:filename] if @options[:filename]
159
193
  require File.dirname(__FILE__) + "/../#{@name.downcase}"
160
194
  end
161
195
  end
162
196
 
163
- # A class encapsulating executable functionality
164
- # specific to Sass.
165
- class Sass < HamlSass # :nodoc:
197
+ # The `sass` executable.
198
+ class Sass < HamlSass
199
+ # @param args [Array<String>] The command-line arguments
166
200
  def initialize(args)
167
201
  super
168
202
  @name = "Sass"
203
+ @options[:for_engine][:load_paths] = ['.'] + (ENV['SASSPATH'] || '').split(File::PATH_SEPARATOR)
169
204
  end
170
205
 
206
+ protected
207
+
208
+ # Tells optparse how to parse the arguments.
209
+ #
210
+ # @param opts [OptionParser]
171
211
  def set_opts(opts)
172
212
  super
173
213
 
@@ -175,34 +215,62 @@ END
175
215
  'Output style. Can be nested (default), compact, compressed, or expanded.') do |name|
176
216
  @options[:for_engine][:style] = name.to_sym
177
217
  end
218
+ opts.on('-l', '--line-comments',
219
+ 'Line Comments. Emit comments in the generated CSS indicating the corresponding sass line.') do
220
+ @options[:for_engine][:line_comments] = true
221
+ end
222
+ opts.on('-i', '--interactive',
223
+ 'Run an interactive SassScript shell.') do
224
+ @options[:interactive] = true
225
+ end
226
+ opts.on('-I', '--load-path PATH', 'Add a sass import path.') do |path|
227
+ @options[:for_engine][:load_paths] << path
228
+ end
229
+ opts.on('--cache-location', 'The path to put cached Sass files. Defaults to .sass-cache.') do |loc|
230
+ @options[:for_engine][:cache_location] = path
231
+ end
232
+ opts.on('-C', '--no-cache', "Don't cache to sassc files.") do
233
+ @options[:for_engine][:cache] = false
234
+ end
178
235
  end
179
236
 
237
+ # Processes the options set by the command-line arguments,
238
+ # and runs the Sass compiler appropriately.
180
239
  def process_result
240
+ if @options[:interactive]
241
+ require 'sass'
242
+ require 'sass/repl'
243
+ ::Sass::Repl.new(@options).run
244
+ return
245
+ end
246
+
181
247
  super
182
248
  input = @options[:input]
183
249
  output = @options[:output]
184
250
 
185
- template = input.read()
186
- input.close() if input.is_a? File
251
+ tree =
252
+ if input.is_a?(File) && !@options[:check_syntax]
253
+ ::Sass::Files.tree_for(input.path, @options[:for_engine])
254
+ else
255
+ # We don't need to do any special handling of @options[:check_syntax] here,
256
+ # because the Sass syntax checking happens alongside evaluation
257
+ # and evaluation doesn't actually evaluate any code anyway.
258
+ ::Sass::Engine.new(input.read(), @options[:for_engine]).to_tree
259
+ end
187
260
 
188
- begin
189
- # We don't need to do any special handling of @options[:check_syntax] here,
190
- # because the Sass syntax checking happens alongside evaluation
191
- # and evaluation doesn't actually evaluate any code anyway.
192
- result = ::Sass::Engine.new(template, @options[:for_engine]).render
193
- rescue ::Sass::SyntaxError => e
194
- raise e if @options[:trace]
195
- raise "Syntax error on line #{get_line e}: #{e.message}"
196
- end
261
+ input.close() if input.is_a?(File)
197
262
 
198
- output.write(result)
263
+ output.write(tree.render)
199
264
  output.close() if output.is_a? File
265
+ rescue ::Sass::SyntaxError => e
266
+ raise e if @options[:trace]
267
+ raise "Syntax error on line #{get_line e}: #{e.message}"
200
268
  end
201
269
  end
202
270
 
203
- # A class encapsulating executable functionality
204
- # specific to Haml.
205
- class Haml < HamlSass # :nodoc:
271
+ # The `haml` executable.
272
+ class Haml < HamlSass
273
+ # @param args [Array<String>] The command-line arguments
206
274
  def initialize(args)
207
275
  super
208
276
  @name = "Haml"
@@ -210,6 +278,9 @@ END
210
278
  @options[:load_paths] = []
211
279
  end
212
280
 
281
+ # Tells optparse how to parse the arguments.
282
+ #
283
+ # @param opts [OptionParser]
213
284
  def set_opts(opts)
214
285
  super
215
286
 
@@ -241,6 +312,8 @@ END
241
312
  end
242
313
  end
243
314
 
315
+ # Processes the options set by the command-line arguments,
316
+ # and runs the Haml compiler appropriately.
244
317
  def process_result
245
318
  super
246
319
  input = @options[:input]
@@ -280,9 +353,9 @@ END
280
353
  end
281
354
  end
282
355
 
283
- # A class encapsulating executable functionality
284
- # specific to the html2haml executable.
285
- class HTML2Haml < Generic # :nodoc:
356
+ # The `html2haml` executable.
357
+ class HTML2Haml < Generic
358
+ # @param args [Array<String>] The command-line arguments
286
359
  def initialize(args)
287
360
  super
288
361
 
@@ -297,6 +370,9 @@ END
297
370
  end
298
371
  end
299
372
 
373
+ # Tells optparse how to parse the arguments.
374
+ #
375
+ # @param opts [OptionParser]
300
376
  def set_opts(opts)
301
377
  opts.banner = <<END
302
378
  Usage: html2haml [options] [INPUT] [OUTPUT]
@@ -321,6 +397,8 @@ END
321
397
  super
322
398
  end
323
399
 
400
+ # Processes the options set by the command-line arguments,
401
+ # and runs the HTML compiler appropriately.
324
402
  def process_result
325
403
  super
326
404
 
@@ -334,9 +412,9 @@ END
334
412
  end
335
413
  end
336
414
 
337
- # A class encapsulating executable functionality
338
- # specific to the css2sass executable.
339
- class CSS2Sass < Generic # :nodoc:
415
+ # The `css2sass` executable.
416
+ class CSS2Sass < Generic
417
+ # @param args [Array<String>] The command-line arguments
340
418
  def initialize(args)
341
419
  super
342
420
 
@@ -345,6 +423,9 @@ END
345
423
  require 'sass/css'
346
424
  end
347
425
 
426
+ # Tells optparse how to parse the arguments.
427
+ #
428
+ # @param opts [OptionParser]
348
429
  def set_opts(opts)
349
430
  opts.banner = <<END
350
431
  Usage: css2sass [options] [INPUT] [OUTPUT]
@@ -354,13 +435,17 @@ Description: Transforms a CSS file into corresponding Sass code.
354
435
  Options:
355
436
  END
356
437
 
357
- opts.on('-a', '--alternate', 'Output using alternative Sass syntax (margin: 1px)') do
358
- @module_opts[:alternate] = true
438
+ opts.on('--old', 'Output the old-style ":prop val" property syntax') do
439
+ @module_opts[:old] = true
359
440
  end
360
441
 
442
+ opts.on_tail('-a', '--alternate', 'Ignored') {}
443
+
361
444
  super
362
445
  end
363
446
 
447
+ # Processes the options set by the command-line arguments,
448
+ # and runs the CSS compiler appropriately.
364
449
  def process_result
365
450
  super
366
451
 
@@ -1,72 +1,98 @@
1
1
  module Haml
2
- # The module containing the default filters,
3
- # as well as the base module,
4
- # Haml::Filters::Base.
2
+ # The module containing the default Haml filters,
3
+ # as well as the base module, {Haml::Filters::Base}.
4
+ #
5
+ # @see Haml::Filters::Base
5
6
  module Filters
6
- # Returns a hash of defined filters.
7
+ # @return [Hash<String, Haml::Filters::Base>] a hash of filter names to classes
7
8
  def self.defined
8
9
  @defined ||= {}
9
10
  end
10
11
 
11
12
  # The base module for Haml filters.
12
13
  # User-defined filters should be modules including this module.
14
+ # The name of the filter is taken by downcasing the module name.
15
+ # For instance, if the module is named `FooBar`, the filter will be `:foobar`.
13
16
  #
14
- # A user-defined filter should override either Base#render or Base #compile.
15
- # Base#render is the most common.
17
+ # A user-defined filter should override either \{#render} or {\#compile}.
18
+ # \{#render} is the most common.
16
19
  # It takes a string, the filter source,
17
- # and returns another string,
18
- # the result of the filter.
19
- # For example:
20
+ # and returns another string, the result of the filter.
21
+ # For example, the following will define a filter named `:sass`:
20
22
  #
21
- # module Haml::Filters::Sass
22
- # include Haml::Filters::Base
23
+ # module Haml::Filters::Sass
24
+ # include Haml::Filters::Base
23
25
  #
24
- # def render(text)
25
- # ::Sass::Engine.new(text).render
26
+ # def render(text)
27
+ # ::Sass::Engine.new(text).render
28
+ # end
26
29
  # end
27
- # end
28
30
  #
29
- # For details on overriding #compile, see its documentation.
31
+ # For details on overriding \{#compile}, see its documentation.
30
32
  #
33
+ # Note that filters overriding \{#render} automatically support `#{}`
34
+ # for interpolating Ruby code.
35
+ # Those overriding \{#compile} will need to add such support manually
36
+ # if it's desired.
31
37
  module Base
32
- def self.included(base) # :nodoc:
38
+ # This method is automatically called when {Base} is included in a module.
39
+ # It automatically defines a filter
40
+ # with the downcased name of that module.
41
+ # For example, if the module is named `FooBar`, the filter will be `:foobar`.
42
+ #
43
+ # @param base [Module, Class] The module that this is included in
44
+ def self.included(base)
33
45
  Filters.defined[base.name.split("::").last.downcase] = base
34
46
  base.extend(base)
35
- base.instance_variable_set "@lazy_requires", nil
36
47
  end
37
48
 
38
- # Takes a string, the source text that should be passed to the filter,
39
- # and returns the string resulting from running the filter on <tt>text</tt>.
49
+ # Takes the source text that should be passed to the filter
50
+ # and returns the result of running the filter on that string.
40
51
  #
41
52
  # This should be overridden in most individual filter modules
42
53
  # to render text with the given filter.
43
- # If compile is overridden, however, render doesn't need to be.
54
+ # If \{#compile} is overridden, however, \{#render} doesn't need to be.
55
+ #
56
+ # @param text [String] The source text for the filter to process
57
+ # @return [String] The filtered result
58
+ # @raise [Haml::Error] if it's not overridden
44
59
  def render(text)
45
60
  raise Error.new("#{self.inspect}#render not defined!")
46
61
  end
47
62
 
48
- # Same as render, but takes the Haml options hash as well.
49
- # It's only safe to rely on options made available in Haml::Engine#options_for_buffer.
63
+ # Same as \{#render}, but takes a {Haml::Engine} options hash as well.
64
+ # It's only safe to rely on options made available in {Haml::Engine#options\_for\_buffer}.
65
+ #
66
+ # @see #render
67
+ # @param text [String] The source text for the filter to process
68
+ # @return [String] The filtered result
69
+ # @raise [Haml::Error] if it or \{#render} isn't overridden
50
70
  def render_with_options(text, options)
51
71
  render(text)
52
72
  end
53
73
 
54
- def internal_compile(*args) # :nodoc:
74
+ # Same as \{#compile}, but requires the necessary files first.
75
+ # *This is used by {Haml::Engine} and is not intended to be overridden or used elsewhere.*
76
+ #
77
+ # @see #compile
78
+ def internal_compile(*args)
55
79
  resolve_lazy_requires
56
80
  compile(*args)
57
81
  end
58
82
 
59
- # compile should be overridden when a filter needs to have access
60
- # to the Haml evaluation context.
83
+ # This should be overridden when a filter needs to have access to the Haml evaluation context.
61
84
  # Rather than applying a filter to a string at compile-time,
62
- # compile uses the Haml::Precompiler instance to compile the string to Ruby code
85
+ # \{#compile} uses the {Haml::Precompiler} instance to compile the string to Ruby code
63
86
  # that will be executed in the context of the active Haml template.
64
87
  #
65
- # Warning: the Haml::Precompiler interface is neither well-documented
88
+ # Warning: the {Haml::Precompiler} interface is neither well-documented
66
89
  # nor guaranteed to be stable.
67
- # If you want to make use of it,
68
- # you'll probably need to look at the source code
90
+ # If you want to make use of it, you'll probably need to look at the source code
69
91
  # and should test your filter when upgrading to new Haml versions.
92
+ #
93
+ # @param precompiler [Haml::Precompiler] The precompiler instance
94
+ # @param text [String] The text of the filter
95
+ # @raise [Haml::Error] if none of \{#compile}, \{#render}, and \{#render_with_options} are overridden
70
96
  def compile(precompiler, text)
71
97
  resolve_lazy_requires
72
98
  filter = self
@@ -74,7 +100,7 @@ module Haml
74
100
  if contains_interpolation?(text)
75
101
  return if options[:suppress_eval]
76
102
 
77
- push_script(<<RUBY, false)
103
+ push_script <<RUBY
78
104
  find_and_preserve(#{filter.inspect}.render_with_options(#{unescape_interpolation(text)}, _hamlout.options))
79
105
  RUBY
80
106
  return
@@ -90,22 +116,22 @@ RUBY
90
116
  end
91
117
  end
92
118
 
93
- # This becomes a class method of modules that include Base.
119
+ # This becomes a class method of modules that include {Base}.
94
120
  # It allows the module to specify one or more Ruby files
95
121
  # that Haml should try to require when compiling the filter.
96
122
  #
97
- # The first file specified is tried first,
98
- # then the second, etc.
123
+ # The first file specified is tried first, then the second, etc.
99
124
  # If none are found, the compilation throws an exception.
100
125
  #
101
126
  # For example:
102
127
  #
103
- # module Haml::Filters::Markdown
104
- # lazy_require 'rdiscount', 'peg_markdown', 'maruku', 'bluecloth'
128
+ # module Haml::Filters::Markdown
129
+ # lazy_require 'rdiscount', 'peg_markdown', 'maruku', 'bluecloth'
105
130
  #
106
- # ...
107
- # end
131
+ # ...
132
+ # end
108
133
  #
134
+ # @param reqs [Array<String>] The requires to run
109
135
  def lazy_require(*reqs)
110
136
  @lazy_requires = reqs
111
137
  end
@@ -140,23 +166,29 @@ RUBY
140
166
  end
141
167
  end
142
168
 
143
- # :stopdoc:
144
-
145
169
  begin
146
170
  require 'rubygems'
147
171
  rescue LoadError; end
148
172
 
149
173
  module Haml
150
174
  module Filters
175
+ # Does not parse the filtered text.
176
+ # This is useful for large blocks of text without HTML tags,
177
+ # when you don't want lines starting with `.` or `-`
178
+ # to be parsed.
151
179
  module Plain
152
180
  include Base
153
181
 
182
+ # @see Base#render
154
183
  def render(text); text; end
155
184
  end
156
185
 
186
+ # Surrounds the filtered text with `<script>` and CDATA tags.
187
+ # Useful for including inline Javascript.
157
188
  module Javascript
158
189
  include Base
159
190
 
191
+ # @see Base#render_with_options
160
192
  def render_with_options(text, options)
161
193
  <<END
162
194
  <script type=#{options[:attr_wrapper]}text/javascript#{options[:attr_wrapper]}>
@@ -168,26 +200,37 @@ END
168
200
  end
169
201
  end
170
202
 
203
+ # Surrounds the filtered text with CDATA tags.
171
204
  module Cdata
172
205
  include Base
173
206
 
207
+ # @see Base#render
174
208
  def render(text)
175
209
  "<![CDATA[#{("\n" + text).rstrip.gsub("\n", "\n ")}\n]]>"
176
210
  end
177
211
  end
178
212
 
213
+ # Works the same as {Plain}, but HTML-escapes the text
214
+ # before placing it in the document.
179
215
  module Escaped
180
216
  include Base
181
217
 
218
+ # @see Base#render
182
219
  def render(text)
183
220
  Haml::Helpers.html_escape text
184
221
  end
185
222
  end
186
223
 
224
+ # Parses the filtered text with the normal Ruby interpreter.
225
+ # All output sent to `$stdout`, such as with `puts`,
226
+ # is output into the Haml document.
227
+ # Not available if the {file:HAML_REFERENCE.md#suppress_eval-option `:suppress_eval`} option is set to true.
228
+ # The Ruby code is evaluated in the same context as the Haml template.
187
229
  module Ruby
188
230
  include Base
189
231
  lazy_require 'stringio'
190
232
 
233
+ # @see Base#compile
191
234
  def compile(precompiler, text)
192
235
  return if precompiler.options[:suppress_eval]
193
236
  precompiler.instance_eval do
@@ -202,27 +245,40 @@ END
202
245
  end
203
246
  end
204
247
 
248
+ # Inserts the filtered text into the template with whitespace preserved.
249
+ # `preserve`d blocks of text aren't indented,
250
+ # and newlines are replaced with the HTML escape code for newlines,
251
+ # to preserve nice-looking output.
252
+ #
253
+ # @see Haml::Helpers#preserve
205
254
  module Preserve
206
255
  include Base
207
256
 
257
+ # @see Base#render
208
258
  def render(text)
209
259
  Haml::Helpers.preserve text
210
260
  end
211
261
  end
212
262
 
263
+ # Parses the filtered text with {Sass} to produce CSS output.
213
264
  module Sass
214
265
  include Base
215
266
  lazy_require 'sass/plugin'
216
267
 
268
+ # @see Base#render
217
269
  def render(text)
218
270
  ::Sass::Engine.new(text, ::Sass::Plugin.engine_options).render
219
271
  end
220
272
  end
221
273
 
274
+ # Parses the filtered text with ERB, like an RHTML template.
275
+ # Not available if the {file:HAML_REFERENCE.md#suppress_eval-option `:suppress_eval`} option is set to true.
276
+ # Embedded Ruby code is evaluated in the same context as the Haml template.
222
277
  module ERB
223
278
  include Base
224
279
  lazy_require 'erb'
225
280
 
281
+ # @see Base#compile
226
282
  def compile(precompiler, text)
227
283
  return if precompiler.options[:suppress_eval]
228
284
  src = ::ERB.new(text).src.sub(/^#coding:.*?\n/, '').
@@ -231,10 +287,13 @@ END
231
287
  end
232
288
  end
233
289
 
290
+ # Parses the filtered text with [Textile](http://www.textism.com/tools/textile).
291
+ # Only works if [RedCloth](http://redcloth.org) is installed.
234
292
  module Textile
235
293
  include Base
236
294
  lazy_require 'redcloth'
237
295
 
296
+ # @see Base#render
238
297
  def render(text)
239
298
  ::RedCloth.new(text).to_html(:textile)
240
299
  end
@@ -242,11 +301,16 @@ END
242
301
  RedCloth = Textile
243
302
  Filters.defined['redcloth'] = RedCloth
244
303
 
245
- # Uses BlueCloth or RedCloth to provide only Markdown (not Textile) parsing
304
+ # Parses the filtered text with [Markdown](http://daringfireball.net/projects/markdown).
305
+ # Only works if [RDiscount](http://github.com/rtomayko/rdiscount),
306
+ # [RPeg-Markdown](http://github.com/rtomayko/rpeg-markdown),
307
+ # [Maruku](http://maruku.rubyforge.org),
308
+ # or [BlueCloth](www.deveiate.org/projects/BlueCloth) are installed.
246
309
  module Markdown
247
310
  include Base
248
311
  lazy_require 'rdiscount', 'peg_markdown', 'maruku', 'bluecloth'
249
312
 
313
+ # @see Base#render
250
314
  def render(text)
251
315
  engine = case @required
252
316
  when 'rdiscount'
@@ -262,15 +326,16 @@ END
262
326
  end
263
327
  end
264
328
 
329
+ # Parses the filtered text with [Maruku](http://maruku.rubyforge.org),
330
+ # which has some non-standard extensions to Markdown.
265
331
  module Maruku
266
332
  include Base
267
333
  lazy_require 'maruku'
268
334
 
335
+ # @see Base#render
269
336
  def render(text)
270
337
  ::Maruku.new(text).to_html
271
338
  end
272
339
  end
273
340
  end
274
341
  end
275
-
276
- # :startdoc: