Skip to content

Instantly share code, notes, and snippets.

@Arcovion
Created May 8, 2014 12:33
Show Gist options
  • Select an option

  • Save Arcovion/2586eb92389967715da4 to your computer and use it in GitHub Desktop.

Select an option

Save Arcovion/2586eb92389967715da4 to your computer and use it in GitHub Desktop.
Middleman extension to manage multiple directories
class Middleman::Extensions::DirManager < Middleman::Extension
register :dir_manager
option :dirs, {source: 'pages', destination: '', add: [], remove: ['', 'pages']}
def manipulate_resource_list resources
[options.dirs].flatten.each do |opts|
# Remove pages if specified
opts[:remove].each do |dir|
resources.reject! do |page|
Dir.glob(normalise(dir) + '/*', File::FNM_DOTMATCH).include? page.source_file
end
end
Array(opts[:add]).append(opts[:source]).each do |directory|
# Normalise input
directory = normalise directory
destination = normalise opts[:destination]
# Add files outside the root, else skip as there would be a StackError
unless directory.include? app.root
app.files.reload_path Pathname(directory).relative_path_from Pathname app.root
end
# Build an array of resources from the directory
pages = Dir.glob(directory + '/**/*', File::FNM_DOTMATCH).map do |sourcepath|
next if File.directory? sourcepath
buildpath = app.sitemap.extensionless_path sourcepath.sub directory + ?/, ''
unless app.source_dir == destination
buildpath = File.join File.basename(destination), buildpath
end
Middleman::Sitemap::Resource.new app.sitemap, buildpath, sourcepath
end
# Add the new pages to the resource list
resources.concat pages.compact
end
end
resources
end
private
def normalise directory
if Pathname(directory).relative?
File.expand_path File.join app.source_dir, directory
else
File.expand_path directory
end
end
end
@chrishough
Copy link
Copy Markdown

I just had to use this again man, well done!

@chrishough
Copy link
Copy Markdown

have you converted this to work with v4?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment