sass.rb 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. dir = File.dirname(__FILE__)
  2. $LOAD_PATH.unshift dir unless $LOAD_PATH.include?(dir)
  3. # This is necessary to set so that the Haml code that tries to load Sass
  4. # knows that Sass is indeed loading,
  5. # even if there's some crazy autoload stuff going on.
  6. SASS_BEGUN_TO_LOAD = true unless defined?(SASS_BEGUN_TO_LOAD)
  7. require 'sass/version'
  8. # The module that contains everything Sass-related:
  9. #
  10. # * {Sass::Engine} is the class used to render Sass/SCSS within Ruby code.
  11. # * {Sass::Plugin} is interfaces with web frameworks (Rails and Merb in particular).
  12. # * {Sass::SyntaxError} is raised when Sass encounters an error.
  13. # * {Sass::CSS} handles conversion of CSS to Sass.
  14. #
  15. # Also see the {file:SASS_REFERENCE.md full Sass reference}.
  16. module Sass
  17. # Compile a Sass or SCSS string to CSS.
  18. # Defaults to SCSS.
  19. #
  20. # @param contents [String] The contents of the Sass file.
  21. # @param options [{Symbol => Object}] An options hash;
  22. # see {file:SASS_REFERENCE.md#sass_options the Sass options documentation}
  23. # @raise [Sass::SyntaxError] if there's an error in the document
  24. # @raise [Encoding::UndefinedConversionError] if the source encoding
  25. # cannot be converted to UTF-8
  26. # @raise [ArgumentError] if the document uses an unknown encoding with `@charset`
  27. def self.compile(contents, options = {})
  28. options[:syntax] ||= :scss
  29. Engine.new(contents, options).to_css
  30. end
  31. # Compile a file on disk to CSS.
  32. #
  33. # @param filename [String] The path to the Sass, SCSS, or CSS file on disk.
  34. # @param options [{Symbol => Object}] An options hash;
  35. # see {file:SASS_REFERENCE.md#sass_options the Sass options documentation}
  36. # @raise [Sass::SyntaxError] if there's an error in the document
  37. # @raise [Encoding::UndefinedConversionError] if the source encoding
  38. # cannot be converted to UTF-8
  39. # @raise [ArgumentError] if the document uses an unknown encoding with `@charset`
  40. #
  41. # @overload compile_file(filename, options = {})
  42. # Return the compiled CSS rather than writing it to a file.
  43. #
  44. # @return [String] The compiled CSS.
  45. #
  46. # @overload compile_file(filename, css_filename, options = {})
  47. # Write the compiled CSS to a file.
  48. #
  49. # @param css_filename [String] The location to which to write the compiled CSS.
  50. def self.compile_file(filename, *args)
  51. options = args.last.is_a?(Hash) ? args.pop : {}
  52. css_filename = args.shift
  53. result = Sass::Engine.for_file(filename, options).render
  54. if css_filename
  55. options[:css_filename] ||= css_filename
  56. open(css_filename,"w") {|css_file| css_file.write(result)}
  57. nil
  58. else
  59. result
  60. end
  61. end
  62. end
  63. require 'sass/logger'
  64. require 'sass/util'
  65. require 'sass/engine'
  66. require 'sass/plugin' if defined?(Merb::Plugins)
  67. require 'sass/railtie'