support alternative ext

This commit is contained in:
tdreyno 2009-09-18 09:59:07 -07:00
parent 9e01c6c80f
commit 2dbec48c61
2 changed files with 30 additions and 12 deletions

View file

@ -26,15 +26,22 @@ module Generators
end
if (args[0] === args[1])
newext = case File.extname(args.first)
when '.haml', '.erb', '.mab', '.maruku'
'.html'
when '.sass'
'.css'
file_name_parts = File.basename(args.first).split('.')
if file_name_parts.length > 2
# static ext embedded in filename
newext = ""
else
File.extname(args.first)
# use defaults
newext = case file_name_parts.last
when 'haml', 'erb', 'mab', 'maruku'
'.html'
when 'sass'
'.css'
end
args[1] = args[0].gsub(File.extname(args.first), newext).gsub('views/', '')
end
args[1] = args[0].gsub(".#{file_name_parts.last}", newext).gsub('views/', '')
end
super(name, *args, &block)

View file

@ -14,7 +14,7 @@ class Middleman < Sinatra::Base
set :static, true
set :root, Dir.pwd
set :environment, defined?(MIDDLEMAN_BUILDER) ? :build : :development
set :default_ext, 'html'
set :supported_formats, %w(haml erb builder)
helpers Sinatra::ContentFor
@ -82,15 +82,20 @@ class Middleman < Sinatra::Base
# All other files
get /(.*)/ do |path|
path << "index.html" if path.match(%r{/$})
path << "index.#{options.default_ext}" if path.match(%r{/$})
path.gsub!(%r{^/}, '')
path.gsub!(File.extname(path), '')
path_without_ext = path.gsub(File.extname(path), '')
result = nil
begin
options.supported_formats.detect do |renderer|
next false if !File.exists?(File.join(options.views, "#{path}.#{renderer}"))
if File.exists?(File.join(options.views, "#{path}.#{renderer}"))
result = send(renderer.to_sym, path.to_sym)
elsif File.exists?(File.join(options.views, "#{path_without_ext}.#{renderer}"))
result = send(renderer.to_sym, path_without_ext.to_sym)
else
false
end
end
rescue Haml::Error => e
result = "Haml Error: #{e}"
@ -99,4 +104,10 @@ class Middleman < Sinatra::Base
result || pass
end
get %r{/(.*\.xml)} do |path|
content_type 'text/xml', :charset => 'utf-8'
haml(path.to_sym, :layout => false)
end
end