2013-12-28 01:26:31 +01:00
|
|
|
require 'middleman-core/sitemap/extensions/traversal'
|
|
|
|
require 'middleman-core/sitemap/extensions/content_type'
|
2014-01-03 22:15:02 +01:00
|
|
|
require 'middleman-core/file_renderer'
|
|
|
|
require 'middleman-core/template_renderer'
|
2012-05-07 23:41:39 +02:00
|
|
|
|
|
|
|
module Middleman
|
2012-08-14 00:39:06 +02:00
|
|
|
# Sitemap namespace
|
2012-05-07 23:41:39 +02:00
|
|
|
module Sitemap
|
|
|
|
# Sitemap Resource class
|
|
|
|
class Resource
|
|
|
|
include Middleman::Sitemap::Extensions::Traversal
|
2013-04-06 23:05:26 +02:00
|
|
|
include Middleman::Sitemap::Extensions::ContentType
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
# @return [Middleman::Sitemap::Store]
|
2014-04-28 07:24:27 +02:00
|
|
|
#attr_reader :store
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
# The source path of this resource (relative to the source directory,
|
|
|
|
# without template extensions)
|
|
|
|
# @return [String]
|
|
|
|
attr_reader :path
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2013-11-27 09:19:59 +01:00
|
|
|
# The output path for this resource
|
|
|
|
# @return [String]
|
|
|
|
attr_accessor :destination_path
|
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
# Set the on-disk source file for this resource
|
|
|
|
# @return [String]
|
|
|
|
def source_file
|
2014-04-28 07:24:27 +02:00
|
|
|
# TODO: Make this work when get_source_file doesn't exist
|
2012-05-07 23:41:39 +02:00
|
|
|
@source_file || get_source_file
|
|
|
|
end
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
# Initialize resource with parent store and URL
|
|
|
|
# @param [Middleman::Sitemap::Store] store
|
|
|
|
# @param [String] path
|
|
|
|
# @param [String] source_file
|
|
|
|
def initialize(store, path, source_file=nil)
|
|
|
|
@store = store
|
|
|
|
@app = @store.app
|
2013-08-16 06:12:29 +02:00
|
|
|
@path = path.gsub(' ', '%20') # handle spaces in filenames
|
2012-05-07 23:41:39 +02:00
|
|
|
@source_file = source_file
|
2013-11-27 09:19:59 +01:00
|
|
|
@destination_path = @path
|
2012-04-30 07:32:52 +02:00
|
|
|
|
2014-04-28 06:54:53 +02:00
|
|
|
# Options are generally rendering/sitemap options
|
|
|
|
# Locals are local variables for rendering this resource's template
|
|
|
|
# Page are data that is exposed through this resource's data member.
|
|
|
|
# Note: It is named 'page' for backwards compatibility with older MM.
|
|
|
|
@local_metadata = { options: {}, locals: {}, page: {} }
|
2012-05-07 23:41:39 +02:00
|
|
|
end
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
# Whether this resource has a template file
|
|
|
|
# @return [Boolean]
|
|
|
|
def template?
|
|
|
|
return false if source_file.nil?
|
|
|
|
!::Tilt[source_file].nil?
|
|
|
|
end
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
# Get the metadata for both the current source_file and the current path
|
|
|
|
# @return [Hash]
|
|
|
|
def metadata
|
2014-04-28 07:24:27 +02:00
|
|
|
# TODO: Get rid of all this
|
|
|
|
result = @store.metadata_for_path(path).dup
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2014-04-28 07:24:27 +02:00
|
|
|
file_meta = @store.metadata_for_file(source_file).dup
|
2012-07-29 19:22:57 +02:00
|
|
|
result.deep_merge!(file_meta)
|
2012-04-30 07:32:52 +02:00
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
local_meta = @local_metadata.dup
|
|
|
|
result.deep_merge!(local_meta)
|
2012-04-30 07:32:52 +02:00
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
result
|
|
|
|
end
|
2012-04-30 07:32:52 +02:00
|
|
|
|
2014-04-28 06:54:53 +02:00
|
|
|
# Data about this resource, populated from frontmatter or extensions.
|
|
|
|
# @return [HashWithIndifferentAccess]
|
|
|
|
def data
|
2014-04-28 07:24:27 +02:00
|
|
|
# TODO: Upconvert/freeze at this point?
|
2014-04-28 06:54:53 +02:00
|
|
|
metadata[:page]
|
|
|
|
end
|
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
# Merge in new metadata specific to this resource.
|
2014-01-10 04:49:41 +01:00
|
|
|
# @param [Hash] meta A metadata block like provides_metadata_for_path takes
|
2014-01-04 00:49:54 +01:00
|
|
|
def add_metadata(meta={})
|
|
|
|
@local_metadata.deep_merge!(meta.dup)
|
2012-04-30 07:32:52 +02:00
|
|
|
end
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2013-11-27 09:19:59 +01:00
|
|
|
# The output/preview URL for this resource
|
2012-05-07 23:41:39 +02:00
|
|
|
# @return [String]
|
2013-11-27 09:19:59 +01:00
|
|
|
attr_accessor :destination_path
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
# Extension of the path (i.e. '.js')
|
|
|
|
# @return [String]
|
|
|
|
def ext
|
|
|
|
File.extname(path)
|
|
|
|
end
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
# Render this resource
|
|
|
|
# @return [String]
|
2014-01-04 00:49:54 +01:00
|
|
|
def render(opts={}, locs={})
|
2014-04-29 20:43:05 +02:00
|
|
|
return ::Middleman::FileRenderer.new(@app, source_file).template_data_for_file unless template?
|
2012-04-04 19:26:07 +02:00
|
|
|
|
2014-04-28 07:24:27 +02:00
|
|
|
relative_source = Pathname(source_file).relative_path_from(Pathname(@app.root))
|
2012-07-29 19:22:57 +02:00
|
|
|
|
2014-04-28 07:24:27 +02:00
|
|
|
@app.instrument 'render.resource', path: relative_source, destination_path: destination_path do
|
2012-07-15 20:04:45 +02:00
|
|
|
md = metadata.dup
|
|
|
|
opts = md[:options].deep_merge(opts)
|
|
|
|
locs = md[:locals].deep_merge(locs)
|
2014-04-29 19:50:21 +02:00
|
|
|
locs[:current_path] ||= destination_path
|
2013-06-24 22:39:14 +02:00
|
|
|
|
|
|
|
# Certain output file types don't use layouts
|
2014-04-29 19:50:21 +02:00
|
|
|
unless opts.key?(:layout)
|
|
|
|
opts[:layout] = false if %w(.js .json .css .txt).include?(ext)
|
2013-06-24 22:39:14 +02:00
|
|
|
end
|
|
|
|
|
2014-01-03 22:15:02 +01:00
|
|
|
renderer = ::Middleman::TemplateRenderer.new(@app, source_file)
|
2014-01-04 00:49:54 +01:00
|
|
|
renderer.render(locs, opts)
|
2012-07-15 20:04:45 +02:00
|
|
|
end
|
2012-05-07 23:41:39 +02:00
|
|
|
end
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
# A path without the directory index - so foo/index.html becomes
|
|
|
|
# just foo. Best for linking.
|
|
|
|
# @return [String]
|
|
|
|
def url
|
2012-07-13 03:27:05 +02:00
|
|
|
url_path = destination_path
|
2014-04-28 07:24:27 +02:00
|
|
|
if @app.config[:strip_index_file]
|
|
|
|
url_path = url_path.sub(/(^|\/)#{Regexp.escape(@app.config[:index_file])}$/,
|
|
|
|
@app.config[:trailing_slash] ? '/' : '')
|
2012-07-13 03:27:05 +02:00
|
|
|
end
|
2014-04-28 07:24:27 +02:00
|
|
|
File.join(@app.config[:http_prefix], url_path)
|
2012-05-07 23:41:39 +02:00
|
|
|
end
|
2012-12-31 05:36:06 +01:00
|
|
|
|
|
|
|
# Whether the source file is binary.
|
|
|
|
#
|
2013-03-28 19:15:33 +01:00
|
|
|
# @return [Boolean]
|
2012-12-31 05:36:06 +01:00
|
|
|
def binary?
|
2013-01-12 01:58:02 +01:00
|
|
|
::Middleman::Util.binary?(source_file)
|
2012-12-31 05:36:06 +01:00
|
|
|
end
|
2012-04-04 19:26:07 +02:00
|
|
|
end
|
|
|
|
end
|
2012-05-19 22:28:59 +02:00
|
|
|
end
|