class SassC::Engine
def file_url
def file_url @file_url ||= Util.path_to_file_url(filename || 'stdin') end
def load_paths
def load_paths @load_paths ||= (@options[:load_paths] || []) + SassC.load_paths end
def output_path
def output_path @output_path ||= @options.fetch( :output_path, ("#{File.basename(filename, File.extname(filename))}.css" if filename) ) end
def output_style
def output_style @output_style ||= begin style = @options.fetch(:style, :sass_style_nested).to_s style = "sass_style_#{style}" unless style.include?('sass_style_') raise InvalidStyleError unless OUTPUT_STYLES.include?(style.to_sym) style = style.delete_prefix('sass_style_').to_sym case style when :nested :expanded when :compact :compressed else style end end end
def post_process_css(css)
def post_process_css(css) css += "\n" unless css.empty? unless @source_map.nil? || omit_source_map_url? url = if source_map_embed? "data:application/json;base64,#{Base64.strict_encode64(@source_map)}" else Util::URI_PARSER.escape(relative_path(File.dirname(output_path || ''), source_map_file)) end css += "\n/*# sourceMappingURL=#{url} */" end css end
def post_process_source_map(source_map)
def post_process_source_map(source_map) return unless source_map data = JSON.parse(source_map) source_map_dir = File.dirname(source_map_file || '') data['file'] = Util::URI_PARSER.escape(relative_path(source_map_dir, output_path)) if output_path data['sources'].map! do |source| if source.start_with? 'file:' relative_path(source_map_dir, Util.file_url_to_path(source)) else source end end JSON.generate(data) end
def relative_path(from, to)
def relative_path(from, to) Pathname.new(File.absolute_path(to)).relative_path_from(Pathname.new(File.absolute_path(from))).to_s end
def render
def render return @template.dup if @template.empty? result = ::Sass.compile_string( @template, importer: nil, load_paths: load_paths, syntax: syntax, url: file_url, source_map: source_map_embed? || !source_map_file.nil?, source_map_include_sources: source_map_contents?, style: output_style, functions: FunctionsHandler.new(@options).setup(nil, functions: @functions), importers: ImportHandler.new(@options).setup(nil), alert_ascii: @options.fetch(:alert_ascii, false), alert_color: @options.fetch(:alert_color, nil), logger: @options.fetch(:logger, nil), quiet_deps: @options.fetch(:quiet_deps, false), verbose: @options.fetch(:verbose, false) ) @dependencies = result.loaded_urls .filter { |url| url.start_with?('file:') && url != file_url } .map { |url| Util.file_url_to_path(url) } @source_map = post_process_source_map(result.source_map) return post_process_css(result.css) unless quiet? rescue ::Sass::CompileError => e line = e.span&.start&.line line += 1 unless line.nil? path = Util.file_url_to_path(e.span&.url) path = relative_path(Dir.pwd, path) unless path.nil? raise SyntaxError.new(e.message, filename: path, line: line) end
def syntax
def syntax syntax = @options.fetch(:syntax, :scss) syntax = :indented if syntax.to_sym == :sass syntax end