Class: Nanoc::Filters::Less Private

Inherits:
Nanoc::Filter
  • Object
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.

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 (Pathname)

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

  • root_pathname (Pathname)

    Directory pathname from which the search will start.

Returns:

  • (String, nil)

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



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

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.



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

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.



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

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:

  • content (String)

    The content to filter

Returns:

  • (String)

    The filtered content



16
17
18
19
20
21
22
23
24
25
26
27
28
# 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])]
  on_main_fiber do
    parser = ::Less::Parser.new(paths:)
    parser.parse(content).to_css(params)
  end
end