diff --git a/LICENSE b/LICENSE index 5ce6144d..ccd48240 100755 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2009 Thomas Reynolds +Copyright (c) 2010 Thomas Reynolds Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the diff --git a/Rakefile b/Rakefile index 3fb53724..22787a0f 100644 --- a/Rakefile +++ b/Rakefile @@ -25,7 +25,7 @@ begin gem.add_dependency("compass", "~>0.10.0") gem.add_dependency("json_pure", "~>1.4.0") gem.add_dependency("smusher", "~>0.4.5") - gem.add_dependency("compass-slickmap", "~>0.3.0") + gem.add_dependency("compass-slickmap", "~>0.4.0") # gem.add_dependency("livereload", "~>1.4.0") gem.add_development_dependency("rspec") diff --git a/bin/mm-build b/bin/mm-build index 9ab89010..3fa9ac70 100755 --- a/bin/mm-build +++ b/bin/mm-build @@ -6,7 +6,7 @@ ENV['MM_ENV'] = "build" require File.join(File.dirname(__FILE__), "..", "lib", "middleman") require 'middleman/builder' -# Middleman::Base.init! +# Middleman::Server.init! Middleman::Builder.init! Middleman::Generators.run_cli(Dir.pwd, 'mm-build', 1, %w(build --force).concat(ARGV)) \ No newline at end of file diff --git a/bin/mm-init b/bin/mm-init index eb816c79..a1a3f8d9 100755 --- a/bin/mm-init +++ b/bin/mm-init @@ -22,7 +22,7 @@ module Generators File.join(File.dirname(__FILE__), '..', 'lib', 'middleman', 'template') end - template :init, "init.rb" + template :config, "config.rb" glob! :views glob! :public diff --git a/bin/mm-server b/bin/mm-server index acbd5f0a..0a82130b 100755 --- a/bin/mm-server +++ b/bin/mm-server @@ -36,7 +36,7 @@ OptionParser.new { |opts| env = e } opts.on("--debug", "Debug mode") { - ::Middleman::Base.set :logging, true + ::Middleman::Server.set :logging, true } opts.parse! ARGV @@ -44,13 +44,13 @@ OptionParser.new { |opts| ENV['RACK_ENV'] = env -class Middleman::Base +class Middleman::Server set :root, Dir.pwd end require 'shotgun' config = File.join(File.dirname(__FILE__), '..', 'lib', 'middleman', 'config.ru') -app = Shotgun.new(config, &lambda { |inner_app| Middleman::Base }) +app = Shotgun.new(config, &lambda { |inner_app| Middleman::Server }) require 'rubygems' require 'thin' diff --git a/features/step_definitions/asset_host_steps.rb b/features/step_definitions/asset_host_steps.rb index 75c2c518..e84e3eb2 100644 --- a/features/step_definitions/asset_host_steps.rb +++ b/features/step_definitions/asset_host_steps.rb @@ -1,7 +1,7 @@ Given /^I am using an asset host$/ do - Middleman::Base.enable :asset_host - Middleman::Base.set :asset_host do |asset| + Middleman::Server.enable :asset_host + Middleman::Server.set :asset_host do |asset| "http://assets%d.example.com" % (asset.hash % 4) end - @browser = Rack::Test::Session.new(Rack::MockSession.new(Middleman::Base.new)) + @browser = Rack::Test::Session.new(Rack::MockSession.new(Middleman::Server.new)) end \ No newline at end of file diff --git a/features/step_definitions/middleman_steps.rb b/features/step_definitions/middleman_steps.rb index 60cd9d5b..d8104091 100644 --- a/features/step_definitions/middleman_steps.rb +++ b/features/step_definitions/middleman_steps.rb @@ -1,7 +1,7 @@ Given /^"([^\"]*)" feature is "([^\"]*)"$/ do |feature, state| enable_or_disable = (state == "enabled") ? :enable : :disable - Middleman::Base.send(enable_or_disable, feature.to_sym) - @browser = Rack::Test::Session.new(Rack::MockSession.new(Middleman::Base.new)) + Middleman::Server.send(enable_or_disable, feature.to_sym) + @browser = Rack::Test::Session.new(Rack::MockSession.new(Middleman::Server.new)) end Given /^generated directory at "([^\"]*)"$/ do |dirname| @@ -23,6 +23,5 @@ Then /^I should not see "([^\"]*)"$/ do |expected| end Then /^I should see "([^\"]*)" lines$/ do |lines| - $stderr.puts @browser.last_response.body @browser.last_response.body.chomp.split($/).length.should == lines.to_i end \ No newline at end of file diff --git a/features/step_definitions/page_layout_steps.rb b/features/step_definitions/page_layout_steps.rb index c738ffc5..05f23c48 100644 --- a/features/step_definitions/page_layout_steps.rb +++ b/features/step_definitions/page_layout_steps.rb @@ -1,13 +1,13 @@ Given /^page "([^\"]*)" has layout "([^\"]*)"$/ do |url, layout| - Middleman::Base.set :root, File.join(File.dirname(File.dirname(File.dirname(__FILE__))), "spec", "fixtures", "sample") - Middleman::Base.page(url, :layout => layout.to_sym) - @browser = Rack::Test::Session.new(Rack::MockSession.new(Middleman::Base.new)) + Middleman::Server.set :root, File.join(File.dirname(File.dirname(File.dirname(__FILE__))), "spec", "fixtures", "sample") + Middleman::Server.page(url, :layout => layout.to_sym) + @browser = Rack::Test::Session.new(Rack::MockSession.new(Middleman::Server.new)) end Given /^"([^\"]*)" with_layout block has layout "([^\"]*)"$/ do |url, layout| - Middleman::Base.set :root, File.join(File.dirname(File.dirname(File.dirname(__FILE__))), "spec", "fixtures", "sample") - Middleman::Base.with_layout(:layout => layout.to_sym) do + Middleman::Server.set :root, File.join(File.dirname(File.dirname(File.dirname(__FILE__))), "spec", "fixtures", "sample") + Middleman::Server.with_layout(:layout => layout.to_sym) do page(url) end - @browser = Rack::Test::Session.new(Rack::MockSession.new(Middleman::Base.new)) + @browser = Rack::Test::Session.new(Rack::MockSession.new(Middleman::Server.new)) end \ No newline at end of file diff --git a/lib/middleman.rb b/lib/middleman.rb index 9556fad7..64c36f06 100755 --- a/lib/middleman.rb +++ b/lib/middleman.rb @@ -4,5 +4,5 @@ $LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir) require 'rubygems' module Middleman - autoload :Base, "middleman/base" + autoload :Server, "middleman/server" end diff --git a/lib/middleman/assets.rb b/lib/middleman/assets.rb index 2cacb035..0febf00e 100644 --- a/lib/middleman/assets.rb +++ b/lib/middleman/assets.rb @@ -29,5 +29,5 @@ module Middleman end Middleman::Assets.register :base do |path, prefix, request| - path.include?("://") ? path : File.join(Middleman::Base.http_prefix || "/", prefix, path) + path.include?("://") ? path : File.join(Middleman::Server.http_prefix || "/", prefix, path) end \ No newline at end of file diff --git a/lib/middleman/base.rb b/lib/middleman/base.rb deleted file mode 100644 index bbc89fba..00000000 --- a/lib/middleman/base.rb +++ /dev/null @@ -1,147 +0,0 @@ -# We're riding on Sinatra, so let's include it -require "sinatra/base" -require "sinatra/content_for" - -class Sinatra::Request - attr_accessor :layout -end - -module Middleman - class Base < Sinatra::Base - set :app_file, __FILE__ - set :root, ENV["MM_DIR"] || Dir.pwd - set :reload, false - set :sessions, false - set :logging, false - set :environment, ENV['MM_ENV'] || :development - set :index_file, "index.html" - set :js_dir, "javascripts" - set :css_dir, "stylesheets" - set :images_dir, "images" - set :fonts_dir, "fonts" - set :build_dir, "build" - set :http_prefix, nil - - helpers Sinatra::ContentFor - - set :features, [] - def self.enable(*opts) - set :features, (self.features << opts).flatten - super - end - - def self.disable(*opts) - current = self.features - current -= opts.flatten - set :features, current - super - end - - def self.set(option, value=self, &block) - if block_given? - value = Proc.new { block } - end - - super(option, value, &nil) - end - - @@afters = [] - def self.after_feature_init(&block) - @@afters << block - end - - # Rack helper for adding mime-types during local preview - def self.mime(ext, type) - ext = ".#{ext}" unless ext.to_s[0] == ?. - ::Rack::Mime::MIME_TYPES[ext.to_s] = type - end - - @@layout = nil - def self.page(url, options={}, &block) - layout = @@layout - layout = options[:layout] if !options[:layout].nil? - - get(url) do - return yield if block_given? - process_request(layout) - end - end - - def self.with_layout(layout, &block) - @@layout = layout - class_eval(&block) if block_given? - ensure - @@layout = nil - end - - def self.enabled?(name) - name = (name.to_s << "?").to_sym - self.respond_to?(name) && self.send(name) - end - - def enabled?(name) - self.class.enabled?(name) - end - - # This will match all requests not overridden in the project's init.rb - not_found do - process_request - end - - private - def process_request(layout = :layout) - # Normalize the path and add index if we're looking at a directory - path = request.path - path << settings.index_file if path.match(%r{/$}) - path.gsub!(%r{^/}, '') - - template_path = locate_template_file(path) - if template_path - content_type mime_type(File.extname(path)), :charset => 'utf-8' - - renderer = Middleman::Renderers.get_method(template_path) - if respond_to? renderer - status 200 - return send(renderer, path.to_sym, { :layout => layout }) - end - end - - status 404 - end - - def locate_template_file(path) - template_path = File.join(settings.views, "#{path}.*") - Dir.glob(template_path).first - end - end -end - -require "middleman/assets" -require "middleman/renderers" -require "middleman/features" - -# The Rack App -class Middleman::Base - def self.new(*args, &block) - # Check for and evaluate local configuration - local_config = File.join(self.root, "init.rb") - if File.exists? local_config - puts "== Reading: Local config" if logging? - Middleman::Base.class_eval File.read(local_config) - set :app_file, File.expand_path(local_config) - end - - # loop over enabled feature - features.flatten.each do |feature_name| - next unless send(:"#{feature_name}?") - $stderr.puts "== Enabling: #{feature_name.to_s.capitalize}" if logging? - Middleman::Features.run(feature_name, self) - end - - use ::Rack::ConditionalGet if environment == :development - - @@afters.each { |block| class_eval(&block) } - - super - end -end diff --git a/lib/middleman/builder.rb b/lib/middleman/builder.rb index db2094ec..97fbb1d0 100644 --- a/lib/middleman/builder.rb +++ b/lib/middleman/builder.rb @@ -1,4 +1,4 @@ -require 'middleman/base' +require 'middleman/server' require 'templater' require 'middleman/templater+dynamic_renderer.rb' @@ -6,11 +6,12 @@ require 'middleman/templater+dynamic_renderer.rb' module Middleman class Builder < ::Templater::Generator + # Support all Tilt-enabled templates and treat js like a template @@template_extensions = ::Tilt.mappings.keys << "js" # Define source and desintation def self.source_root; Dir.pwd; end - def destination_root; File.join(Dir.pwd, Middleman::Base.build_dir); end + def destination_root; File.join(Dir.pwd, Middleman::Server.build_dir); end # Override template to ask middleman for the correct extension to output def self.template(name, *args, &block) @@ -21,7 +22,7 @@ module Middleman end if (args[0] === args[1]) - args[1] = args[0].gsub("#{File.basename(Middleman::Base.views)}/", "").gsub("#{File.basename(Middleman::Base.public)}/", "") + args[1] = args[0].gsub("#{File.basename(Middleman::Server.views)}/", "").gsub("#{File.basename(Middleman::Server.public)}/", "") if File.extname(args[1]) != ".js" args[1] = args[1].gsub!(File.extname(args[1]), "") if File.basename(args[1]).split('.').length > 2 end @@ -36,14 +37,14 @@ module Middleman return unless ::Tilt[file_ext].nil? if (args[0] === args[1]) - args[1] = args[0].gsub("#{File.basename(Middleman::Base.views)}/", "").gsub("#{File.basename(Middleman::Base.public)}/", "") + args[1] = args[0].gsub("#{File.basename(Middleman::Server.views)}/", "").gsub("#{File.basename(Middleman::Server.public)}/", "") end super(name, *args, &block) end def self.init! - glob! File.basename(Middleman::Base.public), @@template_extensions - glob! File.basename(Middleman::Base.views), @@template_extensions + glob! File.basename(Middleman::Server.public), @@template_extensions + glob! File.basename(Middleman::Server.views), @@template_extensions end def after_run diff --git a/lib/middleman/config.ru b/lib/middleman/config.ru index de3cfdb9..86f589c0 100755 --- a/lib/middleman/config.ru +++ b/lib/middleman/config.ru @@ -1,2 +1,2 @@ require 'middleman' -run Middleman::Base \ No newline at end of file +run Middleman::Server \ No newline at end of file diff --git a/lib/middleman/features.rb b/lib/middleman/features.rb index f0d3faae..5a1a03dd 100644 --- a/lib/middleman/features.rb +++ b/lib/middleman/features.rb @@ -7,14 +7,16 @@ module Middleman # Default to disabled, unless the class asks to auto-enable activate_method = (options.has_key?(:auto_enable) && options[:auto_enable]) ? :enable : :disable - Middleman::Base.send(activate_method, feature_name) + Middleman::Server.send(activate_method, feature_name) end - def self.run(feature_name, scope) + # Initialize a feature + def self.run(feature_name, feature_config, scope) feature_class = @@features[feature_name] - feature_class.new(scope) unless feature_class.nil? + feature_class.new(scope, feature_config) unless feature_class.nil? end + # Get a list of all features def self.all @@features end diff --git a/lib/middleman/features/asset_host.rb b/lib/middleman/features/asset_host.rb index d363fbc3..fd422b41 100644 --- a/lib/middleman/features/asset_host.rb +++ b/lib/middleman/features/asset_host.rb @@ -1,8 +1,8 @@ class Middleman::Features::AssetHost - def initialize(app) - Middleman::Base.after_feature_init do - if Middleman::Base.asset_host.is_a?(Proc) - ::Compass.configuration.asset_host(&Middleman::Base.asset_host) + def initialize(app, config) + Middleman::Server.after_feature_init do + if Middleman::Server.asset_host.is_a?(Proc) + ::Compass.configuration.asset_host(&Middleman::Server.asset_host) end end @@ -11,7 +11,7 @@ class Middleman::Features::AssetHost valid_extensions = %w(.png .gif .jpg .jpeg .js .css) - asset_prefix = Middleman::Base.asset_host.call(original_output) + asset_prefix = Middleman::Server.asset_host.call(original_output) File.join(asset_prefix, original_output) end diff --git a/lib/middleman/features/automatic_image_sizes.rb b/lib/middleman/features/automatic_image_sizes.rb index c5b0210f..92b9ea1e 100755 --- a/lib/middleman/features/automatic_image_sizes.rb +++ b/lib/middleman/features/automatic_image_sizes.rb @@ -1,9 +1,9 @@ class Middleman::Features::AutomaticImageSizes - def initialize(app) + def initialize(app, config) require "middleman/features/automatic_image_sizes/fastimage" - Middleman::Base.send :alias_method, :pre_automatic_image_tag, :image_tag - Middleman::Base.helpers do + Middleman::Server.send :alias_method, :pre_automatic_image_tag, :image_tag + Middleman::Server.helpers do def image_tag(path, params={}) if (!params[:width] || !params[:height]) && !path.include?("://") params[:alt] ||= "" diff --git a/lib/middleman/features/cache_buster.rb b/lib/middleman/features/cache_buster.rb index 6c0befd5..5e06af1c 100755 --- a/lib/middleman/features/cache_buster.rb +++ b/lib/middleman/features/cache_buster.rb @@ -1,5 +1,5 @@ class Middleman::Features::CacheBuster - def initialize(app) + def initialize(app, config) Middleman::Assets.register :cache_buster do |path, prefix, request| http_path = Middleman::Assets.before(:cache_buster, path, prefix, request) @@ -7,16 +7,16 @@ class Middleman::Features::CacheBuster http_path else begin - prefix = Middleman::Base.images_dir if prefix == Middleman::Base.http_images_path + prefix = Middleman::Server.images_dir if prefix == Middleman::Server.http_images_path rescue end - real_path_static = File.join(Middleman::Base.public, prefix, path) + real_path_static = File.join(Middleman::Server.public, prefix, path) if File.readable?(real_path_static) http_path << "?" + File.mtime(real_path_static).strftime("%s") - elsif Middleman::Base.environment == "build" - real_path_dynamic = File.join(Middleman::Base.root, Middleman::Base.build_dir, prefix, path) + elsif Middleman::Server.environment == :build + real_path_dynamic = File.join(Middleman::Server.root, Middleman::Server.build_dir, prefix, path) http_path << "?" + File.mtime(real_path_dynamic).strftime("%s") if File.readable?(real_path_dynamic) end @@ -24,11 +24,11 @@ class Middleman::Features::CacheBuster end end - Middleman::Base.after_feature_init do + Middleman::Server.after_feature_init do ::Compass.configuration do |config| config.asset_cache_buster do |path, real_path| real_path = real_path.path if real_path.is_a? File - real_path = real_path.gsub(File.join(Middleman::Base.root, Middleman::Base.build_dir), Middleman::Base.public) + real_path = real_path.gsub(File.join(Middleman::Server.root, Middleman::Server.build_dir), Middleman::Server.public) if File.readable?(real_path) File.mtime(real_path).strftime("%s") else diff --git a/lib/middleman/features/default_helpers.rb b/lib/middleman/features/default_helpers.rb index 16690391..8de71c2a 100644 --- a/lib/middleman/features/default_helpers.rb +++ b/lib/middleman/features/default_helpers.rb @@ -1,6 +1,6 @@ class Middleman::Features::DefaultHelpers - def initialize(app) - Middleman::Base.helpers Helpers + def initialize(app, config) + Middleman::Server.helpers Helpers end module Helpers diff --git a/lib/middleman/features/livereload.rb b/lib/middleman/features/livereload.rb index 56a776c0..872aaa8f 100644 --- a/lib/middleman/features/livereload.rb +++ b/lib/middleman/features/livereload.rb @@ -1,6 +1,6 @@ class Middleman::Features::LiveReload - def initialize(app) - return unless Middleman::Base.environment == :development + def initialize(app, config) + return unless Middleman::Server.environment == :development begin require 'livereload' @@ -12,7 +12,7 @@ class Middleman::Features::LiveReload config.exts = %w(haml sass scss coffee less builder) end - ::LiveReload.run [Middleman::Base.public, Middleman::Base.views], new_config + ::LiveReload.run [Middleman::Server.public, Middleman::Server.views], new_config end end diff --git a/lib/middleman/features/minify_css.rb b/lib/middleman/features/minify_css.rb index 158799d9..a4f348a2 100644 --- a/lib/middleman/features/minify_css.rb +++ b/lib/middleman/features/minify_css.rb @@ -1,6 +1,6 @@ class Middleman::Features::MinifyCSS - def initialize(app) - Middleman::Base.after_feature_init do + def initialize(app, config) + Middleman::Server.after_feature_init do ::Compass.configuration.output_style = :compressed end end diff --git a/lib/middleman/features/minify_javascript.rb b/lib/middleman/features/minify_javascript.rb index eb812b9f..0d3ba073 100755 --- a/lib/middleman/features/minify_javascript.rb +++ b/lib/middleman/features/minify_javascript.rb @@ -1,5 +1,5 @@ class Middleman::Features::MinifyJavascript - def initialize(app) + def initialize(app, config) Haml::Javascript.send :include, ::Haml::Filters::Base require "middleman/features/minify_javascript/rack" diff --git a/lib/middleman/features/relative_assets.rb b/lib/middleman/features/relative_assets.rb index f2874a49..0a1277a7 100755 --- a/lib/middleman/features/relative_assets.rb +++ b/lib/middleman/features/relative_assets.rb @@ -1,10 +1,10 @@ class Middleman::Features::RelativeAssets - def initialize(app) + def initialize(app, config) ::Compass.configuration.relative_assets = true Middleman::Assets.register :relative_assets do |path, prefix, request| begin - prefix = Middleman::Base.images_dir if prefix == Middleman::Base.http_images_path + prefix = Middleman::Server.images_dir if prefix == Middleman::Server.http_images_path rescue end @@ -15,7 +15,7 @@ class Middleman::Features::RelativeAssets else path = File.join(prefix, path) if prefix.length > 0 request_path = request.path_info.dup - request_path << Middleman::Base.index_file if path.match(%r{/$}) + request_path << Middleman::Server.index_file if path.match(%r{/$}) request_path.gsub!(%r{^/}, '') parts = request_path.split('/') diff --git a/lib/middleman/features/slickmap.rb b/lib/middleman/features/slickmap.rb index e1ee8dfc..e7ccdaa1 100755 --- a/lib/middleman/features/slickmap.rb +++ b/lib/middleman/features/slickmap.rb @@ -1,44 +1,16 @@ Entry = Struct.new(:dir, :children) class Middleman::Features::Slickmap - def initialize(app) + def initialize(app, config) require 'slickmap' - if Middleman::Base.environment == "build" - Middleman::Builder.template :slickmap, "sitemap.html", "sitemap.html" - end + @sitemap_url = config[:url] || "sitemap.html" - def build_sitemap(&block) - @@utility = [] - [recurse_sitemap(Middleman::Base.views, &block), @@utility] - end - - def recurse_sitemap(path, &block) - bad_ext = path.split('.html')[1] - path = path.gsub(bad_ext, '') if bad_ext - entry = Entry.new(path, []) - - #no "." or ".." dirs - Dir[File.join(path, "*")].each do |e| - next if !File.directory?(e) && !e.include?(".html") - if File.directory?(e) - entry.children << recurse_sitemap(e, &block) - elsif block_given? - how_to_handle = block.call(e) - if how_to_handle == :valid - entry.children << recurse_sitemap(e, &block) - elsif how_to_handle == :utility - bad_ext = e.split('.html')[1] - e = e.gsub(bad_ext, '') if bad_ext - @@utility << e.gsub(Middleman::Base.views + "/", '') - end - end - end - - entry + if Middleman::Server.environment == :build + Middleman::Builder.template :slickmap, @sitemap_url, @sitemap_url end - Middleman::Base.helpers do + Middleman::Server.helpers do def sitemap_node(n, first=false) if n.children.length < 1 if !first && File.extname(n.dir).length > 0 @@ -72,47 +44,45 @@ class Middleman::Features::Slickmap end end - Middleman::Base.get '/sitemap.html' do + Middleman::Server.get "/#{@sitemap_url}" do # Return :utility to put it util top menu. False to ignore - @tree, @utility = build_sitemap do |file_name| + @tree, @utility = Middleman::Features::Slickmap.build_sitemap do |file_name| :valid end - haml :sitemap, :layout => false + + haml "template.html".to_sym, :layout => false, :views => File.expand_path(File.join(File.dirname(__FILE__), "slickmap")) + end + end + + def self.build_sitemap(&block) + @@utility = [] + [recurse_sitemap(Middleman::Server.views, &block), @@utility] + end + + def self.recurse_sitemap(path, &block) + bad_ext = path.split('.html')[1] + path = path.gsub(bad_ext, '') if bad_ext + entry = Entry.new(path, []) + + #no "." or ".." dirs + Dir[File.join(path, "*")].each do |e| + next if !File.directory?(e) && !e.include?(".html") + if File.directory?(e) + entry.children << recurse_sitemap(e, &block) + elsif block_given? + how_to_handle = block.call(e) + if how_to_handle == :valid + entry.children << recurse_sitemap(e, &block) + elsif how_to_handle == :utility + bad_ext = e.split('.html')[1] + e = e.gsub(bad_ext, '') if bad_ext + @@utility << e.gsub(Middleman::Server.views + "/", '') + end + end end - Middleman::Base.use_in_file_templates! + entry end end -Middleman::Features.register :slickmap, Middleman::Features::Slickmap - -__END__ - -@@ sitemap -!!! -%html{ :xmlns => "http://www.w3.org/1999/xhtml" } - %head - %meta{ :content => "text/html; charset=utf-8", "http-equiv" => "Content-type" } - %title Sitemap - %style{ :type => "text/css" } - :sass - @import "slickmap" - +slickmap - :javascript - window.onload = function() { - document.getElementById('primaryNav').className = "col" + document.querySelectorAll("#primaryNav > li:not(#home)").length; - }; - - %body - .logo - %h1= @project_name || "Sitemap" - - if @project_subtitle - %h2= @project_subtitle - - - if @utility.length > 0 - %ul#utilityNav - - @utility.each do |u| - %li= link_to u, u - - %ul#primaryNav - - sitemap_node(@tree, true) \ No newline at end of file +Middleman::Features.register :slickmap, Middleman::Features::Slickmap \ No newline at end of file diff --git a/lib/middleman/features/slickmap/template.html.haml b/lib/middleman/features/slickmap/template.html.haml new file mode 100644 index 00000000..ef301efb --- /dev/null +++ b/lib/middleman/features/slickmap/template.html.haml @@ -0,0 +1,27 @@ +!!! +%html{ :xmlns => "http://www.w3.org/1999/xhtml" } + %head + %meta{ :content => "text/html; charset=utf-8", "http-equiv" => "Content-type" } + %title Sitemap + %style{ :type => "text/css" } + :sass + @import "slickmap" + +slickmap + :javascript + window.onload = function() { + document.getElementById('primaryNav').className = "col" + document.querySelectorAll("#primaryNav > li:not(#home)").length; + }; + + %body + .logo + %h1= @project_name || "Sitemap" + - if @project_subtitle + %h2= @project_subtitle + + - if @utility.length > 0 + %ul#utilityNav + - @utility.each do |u| + %li= link_to u, u + + %ul#primaryNav + - sitemap_node(@tree, true) \ No newline at end of file diff --git a/lib/middleman/features/smush_pngs.rb b/lib/middleman/features/smush_pngs.rb index b3cd7759..8f293382 100644 --- a/lib/middleman/features/smush_pngs.rb +++ b/lib/middleman/features/smush_pngs.rb @@ -1,14 +1,14 @@ class Middleman::Features::SmushPngs - def initialize(app) + def initialize(app, config) require "middleman/builder" - Middleman::Base.alias_method :pre_smush_after_run, :after_run - Middleman::Base.define_method :after_run do + Middleman::Server.alias_method :pre_smush_after_run, :after_run + Middleman::Server.define_method :after_run do pre_smush_after_run - smush_dir = File.join(Middleman::Base.build_dir, Middleman::Base.images_dir) + smush_dir = File.join(Middleman::Server.build_dir, Middleman::Server.images_dir) # Read cache - cache_file = File.join(Middleman::Base.root, ".smush-cache") + cache_file = File.join(Middleman::Server.root, ".smush-cache") cache_data = if File.exists?(cache_file) Marshal.restore(File.read(cache_file)) else @@ -27,7 +27,7 @@ class Middleman::Features::SmushPngs write_optimized_data(file) cache_data[file] = size(file) # Add or update cache File.open(cache_file, "w") { |f| f.write Marshal.dump(cache_data) } # Write cache - say "<%= color('#{"[SMUSHED]".rjust(12)}', :yellow) %> " + file.gsub(Middleman::Base.build_dir+"/", '') + say "<%= color('#{"[SMUSHED]".rjust(12)}', :yellow) %> " + file.gsub(Middleman::Server.build_dir+"/", '') end end end diff --git a/lib/middleman/features/ugly_haml.rb b/lib/middleman/features/ugly_haml.rb index cbc159aa..31805db0 100644 --- a/lib/middleman/features/ugly_haml.rb +++ b/lib/middleman/features/ugly_haml.rb @@ -1,6 +1,6 @@ class Middleman::Features::UglyHaml - def initialize(app) - Middleman::Base.set :haml, Middleman::Base.settings.haml.merge({ :ugly_haml => true }) + def initialize(app, config) + Middleman::Server.set :haml, Middleman::Server.settings.haml.merge({ :ugly_haml => true }) end end diff --git a/lib/middleman/renderers/coffee.rb b/lib/middleman/renderers/coffee.rb index 2e2c5a39..d96b31b9 100755 --- a/lib/middleman/renderers/coffee.rb +++ b/lib/middleman/renderers/coffee.rb @@ -1,4 +1,4 @@ -class Middleman::Base +class Middleman::Server def coffee(template, options={}, locals={}) options[:layout] = false render :coffee, template, options, locals diff --git a/lib/middleman/renderers/haml.rb b/lib/middleman/renderers/haml.rb index 2b315a5a..0e528d60 100755 --- a/lib/middleman/renderers/haml.rb +++ b/lib/middleman/renderers/haml.rb @@ -42,6 +42,6 @@ module Middleman end end -class Middleman::Base +class Middleman::Server helpers Middleman::Haml::Helpers end \ No newline at end of file diff --git a/lib/middleman/renderers/sass.rb b/lib/middleman/renderers/sass.rb index 3fa01427..752dac23 100644 --- a/lib/middleman/renderers/sass.rb +++ b/lib/middleman/renderers/sass.rb @@ -1,7 +1,7 @@ require "sass" require "compass" -class Middleman::Base +class Middleman::Server def scss(template, options={}, locals={}) options[:layout] = false render :scss, template, options, locals @@ -34,13 +34,13 @@ end class Tilt::SassPlusCSSFilenameTemplate < Tilt::SassTemplate def sass_options - location_of_sass_file = Middleman::Base.environment == "build" ? - File.join(Middleman::Base.root, Middleman::Base.build_dir) : - Middleman::Base.public + location_of_sass_file = Middleman::Server.environment == :build ? + File.join(Middleman::Server.root, Middleman::Server.build_dir) : + Middleman::Server.public parts = basename.split('.') parts.pop - css_filename = File.join(location_of_sass_file, Middleman::Base.css_dir, parts.join(".")) + css_filename = File.join(location_of_sass_file, Middleman::Server.css_dir, parts.join(".")) super.merge(::Compass.configuration.to_sass_engine_options).merge(:css_filename => css_filename) end end diff --git a/lib/middleman/server.rb b/lib/middleman/server.rb new file mode 100644 index 00000000..e686a59e --- /dev/null +++ b/lib/middleman/server.rb @@ -0,0 +1,168 @@ +# We're riding on Sinatra, so let's include it. +require "sinatra/base" + +# The content_for plugin allows Sinatra to use the throw/yield block +# system similar to Rails views. +require "sinatra/content_for" + +# Monkey-patch Sinatra to expose the layout parameter +class Sinatra::Request + attr_accessor :layout +end + +module Middleman + class Server < Sinatra::Base + # Basic Sinatra config + set :app_file, __FILE__ + set :root, ENV["MM_DIR"] || Dir.pwd + set :reload, false + set :sessions, false + set :logging, false + set :environment, (ENV['MM_ENV'] && ENV['MM_ENV'].to_sym) || :development + + # Import content_for methods + helpers Sinatra::ContentFor + + # Middleman-specific options + set :index_file, "index.html" # What file responds to folder requests + # Such as the homepage (/) or subfolders (/about/) + + # These directories are passed directly to Compass + set :js_dir, "javascripts" # Where to look for javascript files + set :css_dir, "stylesheets" # Where to look for CSS files + set :images_dir, "images" # Where to look for images + set :fonts_dir, "fonts" # Where to look for fonts + + set :build_dir, "build" # Which folder are builds output to + set :http_prefix, nil # During build, add a prefix for absolute paths + + # A hash of enabled features + @@enabled_features = {} + + # Override Sinatra's enable to keep track of enabled features + def self.enable(feature_name, config={}) + @@enabled_features[feature_name] = config + super(feature_name) + end + + # Disable a feature, then pass to Sinatra's method + def self.disable(feature_name) + @@enabled_features.delete(feature_name) + super(feature_name) + end + + # Override Sinatra's set to accept a block + def self.set(option, value=self, &block) + if block_given? + value = Proc.new { block } + end + + super(option, value, &nil) + end + + # An array of callback procs to run after all features have been setup + @@run_after_features = [] + + # Add a block/proc to be run after features have been setup + def self.after_feature_init(&block) + @@run_after_features << block + end + + # Rack helper for adding mime-types during local preview + def self.mime(ext, type) + ext = ".#{ext}" unless ext.to_s[0] == ?. + ::Rack::Mime::MIME_TYPES[ext.to_s] = type + end + + # Keep track of a block-specific layout + @@layout = nil + + # Takes a block which allows many pages to have the same layout + # with_layout :admin do + # page "/admin/" + # page "/admin/login.html" + # end + def self.with_layout(layout, &block) + @@layout = layout + class_eval(&block) if block_given? + ensure + @@layout = nil + end + + # The page method allows the layout to be set on a specific path + # page "/about.html", :layout => false + # page "/", :layout => :homepage_layout + def self.page(url, options={}, &block) + layout = @@layout + layout = options[:layout] if !options[:layout].nil? + + get(url) do + return yield if block_given? + process_request(layout) + end + end + + # This will match all requests not overridden in the project's config.rb + not_found do + process_request + end + + private + # Internal method to look for templates and evaluate them if found + def process_request(layout = :layout) + # Normalize the path and add index if we're looking at a directory + path = request.path + path << settings.index_file if path.match(%r{/$}) + path.gsub!(%r{^/}, '') + + if template_path = Dir.glob(File.join(settings.views, "#{path}.*")).first + content_type mime_type(File.extname(path)), :charset => 'utf-8' + + renderer = Middleman::Renderers.get_method(template_path) + if respond_to? renderer + status 200 + return send(renderer, path.to_sym, { :layout => layout }) + end + end + + status 404 + end + end +end + +require "middleman/assets" +require "middleman/renderers" +require "middleman/features" + +# The Rack App +class Middleman::Server + def self.new(*args, &block) + # If the old init.rb exists, use it, but issue warning + old_config = File.join(self.root, "init.rb") + if File.exists? old_config + $stderr.puts "== Warning: The init.rb file has been renamed to config.rb" + local_config = old_config + end + + # Check for and evaluate local configuration + local_config ||= File.join(self.root, "config.rb") + if File.exists? local_config + $stderr.puts "== Reading: Local config" if logging? + Middleman::Server.class_eval File.read(local_config) + set :app_file, File.expand_path(local_config) + end + + # loop over enabled feature + @@enabled_features.each do |feature_name, feature_config| + next unless send(:"#{feature_name}?") + $stderr.puts "== Enabling: #{feature_name.to_s.capitalize}" if logging? + Middleman::Features.run(feature_name, feature_config, self) + end + + use ::Rack::ConditionalGet if environment == :development + + @@run_after_features.each { |block| class_eval(&block) } + + super + end +end diff --git a/lib/middleman/template/init.rbt b/lib/middleman/template/config.rbt similarity index 100% rename from lib/middleman/template/init.rbt rename to lib/middleman/template/config.rbt diff --git a/lib/middleman/templater+dynamic_renderer.rb b/lib/middleman/templater+dynamic_renderer.rb index 257d255f..acdf5055 100755 --- a/lib/middleman/templater+dynamic_renderer.rb +++ b/lib/middleman/templater+dynamic_renderer.rb @@ -14,11 +14,11 @@ end class Templater::Actions::Template def render - @@rack_test ||= Rack::Test::Session.new(Rack::MockSession.new(Middleman::Base)) + @@rack_test ||= Rack::Test::Session.new(Rack::MockSession.new(Middleman::Server)) @render_cache ||= begin # The default render just requests the page over Rack and writes the response - request_path = destination.gsub(File.join(Dir.pwd, Middleman::Base.build_dir), "") + request_path = destination.gsub(File.join(Dir.pwd, Middleman::Server.build_dir), "") @@rack_test.get(request_path) @@rack_test.last_response.body end diff --git a/spec/fixtures/sample/init.rb b/spec/fixtures/sample/config.rb similarity index 100% rename from spec/fixtures/sample/init.rb rename to spec/fixtures/sample/config.rb diff --git a/spec/helpers_spec.rb b/spec/helpers_spec.rb index 739ecbf9..d07d4712 100644 --- a/spec/helpers_spec.rb +++ b/spec/helpers_spec.rb @@ -1,6 +1,6 @@ require File.join(File.dirname(__FILE__), "spec_helper") -base = ::Middleman::Base +base = ::Middleman::Server describe "page_classes helper" do it "should generate root paths correctly" do