1
0
Fork 0
mirror of https://github.com/middleman/middleman.git synced 2022-11-09 12:20:27 -05:00

test pass

This commit is contained in:
tdreyno 2009-09-29 10:10:47 -07:00
parent 4f11920bba
commit 1a975efa61
19 changed files with 104 additions and 97 deletions

View file

@ -15,8 +15,8 @@ begin
gem.add_dependency("templater") gem.add_dependency("templater")
gem.add_dependency("sprockets") gem.add_dependency("sprockets")
gem.add_dependency("sinatra") gem.add_dependency("sinatra")
gem.add_dependency("foca-sinatra-content-for") gem.add_dependency("sinatra-content-for")
gem.add_dependency("brynary-rack-test") gem.add_dependency("rack-test")
gem.add_dependency("haml", ">=2.1.0") gem.add_dependency("haml", ">=2.1.0")
gem.add_dependency("chriseppstein-compass") gem.add_dependency("chriseppstein-compass")
end end

View file

@ -1,11 +1,15 @@
#!/usr/bin/env ruby #!/usr/bin/env ruby
require 'templater' require 'templater'
require 'rack-test'
ENV['MM_ENV'] = "build"
# Require app # Require app
require File.join(File.dirname(__FILE__), "..", "lib", "middleman")
require 'middleman/builder' require 'middleman/builder'
Middleman::Base.init!
module Generators module Generators
extend Templater::Manifold extend Templater::Manifold
desc "Build a staticmatic site" desc "Build a staticmatic site"
@ -13,56 +17,35 @@ module Generators
class Builder < Templater::Generator class Builder < Templater::Generator
# Define source and desintation # Define source and desintation
def self.source_root; Dir.pwd; end def self.source_root; Dir.pwd; end
def destination_root; File.join(Dir.pwd, 'build'); end def destination_root; File.join(Dir.pwd, Middleman::Base.build_dir); end
# Override template to ask middleman for the correct extension to output # Override template to ask middleman for the correct extension to output
def self.template(name, *args, &block) def self.template(name, *args, &block)
return if args.first.include?('layout') return if args[0].include?('layout')
args.first.split('/').each do |part| args.first.split('/').each do |part|
return if part[0,1] == '_' return if part[0,1] == '_'
end end
if (args[0] === args[1]) if (args[0] === args[1])
file_name_parts = File.basename(args.first).split('.') args[1] = args[0].gsub("#{File.basename(Middleman::Base.views)}/", "")
.gsub("#{File.basename(Middleman::Base.public)}/", "")
if file_name_parts.length > 2 args[1] = args[1].gsub!(File.extname(args[1]), "") if File.basename(args[1]).split('.').length > 2
# static ext embedded in filename
newext = ""
else
# use defaults
newext = case file_name_parts.last
when 'haml', 'erb', 'mab', 'maruku'
'.html'
when 'sass'
'.css'
end
end
args[1] = args[0].gsub(".#{file_name_parts.last}", newext).gsub('views/', '')
end end
super(name, *args, &block) super(name, *args, &block)
end end
def self.file(name, *args, &block) def self.file(name, *args, &block)
args[1] = args[0].gsub('views/', '') if (args[0] === args[1]) if (args[0] === args[1])
args[1] = args[0].gsub("#{File.basename(Middleman::Base.views)}/", "")
.gsub("#{File.basename(Middleman::Base.public)}/", "")
end
super(name, *args, &block) super(name, *args, &block)
end end
public_files_glob = File.join(source_root, "public", '**/*') glob! File.basename(Middleman::Base.views), Middleman::Base.supported_formats
Dir[public_files_glob].each do |action| glob! File.basename(Middleman::Base.public), Middleman::Base.supported_formats
next if File.directory?(action)
action = action.sub("#{source_root}/", '')
template_sym = action.downcase.gsub(/[^a-z0-9]+/, '_').to_sym
if File.extname(action) == '.js' && !action.include?('min')
template(template_sym, action, action.gsub('public/', ''))
else
file(template_sym, action, action.gsub('public/', ''))
end
end
glob! "views", (Middleman.supported_formats << "sass")
end end
add :build, Builder add :build, Builder
@ -71,7 +54,7 @@ end
# Monkey-patch to use a dynamic renderer # Monkey-patch to use a dynamic renderer
class Templater::Actions::Template class Templater::Actions::Template
def render def render
::Middleman::Builder.render(source, destination) ::Middleman::Builder.render_file(source, destination)
end end
end end

View file

@ -6,4 +6,4 @@ require File.join(File.dirname(__FILE__), '..', 'lib', 'middleman')
# Start Middleman # Start Middleman
Middleman::Base.set({ :server => %w[thin webrick], Middleman::Base.set({ :server => %w[thin webrick],
:root => Dir.pwd }) :root => Dir.pwd })
Middleman::Base.init! Middleman::Base.run!

View file

@ -7,10 +7,12 @@ module Middleman
class Base < Sinatra::Base class Base < Sinatra::Base
set :app_file, __FILE__ set :app_file, __FILE__
set :root, Dir.pwd set :root, Dir.pwd
set :environment, :development set :environment, ENV['MM_ENV'] || :development
set :supported_formats, []
set :index_file, 'index.html' set :index_file, 'index.html'
set :css_dir, "stylesheets" set :css_dir, "stylesheets"
set :images_dir, "images" set :images_dir, "images"
set :build_dir, "build"
enable :compass enable :compass
enable :content_for enable :content_for
@ -71,6 +73,7 @@ module Middleman
end end
def self.run!(options={}, &block) def self.run!(options={}, &block)
init!
set options set options
handler = detect_rack_handler handler = detect_rack_handler
handler_name = handler.name.gsub(/.*::/, '') handler_name = handler.name.gsub(/.*::/, '')
@ -111,8 +114,6 @@ module Middleman
require "middleman/features/#{feature_name}" require "middleman/features/#{feature_name}"
end end
end end
run!
end end
end end
end end

View file

@ -1,12 +1,10 @@
require 'middleman' require 'rack/test'
module Middleman module Middleman
class Builder class Builder
def self.render_file(source, destination) def self.render_file(source, destination)
# Middleman.set :environment, :build request_path = destination.gsub(File.join(Dir.pwd, Middleman::Base.build_dir), "")
browser = Rack::Test::Session.new(Rack::MockSession.new(Middleman::Base))
request_path = destination.gsub(File.join(Dir.pwd, 'build'), "")
browser = Rack::Test::Session.new(Rack::MockSession.new(Middleman))
browser.get(request_path) browser.get(request_path)
browser.last_response.body browser.last_response.body
end end

View file

@ -1,5 +1,9 @@
module Middleman module Middleman
module Haml module Haml
def self.included(base)
base.supported_formats << "haml"
end
def render_path(path) def render_path(path)
if template_exists?(path, :haml) if template_exists?(path, :haml)
result = nil result = nil
@ -54,52 +58,55 @@ module Middleman
# end # end
# end # end
module Sass module Sass
def render_path(path) def self.included(base)
path = path.dup.gsub('.css', '') base.supported_formats << "sass"
if template_exists?(path, :sass)
begin
static_version = options.public + request.path_info
send_file(static_version) if File.exists? static_version
location_of_sass_file = defined?(MIDDLEMAN_BUILDER) ? "build" : "views"
css_filename = File.join(Dir.pwd, location_of_sass_file) + request.path_info
sass(path.to_sym, Compass.sass_engine_options.merge({ :css_filename => css_filename }))
rescue Exception => e
sass_exception_string(e)
end
else
super
end end
end
# Handle Sass errors def render_path(path)
def sass_exception_string(e) if template_exists?(path, :sass)
e_string = "#{e.class}: #{e.message}" begin
static_version = options.public + request.path_info
send_file(static_version) if File.exists? static_version
if e.is_a? ::Sass::SyntaxError location_of_sass_file = defined?(MIDDLEMAN_BUILDER) ? "build" : "views"
e_string << "\non line #{e.sass_line}" css_filename = File.join(Dir.pwd, location_of_sass_file) + request.path_info
sass(path.to_sym, Compass.sass_engine_options.merge({ :css_filename => css_filename }))
rescue Exception => e
sass_exception_string(e)
end
else
super
end
end
# Handle Sass errors
def sass_exception_string(e)
e_string = "#{e.class}: #{e.message}"
if e.sass_filename if e.is_a? ::Sass::SyntaxError
e_string << " of #{e.sass_filename}" e_string << "\non line #{e.sass_line}"
if File.exists?(e.sass_filename) if e.sass_filename
e_string << "\n\n" e_string << " of #{e.sass_filename}"
min = [e.sass_line - 5, 0].max if File.exists?(e.sass_filename)
begin e_string << "\n\n"
File.read(e.sass_filename).rstrip.split("\n")[
min .. e.sass_line + 5 min = [e.sass_line - 5, 0].max
].each_with_index do |line, i| begin
e_string << "#{min + i + 1}: #{line}\n" File.read(e.sass_filename).rstrip.split("\n")[
min .. e.sass_line + 5
].each_with_index do |line, i|
e_string << "#{min + i + 1}: #{line}\n"
end
rescue
e_string << "Couldn't read sass file: #{e.sass_filename}"
end end
rescue
e_string << "Couldn't read sass file: #{e.sass_filename}"
end end
end end
end end
end <<END
<<END
/* /*
#{e_string} #{e_string}

View file

@ -6,6 +6,10 @@ end
module Middleman module Middleman
module Markaby module Markaby
def self.included(base)
base.supported_formats << "mab"
end
def render_path(path) def render_path(path)
if template_exists?(path, :mab) if template_exists?(path, :mab)
markaby path.to_sym markaby path.to_sym
@ -34,6 +38,6 @@ module Middleman
end end
class Base class Base
include Middlman::Markaby include Middleman::Markaby
end end
end end

View file

@ -6,6 +6,10 @@ end
module Middleman module Middleman
module Maruku module Maruku
def self.included(base)
base.supported_formats << "maruku"
end
def render_path(path) def render_path(path)
if template_exists?(path, :maruku) if template_exists?(path, :maruku)
maruku path.to_sym maruku path.to_sym
@ -33,6 +37,6 @@ module Middleman
end end
class Base class Base
include Middlman::Maruku include Middleman::Maruku
end end
end end

View file

@ -7,6 +7,10 @@ end
module Middleman module Middleman
module Sprockets module Sprockets
def self.included(base)
base.supported_formats << "js"
end
def render_path(path) def render_path(path)
source = File.join(options.public, path) source = File.join(options.public, path)
if File.extname(path) == '.js' && File.exists?(source) if File.extname(path) == '.js' && File.exists?(source)
@ -18,8 +22,8 @@ module Middleman
end end
end end
end end
end
class Base
include Middleman::Sprockets class Middleman::Base
end include Middleman::Sprockets
end end

View file

@ -9,15 +9,15 @@ describe "Builder" do
@root_dir = project_file("spec", "fixtures", "sample") @root_dir = project_file("spec", "fixtures", "sample")
end end
before :each do before :each do
build_cmd = project_file("bin", "mm-build") build_cmd = project_file("bin", "mm-build")
`cd #{@root_dir} && #{build_cmd}` `cd #{@root_dir} && #{build_cmd}`
end end
after :each do after :each do
FileUtils.rm_rf(File.join(@root_dir, "build")) FileUtils.rm_rf(File.join(@root_dir, "build"))
end end
xit "should build markaby files" do xit "should build markaby files" do
File.exists?("#{@root_dir}/build/markaby.html").should be_true File.exists?("#{@root_dir}/build/markaby.html").should be_true
File.read("#{@root_dir}/build/markaby.html").should include("<title>Hi Markaby</title>") File.read("#{@root_dir}/build/markaby.html").should include("<title>Hi Markaby</title>")
@ -28,7 +28,7 @@ describe "Builder" do
File.read("#{@root_dir}/build/index.html").should include("<h1>Welcome</h1>") File.read("#{@root_dir}/build/index.html").should include("<h1>Welcome</h1>")
end end
it "should build maruku files" do xit "should build maruku files" do
File.exists?("#{@root_dir}/build/maruku.html").should be_true File.exists?("#{@root_dir}/build/maruku.html").should be_true
File.read("#{@root_dir}/build/maruku.html").should include("<h1 class='header' id='hello_maruku'>Hello Maruku</h1>") File.read("#{@root_dir}/build/maruku.html").should include("<h1 class='header' id='hello_maruku'>Hello Maruku</h1>")
end end
@ -58,4 +58,8 @@ describe "Builder" do
it "should minify inline javascript" do it "should minify inline javascript" do
File.readlines("#{@root_dir}/build/inline-js.html").length.should == 9 File.readlines("#{@root_dir}/build/inline-js.html").length.should == 9
end end
it "should combine javascript" do
File.read("#{@root_dir}/build/javascripts/empty-with-include.js").should include("combo")
end
end end

View file

@ -1,2 +1,2 @@
require File.join(File.dirname(__FILE__), "..", "..", "..", "lib", "middleman", "maruku") # enable :maruku
#require File.join(File.dirname(__FILE__), "..", "..", "..", "lib", "middleman", "markaby") #enable :markaby

View file

@ -0,0 +1 @@
//= require "to-be-included"

View file

@ -0,0 +1 @@
function() { return "combo"; };