Clean up i18n a bit, stake out some territory around routing and resource

This commit is contained in:
Ben Hollis 2014-04-27 22:24:27 -07:00
parent 69396d34c1
commit c285848866
4 changed files with 24 additions and 50 deletions

View file

@ -55,7 +55,6 @@ module Middleman::CoreExtensions
def after_configuration def after_configuration
app.ignore %r{\.frontmatter$} app.ignore %r{\.frontmatter$}
# TODO: Add to file watcher ignore?
# TODO: Replace all of this functionality # TODO: Replace all of this functionality
#::Middleman::Sitemap::Resource.send :include, ResourceInstanceMethods #::Middleman::Sitemap::Resource.send :include, ResourceInstanceMethods

View file

@ -42,7 +42,6 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
# Don't output localizable files # Don't output localizable files
app.ignore File.join(options[:templates_dir], '**') app.ignore File.join(options[:templates_dir], '**')
app.sitemap.provides_metadata_for_path(&method(:metadata_for_path))
app.files.changed(&method(:on_file_changed)) app.files.changed(&method(:on_file_changed))
app.files.deleted(&method(:on_file_changed)) app.files.deleted(&method(:on_file_changed))
end end
@ -56,14 +55,12 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
delegate :logger, to: :app delegate :logger, to: :app
def langs def langs
@_langs ||= known_languages @langs ||= known_languages
end end
# Update the main sitemap resource list # Update the main sitemap resource list
# @return [void] # @return [void]
def manipulate_resource_list(resources) def manipulate_resource_list(resources)
@_localization_data = {}
new_resources = [] new_resources = []
resources.each do |resource| resources.each do |resource|
@ -81,6 +78,10 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
new_resources << build_resource(path, resource.path, page_id, lang) new_resources << build_resource(path, resource.path, page_id, lang)
end end
end end
# This is for backwards compatibility with the old provides_metadata-based code
# that used to be in this extension, but I don't know how much sense it makes.
resource.add_metadata options: { lang: @mount_at_root }, locals: { lang: @mount_at_root }
end end
resources + new_resources resources + new_resources
@ -90,7 +91,7 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
def on_file_changed(file) def on_file_changed(file)
if @locales_regex =~ file if @locales_regex =~ file
@_langs = nil # Clear langs cache @langs = nil # Clear langs cache
::I18n.reload! ::I18n.reload!
end end
end end
@ -112,24 +113,6 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
end end
end end
def metadata_for_path(url)
if d = localization_data(url)
lang, page_id = d
else
# Default to the @mount_at_root lang
page_id = nil
lang = @mount_at_root
end
{
locals: {
lang: lang,
page_id: page_id
},
options: { lang: lang }
}
end
def known_languages def known_languages
if options[:langs] if options[:langs]
Array(options[:langs]).map(&:to_sym) Array(options[:langs]).map(&:to_sym)
@ -144,11 +127,6 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
end end
end end
def localization_data(path)
@_localization_data ||= {}
@_localization_data[path]
end
# Parse locale extension filename # Parse locale extension filename
# @return [lang, path, basename] # @return [lang, path, basename]
# will return +nil+ if no locale extension # will return +nil+ if no locale extension
@ -183,10 +161,9 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
path = path.sub(options[:templates_dir] + '/', '') path = path.sub(options[:templates_dir] + '/', '')
@_localization_data[path] = [lang, path, localized_page_id]
p = ::Middleman::Sitemap::Resource.new(app.sitemap, path) p = ::Middleman::Sitemap::Resource.new(app.sitemap, path)
p.proxy_to(source_path) p.proxy_to(source_path)
p.add_metadata locals: { lang: lang, page_id: path }, options: { lang: lang }
::I18n.locale = old_locale ::I18n.locale = old_locale
p p

View file

