From 441dc2faa68822ed06f09cdc9105a7256a3da7d4 Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Wed, 9 Sep 2015 11:55:56 -0700 Subject: [PATCH] Revert breaking resource source_file api. For #1595 --- middleman-core/features/collections.feature | 2 +- .../frontmatter-neighbor-app/config.rb | 4 +- .../config.rb | 6 +-- .../more-traversal-app/source/layout.erb | 2 +- .../fixtures/traversal-app/source/layout.erb | 2 +- middleman-core/lib/middleman-core/builder.rb | 2 +- .../core_extensions/front_matter.rb | 4 +- .../meta_pages/sitemap_resource.rb | 2 +- middleman-core/lib/middleman-core/rack.rb | 2 +- .../lib/middleman-core/renderers/liquid.rb | 4 +- .../sitemap/extensions/proxies.rb | 6 +-- .../lib/middleman-core/sitemap/resource.rb | 37 +++++++++++-------- .../lib/middleman-core/template_context.rb | 4 +- 13 files changed, 42 insertions(+), 35 deletions(-) diff --git a/middleman-core/features/collections.feature b/middleman-core/features/collections.feature index 30f32691..a745a5a1 100644 --- a/middleman-core/features/collections.feature +++ b/middleman-core/features/collections.feature @@ -98,7 +98,7 @@ Feature: Collections And a file named "source/index.html.erb" with: """ <% collection(:articles).each do |article| %> - Article: <%= article.data.title || article.source_file[:relative_path] %> + Article: <%= article.data.title || article.file_descriptor[:relative_path] %> <% end %> """ Given the Server is running at "collections-app" diff --git a/middleman-core/fixtures/frontmatter-neighbor-app/config.rb b/middleman-core/fixtures/frontmatter-neighbor-app/config.rb index babfbc2c..b9f82b36 100644 --- a/middleman-core/fixtures/frontmatter-neighbor-app/config.rb +++ b/middleman-core/fixtures/frontmatter-neighbor-app/config.rb @@ -7,9 +7,9 @@ class NeighborFrontmatter < ::Middleman::Extension def manipulate_resource_list(resources) resources.each do |resource| - next unless resource.source_file + next unless resource.file_descriptor - neighbor = "#{resource.source_file[:relative_path]}.frontmatter" + neighbor = "#{resource.file_descriptor[:relative_path]}.frontmatter" file = app.files.find(:source, neighbor) diff --git a/middleman-core/fixtures/frontmatter-settings-neighbor-app/config.rb b/middleman-core/fixtures/frontmatter-settings-neighbor-app/config.rb index 1fc88608..93cd8f1f 100644 --- a/middleman-core/fixtures/frontmatter-settings-neighbor-app/config.rb +++ b/middleman-core/fixtures/frontmatter-settings-neighbor-app/config.rb @@ -12,12 +12,12 @@ class NeighborFrontmatter < ::Middleman::Extension def manipulate_resource_list(resources) resources.each do |resource| - next unless resource.source_file - next if resource.source_file[:relative_path].extname == '.frontmatter' + next unless resource.file_descriptor + next if resource.file_descriptor[:relative_path].extname == '.frontmatter' [ "#{resource.url.sub(/^\//, '')}.frontmatter", - "#{resource.source_file[:relative_path]}.frontmatter" + "#{resource.file_descriptor[:relative_path]}.frontmatter" ].each do |n| file = app.files.find(:source, n) apply_neighbor_data(resource, file) if file diff --git a/middleman-core/fixtures/more-traversal-app/source/layout.erb b/middleman-core/fixtures/more-traversal-app/source/layout.erb index 25411d39..f0aea54a 100644 --- a/middleman-core/fixtures/more-traversal-app/source/layout.erb +++ b/middleman-core/fixtures/more-traversal-app/source/layout.erb @@ -1,6 +1,6 @@ Path: <%= current_page.path %> -Source: <%= current_page.source_file[:full_path].sub(root + "/", "") %> +Source: <%= current_page.file_descriptor[:full_path].sub(root + "/", "") %> <% if current_page.parent %> Parent: <%= current_page.parent.path %> diff --git a/middleman-core/fixtures/traversal-app/source/layout.erb b/middleman-core/fixtures/traversal-app/source/layout.erb index 25411d39..f0aea54a 100644 --- a/middleman-core/fixtures/traversal-app/source/layout.erb +++ b/middleman-core/fixtures/traversal-app/source/layout.erb @@ -1,6 +1,6 @@ Path: <%= current_page.path %> -Source: <%= current_page.source_file[:full_path].sub(root + "/", "") %> +Source: <%= current_page.file_descriptor[:full_path].sub(root + "/", "") %> <% if current_page.parent %> Parent: <%= current_page.parent.path %> diff --git a/middleman-core/lib/middleman-core/builder.rb b/middleman-core/lib/middleman-core/builder.rb index 0d0966c4..e386c2d0 100644 --- a/middleman-core/lib/middleman-core/builder.rb +++ b/middleman-core/lib/middleman-core/builder.rb @@ -169,7 +169,7 @@ module Middleman begin if resource.binary? - export_file!(output_file, resource.source_file[:full_path]) + export_file!(output_file, resource.file_descriptor[:full_path]) else response = @rack.get(URI.escape(resource.request_path)) 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 0e26259a..03d2f431 100644 --- a/middleman-core/lib/middleman-core/core_extensions/front_matter.rb +++ b/middleman-core/lib/middleman-core/core_extensions/front_matter.rb @@ -28,9 +28,9 @@ module Middleman::CoreExtensions Contract ResourceList => ResourceList def manipulate_resource_list(resources) resources.each do |resource| - next if resource.source_file.nil? + next if resource.file_descriptor.nil? - fmdata = data(resource.source_file[:full_path].to_s).first.dup + fmdata = data(resource.file_descriptor[:full_path].to_s).first.dup # Copy over special options # TODO: Should we make people put these under "options" instead of having diff --git a/middleman-core/lib/middleman-core/meta_pages/sitemap_resource.rb b/middleman-core/lib/middleman-core/meta_pages/sitemap_resource.rb index a42f9157..d746be95 100644 --- a/middleman-core/lib/middleman-core/meta_pages/sitemap_resource.rb +++ b/middleman-core/lib/middleman-core/meta_pages/sitemap_resource.rb @@ -39,7 +39,7 @@ module Middleman build_path = 'Not built' if ignored? props['Build Path'] = build_path if @resource.path != build_path props['URL'] = content_tag(:a, @resource.url, href: @resource.url) unless ignored? - props['Source File'] = @resource.source_file ? @resource.source_file[:full_path].to_s.sub(/^#{Regexp.escape(ENV['MM_ROOT'] + '/')}/, '') : 'Dynamic' + props['Source File'] = @resource.file_descriptor ? @resource.file_descriptor[:full_path].to_s.sub(/^#{Regexp.escape(ENV['MM_ROOT'] + '/')}/, '') : 'Dynamic' data = @resource.data props['Data'] = data.inspect unless data.empty? diff --git a/middleman-core/lib/middleman-core/rack.rb b/middleman-core/lib/middleman-core/rack.rb index 583056de..a6743c59 100644 --- a/middleman-core/lib/middleman-core/rack.rb +++ b/middleman-core/lib/middleman-core/rack.rb @@ -126,7 +126,7 @@ module Middleman # Immediately send static file def send_file(resource, env) file = ::Rack::File.new nil - file.path = resource.source_file[:full_path] + file.path = resource.file_descriptor[:full_path] response = file.serving(env) status = response[0] response[1]['Content-Encoding'] = 'gzip' if %w(.svgz .gz).include?(resource.ext) diff --git a/middleman-core/lib/middleman-core/renderers/liquid.rb b/middleman-core/lib/middleman-core/renderers/liquid.rb index e09306b4..daa7d220 100644 --- a/middleman-core/lib/middleman-core/renderers/liquid.rb +++ b/middleman-core/lib/middleman-core/renderers/liquid.rb @@ -23,8 +23,8 @@ module Middleman return resources unless app.extensions[:data] resources.each do |resource| - next if resource.source_file.nil? - next unless resource.source_file[:relative_path].to_s =~ %r{\.liquid$} + next if resource.file_descriptor.nil? + next unless resource.file_descriptor[:relative_path].to_s =~ %r{\.liquid$} # Convert data object into a hash for liquid resource.add_metadata locals: { diff --git a/middleman-core/lib/middleman-core/sitemap/extensions/proxies.rb b/middleman-core/lib/middleman-core/sitemap/extensions/proxies.rb index 0ad0a512..78d6e17a 100644 --- a/middleman-core/lib/middleman-core/sitemap/extensions/proxies.rb +++ b/middleman-core/lib/middleman-core/sitemap/extensions/proxies.rb @@ -87,7 +87,7 @@ module Middleman # Initialize resource with parent store and URL # @param [Middleman::Sitemap::Store] store # @param [String] path - # @param [String] source_file + # @param [String] target def initialize(store, path, target) super(store, path) @@ -115,8 +115,8 @@ module Middleman end Contract IsA['Middleman::SourceFile'] - def source_file - target_resource.source_file + def file_descriptor + target_resource.file_descriptor end Contract Maybe[String] diff --git a/middleman-core/lib/middleman-core/sitemap/resource.rb b/middleman-core/lib/middleman-core/sitemap/resource.rb index 56768900..14b43fd7 100644 --- a/middleman-core/lib/middleman-core/sitemap/resource.rb +++ b/middleman-core/lib/middleman-core/sitemap/resource.rb @@ -24,7 +24,7 @@ module Middleman # The on-disk source file for this resource, if there is one # @return [String] Contract Maybe[IsA['Middleman::SourceFile']] - attr_reader :source_file + attr_reader :file_descriptor # The path to use when requesting this resource. Normally it's # the same as {#destination_path} but it can be overridden in subclasses. @@ -41,21 +41,21 @@ module Middleman # Initialize resource with parent store and URL # @param [Middleman::Sitemap::Store] store # @param [String] path - # @param [String] source_file + # @param [String] source Contract IsA['Middleman::Sitemap::Store'], String, Maybe[Or[IsA['Middleman::SourceFile'], String]] => Any - def initialize(store, path, source_file=nil) + def initialize(store, path, source=nil) @store = store @app = @store.app @path = path - if source_file && source_file.is_a?(String) - source_file = Pathname(source_file) + if source && source.is_a?(String) + source = Pathname(source) end - if source_file && source_file.is_a?(Pathname) - @source_file = ::Middleman::SourceFile.new(source_file.relative_path_from(@app.source_dir), source_file, @app.source_dir, Set.new([:source])) + if source && source.is_a?(Pathname) + @file_descriptor = ::Middleman::SourceFile.new(source.relative_path_from(@app.source_dir), source, @app.source_dir, Set.new([:source])) else - @source_file = source_file + @file_descriptor = source end @destination_path = @path @@ -71,8 +71,15 @@ module Middleman # @return [Boolean] Contract Bool def template? - return false if source_file.nil? - !::Tilt[source_file[:full_path].to_s].nil? + return false if file_descriptor.nil? + !::Tilt[file_descriptor[:full_path].to_s].nil? + end + + # Backwards compatible method for turning descriptor into a string. + # @return [String] + Contract String + def source_file + file_descriptor && file_descriptor[:full_path].to_s end # Merge in new metadata specific to this resource. @@ -119,9 +126,9 @@ module Middleman # @return [String] Contract Hash, Hash => String def render(opts={}, locs={}) - return ::Middleman::FileRenderer.new(@app, source_file[:full_path].to_s).template_data_for_file unless template? + return ::Middleman::FileRenderer.new(@app, file_descriptor[:full_path].to_s).template_data_for_file unless template? - ::Middleman::Util.instrument 'render.resource', path: source_file[:full_path].to_s, destination_path: destination_path do + ::Middleman::Util.instrument 'render.resource', path: file_descriptor[:full_path].to_s, destination_path: destination_path do md = metadata opts = md[:options].deep_merge(opts) locs = md[:locals].deep_merge(locs) @@ -132,7 +139,7 @@ module Middleman opts[:layout] = false if %w(.js .json .css .txt).include?(ext) end - renderer = ::Middleman::TemplateRenderer.new(@app, source_file[:full_path].to_s) + renderer = ::Middleman::TemplateRenderer.new(@app, file_descriptor[:full_path].to_s) renderer.render(locs, opts) end end @@ -155,7 +162,7 @@ module Middleman # @return [Boolean] Contract Bool def binary? - !source_file.nil? && ::Middleman::Util.binary?(source_file[:full_path].to_s) + !file_descriptor.nil? && ::Middleman::Util.binary?(file_descriptor[:full_path].to_s) end # Ignore a resource directly, without going through the whole @@ -174,7 +181,7 @@ module Middleman # Ignore based on the source path (without template extensions) return true if @app.sitemap.ignored?(path) # This allows files to be ignored by their source file name (with template extensions) - if !self.is_a?(ProxyResource) && source_file && @app.sitemap.ignored?(source_file[:relative_path].to_s) + if !self.is_a?(ProxyResource) && file_descriptor && @app.sitemap.ignored?(file_descriptor[:relative_path].to_s) true else false diff --git a/middleman-core/lib/middleman-core/template_context.rb b/middleman-core/lib/middleman-core/template_context.rb index 368742b4..48498d85 100644 --- a/middleman-core/lib/middleman-core/template_context.rb +++ b/middleman-core/lib/middleman-core/template_context.rb @@ -128,7 +128,7 @@ module Middleman return unless resource = sitemap.find_resource_by_destination_path(current_path) # Look for partials relative to the current path - current_dir = resource.source_file[:relative_path].dirname + current_dir = resource.file_descriptor[:relative_path].dirname non_root = partial_path.to_s.sub(/^\//, '') relative_dir = current_dir + Pathname(non_root) @@ -138,7 +138,7 @@ module Middleman partial_file = nil [ - [relative_dir.to_s, { preferred_engine: resource.source_file[:relative_path].extname[1..-1].to_sym }], + [relative_dir.to_s, { preferred_engine: resource.file_descriptor[:relative_path].extname[1..-1].to_sym }], [non_root], [non_root, { try_static: try_static }], [relative_dir_no_underscore.to_s, { try_static: try_static }],