Sha256: 38cf6fa70ebf6fb960a654b3f659322631c8868d9ea6bde269cdafeb3bfa598d

Contents?: true

Size: 1.79 KB

Versions: 11

Compression:

Stored size: 1.79 KB

Contents

# frozen_string_literal: true
require_relative 'template'

module Tilt
  # Sass template implementation for generating CSS. See: https://sass-lang.com/
  #
  # Sass templates do not support object scopes, locals, or yield.
  class SassTemplate < StaticTemplate
    self.default_mime_type = 'text/css'

    begin
      require 'sass-embedded'
    # :nocov:
      require 'uri'

      ALLOWED_KEYS = (defined?(::Sass::Compiler) ? ::Sass::Compiler : ::Sass::Embedded).
        instance_method(:compile_string).
        parameters.
        map{|k, v| v if k == :key}.
        compact rescue nil
      private_constant :ALLOWED_KEYS

      private

      def _prepare_output
        ::Sass.compile_string(@data, **sass_options).css
      end

      def sass_options
        path = File.absolute_path(eval_file)
        path = '/' + path unless path.start_with?('/')
        opts = @options.dup
        opts[:url] = ::URI::File.build([nil, ::URI::DEFAULT_PARSER.escape(path)]).to_s
        opts[:syntax] = :indented
        opts.delete_if{|k| !ALLOWED_KEYS.include?(k)} if ALLOWED_KEYS
        opts
      end
    rescue LoadError => err
      begin
        require 'sassc'
        Engine = ::SassC::Engine
      rescue LoadError
        begin
          require 'sass'
          Engine = ::Sass::Engine
        rescue LoadError
          raise err
        end
      end

      private

      def _prepare_output
        Engine.new(@data, sass_options).render
      end

      def sass_options
        @options[:filename] = eval_file
        @options[:line] = @line
        @options[:syntax] = :sass
        @options
      end
    # :nocov:
    end
  end

  class ScssTemplate < SassTemplate
    self.default_mime_type = 'text/css'

    private

    def sass_options
      opts = super
      opts[:syntax] = :scss
      opts
    end
  end
end

Version data entries

11 entries across 10 versions & 4 rubygems

Version Path
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.1.0/gems/tilt-2.4.0/lib/tilt/sass.rb
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.3.0/gems/tilt-2.4.0/lib/tilt/sass.rb
tilt-2.6.0 lib/tilt/sass.rb
brakeman-7.0.0 bundle/ruby/3.1.0/gems/tilt-2.5.0/lib/tilt/sass.rb
tilt-2.5.0 lib/tilt/sass.rb
brakeman-6.2.2 bundle/ruby/3.1.0/gems/tilt-2.4.0/lib/tilt/sass.rb
brakeman-6.2.2.rc1 bundle/ruby/3.3.0/gems/tilt-2.4.0/lib/tilt/sass.rb
brakeman-6.2.1 bundle/ruby/3.1.0/gems/tilt-2.4.0/lib/tilt/sass.rb
brakeman-6.2.0 bundle/ruby/3.1.0/gems/tilt-2.4.0/lib/tilt/sass.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/tilt-2.4.0/lib/tilt/sass.rb
tilt-2.4.0 lib/tilt/sass.rb