Merge pull request #1318 from bhollis/partials_dir

Remove partials_dir functionality
This commit is contained in:
Thomas Reynolds 2014-07-08 09:19:36 -07:00
commit 52dcf37f24
12 changed files with 15 additions and 71 deletions

View file

@ -1,6 +1,8 @@
master master
=== ===
* Remove the `partials_dir` setting. Partials should live next to content, or be addressed with absolute paths.
* Partials must be named with a leading underscore. `_my_snippet.html.erb`, not `my_snippet.html.erb`.
* Removed the `proxy` and `ignore` options for the `page` command in `config.rb`. Use the `proxy` and `ignore` commands instead of passing these options to `page`. * Removed the `proxy` and `ignore` options for the `page` command in `config.rb`. Use the `proxy` and `ignore` commands instead of passing these options to `page`.
* The `page` command in `config.rb` can now be used to add data to the page via the `data` argument. It is accessed the same way as frontmatter data, via `current_resource.data`. * The `page` command in `config.rb` can now be used to add data to the page via the `data` argument. It is accessed the same way as frontmatter data, via `current_resource.data`.
* Add support for `environments` with the `-e` CLI flag. Loads additional config from `environments/envname.rb`. Removed `development?` helper in favor of `environment?(:development)`. Added `server?` helper to differentiate between build and server mode. * Add support for `environments` with the `-e` CLI flag. Loads additional config from `environments/envname.rb`. Removed `development?` helper in favor of `environment?(:development)`. Added `server?` helper to differentiate between build and server mode.

View file

@ -1,30 +0,0 @@
Feature: Partials dir
Scenario: Find partials in a custom partials dir
Given a fixture app "partials-dir-app"
And a file named "config.rb" with:
"""
set :partials_dir, 'partials'
"""
And the Server is running
When I go to "/index.html"
Then I should see "contents of the partial"
Scenario: Find partials in a nested custom partials dir
Given a fixture app "partials-dir-app"
And a file named "config.rb" with:
"""
set :partials_dir, 'nested/partials'
"""
And the Server is running
When I go to "/index.html"
Then I should see "contents of the nested partial"
Scenario: Find partials in the default partials dir
Given a fixture app "default-partials-dir-app"
And a file named "config.rb" with:
"""
"""
And the Server is running
When I go to "/index.html"
Then I should see "contents of the partial"

View file

@ -1 +0,0 @@
contents of the partial

View file

@ -1,2 +0,0 @@
<%= partial 'partial' %>

View file

@ -1,3 +1,3 @@
<%= partial "shared/header" %> <%= partial "shared/header" %>
<%= partial "local" %> <%= partial "local" %>
<%= partial "shared/footer" %> <%= partial "../shared/footer" %>

View file

@ -1,2 +0,0 @@
<%= partial 'partial' %>

View file

@ -1 +0,0 @@
contents of the nested partial

View file

@ -1 +0,0 @@
contents of the partial

View file

@ -113,10 +113,6 @@ module Middleman
# @return [String] # @return [String]
config.define_setting :fonts_dir, 'fonts', 'Location of fonts within source' config.define_setting :fonts_dir, 'fonts', 'Location of fonts within source'
# Location of partials within source. Used by renderers.
# @return [String]
config.define_setting :partials_dir, '', 'Location of partials within source'
# Location of layouts within source. Used by renderers. # Location of layouts within source. Used by renderers.
# @return [String] # @return [String]
config.define_setting :layouts_dir, 'layouts', 'Location of layouts within source' config.define_setting :layouts_dir, 'layouts', 'Location of layouts within source'

View file

@ -91,14 +91,13 @@ module Middleman
# Sinatra/Padrino compatible render method signature referenced by some view # Sinatra/Padrino compatible render method signature referenced by some view
# helpers. Especially partials. # helpers. Especially partials.
# #
# @param [] _ Unused parameter.
# @param [String, Symbol] name The partial to render. # @param [String, Symbol] name The partial to render.
# @param [Hash] options # @param [Hash] options
# @return [String] # @return [String]
def render(_, name, options={}, &block) def render(_, name, options={}, &block)
name = name.to_s name = name.to_s
partial_path = locate_partial_relative(name) || locate_partial_in_partials_dir(name) partial_path = locate_partial(name)
raise ::Middleman::TemplateRenderer::TemplateNotFound, "Could not locate partial: #{name}" unless partial_path raise ::Middleman::TemplateRenderer::TemplateNotFound, "Could not locate partial: #{name}" unless partial_path
@ -110,38 +109,28 @@ module Middleman
protected protected
# Locate a partial reltive to the current path, given a name. # Locate a partial relative to the current path or the source dir, given a partial's path.
# #
# @api private # @api private
# @param [String] name # @param [String] partial_path
# @return [String] # @return [String]
def locate_partial_relative(name) def locate_partial(partial_path)
return unless resource = sitemap.find_resource_by_path(current_path) return unless resource = sitemap.find_resource_by_path(current_path)
# Look for partials relative to the current path # Look for partials relative to the current path
current_dir = File.dirname(resource.source_file) current_dir = File.dirname(resource.source_file)
relative_dir = File.join(current_dir.sub(%r{^#{Regexp.escape(source_dir)}/?}, ''), name) relative_dir = File.join(current_dir.sub(%r{^#{Regexp.escape(source_dir)}/?}, ''), partial_path)
::Middleman::TemplateRenderer.resolve_template( partial = ::Middleman::TemplateRenderer.resolve_template(
@app, @app,
relative_dir, relative_dir,
try_without_underscore: true,
preferred_engine: File.extname(resource.source_file)[1..-1].to_sym preferred_engine: File.extname(resource.source_file)[1..-1].to_sym
) )
end
# Locate a partial reltive to the partials dir, given a name. return partial if partial
#
# @api private # Try to find one relative to the source dir
# @param [String] name ::Middleman::TemplateRenderer.resolve_template(@app, File.join('', partial_path))
# @return [String]
def locate_partial_in_partials_dir(name)
partials_path = File.join(@app.config[:partials_dir], name)
::Middleman::TemplateRenderer.resolve_template(
@app,
partials_path,
try_without_underscore: true
)
end end
# Render a path with locs, opts and contents block. # Render a path with locs, opts and contents block.

View file

@ -150,7 +150,6 @@ module Middleman
# Find a template on disk given a output path # Find a template on disk given a output path
# @param [String] request_path # @param [String] request_path
# @option options [Boolean] :preferred_engine If set, try this engine first, then fall back to any engine. # @option options [Boolean] :preferred_engine If set, try this engine first, then fall back to any engine.
# @option options [Boolean] :try_without_underscore
# @return [String, Boolean] Either the path to the template, or false # @return [String, Boolean] Either the path to the template, or false
def self.resolve_template(app, request_path, options={}) def self.resolve_template(app, request_path, options={})
# Find the path by searching or using the cache # Find the path by searching or using the cache
@ -177,13 +176,8 @@ module Middleman
end end
end end
search_paths = preferred_engines.flat_map do |preferred_engine| search_paths = preferred_engines.map do |preferred_engine|
path_with_ext = on_disk_path + '.' + preferred_engine on_disk_path + '.' + preferred_engine
paths = [path_with_ext]
if options[:try_without_underscore]
paths << path_with_ext.sub(relative_path, relative_path.sub(/^_/, '').sub(/\/_/, '/'))
end
paths
end end
found_path = nil found_path = nil