jwhitmire-haml 2.1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (168) hide show
  1. data/README.rdoc +332 -0
  2. data/bin/css2sass +7 -0
  3. data/bin/haml +9 -0
  4. data/bin/html2haml +7 -0
  5. data/bin/sass +8 -0
  6. data/lib/haml/buffer.rb +255 -0
  7. data/lib/haml/engine.rb +268 -0
  8. data/lib/haml/error.rb +22 -0
  9. data/lib/haml/exec.rb +395 -0
  10. data/lib/haml/filters.rb +276 -0
  11. data/lib/haml/helpers/action_view_extensions.rb +45 -0
  12. data/lib/haml/helpers/action_view_mods.rb +181 -0
  13. data/lib/haml/helpers.rb +468 -0
  14. data/lib/haml/html.rb +218 -0
  15. data/lib/haml/precompiler.rb +889 -0
  16. data/lib/haml/shared.rb +45 -0
  17. data/lib/haml/template/patch.rb +58 -0
  18. data/lib/haml/template/plugin.rb +72 -0
  19. data/lib/haml/template.rb +51 -0
  20. data/lib/haml/util.rb +77 -0
  21. data/lib/haml/version.rb +47 -0
  22. data/lib/haml.rb +1042 -0
  23. data/lib/sass/css.rb +388 -0
  24. data/lib/sass/engine.rb +499 -0
  25. data/lib/sass/environment.rb +33 -0
  26. data/lib/sass/error.rb +35 -0
  27. data/lib/sass/plugin/merb.rb +56 -0
  28. data/lib/sass/plugin/rails.rb +24 -0
  29. data/lib/sass/plugin.rb +203 -0
  30. data/lib/sass/repl.rb +51 -0
  31. data/lib/sass/script/bool.rb +13 -0
  32. data/lib/sass/script/color.rb +97 -0
  33. data/lib/sass/script/funcall.rb +28 -0
  34. data/lib/sass/script/functions.rb +122 -0
  35. data/lib/sass/script/lexer.rb +152 -0
  36. data/lib/sass/script/literal.rb +60 -0
  37. data/lib/sass/script/number.rb +231 -0
  38. data/lib/sass/script/operation.rb +30 -0
  39. data/lib/sass/script/parser.rb +142 -0
  40. data/lib/sass/script/string.rb +42 -0
  41. data/lib/sass/script/unary_operation.rb +21 -0
  42. data/lib/sass/script/variable.rb +20 -0
  43. data/lib/sass/script.rb +38 -0
  44. data/lib/sass/tree/attr_node.rb +64 -0
  45. data/lib/sass/tree/comment_node.rb +34 -0
  46. data/lib/sass/tree/debug_node.rb +22 -0
  47. data/lib/sass/tree/directive_node.rb +50 -0
  48. data/lib/sass/tree/file_node.rb +27 -0
  49. data/lib/sass/tree/for_node.rb +29 -0
  50. data/lib/sass/tree/if_node.rb +27 -0
  51. data/lib/sass/tree/mixin_def_node.rb +18 -0
  52. data/lib/sass/tree/mixin_node.rb +34 -0
  53. data/lib/sass/tree/node.rb +99 -0
  54. data/lib/sass/tree/rule_node.rb +120 -0
  55. data/lib/sass/tree/variable_node.rb +24 -0
  56. data/lib/sass/tree/while_node.rb +20 -0
  57. data/lib/sass.rb +1062 -0
  58. data/test/benchmark.rb +99 -0
  59. data/test/haml/engine_test.rb +734 -0
  60. data/test/haml/helper_test.rb +224 -0
  61. data/test/haml/html2haml_test.rb +92 -0
  62. data/test/haml/markaby/standard.mab +52 -0
  63. data/test/haml/mocks/article.rb +6 -0
  64. data/test/haml/results/content_for_layout.xhtml +15 -0
  65. data/test/haml/results/eval_suppressed.xhtml +9 -0
  66. data/test/haml/results/filters.xhtml +62 -0
  67. data/test/haml/results/helpers.xhtml +93 -0
  68. data/test/haml/results/helpful.xhtml +10 -0
  69. data/test/haml/results/just_stuff.xhtml +68 -0
  70. data/test/haml/results/list.xhtml +12 -0
  71. data/test/haml/results/nuke_inner_whitespace.xhtml +40 -0
  72. data/test/haml/results/nuke_outer_whitespace.xhtml +148 -0
  73. data/test/haml/results/original_engine.xhtml +20 -0
  74. data/test/haml/results/partial_layout.xhtml +5 -0
  75. data/test/haml/results/partials.xhtml +21 -0
  76. data/test/haml/results/render_layout.xhtml +3 -0
  77. data/test/haml/results/silent_script.xhtml +74 -0
  78. data/test/haml/results/standard.xhtml +42 -0
  79. data/test/haml/results/tag_parsing.xhtml +23 -0
  80. data/test/haml/results/very_basic.xhtml +5 -0
  81. data/test/haml/results/whitespace_handling.xhtml +89 -0
  82. data/test/haml/rhtml/_av_partial_1.rhtml +12 -0
  83. data/test/haml/rhtml/_av_partial_2.rhtml +8 -0
  84. data/test/haml/rhtml/action_view.rhtml +62 -0
  85. data/test/haml/rhtml/standard.rhtml +54 -0
  86. data/test/haml/template_test.rb +204 -0
  87. data/test/haml/templates/_av_partial_1.haml +9 -0
  88. data/test/haml/templates/_av_partial_1_ugly.haml +9 -0
  89. data/test/haml/templates/_av_partial_2.haml +5 -0
  90. data/test/haml/templates/_av_partial_2_ugly.haml +5 -0
  91. data/test/haml/templates/_layout.erb +3 -0
  92. data/test/haml/templates/_layout_for_partial.haml +3 -0
  93. data/test/haml/templates/_partial.haml +8 -0
  94. data/test/haml/templates/_text_area.haml +3 -0
  95. data/test/haml/templates/action_view.haml +47 -0
  96. data/test/haml/templates/action_view_ugly.haml +47 -0
  97. data/test/haml/templates/breakage.haml +8 -0
  98. data/test/haml/templates/content_for_layout.haml +10 -0
  99. data/test/haml/templates/eval_suppressed.haml +11 -0
  100. data/test/haml/templates/filters.haml +66 -0
  101. data/test/haml/templates/helpers.haml +95 -0
  102. data/test/haml/templates/helpful.haml +11 -0
  103. data/test/haml/templates/just_stuff.haml +83 -0
  104. data/test/haml/templates/list.haml +12 -0
  105. data/test/haml/templates/nuke_inner_whitespace.haml +32 -0
  106. data/test/haml/templates/nuke_outer_whitespace.haml +144 -0
  107. data/test/haml/templates/original_engine.haml +17 -0
  108. data/test/haml/templates/partial_layout.haml +3 -0
  109. data/test/haml/templates/partialize.haml +1 -0
  110. data/test/haml/templates/partials.haml +12 -0
  111. data/test/haml/templates/render_layout.haml +2 -0
  112. data/test/haml/templates/silent_script.haml +40 -0
  113. data/test/haml/templates/standard.haml +42 -0
  114. data/test/haml/templates/standard_ugly.haml +1 -0
  115. data/test/haml/templates/tag_parsing.haml +21 -0
  116. data/test/haml/templates/very_basic.haml +4 -0
  117. data/test/haml/templates/whitespace_handling.haml +87 -0
  118. data/test/linked_rails.rb +12 -0
  119. data/test/sass/css2sass_test.rb +193 -0
  120. data/test/sass/engine_test.rb +786 -0
  121. data/test/sass/functions_test.rb +96 -0
  122. data/test/sass/more_results/more1.css +9 -0
  123. data/test/sass/more_results/more1_with_line_comments.css +26 -0
  124. data/test/sass/more_results/more_import.css +29 -0
  125. data/test/sass/more_templates/_more_partial.sass +2 -0
  126. data/test/sass/more_templates/more1.sass +23 -0
  127. data/test/sass/more_templates/more_import.sass +11 -0
  128. data/test/sass/plugin_test.rb +208 -0
  129. data/test/sass/results/alt.css +4 -0
  130. data/test/sass/results/basic.css +9 -0
  131. data/test/sass/results/compact.css +5 -0
  132. data/test/sass/results/complex.css +87 -0
  133. data/test/sass/results/compressed.css +1 -0
  134. data/test/sass/results/expanded.css +19 -0
  135. data/test/sass/results/import.css +29 -0
  136. data/test/sass/results/line_numbers.css +49 -0
  137. data/test/sass/results/mixins.css +95 -0
  138. data/test/sass/results/multiline.css +24 -0
  139. data/test/sass/results/nested.css +22 -0
  140. data/test/sass/results/parent_ref.css +13 -0
  141. data/test/sass/results/script.css +16 -0
  142. data/test/sass/results/subdir/nested_subdir/nested_subdir.css +1 -0
  143. data/test/sass/results/subdir/subdir.css +3 -0
  144. data/test/sass/results/units.css +11 -0
  145. data/test/sass/script_test.rb +153 -0
  146. data/test/sass/templates/_partial.sass +2 -0
  147. data/test/sass/templates/alt.sass +16 -0
  148. data/test/sass/templates/basic.sass +23 -0
  149. data/test/sass/templates/bork.sass +2 -0
  150. data/test/sass/templates/bork2.sass +2 -0
  151. data/test/sass/templates/compact.sass +17 -0
  152. data/test/sass/templates/complex.sass +309 -0
  153. data/test/sass/templates/compressed.sass +15 -0
  154. data/test/sass/templates/expanded.sass +17 -0
  155. data/test/sass/templates/import.sass +11 -0
  156. data/test/sass/templates/importee.sass +19 -0
  157. data/test/sass/templates/line_numbers.sass +13 -0
  158. data/test/sass/templates/mixins.sass +76 -0
  159. data/test/sass/templates/multiline.sass +20 -0
  160. data/test/sass/templates/nested.sass +25 -0
  161. data/test/sass/templates/parent_ref.sass +25 -0
  162. data/test/sass/templates/script.sass +101 -0
  163. data/test/sass/templates/subdir/nested_subdir/_nested_partial.sass +2 -0
  164. data/test/sass/templates/subdir/nested_subdir/nested_subdir.sass +3 -0
  165. data/test/sass/templates/subdir/subdir.sass +6 -0
  166. data/test/sass/templates/units.sass +11 -0
  167. data/test/test_helper.rb +21 -0
  168. metadata +245 -0
