rename old padrino tests, remove sinatra tests, use metadata hooks for frontmatter and liquid

This commit is contained in:
Thomas Reynolds 2011-11-18 13:38:18 -08:00
parent c82a40dde5
commit 0e5ce50787
13 changed files with 117 additions and 156 deletions

1
.gitignore vendored
View file

@ -11,3 +11,4 @@ docs
.rbenv-version
fixtures/test-app/build
.*.swp
build

View file

@ -3,7 +3,7 @@ Feature: Built-in macro view helpers
Scenario: Using the link_to helper
Given the Server is running at "test-app"
When I go to "/padrino_test.html"
When I go to "/former_padrino_test.html"
And I should see 'href="test2.com"'
And I should see 'src="/images/test2.png"'
Then I should see 'src="/javascripts/test1.js"'

View file

@ -1,6 +0,0 @@
Feature: Sinatra Routes
Scenario: Rendering html
Given the Server is running at "test-app"
When I go to "/sinatra_test"
Then I should see "Ratpack"

View file

@ -1,8 +1,9 @@
Given /^I am using an asset host$/ do
@server ||= Middleman.server
@server.set :show_exceptions, false
@server.activate :asset_host
@server.set :asset_host do |asset|
@initialize_commands ||= []
@initialize_commands << lambda {
activate :asset_host
set :asset_host do |asset|
"http://assets%d.example.com" % (asset.hash % 4)
end
}
end

View file

@ -1,23 +1,18 @@
Given /^a clean server$/ do
@server = Middleman.server
@server.set :show_exceptions, false
@initialize_commands = []
end
Given /^"([^\"]*)" feature is "([^\"]*)"$/ do |feature, state|
@server = Middleman.server
@server.set :show_exceptions, false
@initialize_commands ||= []
if state == "enabled"
@server.activate(feature.to_sym)
@initialize_commands << lambda { activate(feature.to_sym) }
end
@server.set :environment, @current_env || :development
end
Given /^"([^\"]*)" is set to "([^\"]*)"$/ do |variable, value|
@server ||= Middleman.server
@server.set :show_exceptions, false
@server.set variable.to_sym, value
@initialize_commands ||= []
@initialize_commands << lambda { set(variable.to_sym, value) }
end
Given /^current environment is "([^\"]*)"$/ do |env|
@ -25,13 +20,22 @@ Given /^current environment is "([^\"]*)"$/ do |env|
end
Given /^the Server is running at "([^\"]*)"$/ do |app_path|
@server ||= Middleman.server
@server.set :show_exceptions, false
root = File.dirname(File.dirname(File.dirname(__FILE__)))
@server.set :root, File.join(root, "fixtures", app_path)
@app = @server.new!
app_rack = @server.build_new(@app)
@browser = Rack::Test::Session.new(Rack::MockSession.new(app_rack))
root_dir = File.dirname(File.dirname(File.dirname(__FILE__)))
initialize_commands = @initialize_commands || []
initialize_commands.unshift lambda {
set :root, File.join(root_dir, "fixtures", app_path)
set :environment, @current_env || :development
}
@server_inst = Middleman.server.inst do
initialize_commands.each do |p|
instance_exec(&p)
end
end
app_rack = @server_inst.class.to_rack_app
@browser = ::Rack::Test::Session.new(::Rack::MockSession.new(app_rack))
end
When /^I go to "([^\"]*)"$/ do |url|

View file

@ -1,13 +1,15 @@
Given /^page "([^\"]*)" has layout "([^\"]*)"$/ do |url, layout|
@server ||= Middleman.server
@server.set :show_exceptions, false
@server.page(url, :layout => layout.to_sym)
@initialize_commands ||= []
@initialize_commands << lambda {
page(url, :layout => layout.to_sym)
}
end
Given /^"([^\"]*)" with_layout block has layout "([^\"]*)"$/ do |url, layout|
@server ||= Middleman.server
@server.set :show_exceptions, false
@server.with_layout(layout.to_sym) do
@initialize_commands ||= []
@initialize_commands << lambda {
with_layout(layout.to_sym) do
page(url)
end
}
end

View file

