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("sprockets")
gem.add_dependency("sinatra")
gem.add_dependency("foca-sinatra-content-for")
gem.add_dependency("brynary-rack-test")
gem.add_dependency("sinatra-content-for")
gem.add_dependency("rack-test")
gem.add_dependency("haml", ">=2.1.0")
gem.add_dependency("chriseppstein-compass")
end

View file

@ -1,11 +1,15 @@
#!/usr/bin/env ruby
require 'templater'
require 'rack-test'
ENV['MM_ENV'] = "build"
# Require app
require File.join(File.dirname(__FILE__), "..", "lib", "middleman")
require 'middleman/builder'
Middleman::Base.init!
module Generators
extend Templater::Manifold
desc "Build a staticmatic site"
@ -13,56 +17,35 @@ module Generators
class Builder < Templater::Generator
# Define source and desintation
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
def self.template(name, *args, &block)
return if args.first.include?('layout')
return if args[0].include?('layout')
args.first.split('/').each do |part|
return if part[0,1] == '_'
end
if (args[0] === args[1])
file_name_parts = File.basename(args.first).split('.')
if file_name_parts.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/', '')
args[1] = args[0].gsub("#{File.basename(Middleman::Base.views)}/", "")
.gsub("#{File.basename(Middleman::Base.public)}/", "")
args[1] = args[1].gsub!(File.extname(args[1]), "") if File.basename(args[1]).split('.').length > 2
end
super(name, *args, &block)
end
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)
end
public_files_glob = File.join(source_root, "public", '**/*')
Dir[public_files_glob].each do |action|
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")
glob! File.basename(Middleman::Base.views), Middleman::Base.supported_formats
glob! File.basename(Middleman::Base.public), Middleman::Base.supported_formats
end
add :build, Builder
@ -71,7 +54,7 @@ end
# Monkey-patch to use a dynamic renderer
class Templater::Actions::Template
def render
::Middleman::Builder.render(source, destination)
::Middleman::Builder.render_file(source, destination)
end
end

View file

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

View file

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

View file

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

View file

@ -1,5 +1,9 @@
module Middleman
module Haml
def self.included(base)
base.supported_formats << "haml"
end
def render_path(path)
if template_exists?(path, :haml)
result = nil
@ -54,52 +58,55 @@ module Middleman
# end
# end
module Sass
def render_path(path)
path = path.dup.gsub('.css', '')
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
module Sass
def self.included(base)
base.supported_formats << "sass"
end
end
# Handle Sass errors
def sass_exception_string(e)
e_string = "#{e.class}: #{e.message}"
def render_path(path)
if template_exists?(path, :sass)
begin
static_version = options.public + request.path_info
send_file(static_version) if File.exists? static_version
if e.is_a? ::Sass::SyntaxError
e_string << "\non line #{e.sass_line}"
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
if e.sass_filename
e_string << " of #{e.sass_filename}"
# Handle Sass errors
def sass_exception_string(e)
e_string = "#{e.class}: #{e.message}"
if File.exists?(e.sass_filename)
e_string << "\n\n"
if e.is_a? ::Sass::SyntaxError
e_string << "\non line #{e.sass_line}"
min = [e.sass_line - 5, 0].max
begin
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"
if e.sass_filename
e_string << " of #{e.sass_filename}"
if File.exists?(e.sass_filename)
e_string << "\n\n"
min = [e.sass_line - 5, 0].max
begin
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
rescue
e_string << "Couldn't read sass file: #{e.sass_filename}"
end
end
end
end
<<END
<<END
/*
#{e_string}

View file

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

View file

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

View file

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

View file

@ -28,7 +28,7 @@ describe "Builder" do
File.read("#{@root_dir}/build/index.html").should include("<h1>Welcome</h1>")
end
it "should build maruku files" do
xit "should build maruku files" do
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>")
end
@ -58,4 +58,8 @@ describe "Builder" do
it "should minify inline javascript" do
File.readlines("#{@root_dir}/build/inline-js.html").length.should == 9
end
it "should combine javascript" do
File.read("#{@root_dir}/build/javascripts/empty-with-include.js").should include("combo")
end
end

View file

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

View file

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

View file

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