diff --git a/lib/brochure/application.rb b/lib/brochure/application.rb index d4ef74c..209407a 100644 --- a/lib/brochure/application.rb +++ b/lib/brochure/application.rb @@ -47,8 +47,8 @@ def plugins def call(env) if forbidden?(env["PATH_INFO"]) forbidden - elsif template = find_template(env["PATH_INFO"][/[^.]+/]) - success render_template(template, env) + elsif template = find_template(env["PATH_INFO"]) + success render_template(template, env), template.content_type else not_found(env) end @@ -58,27 +58,26 @@ def forbidden?(path) path[".."] || File.basename(path)[/^_/] end - def find_template(logical_path) - if template_path = find_template_path(logical_path) + def find_template(logical_path, format_extension = ".html") + if template_path = find_template_path(logical_path, format_extension) template_for(template_path) end end - def find_partial(logical_path, format_extension) + def find_partial(logical_path, format_extension = ".html") if template_path = find_partial_path(logical_path, format_extension) template_for(template_path) end end - def find_template_path(logical_path) - candidates = [logical_path + ".html", logical_path + "/index.html"] - template_trail.find(*candidates) + def find_template_path(logical_path, format_extension) + template_trail.find(logical_path, logical_path + format_extension, logical_path + "/index" + format_extension) end def find_partial_path(logical_path, format_extension) dirname, basename = File.split(logical_path) - partial_path = File.join(dirname, "_" + basename + format_extension) - template_trail.find(partial_path) + partial_path = File.join(dirname, "_" + basename) + template_trail.find(partial_path, partial_path + format_extension) end def template_for(template_path) @@ -101,8 +100,8 @@ def respond_with(status, body, content_type = "text/html; charset=utf-8") [status, headers, [body]] end - def success(body) - respond_with 200, body + def success(body, content_type) + respond_with 200, body, content_type end def not_found(env) diff --git a/lib/brochure/template.rb b/lib/brochure/template.rb index c93462b..8fd7612 100644 --- a/lib/brochure/template.rb +++ b/lib/brochure/template.rb @@ -27,5 +27,10 @@ def format_extension ext = File.extname(File.basename(path, engine_extension)) ext.empty? ? ".html" : ext end + + def content_type + type = Rack::Mime.mime_type(format_extension) + type[/^text/] ? "#{type}; charset=utf-8" : type + end end end diff --git a/test/fixtures/default/templates/hello.js.erb b/test/fixtures/default/templates/hello.js.erb new file mode 100644 index 0000000..6909023 --- /dev/null +++ b/test/fixtures/default/templates/hello.js.erb @@ -0,0 +1 @@ +var domain = "<%= @domain %>"; diff --git a/test/test_brochure.rb b/test/test_brochure.rb index dbc7ac9..598e29a 100644 --- a/test/test_brochure.rb +++ b/test/test_brochure.rb @@ -21,6 +21,7 @@ def test_templates_are_rendered_when_present get "/signup" assert last_response.ok? assert_equal "

Sign up

", last_response.body.strip + assert_equal "text/html; charset=utf-8", last_response.content_type end def test_index_templates_are_rendered_for_directories @@ -113,4 +114,10 @@ def test_assigns_are_available_in_templates get "/hello" assert last_response.body['Home'] end + + def test_alternate_template_format + get "/hello.js" + assert last_response.body['var domain = "37signals.com";'] + assert_equal "application/javascript", last_response.content_type + end end