diff --git a/middleman-core/lib/middleman-core/contracts.rb b/middleman-core/lib/middleman-core/contracts.rb index e99d0d59..55aa0aad 100644 --- a/middleman-core/lib/middleman-core/contracts.rb +++ b/middleman-core/lib/middleman-core/contracts.rb @@ -1,23 +1,112 @@ -require 'contracts' -require 'hamster' +if ENV['CONTRACTS'] != 'false' + require 'contracts' + require 'hamster' -module Contracts - class IsA - def self.[](val) - @lookup ||= {} - @lookup[val] ||= new(val) + module Contracts + class IsA + def self.[](val) + @lookup ||= {} + @lookup[val] ||= new(val) + end + + def initialize(val) + @val = val + end + + def valid?(val) + val.is_a? @val.constantize + end end - def initialize(val) - @val = val + VectorOf = Contracts::CollectionOf::Factory.new(::Hamster::Vector) + ResourceList = Contracts::ArrayOf[IsA['Middleman::Sitemap::Resource']] + end +else + module Contracts + def self.included(base) + base.extend self end - def valid?(val) - val.is_a? @val.constantize + # rubocop:disable MethodName + def Contract(*) + end + + class Callable + def self.[](*) + end + end + + class Bool + end + + class Num + end + + class Pos + end + + class Neg + end + + class Any + end + + class None + end + + class Or < Callable + end + + class Xor < Callable + end + + class And < Callable + end + + class Not < Callable + end + + class RespondTo < Callable + end + + class Send < Callable + end + + class Exactly < Callable + end + + class ArrayOf < Callable + end + + class ResourceList < Callable + end + + class Args < Callable + end + + class HashOf < Callable + end + + class Bool + end + + class Maybe < Callable + end + + class IsA < Callable + end + + class SetOf < Callable + end + + class Frozen < Callable + end + + class VectorOf < Callable end end +end - VectorOf = ::Contracts::CollectionOf::Factory.new(::Hamster::Vector) - ResourceList = ::Contracts::ArrayOf[IsA['Middleman::Sitemap::Resource']] +module Contracts PATH_MATCHER = Or[String, RespondTo[:match], RespondTo[:call], RespondTo[:to_s]] end diff --git a/middleman-core/lib/middleman-core/core_extensions/front_matter.rb b/middleman-core/lib/middleman-core/core_extensions/front_matter.rb index 8809004f..a59f7bb1 100644 --- a/middleman-core/lib/middleman-core/core_extensions/front_matter.rb +++ b/middleman-core/lib/middleman-core/core_extensions/front_matter.rb @@ -36,6 +36,7 @@ module Middleman::CoreExtensions resources.each do |resource| next if resource.binary? next if resource.file_descriptor.nil? + next if resource.file_descriptor[:types].include?(:no_frontmatter) fmdata = data(resource.file_descriptor[:full_path].to_s).first.dup @@ -74,6 +75,11 @@ module Middleman::CoreExtensions return @cache[file[:full_path]] if @cache.key?(file[:full_path]) + if file[:types].include?(:no_frontmatter) + $stderr.puts file[:relative_path].to_s + require 'pry' + end + @cache[file[:full_path]] = ::Middleman::Util::Data.parse( file, app.config[:frontmatter_delims] diff --git a/middleman-core/lib/middleman-core/extensions/external_pipeline.rb b/middleman-core/lib/middleman-core/extensions/external_pipeline.rb index 44a4e6b3..6b51d944 100644 --- a/middleman-core/lib/middleman-core/extensions/external_pipeline.rb +++ b/middleman-core/lib/middleman-core/extensions/external_pipeline.rb @@ -14,7 +14,8 @@ class Middleman::Extensions::ExternalPipeline < ::Middleman::Extension @watcher = app.files.watch :source, path: File.expand_path(options[:source], app.root), - latency: options[:latency] + latency: options[:latency], + frontmatter: false logger.info "== Executing: `#{options[:command]}`" diff --git a/middleman-core/lib/middleman-core/file_renderer.rb b/middleman-core/lib/middleman-core/file_renderer.rb index 3f385ed6..67f9411f 100644 --- a/middleman-core/lib/middleman-core/file_renderer.rb +++ b/middleman-core/lib/middleman-core/file_renderer.rb @@ -96,12 +96,13 @@ module Middleman # @return [String] Contract String def template_data_for_file - if @app.extensions[:front_matter] + file = @app.files.find(:source, @path) + + if @app.extensions[:front_matter] || (file && !file[:types].include?(:no_frontmatter)) result = @app.extensions[:front_matter].template_data_for_file(@path) return result unless result.nil? end - file = @app.files.find(:source, @path) file ? file.read : File.read(@path) end diff --git a/middleman-core/lib/middleman-core/sitemap/extensions/ignores.rb b/middleman-core/lib/middleman-core/sitemap/extensions/ignores.rb index beba7d7a..dfa80304 100644 --- a/middleman-core/lib/middleman-core/sitemap/extensions/ignores.rb +++ b/middleman-core/lib/middleman-core/sitemap/extensions/ignores.rb @@ -11,7 +11,7 @@ module Middleman # Ignore a path or add an ignore callback # @param [String, Regexp] path Path glob expression, or path regex # @return [IgnoreDescriptor] - Contract Maybe[Or[String, Regexp]], Maybe[Proc] => RespondTo[:execute_descriptor] + Contract Or[String, Regexp, Proc] => RespondTo[:execute_descriptor] def ignore(path=nil, &block) @app.sitemap.invalidate_resources_not_ignored_cache! IgnoreDescriptor.new(path, block) @@ -49,7 +49,7 @@ module Middleman else match_path == path_clean end - elsif block_given? + elsif block block.call(match_path) end end diff --git a/middleman-core/lib/middleman-core/sitemap/extensions/import.rb b/middleman-core/lib/middleman-core/sitemap/extensions/import.rb index eba04a44..239aa917 100644 --- a/middleman-core/lib/middleman-core/sitemap/extensions/import.rb +++ b/middleman-core/lib/middleman-core/sitemap/extensions/import.rb @@ -12,8 +12,10 @@ module Middleman ImportFileDescriptor = Struct.new(:from, :to) do def execute_descriptor(app, resources) + source = ::Middleman::SourceFile.new(Pathname(from).relative_path_from(app.source_dir), Pathname(from), app.source_dir, Set.new([:source, :binary]), 0) + resources + [ - ::Middleman::Sitemap::Resource.new(app.sitemap, to, from) + ::Middleman::Sitemap::Resource.new(app.sitemap, to, source) ] end end diff --git a/middleman-core/lib/middleman-core/sitemap/resource.rb b/middleman-core/lib/middleman-core/sitemap/resource.rb index adb36cfe..b1f614e4 100644 --- a/middleman-core/lib/middleman-core/sitemap/resource.rb +++ b/middleman-core/lib/middleman-core/sitemap/resource.rb @@ -161,7 +161,7 @@ module Middleman # @return [Boolean] Contract Bool def binary? - !file_descriptor.nil? && ::Middleman::Util.binary?(file_descriptor[:full_path].to_s) + !file_descriptor.nil? && (file_descriptor[:types].include?(:binary) || ::Middleman::Util.binary?(file_descriptor[:full_path].to_s)) end # Ignore a resource directly, without going through the whole diff --git a/middleman-core/lib/middleman-core/sources/source_watcher.rb b/middleman-core/lib/middleman-core/sources/source_watcher.rb index 70f749b0..328afb9e 100644 --- a/middleman-core/lib/middleman-core/sources/source_watcher.rb +++ b/middleman-core/lib/middleman-core/sources/source_watcher.rb @@ -66,6 +66,8 @@ module Middleman @files = {} @extensionless_files = {} + @frontmatter = options.fetch(:frontmatter, true) + @binary = options.fetch(:binary, false) @validator = options.fetch(:validator, proc { true }) @ignored = options.fetch(:ignored, proc { false }) @only = Array(options.fetch(:only, [])) @@ -181,7 +183,7 @@ module Middleman Contract ArrayOf[Pathname] def find_new_files! new_files = ::Middleman::Util.all_files_under(@directory.to_s) - .reject { |p| @files.key?(p) } + .reject { |p| @files.key?(p) } update(new_files, []) @@ -278,6 +280,8 @@ module Middleman Contract Pathname, Pathname, Symbol, Maybe[String] => ::Middleman::SourceFile def path_to_source_file(path, directory, type, destination_dir) types = Set.new([type]) + types << :no_frontmatter unless @frontmatter + types << :@binary if @binary relative_path = path.relative_path_from(directory) relative_path = File.join(destination_dir, relative_path) if destination_dir diff --git a/middleman-core/lib/middleman-core/util/data.rb b/middleman-core/lib/middleman-core/util/data.rb index 246ae2e1..c18e6975 100644 --- a/middleman-core/lib/middleman-core/util/data.rb +++ b/middleman-core/lib/middleman-core/util/data.rb @@ -18,7 +18,7 @@ module Middleman Contract IsA['Middleman::SourceFile'], Maybe[Symbol] => [Hash, Maybe[String]] def parse(file, frontmatter_delims, known_type=nil) full_path = file[:full_path] - return [{}, nil] if ::Middleman::Util.binary?(full_path) + return [{}, nil] if ::Middleman::Util.binary?(full_path) || file[:types].include?(:binary) # Avoid weird race condition when a file is renamed begin