css.rb 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. require 'tilt/template'
  2. module Tilt
  3. # Sass template implementation. See:
  4. # http://haml.hamptoncatlin.com/
  5. #
  6. # Sass templates do not support object scopes, locals, or yield.
  7. class SassTemplate < Template
  8. self.default_mime_type = 'text/css'
  9. def self.engine_initialized?
  10. defined? ::Sass::Engine
  11. end
  12. def initialize_engine
  13. require_template_library 'sass'
  14. end
  15. def prepare
  16. @engine = ::Sass::Engine.new(data, sass_options)
  17. end
  18. def evaluate(scope, locals, &block)
  19. @output ||= @engine.render
  20. end
  21. private
  22. def sass_options
  23. options.merge(:filename => eval_file, :line => line, :syntax => :sass)
  24. end
  25. end
  26. # Sass's new .scss type template implementation.
  27. class ScssTemplate < SassTemplate
  28. self.default_mime_type = 'text/css'
  29. private
  30. def sass_options
  31. options.merge(:filename => eval_file, :line => line, :syntax => :scss)
  32. end
  33. end
  34. # Lessscss template implementation. See:
  35. # http://lesscss.org/
  36. #
  37. # Less templates do not support object scopes, locals, or yield.
  38. class LessTemplate < Template
  39. self.default_mime_type = 'text/css'
  40. def self.engine_initialized?
  41. defined? ::Less
  42. end
  43. def initialize_engine
  44. require_template_library 'less'
  45. end
  46. def prepare
  47. if ::Less.const_defined? :Engine
  48. @engine = ::Less::Engine.new(data)
  49. else
  50. parser = ::Less::Parser.new(:filename => eval_file, :line => line)
  51. @engine = parser.parse(data)
  52. end
  53. end
  54. def evaluate(scope, locals, &block)
  55. @output ||= @engine.to_css
  56. end
  57. end
  58. end