remove dup methods from base

This commit is contained in:
Thomas Reynolds 2011-11-20 17:30:53 -08:00
parent 51cb3980bc
commit 1fbeedb573
4 changed files with 16 additions and 85 deletions

View file

@ -138,6 +138,8 @@ class Middleman::Base
register Middleman::CoreExtensions::FrontMatter register Middleman::CoreExtensions::FrontMatter
def initialize(&block) def initialize(&block)
@current_path = nil
self.class.superclass.defaults.each do |k, v| self.class.superclass.defaults.each do |k, v|
set(k, v) set(k, v)
end end
@ -158,9 +160,8 @@ class Middleman::Base
attr :env attr :env
attr :req attr :req
attr :res attr :res
attr :options
attr :locals
# Rack Interface
def call(env) def call(env)
@env = env @env = env
@req = Rack::Request.new(env) @req = Rack::Request.new(env)
@ -212,10 +213,8 @@ public
def logging? def logging?
logging logging
end end
def current_path attr_accessor :current_path
@current_path || nil
end
def full_path(path) def full_path(path)
cache.fetch(:full_path, path) do cache.fetch(:full_path, path) do
@ -233,44 +232,6 @@ public
@res.finish @res.finish
end end
def resolve_template(request_path, options={})
request_path = request_path.to_s
cache.fetch(:resolve_template, request_path, options) do
relative_path = request_path.sub(%r{^/}, "")
on_disk_path = File.expand_path(relative_path, source_dir)
preferred_engine = if options.has_key?(:preferred_engine)
extension_class = Tilt[options[:preferred_engine]]
matched_exts = []
# TODO: Cache this
Tilt.mappings.each do |ext, engines|
next unless engines.include? extension_class
matched_exts << ext
end
"{" + matched_exts.join(",") + "}"
else
"*"
end
path_with_ext = on_disk_path + "." + preferred_engine
found_path = Dir[path_with_ext].find do |path|
::Tilt[path]
end
result = if found_path || File.exists?(on_disk_path)
engine = found_path ? File.extname(found_path)[1..-1].to_sym : nil
[ found_path || on_disk_path, engine ]
else
false
end
result
end
end
def send_file(path) def send_file(path)
matched_mime = mime_type(File.extname(path)) matched_mime = mime_type(File.extname(path))
matched_mime = "application/octet-stream" if matched_mime.nil? matched_mime = "application/octet-stream" if matched_mime.nil?
@ -281,23 +242,12 @@ public
halt file.serving(env) halt file.serving(env)
end end
# Sinatra render method signature # Sinatra/Padrino render method signature
def render(engine, data, options={}, locals={}, &block) def render(engine, data, options={}, locals={}, &block)
internal_render(data, locals, options, &block) if sitemap.exists?(data)
end sitemap.page(data).render(options, locals, &block)
else
def options_for_ext(ext) throw "Could not find file to render: #{data}"
cache.fetch(:options_for_ext, ext) do
options = {}
extension_class = Tilt[ext]
Tilt.mappings.each do |ext, engines|
next unless engines.include? extension_class
engine_options = respond_to?(ext.to_sym) ? send(ext.to_sym) : {}
options.merge!(engine_options)
end
options
end end
end end
@ -337,20 +287,4 @@ public
def map(map, &block) def map(map, &block)
self.class.map(map, &block) self.class.map(map, &block)
end end
def internal_render(path, locals = {}, options = {}, &block)
path = path.to_s
options.merge!(options_for_ext(File.extname(path)))
body = cache.fetch(:raw_template, path) do
File.read(path)
end
template = cache.fetch(:compiled_template, options, body) do
::Tilt.new(path, 1, options) { body }
end
template.render(self, locals, &block)
end
end end

View file

@ -54,10 +54,8 @@ module Middleman::CoreExtensions::DefaultHelpers
path = path.sub(%r{^/}, '') path = path.sub(%r{^/}, '')
path = path.gsub(File.extname(path), ".#{asset_ext}") path = path.gsub(File.extname(path), ".#{asset_ext}")
path = path.gsub("/", separator) path = path.gsub("/", separator)
matching_file = resolve_template(File.join(asset_dir, path))
yield path if matching_file yield path if sitemap.exists?(File.join(asset_dir, path))
end end
def page_classes def page_classes

View file

@ -31,14 +31,14 @@ module Middleman::Features::CacheBuster
end end
real_path_static = File.join(prefix, path) real_path_static = File.join(prefix, path)
result = resolve_template(real_path_static)
if self.build? if self.build?
real_path_dynamic = File.join(self.build_dir, prefix, path) real_path_dynamic = File.join(self.build_dir, prefix, path)
real_path_dynamic = File.expand_path(real_path_dynamic, self.root) real_path_dynamic = File.expand_path(real_path_dynamic, self.root)
http_path << "?" + File.mtime(real_path_dynamic).strftime("%s") if File.readable?(real_path_dynamic) http_path << "?" + File.mtime(real_path_dynamic).strftime("%s") if File.readable?(real_path_dynamic)
elsif result elsif sitemap.exists?(real_path_static)
if result[1].nil? page = sitemap.page(real_path_static)
if !page.template?
http_path << "?" + File.mtime(result[0]).strftime("%s") http_path << "?" + File.mtime(result[0]).strftime("%s")
else else
# It's a template, possible with partials. We can't really know when # It's a template, possible with partials. We can't really know when

View file

@ -5,12 +5,11 @@ module Middleman::Features::DirectoryIndexes
app.before do app.before do
prefix = @original_path.sub(/\/$/, "") prefix = @original_path.sub(/\/$/, "")
indexed_path = prefix + "/" + self.index_file indexed_path = prefix + "/" + self.index_file
indexed_exists = resolve_template(indexed_path)
extensioned_path = prefix + File.extname(self.index_file) extensioned_path = prefix + File.extname(self.index_file)
is_ignored = self.ignored_directory_indexes.include?(extensioned_path) is_ignored = self.ignored_directory_indexes.include?(extensioned_path)
if !indexed_exists && !is_ignored if !sitemap.exists?(indexed_path) && !is_ignored
parts = @original_path.split("/") parts = @original_path.split("/")
last_part = parts.last last_part = parts.last
last_part_ext = File.extname(last_part) last_part_ext = File.extname(last_part)