Sha256: 45ca2d30108b1a050616383cf0523d9b4bae9e95a7616cf3c435fb497756876d

Contents?: true

Size: 1.25 KB

Versions: 5

Compression:

Stored size: 1.25 KB

Contents

module MagLove
  module Tilt
    class CoffeeTemplate < ::Tilt::Template
      self.default_mime_type = 'application/javascript'

      def self.engine_initialized?
        defined? ::CoffeeScript
      end

      def initialize_engine
        require_template_library 'coffee_script'
      end

      def prepare
        options[:bare] = true
      end

      def evaluate(scope, locals, &block)
        @output = CoffeeScript.compile(@data, options)

        # handle includes
        @output.gsub!(/^include\("([^"]+)"\);$/) do |match|
          path = Regexp.last_match[1]
          path = "#{path}.coffee" if File.extname(path).empty?
          include_path = File.absolute_path(path, File.dirname(file))

          # check if base path exists
          unless File.exist?(include_path)
            include_path = File.absolute_path(path, locals[:base_path])
          end

          if File.exist?(include_path)
            include_template = ::Tilt.new(include_path)
            include_template.render(Object.new, locals)
          else
            raise "Path not found: #{include_path}"
          end
        end

        @output
      end

      def allows_script?
        false
      end
    end
  end
end

Tilt.mappings["coffee"] = [MagLove::Tilt::CoffeeTemplate]

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
maglove-2.0.11 lib/maglove/tilt/coffee_template.rb
maglove-2.0.10 lib/maglove/tilt/coffee_template.rb
maglove-2.0.9 lib/maglove/tilt/coffee_template.rb
maglove-2.0.8 lib/maglove/tilt/coffee_template.rb
maglove-2.0.7 lib/maglove/tilt/coffee_template.rb