class Webgen::ContentProcessor::Sass::NodeTreeImporter

Custom importer for Sass to load files by resolving them in the node tree.

Public Class Methods

new(context) click to toggle source

Creates a new importer that imports files from the node tree relative to the given node alcn.

# File lib/webgen/content_processor/sass.rb, line 32
def initialize(context)
  @context = context
end

Public Instance Methods

find(name, options) click to toggle source

@see Base#find

# File lib/webgen/content_processor/sass.rb, line 48
def find(name, options)
  _find(@context.ref_node.alcn, name, options)
end
find_relative(name, base, options) click to toggle source

@see Base#find_relative

# File lib/webgen/content_processor/sass.rb, line 43
def find_relative(name, base, options)
  _find(base, name, options)
end

Private Instance Methods

_find(base, name, options) click to toggle source

Find the @import-ed name under the given base filename.

Returns a Sass::Engine object if found or nil otherwise.

# File lib/webgen/content_processor/sass.rb, line 72
def _find(base, name, options)
  node, syntax = resolve_node(base, name)
  return unless node

  @context.website.ext.item_tracker.add(@context.dest_node, :node_content, node)
  options[:syntax] = syntax
  options[:filename] = node.alcn
  options[:importer] = self
  ::Sass::Engine.new(node.node_info[:path].data, options)
end
possible_filenames(path) click to toggle source

Return an array of all possible (filename, syntax) pairs for the given path.

# File lib/webgen/content_processor/sass.rb, line 95
def possible_filenames(path)
  dirname, basename = File.split(path)
  basename, ext = basename.scan(/^(.*?)(?:\.(sass|scss))?$/).first
  (ext.nil? ? %w{sass scss} : [ext]).map do |iext|
    [["#{dirname}/_#{basename}.#{iext}", iext.to_sym], ["#{dirname}/#{basename}.#{iext}", iext.to_sym]]
  end.flatten(1)
end
resolve_node(base, path) click to toggle source

Resolve the path using the given base filename.

Returns [node, syntax] if a node was found or nil otherwise

# File lib/webgen/content_processor/sass.rb, line 86
def resolve_node(base, path)
  possible_filenames(path).each do |filename, syntax|
    node = @context.website.tree.resolve_node(Webgen::Path.append(base, filename), nil)
    return [node, syntax] if node
  end
  nil
end