Class: Nanoc::Filters::Less Private

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

Overview

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.

API:

  • private

Instance Method Summary collapse

Instance Method Details

#find_file(pathname, root_pathname) ⇒ 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.

Returns A string containing the full path if a file is found, otherwise nil.

Parameters:

  • Pathname of the file to find. Can be relative or absolute.

  • Directory pathname from which the search will start.

Returns:

  • A string containing the full path if a file is found, otherwise nil.

API:

  • private



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/nanoc/filters/less.rb', line 62

def find_file(pathname, root_pathname)
  absolute_pathname =
    if pathname.relative?
      root_pathname + pathname
    else
      pathname
    end

  if absolute_pathname.exist?
    absolute_pathname.realpath
  else
    nil
  end
end

#imported_filenames_from(content) ⇒ Object

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.

API:

  • private



28
29
30
31
32
33
34
# File 'lib/nanoc/filters/less.rb', line 28

def imported_filenames_from(content)
  imports = []
  imports.concat(content.scan(/^@import\s+(["'])([^\1]+?)\1;/))
  imports.concat(content.scan(/^@import\s+url\((["']?)([^)]+?)\1\);/))

  imports.map { |i| /\.(less|css)$/.match?(i[1]) ? i[1] : i[1] + '.less' }
end

#imported_filenames_to_items(imported_filenames) ⇒ Object

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.

API:

  • private



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/nanoc/filters/less.rb', line 36

def imported_filenames_to_items(imported_filenames)
  item_dir_path = Pathname.new(@item[:content_filename]).dirname.realpath
  cwd = Pathname.pwd # FIXME: ugly (get site dir instead)

  imported_filenames.map do |filename|
    full_paths = Set.new

    imported_pathname = Pathname.new(filename)
    full_paths << find_file(imported_pathname, item_dir_path)
    full_paths << find_file(imported_pathname, cwd)

    # Find matching item
    @items.find do |i|
      next if i[:content_filename].nil?

      item_path = Pathname.new(i[:content_filename]).realpath
      full_paths.any? { |fp| fp == item_path }
    end
  end.compact
end

#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:

  • The content to filter

Returns:

  • The filtered content

API:

  • private



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/nanoc/filters/less.rb', line 16

def run(content, params = {})
  # Create dependencies
  imported_filenames = imported_filenames_from(content)
  imported_items = imported_filenames_to_items(imported_filenames)
  depend_on(imported_items)

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