Skip to content

Commit

Permalink
test pass
Browse files Browse the repository at this point in the history
  • Loading branch information
tdreyno committed Sep 29, 2009
1 parent 4f11920 commit 1a975ef
Show file tree
Hide file tree
Showing 19 changed files with 102 additions and 95 deletions.
4 changes: 2 additions & 2 deletions Rakefile
Expand Up @@ -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
Expand Down
57 changes: 20 additions & 37 deletions bin/mm-build
@@ -1,68 +1,51 @@
#!/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"

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])
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/', ''))
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

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
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion bin/mm-server
Expand Up @@ -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!
7 changes: 4 additions & 3 deletions lib/middleman/base.rb
Expand Up @@ -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
Expand Down Expand Up @@ -71,6 +73,7 @@ def render_path(path)
end

def self.run!(options={}, &block)
init!
set options
handler = detect_rack_handler
handler_name = handler.name.gsub(/.*::/, '')
Expand Down Expand Up @@ -111,8 +114,6 @@ def self.init!
require "middleman/features/#{feature_name}"
end
end

run!
end
end
end
10 changes: 4 additions & 6 deletions lib/middleman/builder.rb
@@ -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))
def self.render_file(source, destination)
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
Expand Down
75 changes: 41 additions & 34 deletions lib/middleman/features/haml.rb
@@ -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
Expand Down Expand Up @@ -54,52 +58,55 @@ def render_path(path)
# 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
module Sass
def self.included(base)
base.supported_formats << "sass"
end

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

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)
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
else
super
end
end

# Handle Sass errors
def sass_exception_string(e)
e_string = "#{e.class}: #{e.message}"
# Handle Sass errors
def sass_exception_string(e)
e_string = "#{e.class}: #{e.message}"

if e.is_a? ::Sass::SyntaxError
e_string << "\non line #{e.sass_line}"
if e.is_a? ::Sass::SyntaxError
e_string << "\non line #{e.sass_line}"

if e.sass_filename
e_string << " of #{e.sass_filename}"
if e.sass_filename
e_string << " of #{e.sass_filename}"

if File.exists?(e.sass_filename)
e_string << "\n\n"
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"
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}
Expand Down
6 changes: 5 additions & 1 deletion lib/middleman/features/markaby.rb
Expand Up @@ -6,6 +6,10 @@

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
Expand Down Expand Up @@ -34,6 +38,6 @@ def render_mab(template, data, options, locals, &block)
end

class Base
include Middlman::Markaby
include Middleman::Markaby
end
end
6 changes: 5 additions & 1 deletion lib/middleman/features/maruku.rb
Expand Up @@ -6,6 +6,10 @@

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
Expand Down Expand Up @@ -33,6 +37,6 @@ def render_maruku(data, options, locals, &block)
end

class Base
include Middlman::Maruku
include Middleman::Maruku
end
end
12 changes: 8 additions & 4 deletions lib/middleman/features/sprockets.rb
Expand Up @@ -7,6 +7,10 @@

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)
Expand All @@ -18,8 +22,8 @@ def render_path(path)
end
end
end
class Base
include Middleman::Sprockets
end
end

class Middleman::Base
include Middleman::Sprockets
end
12 changes: 8 additions & 4 deletions spec/builder_spec.rb
Expand Up @@ -9,15 +9,15 @@ def project_file(*parts)
@root_dir = project_file("spec", "fixtures", "sample")
end

before :each do
before :each do
build_cmd = project_file("bin", "mm-build")
`cd #{@root_dir} && #{build_cmd}`
end

after :each do
FileUtils.rm_rf(File.join(@root_dir, "build"))
end

xit "should build markaby files" do
File.exists?("#{@root_dir}/build/markaby.html").should be_true
File.read("#{@root_dir}/build/markaby.html").should include("<title>Hi Markaby</title>")
Expand All @@ -28,7 +28,7 @@ def project_file(*parts)
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
Expand Down Expand Up @@ -58,4 +58,8 @@ def project_file(*parts)
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
4 changes: 2 additions & 2 deletions spec/fixtures/sample/init.rb
@@ -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
@@ -0,0 +1 @@
//= require "to-be-included"
1 change: 1 addition & 0 deletions spec/fixtures/sample/public/javascripts/to-be-included.js
@@ -0,0 +1 @@
function() { return "combo"; };
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 1a975ef

Please sign in to comment.