gohanlonllc-haml 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (173) hide show
  1. data/README.rdoc +332 -0
  2. data/Rakefile +250 -0
  3. data/VERSION.yml +4 -0
  4. data/bin/css2sass +7 -0
  5. data/bin/haml +9 -0
  6. data/bin/html2haml +7 -0
  7. data/bin/sass +8 -0
  8. data/lib/haml/buffer.rb +255 -0
  9. data/lib/haml/engine.rb +268 -0
  10. data/lib/haml/error.rb +22 -0
  11. data/lib/haml/exec.rb +403 -0
  12. data/lib/haml/filters.rb +275 -0
  13. data/lib/haml/helpers/action_view_extensions.rb +45 -0
  14. data/lib/haml/helpers/action_view_mods.rb +181 -0
  15. data/lib/haml/helpers.rb +488 -0
  16. data/lib/haml/html.rb +219 -0
  17. data/lib/haml/precompiler.rb +904 -0
  18. data/lib/haml/shared.rb +47 -0
  19. data/lib/haml/template/patch.rb +58 -0
  20. data/lib/haml/template/plugin.rb +72 -0
  21. data/lib/haml/template.rb +42 -0
  22. data/lib/haml/util.rb +93 -0
  23. data/lib/haml/version.rb +46 -0
  24. data/lib/haml.rb +1044 -0
  25. data/lib/sass/css.rb +337 -0
  26. data/lib/sass/engine.rb +439 -0
  27. data/lib/sass/environment.rb +48 -0
  28. data/lib/sass/error.rb +42 -0
  29. data/lib/sass/files.rb +100 -0
  30. data/lib/sass/plugin/merb.rb +57 -0
  31. data/lib/sass/plugin/rails.rb +25 -0
  32. data/lib/sass/plugin.rb +203 -0
  33. data/lib/sass/repl.rb +51 -0
  34. data/lib/sass/script/bool.rb +13 -0
  35. data/lib/sass/script/color.rb +97 -0
  36. data/lib/sass/script/funcall.rb +29 -0
  37. data/lib/sass/script/functions.rb +134 -0
  38. data/lib/sass/script/lexer.rb +148 -0
  39. data/lib/sass/script/literal.rb +82 -0
  40. data/lib/sass/script/number.rb +231 -0
  41. data/lib/sass/script/operation.rb +30 -0
  42. data/lib/sass/script/parser.rb +142 -0
  43. data/lib/sass/script/string.rb +9 -0
  44. data/lib/sass/script/unary_operation.rb +21 -0
  45. data/lib/sass/script/variable.rb +20 -0
  46. data/lib/sass/script.rb +38 -0
  47. data/lib/sass/tree/attr_node.rb +72 -0
  48. data/lib/sass/tree/comment_node.rb +39 -0
  49. data/lib/sass/tree/debug_node.rb +22 -0
  50. data/lib/sass/tree/directive_node.rb +51 -0
  51. data/lib/sass/tree/file_node.rb +27 -0
  52. data/lib/sass/tree/for_node.rb +29 -0
  53. data/lib/sass/tree/if_node.rb +32 -0
  54. data/lib/sass/tree/mixin_def_node.rb +18 -0
  55. data/lib/sass/tree/mixin_node.rb +35 -0
  56. data/lib/sass/tree/node.rb +117 -0
  57. data/lib/sass/tree/rule_node.rb +161 -0
  58. data/lib/sass/tree/variable_node.rb +24 -0
  59. data/lib/sass/tree/while_node.rb +21 -0
  60. data/lib/sass.rb +1070 -0
  61. data/rails/init.rb +1 -0
  62. data/test/benchmark.rb +99 -0
  63. data/test/haml/engine_test.rb +795 -0
  64. data/test/haml/helper_test.rb +233 -0
  65. data/test/haml/html2haml_test.rb +108 -0
  66. data/test/haml/markaby/standard.mab +52 -0
  67. data/test/haml/mocks/article.rb +6 -0
  68. data/test/haml/results/content_for_layout.xhtml +15 -0
  69. data/test/haml/results/eval_suppressed.xhtml +9 -0
  70. data/test/haml/results/filters.xhtml +62 -0
  71. data/test/haml/results/helpers.xhtml +93 -0
  72. data/test/haml/results/helpful.xhtml +10 -0
  73. data/test/haml/results/just_stuff.xhtml +68 -0
  74. data/test/haml/results/list.xhtml +12 -0
  75. data/test/haml/results/nuke_inner_whitespace.xhtml +40 -0
  76. data/test/haml/results/nuke_outer_whitespace.xhtml +148 -0
  77. data/test/haml/results/original_engine.xhtml +20 -0
  78. data/test/haml/results/partial_layout.xhtml +5 -0
  79. data/test/haml/results/partials.xhtml +21 -0
  80. data/test/haml/results/render_layout.xhtml +3 -0
  81. data/test/haml/results/silent_script.xhtml +74 -0
  82. data/test/haml/results/standard.xhtml +162 -0
  83. data/test/haml/results/tag_parsing.xhtml +23 -0
  84. data/test/haml/results/very_basic.xhtml +5 -0
  85. data/test/haml/results/whitespace_handling.xhtml +89 -0
  86. data/test/haml/rhtml/_av_partial_1.rhtml +12 -0
  87. data/test/haml/rhtml/_av_partial_2.rhtml +8 -0
  88. data/test/haml/rhtml/action_view.rhtml +62 -0
  89. data/test/haml/rhtml/standard.rhtml +54 -0
  90. data/test/haml/template_test.rb +204 -0
  91. data/test/haml/templates/_av_partial_1.haml +9 -0
  92. data/test/haml/templates/_av_partial_1_ugly.haml +9 -0
  93. data/test/haml/templates/_av_partial_2.haml +5 -0
  94. data/test/haml/templates/_av_partial_2_ugly.haml +5 -0
  95. data/test/haml/templates/_layout.erb +3 -0
  96. data/test/haml/templates/_layout_for_partial.haml +3 -0
  97. data/test/haml/templates/_partial.haml +8 -0
  98. data/test/haml/templates/_text_area.haml +3 -0
  99. data/test/haml/templates/action_view.haml +47 -0
  100. data/test/haml/templates/action_view_ugly.haml +47 -0
  101. data/test/haml/templates/breakage.haml +8 -0
  102. data/test/haml/templates/content_for_layout.haml +10 -0
  103. data/test/haml/templates/eval_suppressed.haml +11 -0
  104. data/test/haml/templates/filters.haml +66 -0
  105. data/test/haml/templates/helpers.haml +95 -0
  106. data/test/haml/templates/helpful.haml +11 -0
  107. data/test/haml/templates/just_stuff.haml +83 -0
  108. data/test/haml/templates/list.haml +12 -0
  109. data/test/haml/templates/nuke_inner_whitespace.haml +32 -0
  110. data/test/haml/templates/nuke_outer_whitespace.haml +144 -0
  111. data/test/haml/templates/original_engine.haml +17 -0
  112. data/test/haml/templates/partial_layout.haml +3 -0
  113. data/test/haml/templates/partialize.haml +1 -0
  114. data/test/haml/templates/partials.haml +12 -0
  115. data/test/haml/templates/render_layout.haml +2 -0
  116. data/test/haml/templates/silent_script.haml +40 -0
  117. data/test/haml/templates/standard.haml +42 -0
  118. data/test/haml/templates/standard_ugly.haml +42 -0
  119. data/test/haml/templates/tag_parsing.haml +21 -0
  120. data/test/haml/templates/very_basic.haml +4 -0
  121. data/test/haml/templates/whitespace_handling.haml +87 -0
  122. data/test/haml/util_test.rb +92 -0
  123. data/test/linked_rails.rb +12 -0
  124. data/test/sass/css2sass_test.rb +215 -0
  125. data/test/sass/engine_test.rb +773 -0
  126. data/test/sass/functions_test.rb +109 -0
  127. data/test/sass/more_results/more1.css +9 -0
  128. data/test/sass/more_results/more1_with_line_comments.css +26 -0
  129. data/test/sass/more_results/more_import.css +29 -0
  130. data/test/sass/more_templates/_more_partial.sass +2 -0
  131. data/test/sass/more_templates/more1.sass +23 -0
  132. data/test/sass/more_templates/more_import.sass +11 -0
  133. data/test/sass/plugin_test.rb +214 -0
  134. data/test/sass/results/alt.css +4 -0
  135. data/test/sass/results/basic.css +9 -0
  136. data/test/sass/results/compact.css +5 -0
  137. data/test/sass/results/complex.css +87 -0
  138. data/test/sass/results/compressed.css +1 -0
  139. data/test/sass/results/expanded.css +19 -0
  140. data/test/sass/results/import.css +29 -0
  141. data/test/sass/results/line_numbers.css +49 -0
  142. data/test/sass/results/mixins.css +95 -0
  143. data/test/sass/results/multiline.css +24 -0
  144. data/test/sass/results/nested.css +22 -0
  145. data/test/sass/results/parent_ref.css +13 -0
  146. data/test/sass/results/script.css +16 -0
  147. data/test/sass/results/subdir/nested_subdir/nested_subdir.css +1 -0
  148. data/test/sass/results/subdir/subdir.css +3 -0
  149. data/test/sass/results/units.css +11 -0
  150. data/test/sass/script_test.rb +250 -0
  151. data/test/sass/templates/_partial.sass +2 -0
  152. data/test/sass/templates/alt.sass +16 -0
  153. data/test/sass/templates/basic.sass +23 -0
  154. data/test/sass/templates/bork.sass +2 -0
  155. data/test/sass/templates/bork2.sass +2 -0
  156. data/test/sass/templates/compact.sass +17 -0
  157. data/test/sass/templates/complex.sass +309 -0
  158. data/test/sass/templates/compressed.sass +15 -0
  159. data/test/sass/templates/expanded.sass +17 -0
  160. data/test/sass/templates/import.sass +11 -0
  161. data/test/sass/templates/importee.sass +19 -0
  162. data/test/sass/templates/line_numbers.sass +13 -0
  163. data/test/sass/templates/mixins.sass +76 -0
  164. data/test/sass/templates/multiline.sass +20 -0
  165. data/test/sass/templates/nested.sass +25 -0
  166. data/test/sass/templates/parent_ref.sass +25 -0
  167. data/test/sass/templates/script.sass +101 -0
  168. data/test/sass/templates/subdir/nested_subdir/_nested_partial.sass +2 -0
  169. data/test/sass/templates/subdir/nested_subdir/nested_subdir.sass +3 -0
  170. data/test/sass/templates/subdir/subdir.sass +6 -0
  171. data/test/sass/templates/units.sass +11 -0
  172. data/test/test_helper.rb +27 -0
  173. metadata +241 -0
