Skip to content

Commit

Permalink
Refactor: Extract Less#imported_filenames_to_items
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdefreyne committed Nov 27, 2016
1 parent e519620 commit 4802597
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions lib/nanoc/filters/less.rb
Expand Up @@ -10,28 +10,9 @@ class Less < Nanoc::Filter
#
# @return [String] The filtered content
def run(content, params = {})
imported_filenames = imported_filenames_from(content)

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

# Convert to items
imported_items = 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

# 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
Expand All @@ -52,6 +33,26 @@ def imported_filenames_from(content)
end
end

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_items = 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

# @param [Pathname] pathname Pathname of the file to find. Can be relative or absolute.
#
# @param [Pathname] root_pathname Directory pathname from which the search will start.
Expand Down

0 comments on commit 4802597

Please sign in to comment.