2015-11-29 03:02:45 +01:00
|
|
|
require 'set'
|
|
|
|
require 'middleman-core/contracts'
|
|
|
|
|
|
|
|
module Middleman
|
|
|
|
module Sitemap
|
|
|
|
module Extensions
|
2016-01-03 02:37:11 +01:00
|
|
|
class Import < ConfigExtension
|
2015-11-29 03:02:45 +01:00
|
|
|
self.resource_list_manipulator_priority = 1
|
|
|
|
|
2016-01-12 19:35:12 +01:00
|
|
|
# Expose methods
|
|
|
|
expose_to_config :import_file, :import_path
|
2015-11-29 03:02:45 +01:00
|
|
|
|
2016-01-03 02:37:11 +01:00
|
|
|
ImportFileDescriptor = Struct.new(:from, :to) do
|
|
|
|
def execute_descriptor(app, resources)
|
|
|
|
resources + [
|
|
|
|
::Middleman::Sitemap::Resource.new(app.sitemap, to, from)
|
|
|
|
]
|
|
|
|
end
|
2015-11-29 03:02:45 +01:00
|
|
|
end
|
|
|
|
|
2016-01-03 02:37:11 +01:00
|
|
|
ImportPathDescriptor = Struct.new(:from, :renameProc) do
|
|
|
|
def execute_descriptor(app, resources)
|
|
|
|
resources + ::Middleman::Util.glob_directory(File.join(from, '**/*'))
|
|
|
|
.reject { |path| File.directory?(path) }
|
|
|
|
.map do |path|
|
2016-01-14 20:21:42 +01:00
|
|
|
target_path = Pathname(path).relative_path_from(Pathname(from).parent).to_s
|
2015-11-29 03:02:45 +01:00
|
|
|
|
2016-01-14 20:21:42 +01:00
|
|
|
::Middleman::Sitemap::Resource.new(
|
|
|
|
app.sitemap,
|
|
|
|
renameProc.call(target_path, path),
|
|
|
|
path
|
|
|
|
)
|
|
|
|
end
|
2016-01-03 02:37:11 +01:00
|
|
|
end
|
2015-11-29 03:02:45 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# Import an external file into `source`
|
|
|
|
# @param [String] from The original path.
|
|
|
|
# @param [String] to The new path.
|
2016-01-03 02:37:11 +01:00
|
|
|
# @return [void]
|
2015-11-29 03:02:45 +01:00
|
|
|
Contract String, String => ImportFileDescriptor
|
2016-01-12 19:35:12 +01:00
|
|
|
def import_file(from, to)
|
2015-11-29 03:02:45 +01:00
|
|
|
ImportFileDescriptor.new(
|
|
|
|
File.expand_path(from, @app.root),
|
|
|
|
::Middleman::Util.normalize_path(to)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Import an external glob into `source`
|
|
|
|
# @param [String] from The original path.
|
|
|
|
# @param [Proc] block Renaming method
|
|
|
|
# @return [void]
|
2016-01-03 02:37:11 +01:00
|
|
|
Contract String, Maybe[Proc] => ImportPathDescriptor
|
2016-01-12 19:35:12 +01:00
|
|
|
def import_path(from, &block)
|
2015-11-29 03:02:45 +01:00
|
|
|
ImportPathDescriptor.new(
|
|
|
|
from,
|
2016-01-03 02:37:11 +01:00
|
|
|
block_given? ? block : proc { |path| path }
|
2015-11-29 03:02:45 +01:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|