data/lib/haml/exec.rb ADDED
@@ -0,0 +1,403 @@
1
+ require 'optparse'
2
+ require 'fileutils'
3
+
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:
13
+ def initialize(args)
14
+ @args = args
15
+ @options = {}
16
+ end
17
+
18
+ def parse!
19
+ begin
20
+ @opts = OptionParser.new(&method(:set_opts))
21
+ @opts.parse!(@args)
22
+
23
+ process_result
24
+
25
+ @options
26
+ rescue Exception => e
27
+ raise e if @options[:trace] || e.is_a?(SystemExit)
28
+
29
+ $stderr.puts e.message
30
+ exit 1
31
+ end
32
+ exit 0
33
+ end
34
+
35
+ def to_s
36
+ @opts.to_s
37
+ end
38
+
39
+ protected
40
+
41
+ def get_line(exception)
42
+ # SyntaxErrors have weird line reporting
43
+ # when there's trailing whitespace,
44
+ # which there is for Haml documents.
45
+ return exception.message.scan(/:(\d+)/).first.first if exception.is_a?(::SyntaxError)
46
+ exception.backtrace[0].scan(/:(\d+)/).first.first
47
+ end
48
+
49
+ private
50
+
51
+ def set_opts(opts)
52
+ opts.on('-s', '--stdin', :NONE, 'Read input from standard input instead of an input file') do
53
+ @options[:input] = $stdin
54
+ end
55
+
56
+ opts.on('--trace', :NONE, 'Show a full traceback on error') do
57
+ @options[:trace] = true
58
+ end
59
+
60
+ opts.on_tail("-?", "-h", "--help", "Show this message") do
61
+ puts opts
62
+ exit
63
+ end
64
+
65
+ opts.on_tail("-v", "--version", "Print version") do
66
+ puts("Haml #{::Haml.version[:string]}")
67
+ exit
68
+ end
69
+ end
70
+
71
+ def process_result
72
+ input, output = @options[:input], @options[:output]
73
+ input_file, output_file = if input
74
+ [nil, open_file(ARGV[0], 'w')]
75
+ else
76
+ @options[:filename] = ARGV[0]
77
+ [open_file(ARGV[0]), open_file(ARGV[1], 'w')]
78
+ end
79
+
80
+ input ||= input_file
81
+ output ||= output_file
82
+ input ||= $stdin
83
+ output ||= $stdout
84
+
85
+ @options[:input], @options[:output] = input, output
86
+ end
87
+
88
+ def open_file(filename, flag = 'r')
89
+ return if filename.nil?
90
+ File.open(filename, flag)
91
+ end
92
+ end
93
+
94
+ # A class encapsulating the executable functionality
95
+ # specific to Haml and Sass.
96
+ class HamlSass < Generic # :nodoc:
97
+ def initialize(args)
98
+ super
99
+ @options[:for_engine] = {}
100
+ end
101
+
102
+ private
103
+
104
+ def set_opts(opts)
105
+ opts.banner = <<END
106
+ Usage: #{@name.downcase} [options] [INPUT] [OUTPUT]
107
+
108
+ Description:
109
+ Uses the #{@name} engine to parse the specified template
110
+ and outputs the result to the specified file.
111
+
112
+ Options:
113
+ END
114
+
115
+ opts.on('--rails RAILS_DIR', "Install Haml and Sass from the Gem to a Rails project") do |dir|
116
+ original_dir = dir
117
+
118
+ dir = File.join(dir, 'vendor', 'plugins')
119
+
120
+ unless File.exists?(dir)
121
+ puts "Directory #{dir} doesn't exist"
122
+ exit
123
+ end
124
+
125
+ dir = File.join(dir, 'haml')
126
+
127
+ if File.exists?(dir)
128
+ print "Directory #{dir} already exists, overwrite [y/N]? "
129
+ exit if gets !~ /y/i
130
+ FileUtils.rm_rf(dir)
131
+ end
132
+
133
+ begin
134
+ Dir.mkdir(dir)
135
+ rescue SystemCallError
136
+ puts "Cannot create #{dir}"
137
+ exit
138
+ end
139
+
140
+ File.open(File.join(dir, 'init.rb'), 'w') do |file|
141
+ file.puts "require 'rubygems'"
142
+ file << File.read(File.dirname(__FILE__) + "/../../init.rb")
143
+ end
144
+
145
+ puts "Haml plugin added to #{original_dir}"
146
+ exit
147
+ end
148
+
149
+ opts.on('-c', '--check', "Just check syntax, don't evaluate.") do
150
+ require 'stringio'
151
+ @options[:check_syntax] = true
152
+ @options[:output] = StringIO.new
153
+ end
154
+
155
+ super
156
+ end
157
+
158
+ def process_result
159
+ super
160
+ @options[:for_engine][:filename] = @options[:filename] if @options[:filename]
161
+ require File.dirname(__FILE__) + "/../#{@name.downcase}"
162
+ end
163
+ end
164
+
165
+ # A class encapsulating executable functionality
166
+ # specific to Sass.
167
+ class Sass < HamlSass # :nodoc:
168
+ def initialize(args)
169
+ super
170
+ @name = "Sass"
171
+ @options[:for_engine][:load_paths] = ['.'] + (ENV['SASSPATH'] || '').split(File::PATH_SEPARATOR)
172
+ end
173
+
174
+ def set_opts(opts)
175
+ super
176
+
177
+ opts.on('-t', '--style NAME',
178
+ 'Output style. Can be nested (default), compact, compressed, or expanded.') do |name|
179
+ @options[:for_engine][:style] = name.to_sym
180
+ end
181
+ opts.on('-l', '--line-comments',
182
+ 'Line Comments. Emit comments in the generated CSS indicating the corresponding sass line.') do
183
+ @options[:for_engine][:line_comments] = true
184
+ end
185
+ opts.on('-i', '--interactive',
186
+ 'Run an interactive SassScript shell.') do
187
+ @options[:interactive] = true
188
+ end
189
+ opts.on('-I', '--load-path PATH', 'Add a sass import path.') do |path|
190
+ @options[:for_engine][:load_paths] << path
191
+ end
192
+ opts.on('--cache-location', 'The path to put cached Sass files. Defaults to .sass-cache.') do |loc|
193
+ @options[:for_engine][:cache_location] = path
194
+ end
195
+ opts.on('-C', '--no-cache', "Don't cache to sassc files.") do
196
+ @options[:for_engine][:cache] = false
197
+ end
198
+ end
199
+
200
+ def process_result
201
+ if @options[:interactive]
202
+ require 'sass'
203
+ require 'sass/repl'
204
+ ::Sass::Repl.new(@options).run
205
+ return
206
+ end
207
+
208
+ super
209
+ input = @options[:input]
210
+ output = @options[:output]
211
+
212
+ tree =
213
+ if input.is_a?(File) && !@options[:check_syntax]
214
+ ::Sass::Files.tree_for(input.path, @options[:for_engine])
215
+ else
216
+ # We don't need to do any special handling of @options[:check_syntax] here,
217
+ # because the Sass syntax checking happens alongside evaluation
218
+ # and evaluation doesn't actually evaluate any code anyway.
219
+ ::Sass::Engine.new(input.read(), @options[:for_engine]).to_tree
220
+ end
221
+
222
+ input.close() if input.is_a?(File)
223
+
224
+ output.write(tree.render)
225
+ output.close() if output.is_a? File
226
+ rescue ::Sass::SyntaxError => e
227
+ raise e if @options[:trace]
228
+ raise "Syntax error on line #{get_line e}: #{e.message}"
229
+ end
230
+ end
231
+
232
+ # A class encapsulating executable functionality
233
+ # specific to Haml.
234
+ class Haml < HamlSass # :nodoc:
235
+ def initialize(args)
236
+ super
237
+ @name = "Haml"
238
+ @options[:requires] = []
239
+ @options[:load_paths] = []
240
+ end
241
+
242
+ def set_opts(opts)
243
+ super
244
+
245
+ opts.on('-t', '--style NAME',
246
+ 'Output style. Can be indented (default) or ugly.') do |name|
247
+ @options[:for_engine][:ugly] = true if name.to_sym == :ugly
248
+ end
249
+
250
+ opts.on('-f', '--format NAME',
251
+ 'Output format. Can be xhtml (default), html4, or html5.') do |name|
252
+ @options[:for_engine][:format] = name.to_sym
253
+ end
254
+
255
+ opts.on('-e', '--escape-html',
256
+ 'Escape HTML characters (like ampersands and angle brackets) by default.') do
257
+ @options[:for_engine][:escape_html] = true
258
+ end
259
+
260
+ opts.on('-r', '--require FILE', "Same as 'ruby -r'.") do |file|
261
+ @options[:requires] << file
262
+ end
263
+
264
+ opts.on('-I', '--load-path PATH', "Same as 'ruby -I'.") do |path|
265
+ @options[:load_paths] << path
266
+ end
267
+
268
+ opts.on('--debug', "Print out the precompiled Ruby source.") do
269
+ @options[:debug] = true
270
+ end
271
+ end
272
+
273
+ def process_result
274
+ super
275
+ input = @options[:input]
276
+ output = @options[:output]
277
+
278
+ template = input.read()
279
+ input.close() if input.is_a? File
280
+
281
+ begin
282
+ engine = ::Haml::Engine.new(template, @options[:for_engine])
283
+ if @options[:check_syntax]
284
+ puts "Syntax OK"
285
+ return
286
+ end
287
+
288
+ @options[:load_paths].each {|p| $LOAD_PATH << p}
289
+ @options[:requires].each {|f| require f}
290
+
291
+ if @options[:debug]
292
+ puts engine.precompiled
293
+ puts '=' * 100
294
+ end
295
+
296
+ result = engine.to_html
297
+ rescue Exception => e
298
+ raise e if @options[:trace]
299
+
300
+ case e
301
+ when ::Haml::SyntaxError; raise "Syntax error on line #{get_line e}: #{e.message}"
302
+ when ::Haml::Error; raise "Haml error on line #{get_line e}: #{e.message}"
303
+ else raise "Exception on line #{get_line e}: #{e.message}\n Use --trace for backtrace."
304
+ end
305
+ end
306
+
307
+ output.write(result)
308
+ output.close() if output.is_a? File
309
+ end
310
+ end
311
+
312
+ # A class encapsulating executable functionality
313
+ # specific to the html2haml executable.
314
+ class HTML2Haml < Generic # :nodoc:
315
+ def initialize(args)
316
+ super
317
+
318
+ @module_opts = {}
319
+
320
+ begin
321
+ require 'haml/html'
322
+ rescue LoadError => err
323
+ dep = err.message.scan(/^no such file to load -- (.*)/)[0]
324
+ puts "Required dependency #{dep} not found!"
325
+ exit 1
326
+ end
327
+ end
328
+
329
+ def set_opts(opts)
330
+ opts.banner = <<END
331
+ Usage: html2haml [options] [INPUT] [OUTPUT]
332
+
333
+ Description: Transforms an HTML file into corresponding Haml code.
334
+
335
+ Options:
336
+ END
337
+
338
+ opts.on('-r', '--rhtml', 'Parse RHTML tags.') do
339
+ @module_opts[:rhtml] = true
340
+ end
341
+
342
+ opts.on('--no-rhtml', "Don't parse RHTML tags.") do
343
+ @options[:no_rhtml] = true
344
+ end
345
+
346
+ opts.on('-x', '--xhtml', 'Parse the input using the more strict XHTML parser.') do
347
+ @module_opts[:xhtml] = true
348
+ end
349
+
350
+ super
351
+ end
352
+
353
+ def process_result
354
+ super
355
+
356
+ input = @options[:input]
357
+ output = @options[:output]
358
+
359
+ @module_opts[:rhtml] ||= input.respond_to?(:path) && input.path =~ /\.(rhtml|erb)$/
360
+ @module_opts[:rhtml] &&= @options[:no_rhtml] != false
361
+
362
+ output.write(::Haml::HTML.new(input, @module_opts).render)
363
+ end
364
+ end
365
+
366
+ # A class encapsulating executable functionality
367
+ # specific to the css2sass executable.
368
+ class CSS2Sass < Generic # :nodoc:
369
+ def initialize(args)
370
+ super
371
+
372
+ @module_opts = {}
373
+
374
+ require 'sass/css'
375
+ end
376
+
377
+ def set_opts(opts)
378
+ opts.banner = <<END
379
+ Usage: css2sass [options] [INPUT] [OUTPUT]
380
+
381
+ Description: Transforms a CSS file into corresponding Sass code.
382
+
383
+ Options:
384
+ END
385
+
386
+ opts.on('-a', '--alternate', 'Output using alternative Sass syntax (margin: 1px)') do
387
+ @module_opts[:alternate] = true
388
+ end
389
+
390
+ super
391
+ end
392
+
393
+ def process_result
394
+ super
395
+
396
+ input = @options[:input]
397
+ output = @options[:output]
398
+
399
+ output.write(::Sass::CSS.new(input, @module_opts).render)
400
+ end
401
+ end
402
+ end
403
+ end
@@ -0,0 +1,275 @@
1
+ module Haml
2
+ # The module containing the default filters,
3
+ # as well as the base module,
4
+ # Haml::Filters::Base.
5
+ module Filters
6
+ # Returns a hash of defined filters.
7
+ def self.defined
8
+ @defined ||= {}
9
+ end
10
+
11
+ # The base module for Haml filters.
12
+ # User-defined filters should be modules including this module.
13
+ #
14
+ # A user-defined filter should override either Base#render or Base #compile.
15
+ # Base#render is the most common.
16
+ # It takes a string, the filter source,
17
+ # and returns another string,
18
+ # the result of the filter.
19
+ # For example:
20
+ #
21
+ # module Haml::Filters::Sass
22
+ # include Haml::Filters::Base
23
+ #
24
+ # def render(text)
25
+ # ::Sass::Engine.new(text).render
26
+ # end
27
+ # end
28
+ #
29
+ # For details on overriding #compile, see its documentation.
30
+ #
31
+ module Base
32
+ def self.included(base) # :nodoc:
33
+ Filters.defined[base.name.split("::").last.downcase] = base
34
+ base.extend(base)
35
+ end
36
+
37
+ # Takes a string, the source text that should be passed to the filter,
38
+ # and returns the string resulting from running the filter on <tt>text</tt>.
39
+ #
40
+ # This should be overridden in most individual filter modules
41
+ # to render text with the given filter.
42
+ # If compile is overridden, however, render doesn't need to be.
43
+ def render(text)
44
+ raise Error.new("#{self.inspect}#render not defined!")
45
+ end
46
+
47
+ # Same as render, but takes the Haml options hash as well.
48
+ # It's only safe to rely on options made available in Haml::Engine#options_for_buffer.
49
+ def render_with_options(text, options)
50
+ render(text)
51
+ end
52
+
53
+ def internal_compile(*args) # :nodoc:
54
+ resolve_lazy_requires
55
+ compile(*args)
56
+ end
57
+
58
+ # compile should be overridden when a filter needs to have access
59
+ # to the Haml evaluation context.
60
+ # Rather than applying a filter to a string at compile-time,
61
+ # compile uses the Haml::Precompiler instance to compile the string to Ruby code
62
+ # that will be executed in the context of the active Haml template.
63
+ #
64
+ # Warning: the Haml::Precompiler interface is neither well-documented
65
+ # nor guaranteed to be stable.
66
+ # If you want to make use of it,
67
+ # you'll probably need to look at the source code
68
+ # and should test your filter when upgrading to new Haml versions.
69
+ def compile(precompiler, text)
70
+ resolve_lazy_requires
71
+ filter = self
72
+ precompiler.instance_eval do
73
+ if contains_interpolation?(text)
74
+ return if options[:suppress_eval]
75
+
76
+ push_script <<RUBY
77
+ find_and_preserve(#{filter.inspect}.render_with_options(#{unescape_interpolation(text)}, _hamlout.options))
78
+ RUBY
79
+ return
80
+ end
81
+
82
+ rendered = Haml::Helpers::find_and_preserve(filter.render_with_options(text, precompiler.options), precompiler.options[:preserve])
83
+
84
+ if !options[:ugly]
85
+ push_text(rendered.rstrip.gsub("\n", "\n#{' ' * @output_tabs}"))
86
+ else
87
+ push_text(rendered.rstrip)
88
+ end
89
+ end
90
+ end
91
+
92
+ # This becomes a class method of modules that include Base.
93
+ # It allows the module to specify one or more Ruby files
94
+ # that Haml should try to require when compiling the filter.
95
+ #
96
+ # The first file specified is tried first,
97
+ # then the second, etc.
98
+ # If none are found, the compilation throws an exception.
99
+ #
100
+ # For example:
101
+ #
102
+ # module Haml::Filters::Markdown
103
+ # lazy_require 'rdiscount', 'peg_markdown', 'maruku', 'bluecloth'
104
+ #
105
+ # ...
106
+ # end
107
+ #
108
+ def lazy_require(*reqs)
109
+ @lazy_requires = reqs
110
+ end
111
+
112
+ private
113
+
114
+ def resolve_lazy_requires
115
+ return unless @lazy_requires
116
+
117
+ @lazy_requires[0...-1].each do |req|
118
+ begin
119
+ @required = req
120
+ require @required
121
+ return
122
+ rescue LoadError; end # RCov doesn't see this, but it is run
123
+ end
124
+
125
+ begin
126
+ @required = @lazy_requires[-1]
127
+ require @required
128
+ rescue LoadError => e
129
+ classname = self.name.match(/\w+$/)[0]
130
+
131
+ if @lazy_requires.size == 1
132
+ raise Error.new("Can't run #{classname} filter; required file '#{@lazy_requires.first}' not found")
133
+ else
134
+ raise Error.new("Can't run #{classname} filter; required #{@lazy_requires.map { |r| "'#{r}'" }.join(' or ')}, but none were found")
135
+ end
136
+ end
137
+ end
138
+ end
139
+ end
140
+ end
141
+
142
+ # :stopdoc:
143
+
144
+ begin
145
+ require 'rubygems'
146
+ rescue LoadError; end
147
+
148
+ module Haml
149
+ module Filters
150
+ module Plain
151
+ include Base
152
+
153
+ def render(text); text; end
154
+ end
155
+
156
+ module Javascript
157
+ include Base
158
+
159
+ def render_with_options(text, options)
160
+ <<END
161
+ <script type=#{options[:attr_wrapper]}text/javascript#{options[:attr_wrapper]}>
162
+ //<![CDATA[
163
+ #{text.rstrip.gsub("\n", "\n ")}
164
+ //]]>
165
+ </script>
166
+ END
167
+ end
168
+ end
169
+
170
+ module Cdata
171
+ include Base
172
+
173
+ def render(text)
174
+ "<![CDATA[#{("\n" + text).rstrip.gsub("\n", "\n ")}\n]]>"
175
+ end
176
+ end
177
+
178
+ module Escaped
179
+ include Base
180
+
181
+ def render(text)
182
+ Haml::Helpers.html_escape text
183
+ end
184
+ end
185
+
186
+ module Ruby
187
+ include Base
188
+ lazy_require 'stringio'
189
+
190
+ def compile(precompiler, text)
191
+ return if precompiler.options[:suppress_eval]
192
+ precompiler.instance_eval do
193
+ push_silent <<-FIRST.gsub("\n", ';') + text + <<-LAST.gsub("\n", ';')
194
+ _haml_old_stdout = $stdout
195
+ $stdout = StringIO.new(_hamlout.buffer, 'a')
196
+ FIRST
197
+ _haml_old_stdout, $stdout = $stdout, _haml_old_stdout
198
+ _haml_old_stdout.close
199
+ LAST
200
+ end
201
+ end
202
+ end
203
+
204
+ module Preserve
205
+ include Base
206
+
207
+ def render(text)
208
+ Haml::Helpers.preserve text
209
+ end
210
+ end
211
+
212
+ module Sass
213
+ include Base
214
+ lazy_require 'sass/plugin'
215
+
216
+ def render(text)
217
+ ::Sass::Engine.new(text, ::Sass::Plugin.engine_options).render
218
+ end
219
+ end
220
+
221
+ module ERB
222
+ include Base
223
+ lazy_require 'erb'
224
+
225
+ def compile(precompiler, text)
226
+ return if precompiler.options[:suppress_eval]
227
+ src = ::ERB.new(text).src.sub(/^#coding:.*?\n/, '').
228
+ sub(/^_erbout = '';/, "").gsub("\n", ';')
229
+ precompiler.send(:push_silent, src)
230
+ end
231
+ end
232
+
233
+ module Textile
234
+ include Base
235
+ lazy_require 'redcloth'
236
+
237
+ def render(text)
238
+ ::RedCloth.new(text).to_html(:textile)
239
+ end
240
+ end
241
+ RedCloth = Textile
242
+ Filters.defined['redcloth'] = RedCloth
243
+
244
+ # Uses BlueCloth or RedCloth to provide only Markdown (not Textile) parsing
245
+ module Markdown
246
+ include Base
247
+ lazy_require 'rdiscount', 'peg_markdown', 'maruku', 'bluecloth'
248
+
249
+ def render(text)
250
+ engine = case @required
251
+ when 'rdiscount'
252
+ ::RDiscount
253
+ when 'peg_markdown'
254
+ ::PEGMarkdown
255
+ when 'maruku'
256
+ ::Maruku
257
+ when 'bluecloth'
258
+ ::BlueCloth
259
+ end
260
+ engine.new(text).to_html
261
+ end
262
+ end
263
+
264
+ module Maruku
265
+ include Base
266
+ lazy_require 'maruku'
267
+
268
+ def render(text)
269
+ ::Maruku.new(text).to_html
270
+ end
271
+ end
272
+ end
273
+ end
274
+
275
+ # :startdoc:
@@ -0,0 +1,45 @@
1
+ require 'haml/helpers/action_view_mods'
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:
42
+ end
43
+ end
44
+ end
45
+ end