Class: Nanoc::Filters::Less Private

Inherits:
Nanoc::Filter show all
Defined in:
lib/nanoc/filters/less.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Constant Summary

Constants inherited from Nanoc::Filter

Nanoc::Filter::TMP_BINARY_ITEMS_DIR

Instance Attribute Summary

Attributes inherited from Nanoc::Filter

#assigns

Instance Method Summary collapse

Methods inherited from Nanoc::Filter

#depend_on, #filename, from_binary?, #initialize, #output_filename, requires, setup, #setup_and_run, to_binary?, type

Methods included from Int::PluginRegistry::PluginMethods

#all, #identifier, #identifiers, #named, #register

Methods inherited from Int::Context

#get_binding, #initialize

Constructor Details

This class inherits a constructor from Nanoc::Filter

Instance Method Details

#run(content, params = {}) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Runs the content through [LESS](lesscss.org/). This method takes no options.

Parameters:

  • content (String)

    The content to filter

Returns:

  • (String)

    The filtered content



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/nanoc/filters/less.rb', line 12

def run(content, params = {})
  # Find imports (hacky)
  imports = []
  imports.concat(content.scan(/^@import\s+(["'])([^\1]+?)\1;/))
  imports.concat(content.scan(/^@import\s+url\((["']?)([^)]+?)\1\);/))
  imported_filenames = imports.map do |i|
    i[1] =~ /\.(less|css)$/ ? i[1] : i[1] + '.less'
  end

  # Convert to items
  imported_items = imported_filenames.map do |filename|
    # Find directory for this item
    current_dir_pathname = Pathname.new(@item[:content_filename]).dirname.realpath

    # Find absolute pathname for imported item
    imported_pathname    = Pathname.new(filename)
    if imported_pathname.relative?
      imported_pathname = current_dir_pathname + imported_pathname
    end
    next unless imported_pathname.exist?
    imported_filename = imported_pathname.realpath

    # Find matching item
    @items.find do |i|
      next if i[:content_filename].nil?
      Pathname.new(i[:content_filename]).realpath == imported_filename
    end
  end.compact

  # Create dependencies
  depend_on(imported_items)

  # Add filename to load path
  paths = [File.dirname(@item[:content_filename])]
  parser = ::Less::Parser.new(paths: paths)
  parser.parse(content).to_css params
end