@ -1,46 +0,0 @@
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline; }
body {
line-height: 1; }
ol, ul {
list-style: none; }
table {
border-collapse: collapse;
border-spacing: 0; }
caption, th, td {
text-align: left;
font-weight: normal;
vertical-align: middle; }
q, blockquote {
quotes: none; }
q:before, q:after, blockquote:before, blockquote:after {
content: "";
content: none; }
a img {
border: none; }
article, aside, details, figcaption, figure, footer, header, hgroup, menu, nav, section, summary {
display: block; }

View file

@ -9,6 +9,7 @@ require "active_support/core_ext/string/inflections"
class Middleman::Base
include Hooks
define_hook :before
define_hook :build_config
define_hook :development_config
@ -44,7 +45,7 @@ class Middleman::Base
end
def map(map, &block)
app.map(map, &block)
# app.map(map, &block)
end
def helpers(*extensions, &block)
@ -79,9 +80,10 @@ class Middleman::Base
end
end
def set(key, value)
def set(key, value=nil, &block)
setter = "#{key}=".to_sym
self.class.send(:attr_accessor, key) if !respond_to?(setter)
value = block if block_given?
send(setter, value)
end
@ -159,28 +161,37 @@ class Middleman::Base
set(k, v)
end
instance_exec(&block) if block_given?
set :source_dir, File.join(root, source)
super
instance_exec(&block) if block_given?
run_hook :initialized
end
attr :env
attr :req
attr :res
attr :options
attr :locals
def call(env)
@env = env
@req = Rack::Request.new(env)
@res = Rack::Response.new
catch(:halt) do
process_request
res.status = 404
res.finish
end
end
# Custom 404 handler (to be styled)
# app.error Sinatra::NotFound do
# content_type 'text/html'
# "<html><body><h1>File Not Found</h1><p>#{request.path_info}</p></body>"
# end
def halt(response)
throw :halt, response
end
# Convenience methods to check if we're in a mode
def development?; environment == :development; end
@ -188,14 +199,15 @@ class Middleman::Base
# Internal method to look for templates and evaluate them if found
def process_request
run_hook :before
# Normalize the path and add index if we're looking at a directory
original_path = @env["PATH_INFO"].dup
request_path = full_path(@env["PATH_INFO"].gsub("%20", " "))
original_path = env["PATH_INFO"].dup
request_path = full_path(env["PATH_INFO"].gsub("%20", " "))
return not_found if sitemap.ignored_path?(request_path)
if sitemap.path_is_proxy?(request_path)
# request["is_proxy"] = true
request_path = "/" + sitemap.path_target(request_path)
end
@ -212,12 +224,12 @@ class Middleman::Base
context = sitemap.get_context(original_path) || {}
options = context.has_key?(:options) ? context[:options] : {}
# options.merge!(request['custom_options'] || {})
@options = context.has_key?(:options) ? context[:options] : {}
@locals = context.has_key?(:locals) ? context[:locals] : {}
provides_metadata.each do |callback, matcher|
next if !matcher.nil? && !path.match(matcher)
options.merge!(instance_exec(path, &callback) || {})
instance_exec(path, &callback)
end
local_layout = if options.has_key?(:layout)
@ -232,11 +244,8 @@ class Middleman::Base
instance_eval(&context[:block])
end
# locals = request['custom_locals'] || {}
locals = {}
# content_type mime_type(File.extname(request_path))
@res.status = 200
res.status = 200
output = if local_layout
layout_engine = if options.has_key?(:layout_engine)
@ -256,8 +265,8 @@ class Middleman::Base
render(path, locals)
end
@res.write output
@res.finish
res.write output
halt res.finish
end
public
@ -318,7 +327,6 @@ public
# raw_templates_cache[key]
# end
protected
def full_path(path)
parts = path ? path.split('/') : []
if parts.last.nil? || parts.last.split('.').length == 1
@ -329,14 +337,13 @@ protected
def not_found
@res.status == 404
@res.write "<html><body><h1>File Not Found</h1><p>#{@env["PATH_INFO"]}</p></body>"
@res.write "<html><body><h1>File Not Found</h1><p>#{env["PATH_INFO"]}</p></body>"
@res.finish
end
def resolve_template(request_path, options={})
request_path = request_path.to_s
@_resolved_templates ||= {}
if !@_resolved_templates.has_key?(request_path)
relative_path = request_path.sub(%r{^/}, "")
on_disk_path = File.expand_path(relative_path, source_dir)
@ -384,7 +391,7 @@ protected
file = ::Rack::File.new nil
file.path = path
file.serving(@env)
file.serving(env)
end
def render(path, locals = {}, options = {}, &block)

