Revert breaking resource source_file api. For #1595

feature/livereload-locales-data
Thomas Reynolds 2015-09-09 11:55:56 -07:00
parent d849930e51
commit 441dc2faa6
13 changed files with 42 additions and 35 deletions

View File

@ -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"

View File

@ -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)

View File

@ -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

View File

@ -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 %>

View File

@ -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 %>

View File

@ -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))

View File

@ -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

View File

@ -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?

View File

@ -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)

View File

@ -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: {

View File

@ -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]

View File

@ -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

View File

@ -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 }],