import_file and import_path APIs. Addresses #1632
This commit is contained in:
parent
16b997498b
commit
e47b0ae29a
104 changed files with 19326 additions and 8 deletions
|
@ -94,7 +94,7 @@ module Middleman
|
|||
# @return [void]
|
||||
Contract IsA['Middleman::SourceFile'] => Any
|
||||
def touch_file(file)
|
||||
data_path = file[:relative_path]
|
||||
data_path = file[:full_path]
|
||||
extension = File.extname(data_path)
|
||||
basename = File.basename(data_path, extension)
|
||||
|
||||
|
@ -124,7 +124,7 @@ module Middleman
|
|||
# @return [void]
|
||||
Contract IsA['Middleman::SourceFile'] => Any
|
||||
def remove_file(file)
|
||||
data_path = file[:relative_path]
|
||||
data_path = file[:full_path]
|
||||
extension = File.extname(data_path)
|
||||
basename = File.basename(data_path, extension)
|
||||
|
||||
|
|
|
@ -97,7 +97,7 @@ module Middleman
|
|||
Contract String
|
||||
def template_data_for_file
|
||||
if @app.extensions[:front_matter]
|
||||
@app.extensions[:front_matter].template_data_for_file(@path) || ''
|
||||
@app.extensions[:front_matter].template_data_for_file(@path) || File.read(@path)
|
||||
else
|
||||
file = @app.files.find(:source, @path)
|
||||
file.read if file
|
||||
|
|
|
@ -24,7 +24,7 @@ module Middleman
|
|||
|
||||
resources.each do |resource|
|
||||
next if resource.file_descriptor.nil?
|
||||
next unless resource.file_descriptor[:relative_path].to_s =~ %r{\.liquid$}
|
||||
next unless resource.file_descriptor[:full_path].to_s =~ %r{\.liquid$}
|
||||
|
||||
# Convert data object into a hash for liquid
|
||||
resource.add_metadata locals: {
|
||||
|
|
108
middleman-core/lib/middleman-core/sitemap/extensions/import.rb
Normal file
108
middleman-core/lib/middleman-core/sitemap/extensions/import.rb
Normal file
|
@ -0,0 +1,108 @@
|
|||
require 'set'
|
||||
require 'middleman-core/contracts'
|
||||
|
||||
module Middleman
|
||||
module Sitemap
|
||||
module Extensions
|
||||
class Import < Extension
|
||||
self.resource_list_manipulator_priority = 1
|
||||
|
||||
ImportFileDescriptor = Struct.new(:from, :to)
|
||||
ImportPathDescriptor = Struct.new(:from, :renameProc)
|
||||
|
||||
# Expose `create_import_file` to config as `import_file`
|
||||
expose_to_config import_file: :create_import_file
|
||||
|
||||
# Expose `create_import_path` to config as `import_path`
|
||||
expose_to_config import_path: :create_import_path
|
||||
|
||||
def initialize(app, config={}, &block)
|
||||
super
|
||||
|
||||
@import_file_configs = Set.new
|
||||
@import_path_configs = Set.new
|
||||
end
|
||||
|
||||
def after_configuration
|
||||
::Middleman::CoreExtensions::Collections::StepContext.add_to_context(:import_file, &method(:create_import_file))
|
||||
::Middleman::CoreExtensions::Collections::StepContext.add_to_context(:import_path, &method(:create_import_path))
|
||||
end
|
||||
|
||||
# Import an external file into `source`
|
||||
# @param [String] from The original path.
|
||||
# @param [String] to The new path.
|
||||
# @return [void]
|
||||
Contract String, String => Any
|
||||
def create_import_file(from, to)
|
||||
@import_file_configs << create_anonymous_import_file(from, to)
|
||||
@app.sitemap.rebuild_resource_list!(:added_import_file)
|
||||
end
|
||||
|
||||
# Import an external file into `source`
|
||||
# @param [String] from The original path.
|
||||
# @param [String] to The new path.
|
||||
# @return [ImportFileDescriptor]
|
||||
Contract String, String => ImportFileDescriptor
|
||||
def create_anonymous_import_file(from, to)
|
||||
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]
|
||||
Contract String, Maybe[Proc] => Any
|
||||
def create_import_path(from, &block)
|
||||
rename_proc = block_given? ? block : proc { |path| path }
|
||||
@import_path_configs << create_anonymous_import_path(from, rename_proc)
|
||||
@app.sitemap.rebuild_resource_list!(:added_import_path)
|
||||
end
|
||||
|
||||
# Import an external glob into `source`
|
||||
# @param [String] from The original path.
|
||||
# @param [Proc] block Renaming method
|
||||
# @return [ImportPathDescriptor]
|
||||
Contract String, Proc => ImportPathDescriptor
|
||||
def create_anonymous_import_path(from, block)
|
||||
ImportPathDescriptor.new(
|
||||
from,
|
||||
block
|
||||
)
|
||||
end
|
||||
|
||||
Contract IsA['Middleman::SourceFile'] => Bool
|
||||
def ignored?(file)
|
||||
@app.config[:ignored_sitemap_matchers].any? { |_, fn| fn.call(file, @app) }
|
||||
end
|
||||
|
||||
# Update the main sitemap resource list
|
||||
# @return Array<Middleman::Sitemap::Resource>
|
||||
Contract ResourceList => ResourceList
|
||||
def manipulate_resource_list(resources)
|
||||
resources + @import_file_configs.map { |c|
|
||||
::Middleman::Sitemap::Resource.new(
|
||||
@app.sitemap,
|
||||
c[:to],
|
||||
c[:from]
|
||||
)
|
||||
} + @import_path_configs.flat_map { |c|
|
||||
::Middleman::Util.glob_directory(File.join(c[:from], '**/*'))
|
||||
.reject { |path| File.directory?(path) }
|
||||
.map do |path|
|
||||
target_path = Pathname(path).relative_path_from(Pathname(c[:from]).parent).to_s
|
||||
|
||||
::Middleman::Sitemap::Resource.new(
|
||||
@app.sitemap,
|
||||
c[:renameProc].call(target_path, path),
|
||||
path
|
||||
)
|
||||
end
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -28,7 +28,7 @@ module Middleman
|
|||
# @param [String] from The original path.
|
||||
# @param [String] to The new path.
|
||||
# @return [void]
|
||||
Contract String, String, Maybe[Hash] => Any
|
||||
Contract String, String => Any
|
||||
def create_move_file(from, to)
|
||||
@move_configs << create_anonymous_move(from, to)
|
||||
@app.sitemap.rebuild_resource_list!(:added_move_file)
|
||||
|
|
|
@ -15,6 +15,12 @@ Middleman::Extensions.register :sitemap_ondisk, auto_activate: :before_configura
|
|||
Middleman::Sitemap::Extensions::OnDisk
|
||||
end
|
||||
|
||||
# Files on Disk (outside the project root)
|
||||
Middleman::Extensions.register :sitemap_import, auto_activate: :before_configuration do
|
||||
require 'middleman-core/sitemap/extensions/import'
|
||||
Middleman::Sitemap::Extensions::Import
|
||||
end
|
||||
|
||||
# Endpoints
|
||||
Middleman::Extensions.register :sitemap_endpoint, auto_activate: :before_configuration do
|
||||
require 'middleman-core/sitemap/extensions/request_endpoints'
|
||||
|
|
|
@ -237,7 +237,6 @@ module Middleman
|
|||
related_updates = ::Middleman::Util.find_related_files(app, (updated_paths + removed_paths)).select(&method(:valid?))
|
||||
|
||||
related_updates.each do |f|
|
||||
add_file_to_cache(f)
|
||||
logger.debug "== Possible Change (#{f[:types].inspect}): #{f[:relative_path]}"
|
||||
end
|
||||
|
||||
|
|
|
@ -502,8 +502,8 @@ module Middleman
|
|||
|
||||
all_extensions.uniq!
|
||||
|
||||
app.sitemap.resources.select { |r|
|
||||
local_extensions = collect_extensions(r.file_descriptor[:relative_path].to_s)
|
||||
app.sitemap.resources.select(&:file_descriptor).select { |r|
|
||||
local_extensions = collect_extensions(r.file_descriptor[:full_path].to_s)
|
||||
|
||||
if (local_extensions & sass_type_aliasing).length > 0
|
||||
local_extensions |= sass_type_aliasing
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue