PageRenderTime 55ms CodeModel.GetById 16ms app.highlight 34ms RepoModel.GetById 2ms app.codeStats 0ms

/scalate-jruby/src/main/resources/haml-3.0.25/lib/sass/tree/import_node.rb

http://github.com/scalate/scalate
Ruby | 102 lines | 62 code | 13 blank | 27 comment | 2 complexity | 5fc1411cb076a6bb73af7ff1aaa10e87 MD5 | raw file
  1module Sass
  2  module Tree
  3    # A static node that wraps the {Sass::Tree} for an `@import`ed file.
  4    # It doesn't have a functional purpose other than to add the `@import`ed file
  5    # to the backtrace if an error occurs.
  6    class ImportNode < RootNode
  7      # The name of the imported file as it appears in the Sass document.
  8      #
  9      # @return [String]
 10      attr_reader :imported_filename
 11
 12      # @param imported_filename [String] The name of the imported file
 13      def initialize(imported_filename)
 14        @imported_filename = imported_filename
 15        super(nil)
 16      end
 17
 18      def invisible?; to_s.empty?; end
 19
 20      # Returns the resolved name of the imported file,
 21      # as returned by \{Sass::Files#find\_file\_to\_import}.
 22      #
 23      # @return [String] The filename of the imported file.
 24      #   This is an absolute path if the file is a `".sass"` or `".scss"` file.
 25      # @raise [Sass::SyntaxError] if `filename` ends in `".sass"` or `".scss"`
 26      #   and no corresponding Sass file could be found.
 27      def full_filename
 28        @full_filename ||= import
 29      end
 30
 31      # @see Node#to_sass
 32      def to_sass(tabs = 0, opts = {})
 33        "#{'  ' * tabs}@import #{@imported_filename}\n"
 34      end
 35
 36      # @see Node#to_scss
 37      def to_scss(tabs = 0, opts = {})
 38        "#{'  ' * tabs}@import \"#{@imported_filename}\";\n"
 39      end
 40
 41      # @see Node#cssize
 42      def cssize(*args)
 43        super.first
 44      end
 45
 46      protected
 47
 48      # @see Node#_cssize
 49      def _cssize(*args)
 50        super.children
 51      rescue Sass::SyntaxError => e
 52        e.modify_backtrace(:filename => children.first.filename)
 53        e.add_backtrace(:filename => @filename, :line => @line)
 54        raise e
 55      end
 56
 57      # Returns a static DirectiveNode if this is importing a CSS file,
 58      # or parses and includes the imported Sass file.
 59      #
 60      # @param environment [Sass::Environment] The lexical environment containing
 61      #   variable and mixin values
 62      def _perform(environment)
 63        return DirectiveNode.new("@import url(#{full_filename})") if full_filename =~ /\.css$/
 64        super
 65      end
 66
 67      # Parses the imported file and runs the dynamic Sass for it.
 68      #
 69      # @param environment [Sass::Environment] The lexical environment containing
 70      #   variable and mixin values
 71      def perform!(environment)
 72        environment.push_frame(:filename => @filename, :line => @line)
 73        options = @options.dup
 74        options.delete(:syntax)
 75        root = Sass::Files.tree_for(full_filename, options)
 76        @template = root.template
 77        self.children = root.children
 78        self.children = perform_children(environment)
 79      rescue Sass::SyntaxError => e
 80        e.modify_backtrace(:filename => full_filename)
 81        e.add_backtrace(:filename => @filename, :line => @line)
 82        raise e
 83      ensure
 84        environment.pop_frame
 85      end
 86
 87      private
 88
 89      def import_paths
 90        paths = (@options[:load_paths] || []).dup
 91        paths.unshift(File.dirname(@options[:filename])) if @options[:filename]
 92        paths
 93      end
 94
 95      def import
 96        Sass::Files.find_file_to_import(@imported_filename, import_paths)
 97      rescue Exception => e
 98        raise SyntaxError.new(e.message, :line => self.line, :filename => @filename)
 99      end
100    end
101  end
102end