Sha256: 30c95809117dd9d1a8359b180e5600581e7c23e5dd257462ae176ed7f80aba65

Contents?: true

Size: 1.76 KB

Versions: 3

Compression:

Stored size: 1.76 KB

Contents

require 'epuber-stylus/tilt/stylus'
# Public: A Tilt template to compile Stylus stylesheets with asset helpers.
module Stylus
  module Rails
    class StylusTemplate < ::Tilt::StylusTemplate

      # Public: The default mime type for stylesheets.
      self.default_mime_type = 'text/css'

      # Internal: Appends stylus mixin for asset_url and asset_path support
      def evaluate(scope, locals, &block)
        @data = build_mixin_body(scope) + data
        super
      end

      protected

      # Internal: Builds body of a mixin
      #
      # Returns string representation of a mixin with asset helper functions
      def build_mixin_body(scope)
        @mixin_body ||= if assets_hash(scope).values.all? {|value| value != '' }
                          <<-STYL
asset-url(key)
  return pair[1] if pair[0] == key for pair in #{assets_hash(scope)[:url]} ()
asset-path(key)
  return pair[1] if pair[0] == key for pair in #{assets_hash(scope)[:path]} ()
                          STYL
                        else
                          ''
                        end
      end

      # Internal: Construct Hash with absolute/relative paths in stylus syntax.
      #
      # Returns string representations of hash in Stylus syntax
      def assets_hash(scope)
        @assets_hash ||= scope.environment.each_logical_path.each_with_object({ :url => '', :path => '' }) do |logical_path, assets_hash|
          unless File.extname(logical_path) =~ /^(\.(css|js)|)$/
            path_to_asset = scope.path_to_asset(logical_path)
            assets_hash[:url] << "('#{logical_path}' url(\"#{path_to_asset}\")) "
            assets_hash[:path] << "('#{logical_path}' \"#{path_to_asset}\") "
          end
        end
      end

    end
  end
end

Tilt.register ::Stylus::Rails::StylusTemplate, 'styl'

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
epuber-stylus-1.2.0 lib/epuber-stylus/tilt/rails.rb
epuber-stylus-1.1.1 lib/epuber-stylus/tilt/rails.rb
epuber-stylus-1.1.0 lib/epuber-stylus/tilt/rails.rb