@ -14,7 +14,9 @@ module Middleman
options = opts.dup options = opts.dup
# Default layout # Default layout
# TODO: This seems wrong
options[:layout] = @app.config[:layout] if options[:layout].nil? options[:layout] = @app.config[:layout] if options[:layout].nil?
# TODO: You can set options and locals, but not data
metadata = { options: options, locals: options.delete(:locals) || {} } metadata = { options: options, locals: options.delete(:locals) || {} }
# If the url is a regexp # If the url is a regexp
@ -37,6 +39,9 @@ module Middleman
end end
# Setup a metadata matcher for rendering those options # Setup a metadata matcher for rendering those options
# TODO: How to get rid of this? Perhaps a separate extension that manipulates
# in this sort of data?
# This is harder because sitemap isn't available.
@app.sitemap.provides_metadata_for_path(url) { |_| metadata } @app.sitemap.provides_metadata_for_path(url) { |_| metadata }
end end
end end

View file

@ -11,12 +11,8 @@ module Middleman
include Middleman::Sitemap::Extensions::Traversal include Middleman::Sitemap::Extensions::Traversal
include Middleman::Sitemap::Extensions::ContentType include Middleman::Sitemap::Extensions::ContentType
# @return [Middleman::Application]
attr_reader :app
delegate :logger, :instrument, to: :app
# @return [Middleman::Sitemap::Store] # @return [Middleman::Sitemap::Store]
attr_reader :store #attr_reader :store
# The source path of this resource (relative to the source directory, # The source path of this resource (relative to the source directory,
# without template extensions) # without template extensions)
@ -29,9 +25,8 @@ module Middleman
# Set the on-disk source file for this resource # Set the on-disk source file for this resource
# @return [String] # @return [String]
# attr_reader :source_file
def source_file def source_file
# TODO: Make this work when get_source_file doesn't exist
@source_file || get_source_file @source_file || get_source_file
end end
@ -63,9 +58,10 @@ module Middleman
# Get the metadata for both the current source_file and the current path # Get the metadata for both the current source_file and the current path
# @return [Hash] # @return [Hash]
def metadata def metadata
result = store.metadata_for_path(path).dup # TODO: Get rid of all this
result = @store.metadata_for_path(path).dup
file_meta = store.metadata_for_file(source_file).dup file_meta = @store.metadata_for_file(source_file).dup
result.deep_merge!(file_meta) result.deep_merge!(file_meta)
local_meta = @local_metadata.dup local_meta = @local_metadata.dup
@ -77,6 +73,7 @@ module Middleman
# Data about this resource, populated from frontmatter or extensions. # Data about this resource, populated from frontmatter or extensions.
# @return [HashWithIndifferentAccess] # @return [HashWithIndifferentAccess]
def data def data
# TODO: Upconvert/freeze at this point?
metadata[:page] metadata[:page]
end end
@ -96,18 +93,14 @@ module Middleman
File.extname(path) File.extname(path)
end end
def request_path
destination_path
end
# Render this resource # Render this resource
# @return [String] # @return [String]
def render(opts={}, locs={}) def render(opts={}, locs={})
return ::Middleman::FileRenderer.new(@app, source_file).template_data_for_file unless template? return ::Middleman::FileRenderer.new(@app, source_file).template_data_for_file unless template?
relative_source = Pathname(source_file).relative_path_from(Pathname(app.root)) relative_source = Pathname(source_file).relative_path_from(Pathname(@app.root))
instrument 'render.resource', path: relative_source, destination_path: destination_path do @app.instrument 'render.resource', path: relative_source, destination_path: destination_path do
md = metadata.dup md = metadata.dup
opts = md[:options].deep_merge(opts) opts = md[:options].deep_merge(opts)
locs = md[:locals].deep_merge(locs) locs = md[:locals].deep_merge(locs)
@ -128,11 +121,11 @@ module Middleman
# @return [String] # @return [String]
def url def url
url_path = destination_path url_path = destination_path
if app.config[:strip_index_file] if @app.config[:strip_index_file]
url_path = url_path.sub(/(^|\/)#{Regexp.escape(app.config[:index_file])}$/, url_path = url_path.sub(/(^|\/)#{Regexp.escape(@app.config[:index_file])}$/,
app.config[:trailing_slash] ? '/' : '') @app.config[:trailing_slash] ? '/' : '')
end end
File.join(app.config[:http_prefix], url_path) File.join(@app.config[:http_prefix], url_path)
end end
# Whether the source file is binary. # Whether the source file is binary.