Sha256: 53e7d505ab234db4d3cf6bc74fe6c7655105401142c64d88b63a8b6db46418e9

Contents?: true

Size: 1.23 KB

Versions: 7

Compression:

Stored size: 1.23 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
      end

      def allows_script?
        false
      end
    end
  end
end

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

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
maglove-1.0.9 lib/maglove/tilt/coffee_template.rb
maglove-1.0.8 lib/maglove/tilt/coffee_template.rb
maglove-1.0.7 lib/maglove/tilt/coffee_template.rb
maglove-1.0.5 lib/maglove/tilt/coffee_template.rb
maglove-1.0.4 lib/maglove/tilt/coffee_template.rb
maglove-1.0.3 lib/maglove/tilt/coffee_template.rb
maglove-1.0.2 lib/maglove/tilt/coffee_template.rb