@@ -0,0 +1,45 @@
1
+ require 'strscan'
2
+
3
+ # :stopdoc:
4
+ module Haml
5
+ # This module contains functionality that's shared across Haml and Sass.
6
+ module Shared
7
+ def self.handle_interpolation(str)
8
+ scan = StringScanner.new(str)
9
+ yield scan while scan.scan(/(.*?)(\\*)\#\{/)
10
+ scan.rest
11
+ end
12
+
13
+ def self.balance(scanner, start, finish, count = 0)
14
+ str = ''
15
+ scanner = StringScanner.new(scanner) unless scanner.is_a? StringScanner
16
+ regexp = Regexp.new("(.*?)[\\#{start.chr}\\#{finish.chr}]", Regexp::MULTILINE)
17
+ while scanner.scan(regexp)
18
+ str << scanner.matched
19
+ count += 1 if scanner.matched[-1] == start
20
+ count -= 1 if scanner.matched[-1] == finish
21
+ return [str.strip, scanner.rest] if count == 0
22
+ end
23
+ end
24
+
25
+ def self.human_indentation(indentation, was = false)
26
+ if !indentation.include?(?\t)
27
+ noun = 'space'
28
+ elsif !indentation.include?(?\s)
29
+ noun = 'tab'
30
+ else
31
+ return indentation.inspect + (was ? ' was' : '')
32
+ end
33
+
34
+ singular = indentation.length == 1
35
+ if was
36
+ was = singular ? ' was' : ' were'
37
+ else
38
+ was = ''
39
+ end
40
+
41
+ "#{indentation.length} #{noun}#{'s' unless singular}#{was}"
42
+ end
43
+ end
44
+ end
45
+ # :startdoc:
@@ -0,0 +1,58 @@
1
+ # This file makes Haml work with Rails
2
+ # by monkeypatching the core template-compilation methods.
3
+ # This is necessary for versions <= 2.0.1 because the template handler API
4
+ # wasn't sufficiently powerful to deal with caching and so forth.
5
+
6
+ # This module refers to the ActionView module that's part of Ruby on Rails.
7
+ # Haml can be used as an alternate templating engine for it,
8
+ # and includes several modifications to make it more Haml-friendly.
9
+ # The documentation can be found
10
+ # here[http://rubyonrails.org/api/classes/ActionView/Base.html].
11
+ module ActionView
12
+ class Base # :nodoc:
13
+ def delegate_template_exists_with_haml(template_path)
14
+ template_exists?(template_path, :haml) && [:haml]
15
+ end
16
+ alias_method :delegate_template_exists_without_haml, :delegate_template_exists?
17
+ alias_method :delegate_template_exists?, :delegate_template_exists_with_haml
18
+
19
+ def compile_template_with_haml(extension, template, file_name, local_assigns)
20
+ return compile_haml(template, file_name, local_assigns) if extension.to_s == "haml"
21
+ compile_template_without_haml(extension, template, file_name, local_assigns)
22
+ end
23
+ alias_method :compile_template_without_haml, :compile_template
24
+ alias_method :compile_template, :compile_template_with_haml
25
+
26
+ def compile_haml(template, file_name, local_assigns)
27
+ render_symbol = assign_method_name(:haml, template, file_name)
28
+ locals = local_assigns.keys
29
+
30
+ @@template_args[render_symbol] ||= {}
31
+ locals_keys = @@template_args[render_symbol].keys | locals
32
+ @@template_args[render_symbol] = Haml::Util.to_hash(locals_keys.map {|k| [k, true]})
33
+
34
+ options = Haml::Template.options.dup
35
+ options[:filename] = file_name || 'compiled-template'
36
+
37
+ begin
38
+ Haml::Engine.new(template, options).def_method(CompiledTemplates, render_symbol, *locals_keys)
39
+ rescue Exception => e
40
+ if logger
41
+ logger.debug "ERROR: compiling #{render_symbol} RAISED #{e}"
42
+ logger.debug "Backtrace: #{e.backtrace.join("\n")}"
43
+ end
44
+
45
+ base_path = if defined?(extract_base_path_from)
46
+ # Rails 2.0.x
47
+ extract_base_path_from(file_name) || view_paths.first
48
+ else
49
+ # Rails <=1.2.6
50
+ @base_path
51
+ end
52
+ raise ActionView::TemplateError.new(base_path, file_name || template, @assigns, template, e)
53
+ end
54
+
55
+ @@compile_time[render_symbol] = Time.now
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,72 @@
1
+ # :stopdoc:
2
+ # This file makes Haml work with Rails
3
+ # using the > 2.0.1 template handler API.
4
+
5
+ module Haml
6
+ class Plugin < ActionView::TemplateHandler
7
+ include ActionView::TemplateHandlers::Compilable if defined?(ActionView::TemplateHandlers::Compilable)
8
+
9
+ def compile(template)
10
+ options = Haml::Template.options.dup
11
+
12
+ # template is a template object in Rails >=2.1.0,
13
+ # a source string previously
14
+ if template.respond_to? :source
15
+ options[:filename] = template.filename
16
+ source = template.source
17
+ else
18
+ source = template
19
+ end
20
+
21
+ Haml::Engine.new(source, options).send(:precompiled_with_ambles, [])
22
+ end
23
+
24
+ def cache_fragment(block, name = {}, options = nil)
25
+ @view.fragment_for(block, name, options) do
26
+ eval("_hamlout.buffer", block.binding)
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ if defined? ActionView::Template and ActionView::Template.respond_to? :register_template_handler
33
+ ActionView::Template
34
+ else
35
+ ActionView::Base
36
+ end.register_template_handler(:haml, Haml::Plugin)
37
+
38
+ # In Rails 2.0.2, ActionView::TemplateError took arguments
39
+ # that we can't fill in from the Haml::Plugin context.
40
+ # Thus, we've got to monkeypatch ActionView::Base to catch the error.
41
+ if ActionView::TemplateError.instance_method(:initialize).arity == 5
42
+ class ActionView::Base
43
+ def compile_template(handler, template, file_name, local_assigns)
44
+ render_symbol = assign_method_name(handler, template, file_name)
45
+
46
+ # Move begin up two lines so it captures compilation exceptions.
47
+ begin
48
+ render_source = create_template_source(handler, template, render_symbol, local_assigns.keys)
49
+ line_offset = @@template_args[render_symbol].size + handler.line_offset
50
+
51
+ file_name = 'compiled-template' if file_name.blank?
52
+ CompiledTemplates.module_eval(render_source, file_name, -line_offset)
53
+ rescue Exception => e # errors from template code
54
+ if logger
55
+ logger.debug "ERROR: compiling #{render_symbol} RAISED #{e}"
56
+ logger.debug "Function body: #{render_source}"
57
+ logger.debug "Backtrace: #{e.backtrace.join("\n")}"
58
+ end
59
+
60
+ # There's no way to tell Haml about the filename,
61
+ # so we've got to insert it ourselves.
62
+ e.backtrace[0].gsub!('(haml)', file_name) if e.is_a?(Haml::Error)
63
+
64
+ raise ActionView::TemplateError.new(extract_base_path_from(file_name) || view_paths.first, file_name || template, @assigns, template, e)
65
+ end
66
+
67
+ @@compile_time[render_symbol] = Time.now
68
+ # logger.debug "Compiled template #{file_name || template}\n ==> #{render_symbol}" if logger
69
+ end
70
+ end
71
+ end
72
+ # :startdoc:
@@ -0,0 +1,51 @@
1
+ require 'haml/engine'
2
+
3
+ module Haml
4
+ class Template
5
+ class << self
6
+ @@options = {}
7
+
8
+ # Gets various options for Haml. See README.rdoc for details.
9
+ def options
10
+ @@options
11
+ end
12
+
13
+ # Sets various options for Haml. See README.rdoc for details.
14
+ def options=(value)
15
+ @@options = value
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ # Decide how we want to load Haml into Rails.
22
+ # Patching was necessary for versions <= 2.0.1,
23
+ # but we can make it a normal handler for higher versions.
24
+ if defined?(ActionView::TemplateHandler)
25
+ require 'haml/template/plugin'
26
+ else
27
+ require 'haml/template/patch'
28
+ end
29
+
30
+ if defined?(RAILS_ROOT)
31
+ # Update init.rb to the current version
32
+ # if it's out of date.
33
+ #
34
+ # We can probably remove this as of v1.9,
35
+ # because the new init file is sufficiently flexible
36
+ # to not need updating.
37
+ rails_init_file = File.join(RAILS_ROOT, 'vendor', 'plugins', 'haml', 'init.rb')
38
+ haml_init_file = Haml.scope('init.rb')
39
+ begin
40
+ if File.exists?(rails_init_file)
41
+ require 'fileutils'
42
+ FileUtils.cp(haml_init_file, rails_init_file) unless FileUtils.cmp(rails_init_file, haml_init_file)
43
+ end
44
+ rescue SystemCallError
45
+ warn <<END
46
+ HAML WARNING:
47
+ #{rails_init_file} is out of date and couldn't be automatically updated.
48
+ Please run `haml --rails #{File.expand_path(RAILS_ROOT)}' to update it.
49
+ END
50
+ end
51
+ end
data/lib/haml/util.rb ADDED
@@ -0,0 +1,77 @@
1
+ require 'erb'
2
+ require 'set'
3
+ require 'enumerator'
4
+
5
+ module Haml
6
+ module Util
7
+ class << self; include Haml::Util; end
8
+
9
+ RUBY_VERSION = ::RUBY_VERSION.split(".").map {|s| s.to_i}
10
+
11
+ def to_hash(arr)
12
+ arr.compact.inject({}) {|h, (k, v)| h[k] = v; h}
13
+ end
14
+
15
+ def map_keys(hash)
16
+ to_hash(hash.map {|k, v| [yield(k), v]})
17
+ end
18
+
19
+ def map_vals(hash)
20
+ to_hash(hash.map {|k, v| [k, yield(v)]})
21
+ end
22
+
23
+ def map_hash(hash, &block)
24
+ to_hash(hash.map(&block))
25
+ end
26
+
27
+ def powerset(arr)
28
+ arr.inject([Set.new].to_set) do |powerset, el|
29
+ new_powerset = Set.new
30
+ powerset.each do |subset|
31
+ new_powerset << subset
32
+ new_powerset << subset + [el]
33
+ end
34
+ new_powerset
35
+ end
36
+ end
37
+
38
+ def ruby1_8?
39
+ Haml::Util::RUBY_VERSION[0] == 1 && Haml::Util::RUBY_VERSION[1] < 9
40
+ end
41
+
42
+ def has?(attr, klass, method)
43
+ klass.send("#{attr}s").include?(ruby1_8? ? method.to_s : method.to_sym)
44
+ end
45
+
46
+ def enum_with_index(enum)
47
+ ruby1_8? ? enum.enum_with_index : enum.each_with_index
48
+ end
49
+
50
+ class StaticConditionalContext
51
+ def initialize(set)
52
+ @set = set
53
+ end
54
+
55
+ def method_missing(name, *args, &block)
56
+ super unless args.empty? && block.nil?
57
+ @set.include?(name)
58
+ end
59
+ end
60
+
61
+ def def_static_method(klass, name, args, *vars)
62
+ erb = vars.pop
63
+ powerset(vars).each do |set|
64
+ context = StaticConditionalContext.new(set).instance_eval {binding}
65
+ klass.class_eval(<<METHOD)
66
+ def #{static_method_name(name, *vars.map {|v| set.include?(v)})}(#{args.join(', ')})
67
+ #{ERB.new(erb).result(context)}
68
+ end
69
+ METHOD
70
+ end
71
+ end
72
+
73
+ def static_method_name(name, *vars)
74
+ "#{name}_#{vars.map {|v| !!v}.join('_')}"
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,47 @@
1
+ module Haml
2
+ module Version
3
+ # Returns a hash representing the version of Haml.
4
+ # The :major, :minor, and :teeny keys have their respective numbers.
5
+ # The :string key contains a human-readable string representation of the version.
6
+ # If Haml is checked out from Git,
7
+ # the :rev key will have the revision hash.
8
+ def version
9
+ return @@version if defined?(@@version)
10
+
11
+ numbers = File.read(scope('VERSION')).strip.split('.').map { |n| n.to_i }
12
+ @@version = {
13
+ :major => numbers[0],
14
+ :minor => numbers[1],
15
+ :teeny => numbers[2]
16
+ }
17
+ @@version[:string] = [:major, :minor, :teeny].map { |comp| @@version[comp] }.compact.join('.')
18
+
19
+ if File.exists?(scope('REVISION'))
20
+ rev = File.read(scope('REVISION')).strip
21
+ rev = nil if rev !~ /^([a-f0-9]+|\(.*\))$/
22
+ end
23
+
24
+ if (rev.nil? || rev == '(unknown)') && File.exists?(scope('.git/HEAD'))
25
+ rev = File.read(scope('.git/HEAD')).strip
26
+ if rev =~ /^ref: (.*)$/
27
+ rev = File.read(scope(".git/#{$1}")).strip
28
+ end
29
+ end
30
+
31
+ if rev
32
+ @@version[:rev] = rev
33
+ unless rev[0] == ?(
34
+ @@version[:string] << "."
35
+ @@version[:string] << rev[0...7]
36
+ end
37
+ end
38
+
39
+ @@version
40
+ end
41
+
42
+ # Returns the path of file relative to the Haml root.
43
+ def scope(file) # :nodoc:
44
+ File.expand_path File.join(File.dirname(__FILE__), '..', '..', file)
45
+ end
46
+ end
47
+ end