From 2682bbf6ea1e1cde10eeaf72c78ee19857f20270 Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Thu, 10 Nov 2011 12:26:20 -0800 Subject: [PATCH] simpler guard-based frontmatter, use rackmap everywhere (tests, server, build) --- features/step_definitions/middleman_steps.rb | 4 +- fixtures/clean-app/build/fake/one.html | 4 - fixtures/clean-app/build/fake/two.html | 4 - fixtures/clean-app/build/real/index.html | 4 - lib/middleman/base.rb | 3 +- lib/middleman/builder.rb | 4 +- lib/middleman/core_extensions/compass.rb | 15 +- lib/middleman/core_extensions/data.rb | 8 +- lib/middleman/core_extensions/features.rb | 2 +- lib/middleman/core_extensions/front_matter.rb | 155 +++++++++--------- lib/middleman/core_extensions/rack_map.rb | 5 +- lib/middleman/core_extensions/routing.rb | 8 +- lib/middleman/core_extensions/sitemap.rb | 4 - middleman.gemspec | 4 +- 14 files changed, 104 insertions(+), 120 deletions(-) diff --git a/features/step_definitions/middleman_steps.rb b/features/step_definitions/middleman_steps.rb index c97b746a..18855c78 100644 --- a/features/step_definitions/middleman_steps.rb +++ b/features/step_definitions/middleman_steps.rb @@ -29,7 +29,9 @@ Given /^the Server is running at "([^\"]*)"$/ do |app_path| @server.set :show_exceptions, false root = File.dirname(File.dirname(File.dirname(__FILE__))) @server.set :root, File.join(root, "fixtures", app_path) - @browser = Rack::Test::Session.new(Rack::MockSession.new(@server.new)) + @app = @server.new! + app_rack = @server.build_new(@app) + @browser = Rack::Test::Session.new(Rack::MockSession.new(app_rack)) end When /^I go to "([^\"]*)"$/ do |url| diff --git a/fixtures/clean-app/build/fake/one.html b/fixtures/clean-app/build/fake/one.html index 5037f671..b3c7c8da 100644 --- a/fixtures/clean-app/build/fake/one.html +++ b/fixtures/clean-app/build/fake/one.html @@ -1,5 +1 @@ ---- -layout: false ---- - I am real: one \ No newline at end of file diff --git a/fixtures/clean-app/build/fake/two.html b/fixtures/clean-app/build/fake/two.html index 2513bad9..b9fdebe2 100644 --- a/fixtures/clean-app/build/fake/two.html +++ b/fixtures/clean-app/build/fake/two.html @@ -1,5 +1 @@ ---- -layout: false ---- - I am real: two \ No newline at end of file diff --git a/fixtures/clean-app/build/real/index.html b/fixtures/clean-app/build/real/index.html index a5c39ffa..e9e8914f 100644 --- a/fixtures/clean-app/build/real/index.html +++ b/fixtures/clean-app/build/real/index.html @@ -1,5 +1 @@ ---- -layout: false ---- - I am real: \ No newline at end of file diff --git a/lib/middleman/base.rb b/lib/middleman/base.rb index 93c65d66..f4143475 100644 --- a/lib/middleman/base.rb +++ b/lib/middleman/base.rb @@ -186,7 +186,6 @@ module Middleman::Base # Normalize the path and add index if we're looking at a directory request.path_info = self.class.path_to_index(request.path) - request_path = request.path_info.gsub("%20", " ") found_template = resolve_template(request_path, :raise_exceptions => false) return status(404) unless found_template @@ -206,7 +205,7 @@ module Middleman::Base else false end - + render_options = { :layout => local_layout } render_options[:layout_engine] = options[:layout_engine] if options.has_key? :layout_engine diff --git a/lib/middleman/builder.rb b/lib/middleman/builder.rb index b7084b6d..7a774ff8 100644 --- a/lib/middleman/builder.rb +++ b/lib/middleman/builder.rb @@ -38,7 +38,9 @@ module Middleman def self.shared_rack @shared_rack ||= begin - mock = ::Rack::MockSession.new(SHARED_SERVER) + app = SHARED_SERVER.new! + app_rack = SHARED_SERVER.build_new(app) + mock = ::Rack::MockSession.new(app_rack) sess = ::Rack::Test::Session.new(mock) response = sess.get("__middleman__") sess diff --git a/lib/middleman/core_extensions/compass.rb b/lib/middleman/core_extensions/compass.rb index 640b2959..ec429ea8 100644 --- a/lib/middleman/core_extensions/compass.rb +++ b/lib/middleman/core_extensions/compass.rb @@ -3,10 +3,9 @@ module Middleman::CoreExtensions::Compass def registered(app) # Where to look for fonts app.set :fonts_dir, "fonts" + app.define_hook :compass_config app.define_hook :after_compass_config - - app.extend ClassMethods - + require "compass" # Susy grids @@ -84,16 +83,10 @@ module Middleman::CoreExtensions::Compass end end - run_hook :after_compass_config, ::Compass.configuration + run_hook :compass_config, ::Compass.configuration + run_hook :after_compass_config end end alias :included :registered end - - module ClassMethods - # Add a block/proc to be run after features have been setup - def compass_config(&block) - after_compass_config(&block) - end - end end \ No newline at end of file diff --git a/lib/middleman/core_extensions/data.rb b/lib/middleman/core_extensions/data.rb index 85af4c52..0cdbd1c7 100755 --- a/lib/middleman/core_extensions/data.rb +++ b/lib/middleman/core_extensions/data.rb @@ -20,7 +20,6 @@ module Middleman::CoreExtensions::Data data.remove_file(file) end end - end alias :included :registered end @@ -35,14 +34,11 @@ module Middleman::CoreExtensions::Data def initialize(app) @app = app @local_data = {} - setup - end - - def setup + data_path = File.join(@app.root, @app.data_dir) local_path = File.join(data_path, "*.{yaml,yml,json}") Dir[local_path].each do |f| - touch_file(f)#.sub(data_path, "")) + touch_file(f) end end diff --git a/lib/middleman/core_extensions/features.rb b/lib/middleman/core_extensions/features.rb index 4723715d..71979c01 100644 --- a/lib/middleman/core_extensions/features.rb +++ b/lib/middleman/core_extensions/features.rb @@ -65,7 +65,7 @@ module Middleman::CoreExtensions::Features end # Load features before starting server - def new + def new! # Check for and evaluate local configuration local_config = File.join(self.root, "config.rb") if File.exists? local_config diff --git a/lib/middleman/core_extensions/front_matter.rb b/lib/middleman/core_extensions/front_matter.rb index 6c3a3d4a..35b20356 100644 --- a/lib/middleman/core_extensions/front_matter.rb +++ b/lib/middleman/core_extensions/front_matter.rb @@ -6,42 +6,36 @@ module Middleman::CoreExtensions::FrontMatter def registered(app) app.extend ClassMethods - ::Tilt::register RDiscountTemplate, 'markdown', 'mkd', 'md' - ::Tilt::register RedcarpetTemplate, 'markdown', 'mkd', 'md' - ::Tilt::register MarukuTemplate, 'markdown', 'mkd', 'md' - ::Tilt::register KramdownTemplate, 'markdown', 'mkd', 'md' - app.set :markdown_engine_prefix, ::Middleman::CoreExtensions::FrontMatter + app.file_changed do |file| + data.touch_file(file) + end - ::Tilt::register RedClothTemplate, 'textile' - ::Tilt.prefer(RedClothTemplate) - - ::Tilt::register ERBTemplate, 'erb', 'rhtml' - ::Tilt.prefer(ERBTemplate) - - ::Tilt::register LiquidTemplate, 'liquid' - ::Tilt.prefer(LiquidTemplate) - - ::Tilt::register SlimTemplate, 'slim' - ::Tilt.prefer(SlimTemplate) - - ::Tilt::register HamlTemplate, 'haml' - ::Tilt.prefer(HamlTemplate) + app.file_deleted do |file| + data.remove_file(file) + end app.after_configuration do app.before_processing(:front_matter, 0) do |result| if result && Tilt.mappings.has_key?(result[1].to_s) extensionless_path, template_engine = result full_file_path = "#{extensionless_path}.#{template_engine}" - system_path = File.join(settings.views, full_file_path) - data, content = app.parse_front_matter(File.read(system_path)) - - request['custom_options'] = {} - %w(layout layout_engine).each do |opt| - if data.has_key?(opt) - request['custom_options'][opt.to_sym] = data.delete(opt) + + if app.frontmatter.has_data?(full_file_path) + result = app.frontmatter.data(full_file_path) + data = result.first.dup + + request['custom_options'] = {} + %w(layout layout_engine).each do |opt| + if data.has_key?(opt) + request['custom_options'][opt.to_sym] = data.delete(opt) + end end + + app.settings.templates[extensionless_path] = [result[1], extensionless_path, 1] + else + data = {} end - + # Forward remaining data to helpers app.data_content("page", data) end @@ -51,7 +45,62 @@ module Middleman::CoreExtensions::FrontMatter end end alias :included :registered + end + + module ClassMethods + def frontmatter + @frontmatter ||= FrontmatterData.new(self) + end + end + + class FrontmatterData + def initialize(app) + @app = app + @source ||= File.expand_path(@app.views, @app.root) + @local_data = {} + + views_dir = @app.views + views_dir = File.join(@app.root, @app.views) unless views_dir.include?(@app.root) + + Dir[File.join(views_dir, "**/*")].each do |file| + next if file.match(/\/\./) || + (file.match(/\/_/) && !file.match(/\/__/)) || + File.directory?(file) + + touch_file(file) + end + end + def has_data?(path) + @local_data.has_key?(path.to_s) + end + + def touch_file(file) + content = File.read(file) + file = file.sub(@source, "") + result = parse_front_matter(content) + + if result + @local_data[file] = result + end + end + + def remove_file(file) + file = file.sub(@source, "") + if @local_data.has_key?(file) + @local_data.delete(file) + end + end + + def data(path) + if @local_data.has_key?(path.to_s) + @local_data[path.to_s] + else + nil + end + end + + private def parse_front_matter(content) yaml_regex = /^(---\s*\n.*?\n?)^(---\s*$\n?)/m if content =~ yaml_regex @@ -59,61 +108,15 @@ module Middleman::CoreExtensions::FrontMatter data = YAML.load($1) rescue => e puts "YAML Exception: #{e.message}" + return false end content = content.split(yaml_regex).last + else + return false end - data ||= {} [data, content] end end - - module ClassMethods - def parse_front_matter(content) - Middleman::CoreExtensions::FrontMatter.parse_front_matter(content) - end - end - - module YamlAware - def prepare - @frontmatter, @data = Middleman::CoreExtensions::FrontMatter.parse_front_matter(@data) - super - end - end - - class RDiscountTemplate < ::Tilt::RDiscountTemplate - include Middleman::CoreExtensions::FrontMatter::YamlAware - end - class RedcarpetTemplate < ::Tilt::RedcarpetTemplate - include Middleman::CoreExtensions::FrontMatter::YamlAware - end - class MarukuTemplate < ::Tilt::MarukuTemplate - include Middleman::CoreExtensions::FrontMatter::YamlAware - end - class RedClothTemplate < ::Tilt::RedClothTemplate - include Middleman::CoreExtensions::FrontMatter::YamlAware - end - class KramdownTemplate < ::Tilt::KramdownTemplate - include Middleman::CoreExtensions::FrontMatter::YamlAware - end - - class ERBTemplate < ::Tilt::ERBTemplate - include Middleman::CoreExtensions::FrontMatter::YamlAware - end - class ErubisTemplate < ::Tilt::ErubisTemplate - include Middleman::CoreExtensions::FrontMatter::YamlAware - end - - class LiquidTemplate < ::Tilt::LiquidTemplate - include Middleman::CoreExtensions::FrontMatter::YamlAware - end - - class HamlTemplate < ::Tilt::HamlTemplate - include Middleman::CoreExtensions::FrontMatter::YamlAware - end - - class SlimTemplate < ::Slim::Template - include Middleman::CoreExtensions::FrontMatter::YamlAware - end end \ No newline at end of file diff --git a/lib/middleman/core_extensions/rack_map.rb b/lib/middleman/core_extensions/rack_map.rb index daea8070..9dc2866f 100644 --- a/lib/middleman/core_extensions/rack_map.rb +++ b/lib/middleman/core_extensions/rack_map.rb @@ -18,14 +18,15 @@ module Middleman::CoreExtensions::RackMap # Creates a Rack::Builder instance with all the middleware set up and # an instance of this class as end point. - def build(builder, *args, &bk) + def build_new(inst=false) + builder = Rack::Builder.new setup_default_middleware builder setup_middleware builder maps.each { |p,b| builder.map(p, &b) } app = self builder.map "/" do - run app.new!(*args, &bk) + run (inst || app.new!) end builder diff --git a/lib/middleman/core_extensions/routing.rb b/lib/middleman/core_extensions/routing.rb index 5e329e5a..02fb6bdd 100644 --- a/lib/middleman/core_extensions/routing.rb +++ b/lib/middleman/core_extensions/routing.rb @@ -45,19 +45,21 @@ module Middleman::CoreExtensions::Routing def page(url, options={}, &block) has_block = block_given? options[:layout] = layout if options[:layout].nil? - + if options.has_key?(:proxy) reroute(url, options[:proxy]) if options.has_key?(:ignore) && options[:ignore] ignore(options[:proxy]) - end + end + + options.delete(:proxy) else if options.has_key?(:ignore) && options[:ignore] ignore(url) end end - + paths_for_url(url).each do |p| get(p) do if settings.sitemap.path_is_proxy?(url) diff --git a/lib/middleman/core_extensions/sitemap.rb b/lib/middleman/core_extensions/sitemap.rb index 544926a3..9832ae67 100644 --- a/lib/middleman/core_extensions/sitemap.rb +++ b/lib/middleman/core_extensions/sitemap.rb @@ -50,10 +50,6 @@ module Middleman::CoreExtensions::Sitemap build_static_map end - def setup? - @is_setup - end - # Check to see if we know about a specific path def path_exists?(path) path = path.sub(/^\//, "") diff --git a/middleman.gemspec b/middleman.gemspec index 91e6e6e2..f85ac56b 100644 --- a/middleman.gemspec +++ b/middleman.gemspec @@ -41,7 +41,9 @@ Gem::Specification.new do |s| s.add_dependency("hooks", ["~> 0.2.0"]) s.add_dependency("guard", ["~> 0.8.8"]) - s.add_dependency("eventmachine", ["1.0.0.beta.4"]) + s.add_dependency("rb-fsevent") + s.add_dependency("rb-inotify") + # s.add_dependency("eventmachine", ["1.0.0.beta.4"]) # s.add_dependency("middleman-livereload", ["~> 0.2.0"]) # Development and test