View file

@ -17,7 +17,7 @@ module Middleman
request_path = destination.sub(/^#{SHARED_SERVER_INST.build_dir}/, "")
# begin
begin
# destination, request_path = SHARED_SERVER.reroute_builder(destination, request_path)
request_path.gsub!(/\s/, "%20")
@ -26,8 +26,9 @@ module Middleman
create_file destination, nil, config do
response.body
end if response.status == 200
# rescue
# end
rescue
say_status :error, destination, :red
end
end
end

View file

@ -37,7 +37,7 @@ module Middleman::CoreExtensions::FrontMatter
end
end
data
@options.merge!(data)
end
super

View file

@ -1,53 +1,55 @@
module Middleman::Features::DirectoryIndexes
class << self
def registered(app)
app.set :ignored_directory_indexes, []
app.extend ClassMethods
app.build_reroute do |destination, request_path|
index_ext = File.extname(app.settings.index_file)
new_index_path = "/#{app.settings.index_file}"
indexed_path = request_path.gsub(/\/$/, "") + index_ext
if app.settings.ignored_directory_indexes.include?(request_path)
false
elsif request_path =~ /#{new_index_path}$/
false
else
[
destination.gsub(/#{index_ext.gsub(".", "\\.")}$/, new_index_path),
request_path
]
end
end
app.send :include, InstanceMethods
app.before do
indexed_path = request.path_info.gsub(/\/$/, "") + "/" + app.settings.index_file
indexed_exists = resolve_template(indexed_path, :raise_exceptions => false)
indexed_path = env["PATH_INFO"].sub(/\/$/, "") + "/" + self.index_file
indexed_exists = resolve_template(indexed_path)
extensioned_path = request.path_info.gsub(/\/$/, "") + File.extname(app.settings.index_file)
is_ingored = settings.ignored_directory_indexes.include?(extensioned_path)
extensioned_path = env["PATH_INFO"].sub(/\/$/, "") + File.extname(self.index_file)
is_ignored = self.ignored_directory_indexes.include?(extensioned_path)
if !indexed_exists && !is_ingored
parts = request.path_info.split("/")
if !indexed_exists && !is_ignored
parts = env["PATH_INFO"].split("/")
last_part = parts.last
last_part_ext = File.extname(last_part)
if last_part_ext.blank?
# This is a folder, redirect to index
request.path_info = extensioned_path
env["PATH_INFO"] = extensioned_path
end
end
end
# app.build_reroute do |destination, request_path|
# index_ext = File.extname(app.settings.index_file)
# new_index_path = "/#{app.settings.index_file}"
#
# indexed_path = request_path.gsub(/\/$/, "") + index_ext
#
# if app.settings.ignored_directory_indexes.include?(request_path)
# false
# elsif request_path =~ /#{new_index_path}$/
# false
# else
# [
# destination.gsub(/#{index_ext.gsub(".", "\\.")}$/, new_index_path),
# request_path
# ]
# end
# end
end
alias :included :registered
end
module ClassMethods
module InstanceMethods
def ignored_directory_indexes
@_ignored_directory_indexes ||= []
end
def page(url, options={}, &block)
if options.has_key?(:directory_index) && !options["directory_index"]
settings.ignored_directory_indexes << url
ignored_directory_indexes << url
else
super
end

View file

@ -9,13 +9,8 @@ module Middleman::Renderers::Liquid
app.after_configuration do
Liquid::Template.file_system = Liquid::LocalFileSystem.new(self.source_dir)
app.before_processing(:liquid) do |result|
if result && result[1] == :liquid
request['custom_locals'] ||= {}
request['custom_locals'][:data] = data.to_h
end
true
provides_metadata %r{\.liquid$} do |path|
@locals.merge!(:data => data.to_h)
end
end
rescue LoadError