experimental rack-only middleman
This commit is contained in:
parent
fc91179b4a
commit
f4fa1acd3e
14 changed files with 397 additions and 397 deletions
|
@ -55,13 +55,11 @@
|
|||
libdir = File.dirname(__FILE__)
|
||||
$LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir)
|
||||
|
||||
# We're riding on Sinatra, so let's include it.
|
||||
require "sinatra/base"
|
||||
|
||||
# Top-level Middleman object
|
||||
module Middleman
|
||||
# Auto-load modules on-demand
|
||||
autoload :Base, "middleman/base"
|
||||
autoload :Hooks, "middleman/hooks"
|
||||
autoload :Builder, "middleman/builder"
|
||||
autoload :CLI, "middleman/cli"
|
||||
autoload :Templates, "middleman/templates"
|
||||
|
@ -86,9 +84,6 @@ module Middleman
|
|||
# Add Builder callbacks
|
||||
autoload :Builder, "middleman/core_extensions/builder"
|
||||
|
||||
# Add Rack::Builder.map support
|
||||
autoload :RackMap, "middleman/core_extensions/rack_map"
|
||||
|
||||
# Custom Feature API
|
||||
autoload :Features, "middleman/core_extensions/features"
|
||||
|
||||
|
@ -181,9 +176,8 @@ module Middleman
|
|||
end
|
||||
|
||||
def self.server(&block)
|
||||
sandbox = Class.new(Sinatra::Base)
|
||||
sandbox.register Base
|
||||
sandbox.class_eval(&block) if block_given?
|
||||
sandbox = Class.new(Middleman::Base)
|
||||
# sandbox.class_eval(&block) if block_given?
|
||||
sandbox
|
||||
end
|
||||
|
||||
|
@ -197,8 +191,8 @@ module Middleman
|
|||
opts[:app] = app_class
|
||||
opts[:server] = 'thin'
|
||||
|
||||
require "thin"
|
||||
::Thin::Logging.silent = true if options[:debug] != "true"
|
||||
# require "thin"
|
||||
# ::Thin::Logging.silent = true if options[:debug] != "true"
|
||||
|
||||
server = ::Rack::Server.new(opts)
|
||||
server.start
|
||||
|
|
|
@ -1,134 +1,55 @@
|
|||
require "rack"
|
||||
require "tilt"
|
||||
require "i18n"
|
||||
require "hooks"
|
||||
|
||||
require "active_support"
|
||||
require "active_support/json"
|
||||
require "active_support/core_ext/class/attribute_accessors"
|
||||
require "active_support/core_ext/string/inflections"
|
||||
# require "active_support/core_ext/class/attribute_accessors"
|
||||
|
||||
module Padrino
|
||||
class << self
|
||||
attr_accessor :root_dir, :env
|
||||
def root(*args)
|
||||
File.expand_path(File.join(root_dir, *args))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
ENV['PADRINO_LOG_LEVEL'] = "debug"
|
||||
require "padrino-core/logger"
|
||||
|
||||
module Middleman::Base
|
||||
class << self
|
||||
def registered(app)
|
||||
app.send :include, ::Hooks
|
||||
app.define_hook :initialized
|
||||
|
||||
app.extend ClassMethods
|
||||
app.send :include, InstanceMethods
|
||||
|
||||
# Basic Sinatra config
|
||||
app.set :app_file, __FILE__
|
||||
app.set :root, Dir.pwd
|
||||
app.set :sessions, false
|
||||
app.set :logging, false
|
||||
app.set :protection, false
|
||||
app.set :environment, (ENV['MM_ENV'] && ENV['MM_ENV'].to_sym) || :development
|
||||
|
||||
# Middleman-specific options
|
||||
app.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
|
||||
app.set :js_dir, "javascripts" # Where to look for javascript files
|
||||
app.set :css_dir, "stylesheets" # Where to look for CSS files
|
||||
app.set :images_dir, "images" # Where to look for images
|
||||
|
||||
app.set :build_dir, "build" # Which folder are builds output to
|
||||
app.set :http_prefix, nil # During build, add a prefix for absolute paths
|
||||
|
||||
# Pass all request to Middleman, even "static" files
|
||||
app.set :static, false
|
||||
|
||||
app.set :views, "source"
|
||||
|
||||
# Add Builder Callbacks
|
||||
app.register Middleman::CoreExtensions::Builder
|
||||
|
||||
# Add Rack::Builder.map to Sinatra
|
||||
app.register Middleman::CoreExtensions::RackMap
|
||||
|
||||
# Activate custom features
|
||||
app.register Middleman::CoreExtensions::Features
|
||||
|
||||
# Add Builder Callbacks
|
||||
app.register Middleman::CoreExtensions::FileWatcher
|
||||
|
||||
# Sitemap
|
||||
app.register Middleman::CoreExtensions::Sitemap
|
||||
|
||||
# Activate Yaml Data package
|
||||
app.register Middleman::CoreExtensions::Data
|
||||
|
||||
# Setup custom rendering
|
||||
app.register Middleman::CoreExtensions::Rendering
|
||||
|
||||
# Compass framework
|
||||
app.register Middleman::CoreExtensions::Compass
|
||||
|
||||
# Sprockets asset handling
|
||||
app.register Middleman::CoreExtensions::Sprockets
|
||||
|
||||
# Setup asset path pipeline
|
||||
app.register Middleman::CoreExtensions::Assets
|
||||
|
||||
# Activate built-in helpers
|
||||
app.register Middleman::CoreExtensions::DefaultHelpers
|
||||
|
||||
# with_layout and page routing
|
||||
app.register Middleman::CoreExtensions::Routing
|
||||
|
||||
# Parse YAML from templates
|
||||
app.register Middleman::CoreExtensions::FrontMatter
|
||||
|
||||
app.set :default_features, [
|
||||
:lorem,
|
||||
# :sitemap_tree
|
||||
]
|
||||
|
||||
# Default layout name
|
||||
app.set :layout, :layout
|
||||
|
||||
# This will match all requests not overridden in the project's config.rb
|
||||
app.not_found do
|
||||
process_request
|
||||
end
|
||||
|
||||
# Custom 404 handler (to be styled)
|
||||
app.error Sinatra::NotFound do
|
||||
content_type 'text/html'
|
||||
"<html><body><h1>File Not Found</h1><p>#{request.path_info}</p></body>"
|
||||
end
|
||||
end
|
||||
alias :included :registered
|
||||
end
|
||||
class Middleman::Base
|
||||
include Hooks
|
||||
define_hook :build_config
|
||||
define_hook :development_config
|
||||
|
||||
module ClassMethods
|
||||
# Override Sinatra's set to accept a block
|
||||
# Specifically for the asset_host feature
|
||||
def set(option, value = (not_set = true), ignore_setter = false, &block)
|
||||
if block_given?
|
||||
value = Proc.new { block }
|
||||
end
|
||||
|
||||
super(option, value, ignore_setter, &nil)
|
||||
class << self
|
||||
def reset!
|
||||
@app = nil
|
||||
@prototype = nil
|
||||
end
|
||||
|
||||
def full_path(path)
|
||||
parts = path ? path.split('/') : []
|
||||
if parts.last.nil? || parts.last.split('.').length == 1
|
||||
path = File.join(path, index_file)
|
||||
end
|
||||
"/" + path.sub(%r{^/}, '')
|
||||
def app
|
||||
@app ||= Rack::Builder.new
|
||||
end
|
||||
|
||||
def prototype
|
||||
@prototype ||= app.to_app
|
||||
end
|
||||
|
||||
def call(env)
|
||||
prototype.call(env)
|
||||
end
|
||||
|
||||
def use(middleware, *args, &block)
|
||||
app.use(middleware, *args, &block)
|
||||
end
|
||||
|
||||
def map(map, &block)
|
||||
app.map(map, &block)
|
||||
end
|
||||
|
||||
def helpers(*extensions, &block)
|
||||
class_eval(&block) if block_given?
|
||||
include(*extensions) if extensions.any?
|
||||
end
|
||||
|
||||
def defaults
|
||||
@defaults ||= {}
|
||||
end
|
||||
|
||||
def set(key, value)
|
||||
@defaults ||= {}
|
||||
@defaults[key] = value
|
||||
end
|
||||
|
||||
def before_processing(name=:unnamed, idx=-1, &block)
|
||||
|
@ -144,114 +65,284 @@ module Middleman::Base
|
|||
end
|
||||
end
|
||||
|
||||
# Convenience method to check if we're in build mode
|
||||
def build?; environment == :build; end
|
||||
|
||||
# Creates a Rack::Builder instance with all the middleware set up and
|
||||
# an instance of this class as end point.
|
||||
def build_new(inst=false)
|
||||
builder = Rack::Builder.new
|
||||
setup_default_middleware builder
|
||||
setup_middleware builder
|
||||
builder.run inst || new!
|
||||
builder.to_app
|
||||
end
|
||||
|
||||
def logger
|
||||
Padrino.logger
|
||||
def configure(env, &block)
|
||||
send("#{env}_config", &block)
|
||||
end
|
||||
end
|
||||
|
||||
module InstanceMethods
|
||||
def logger
|
||||
Padrino.logger
|
||||
end
|
||||
def set(key, value)
|
||||
setter = "#{key}=".to_sym
|
||||
self.class.send(:attr_accessor, key) if !respond_to?(setter)
|
||||
send(setter, value)
|
||||
end
|
||||
|
||||
def initialize(*args)
|
||||
super
|
||||
run_hook :initialized, settings
|
||||
|
||||
Padrino.root_dir = settings.root
|
||||
Padrino.env = settings.environment
|
||||
Padrino::Logger::Config[:debug] = { :stream => :stderr }
|
||||
# Padrino::Logger::Config[:development][:stream] = StringIO.new
|
||||
def configure(env, &block)
|
||||
self.class.configure(env, &block)
|
||||
end
|
||||
|
||||
# Basic Sinatra config
|
||||
set :root, Dir.pwd
|
||||
set :source, "source"
|
||||
set :environment, (ENV['MM_ENV'] && ENV['MM_ENV'].to_sym) || :development
|
||||
set :logging, false
|
||||
|
||||
# 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 :build_dir, "build" # Which folder are builds output to
|
||||
set :http_prefix, nil # During build, add a prefix for absolute paths
|
||||
|
||||
set :views, "source"
|
||||
|
||||
set :default_features, [
|
||||
:lorem,
|
||||
# :sitemap_tree
|
||||
]
|
||||
|
||||
# Default layout name
|
||||
set :layout, :layout
|
||||
|
||||
# Activate custom features and extensions
|
||||
include Middleman::CoreExtensions::Features
|
||||
|
||||
# Add Builder Callbacks
|
||||
register Middleman::CoreExtensions::Builder
|
||||
|
||||
# Add Guard Callbacks
|
||||
register Middleman::CoreExtensions::FileWatcher
|
||||
|
||||
# Sitemap
|
||||
register Middleman::CoreExtensions::Sitemap
|
||||
|
||||
# Activate Data package
|
||||
register Middleman::CoreExtensions::Data
|
||||
|
||||
# Setup custom rendering
|
||||
register Middleman::CoreExtensions::Rendering
|
||||
|
||||
# Compass framework
|
||||
register Middleman::CoreExtensions::Compass
|
||||
|
||||
# Sprockets asset handling
|
||||
register Middleman::CoreExtensions::Sprockets
|
||||
|
||||
# Setup asset path pipeline
|
||||
register Middleman::CoreExtensions::Assets
|
||||
|
||||
# Activate built-in helpers
|
||||
register Middleman::CoreExtensions::DefaultHelpers
|
||||
|
||||
# with_layout and page routing
|
||||
register Middleman::CoreExtensions::Routing
|
||||
|
||||
# Parse YAML from templates
|
||||
register Middleman::CoreExtensions::FrontMatter
|
||||
|
||||
define_hook :initialized
|
||||
def initialize(&block)
|
||||
self.class.superclass.defaults.each do |k, v|
|
||||
set(k, v)
|
||||
end
|
||||
|
||||
def forward
|
||||
raise ::Sinatra::NotFound
|
||||
set :source_dir, File.join(root, source)
|
||||
|
||||
super
|
||||
|
||||
run_hook :initialized
|
||||
end
|
||||
|
||||
def call(env)
|
||||
@env = env
|
||||
@req = Rack::Request.new(env)
|
||||
@res = Rack::Response.new
|
||||
|
||||
process_request
|
||||
end
|
||||
|
||||
# Custom 404 handler (to be styled)
|
||||
# app.error Sinatra::NotFound do
|
||||
# content_type 'text/html'
|
||||
# "<html><body><h1>File Not Found</h1><p>#{request.path_info}</p></body>"
|
||||
# end
|
||||
|
||||
# Convenience methods to check if we're in a mode
|
||||
def development?; environment == :development; end
|
||||
def build?; environment == :build; end
|
||||
|
||||
# Internal method to look for templates and evaluate them if found
|
||||
def process_request
|
||||
# Normalize the path and add index if we're looking at a directory
|
||||
original_path = @env["PATH_INFO"].dup
|
||||
request_path = full_path(@env["PATH_INFO"].gsub("%20", " "))
|
||||
|
||||
# return not_found if sitemap.ignored_path?(request_path)
|
||||
|
||||
# if sitemap.path_is_proxy?(request_path)
|
||||
# request["is_proxy"] = true
|
||||
# request_path = "/" + sitemap.path_target(request_path)
|
||||
# end
|
||||
|
||||
found_template = resolve_template(request_path)
|
||||
return not_found unless found_template
|
||||
|
||||
path, engine = found_template
|
||||
|
||||
# Static File
|
||||
return send_file(path) if engine.nil?
|
||||
|
||||
# return unless settings.execute_before_processing!(self, found_template)
|
||||
|
||||
# context = settings.sitemap.get_context(original_path) || {}
|
||||
#
|
||||
options = {}
|
||||
# options = context.has_key?(:options) ? context[:options] : {}
|
||||
# options.merge!(request['custom_options'] || {})
|
||||
#
|
||||
|
||||
local_layout = if options.has_key?(:layout)
|
||||
options[:layout]
|
||||
else
|
||||
layout
|
||||
end
|
||||
|
||||
# Internal method to look for templates and evaluate them if found
|
||||
def process_request
|
||||
if !settings.views.include?(settings.root)
|
||||
settings.set :views, File.join(settings.root, settings.views)
|
||||
# if context.has_key?(:block) && context[:block]
|
||||
# instance_eval(&context[:block])
|
||||
# end
|
||||
|
||||
# locals = request['custom_locals'] || {}
|
||||
locals = {}
|
||||
|
||||
# content_type mime_type(File.extname(request_path))
|
||||
@res.status = 200
|
||||
|
||||
output = if layout
|
||||
layout_engine = if options.has_key?(:layout_engine)
|
||||
options[:layout_engine]
|
||||
else
|
||||
engine
|
||||
end
|
||||
|
||||
# Normalize the path and add index if we're looking at a directory
|
||||
request_path = settings.full_path(request.path.gsub("%20", " "))
|
||||
original_path = request_path.dup
|
||||
|
||||
return status(404) if settings.sitemap.ignored_path?(request_path)
|
||||
|
||||
if settings.sitemap.path_is_proxy?(request_path)
|
||||
request["is_proxy"] = true
|
||||
request_path = "/" + settings.sitemap.path_target(request_path)
|
||||
layout_path, *etc = resolve_template(layout, :preferred_engine => layout_engine)
|
||||
|
||||
render(layout_path, locals) do
|
||||
render(path, locals)
|
||||
end
|
||||
else
|
||||
render(path, locals)
|
||||
end
|
||||
|
||||
@res.write output
|
||||
@res.finish
|
||||
end
|
||||
|
||||
public
|
||||
|
||||
protected
|
||||
|
||||
def full_path(path)
|
||||
parts = path ? path.split('/') : []
|
||||
if parts.last.nil? || parts.last.split('.').length == 1
|
||||
path = File.join(path, index_file)
|
||||
end
|
||||
"/" + path.sub(%r{^/}, '')
|
||||
end
|
||||
|
||||
def not_found
|
||||
@res.status == 404
|
||||
@res.write "<html><body><h1>File Not Found</h1><p>#{@env["PATH_INFO"]}</p></body>"
|
||||
@res.finish
|
||||
end
|
||||
|
||||
def resolve_template(request_path, options={})
|
||||
request_path = request_path.to_s
|
||||
@_resolved_templates ||= {}
|
||||
|
||||
if !@_resolved_templates.has_key?(request_path)
|
||||
relative_path = request_path.sub(%r{^/}, "")
|
||||
on_disk_path = File.expand_path(relative_path, source_dir)
|
||||
|
||||
found_template = resolve_template(request_path, :raise_exceptions => false)
|
||||
return status(404) unless found_template
|
||||
|
||||
path, engine = found_template
|
||||
|
||||
if !::Tilt.mappings.has_key?(engine.to_s)
|
||||
matched_mime = mime_type(File.extname(request_path))
|
||||
matched_mime = "application/octet-stream" if matched_mime.nil?
|
||||
content_type matched_mime
|
||||
status 200
|
||||
send_file File.join(settings.views, request_path)
|
||||
return
|
||||
end
|
||||
preferred_engine = if options.has_key?(:preferred_engine)
|
||||
extension_class = Tilt[options[:preferred_engine]]
|
||||
matched_exts = []
|
||||
|
||||
return unless settings.execute_before_processing!(self, found_template)
|
||||
|
||||
context = settings.sitemap.get_context(original_path) || {}
|
||||
|
||||
options = context.has_key?(:options) ? context[:options] : {}
|
||||
options.merge!(request['custom_options'] || {})
|
||||
|
||||
old_layout = settings.layout
|
||||
settings.set :layout, options[:layout] if !options[:layout].nil?
|
||||
|
||||
local_layout = if settings.layout
|
||||
if options[:layout] == false || request_path =~ /\.(css|js)$/
|
||||
false
|
||||
else
|
||||
settings.fetch_layout_path(settings.layout).to_sym
|
||||
# TODO: Cache this
|
||||
Tilt.mappings.each do |ext, engines|
|
||||
next unless engines.include? extension_class
|
||||
matched_exts << ext
|
||||
end
|
||||
|
||||
"{" + matched_exts.join(",") + "}"
|
||||
else
|
||||
"*"
|
||||
end
|
||||
|
||||
path_with_ext = on_disk_path + "." + preferred_engine
|
||||
|
||||
found_engine = nil
|
||||
found_path = Dir[path_with_ext].find do |path|
|
||||
::Tilt[path]
|
||||
end
|
||||
|
||||
result = if found_path || File.exists?(on_disk_path)
|
||||
engine = found_path ? File.extname(found_path)[1..-1].to_sym : nil
|
||||
[ found_path || on_disk_path, engine ]
|
||||
else
|
||||
false
|
||||
end
|
||||
|
||||
render_options = { :layout => local_layout }
|
||||
render_options[:layout_engine] = options[:layout_engine] if options.has_key?(:layout_engine)
|
||||
|
||||
locals = request['custom_locals'] || {}
|
||||
|
||||
if context.has_key?(:block) && context[:block]
|
||||
instance_eval(&context[:block])
|
||||
end
|
||||
|
||||
begin
|
||||
result = render(engine, path, render_options, locals)
|
||||
|
||||
if result
|
||||
content_type mime_type(File.extname(request_path))
|
||||
status 200
|
||||
body result
|
||||
@_resolved_templates[request_path] = result
|
||||
end
|
||||
|
||||
@_resolved_templates[request_path]
|
||||
end
|
||||
|
||||
def extensionless_path(file)
|
||||
@_extensionless_path_cache ||= {}
|
||||
|
||||
if @_extensionless_path_cache.has_key?(file)
|
||||
@_extensionless_path_cache[file]
|
||||
else
|
||||
path = file.dup
|
||||
end_of_the_line = false
|
||||
while !end_of_the_line
|
||||
file_extension = File.extname(path)
|
||||
|
||||
if ::Tilt.mappings.has_key?(file_extension.gsub(/^\./, ""))
|
||||
path = path.sub(file_extension, "")
|
||||
else
|
||||
end_of_the_line = true
|
||||
end
|
||||
ensure
|
||||
settings.set :layout, old_layout
|
||||
end
|
||||
|
||||
@_extensionless_path_cache[file] = path
|
||||
path
|
||||
end
|
||||
end
|
||||
|
||||
def send_file(path)
|
||||
|
||||
# matched_mime = mime_type(File.extname(request_path))
|
||||
# matched_mime = "application/octet-stream" if matched_mime.nil?
|
||||
# content_type matched_mime
|
||||
|
||||
file = ::Rack::File.new nil
|
||||
file.path = path
|
||||
file.serving# env
|
||||
end
|
||||
|
||||
def render(path, locals = {}, options = {}, &block)
|
||||
path = path.to_s
|
||||
template = ::Tilt.new(path, 1, options)
|
||||
template.render(self, locals, &block)
|
||||
end
|
||||
|
||||
def logging?
|
||||
logging
|
||||
end
|
||||
end
|
|
@ -18,45 +18,45 @@ module Middleman::CoreExtensions::Compass
|
|||
# * line_comments
|
||||
# * sprite_engine
|
||||
# * chunky_png_options
|
||||
compass_config_file = File.join(app.root, "compass.config")
|
||||
compass_config_file = File.join(self.root, "compass.config")
|
||||
if File.exists?(compass_config_file)
|
||||
::Compass.add_project_configuration(compass_config_file)
|
||||
end
|
||||
|
||||
::Compass.configuration do |config|
|
||||
config.project_path = app.root
|
||||
config.project_path = self.root
|
||||
config.environment = :development
|
||||
config.cache_path = File.join(app.root, ".sass-cache")
|
||||
config.cache_path = File.join(self.root, ".sass-cache")
|
||||
|
||||
views_root = File.basename(app.views)
|
||||
config.sass_dir = File.join(views_root, app.css_dir)
|
||||
config.css_dir = File.join(views_root, app.css_dir)
|
||||
config.javascripts_dir = File.join(views_root, app.js_dir)
|
||||
config.fonts_dir = File.join(views_root, app.fonts_dir)
|
||||
config.images_dir = File.join(views_root, app.images_dir)
|
||||
views_root = File.basename(self.views)
|
||||
config.sass_dir = File.join(views_root, self.css_dir)
|
||||
config.css_dir = File.join(views_root, self.css_dir)
|
||||
config.javascripts_dir = File.join(views_root, self.js_dir)
|
||||
config.fonts_dir = File.join(views_root, self.fonts_dir)
|
||||
config.images_dir = File.join(views_root, self.images_dir)
|
||||
|
||||
config.http_images_path = if app.respond_to? :http_images_path
|
||||
app.http_images_path
|
||||
config.http_images_path = if self.respond_to? :http_images_path
|
||||
self.http_images_path
|
||||
else
|
||||
File.join(app.http_prefix || "/", app.images_dir)
|
||||
File.join(self.http_prefix || "/", self.images_dir)
|
||||
end
|
||||
|
||||
config.http_stylesheets_path = if app.respond_to? :http_css_path
|
||||
app.http_css_path
|
||||
config.http_stylesheets_path = if self.respond_to? :http_css_path
|
||||
self.http_css_path
|
||||
else
|
||||
File.join(app.http_prefix || "/", app.css_dir)
|
||||
File.join(self.http_prefix || "/", self.css_dir)
|
||||
end
|
||||
|
||||
config.http_javascripts_path = if app.respond_to? :http_js_path
|
||||
app.http_js_path
|
||||
config.http_javascripts_path = if self.respond_to? :http_js_path
|
||||
self.http_js_path
|
||||
else
|
||||
File.join(app.http_prefix || "/", app.js_dir)
|
||||
File.join(self.http_prefix || "/", self.js_dir)
|
||||
end
|
||||
|
||||
config.http_fonts_path = if app.respond_to? :http_fonts_path
|
||||
app.http_fonts_path
|
||||
config.http_fonts_path = if self.respond_to? :http_fonts_path
|
||||
self.http_fonts_path
|
||||
else
|
||||
File.join(app.http_prefix || "/", app.fonts_dir)
|
||||
File.join(self.http_prefix || "/", self.fonts_dir)
|
||||
end
|
||||
|
||||
config.asset_cache_buster :none
|
||||
|
|
|
@ -30,18 +30,32 @@
|
|||
|
||||
module Middleman::CoreExtensions::Features
|
||||
|
||||
# The Feature API is itself a Feature. Mind blowing!
|
||||
class << self
|
||||
def registered(app)
|
||||
app.set :default_features, []
|
||||
def included(app)
|
||||
# app.set :default_features, []
|
||||
app.define_hook :after_configuration
|
||||
app.define_hook :before_configuration
|
||||
app.extend ClassMethods
|
||||
app.send :include, InstanceMethods
|
||||
end
|
||||
alias :included :registered
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
def extensions
|
||||
@extensions ||= []
|
||||
end
|
||||
|
||||
def register(*new_extensions)
|
||||
@extensions ||= []
|
||||
@extensions += new_extensions
|
||||
new_extensions.each do |extension|
|
||||
extend extension
|
||||
extension.registered(self) if extension.respond_to?(:registered)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
module InstanceMethods
|
||||
# This method is available in the project's `config.rb`.
|
||||
# It takes a underscore-separated symbol, finds the appropriate
|
||||
# feature module and includes it.
|
||||
|
@ -58,39 +72,39 @@ module Middleman::CoreExtensions::Features
|
|||
|
||||
if feature.is_a? String
|
||||
feature = feature.camelize
|
||||
feature = Middleman::Features.const_get(feature)
|
||||
feature = ::Middleman::Features.const_get(feature)
|
||||
end
|
||||
|
||||
puts "== Activating: #{feature}" if logging?
|
||||
register feature
|
||||
self.class.register feature
|
||||
end
|
||||
|
||||
# Load features before starting server
|
||||
def new!
|
||||
def initialize
|
||||
super
|
||||
|
||||
run_hook :before_configuration
|
||||
|
||||
# Check for and evaluate local configuration
|
||||
local_config = File.join(self.root, "config.rb")
|
||||
if File.exists? local_config
|
||||
puts "== Reading: Local config" if logging?
|
||||
class_eval File.read(local_config)
|
||||
set :app_file, File.expand_path(local_config)
|
||||
# instance_eval File.read(local_config)
|
||||
# set :app_file, File.expand_path(local_config)
|
||||
end
|
||||
|
||||
run_hook :after_configuration
|
||||
|
||||
# Add in defaults
|
||||
default_features.each do |ext|
|
||||
activate ext
|
||||
# activate ext
|
||||
end
|
||||
|
||||
run_hook :after_configuration
|
||||
|
||||
if logging?
|
||||
extensions.each do |ext|
|
||||
puts "== Extension: #{ext}"
|
||||
end
|
||||
end
|
||||
|
||||
super
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -6,9 +6,9 @@ module Middleman::CoreExtensions::FileWatcher
|
|||
app.extend ClassMethods
|
||||
app.send :include, InstanceMethods
|
||||
app.before_configuration do
|
||||
Find.find(settings.root) do |path|
|
||||
Find.find(root) do |path|
|
||||
next if File.directory?(path)
|
||||
file_did_change(path.sub("#{settings.root}/", ""))
|
||||
file_did_change(path.sub("#{root}/", ""))
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -28,6 +28,12 @@ module Middleman::CoreExtensions::FileWatcher
|
|||
@_file_deleted << [block, matcher] if block_given?
|
||||
@_file_deleted
|
||||
end
|
||||
end
|
||||
|
||||
module InstanceMethods
|
||||
def file_changed(*args)
|
||||
self.class.file_changed(*args)
|
||||
end
|
||||
|
||||
def file_did_change(path)
|
||||
file_changed.each do |callback, matcher|
|
||||
|
@ -36,6 +42,10 @@ module Middleman::CoreExtensions::FileWatcher
|
|||
instance_exec(path, &callback)
|
||||
end
|
||||
end
|
||||
|
||||
def file_deleted(*args)
|
||||
self.class.file_deleted(*args)
|
||||
end
|
||||
|
||||
def file_did_delete(path)
|
||||
file_deleted.each do |callback, matcher|
|
||||
|
@ -45,14 +55,4 @@ module Middleman::CoreExtensions::FileWatcher
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
module InstanceMethods
|
||||
def file_did_change(path)
|
||||
settings.file_did_change(path)
|
||||
end
|
||||
|
||||
def file_did_delete(path)
|
||||
settings.file_did_delete(path)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,35 +0,0 @@
|
|||
module Middleman::CoreExtensions::RackMap
|
||||
class << self
|
||||
def registered(app)
|
||||
app.extend ClassMethods
|
||||
end
|
||||
alias :included :registered
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
def map(path, &block)
|
||||
@maps ||= []
|
||||
@maps << [path, block]
|
||||
end
|
||||
|
||||
def maps
|
||||
@maps || []
|
||||
end
|
||||
|
||||
# Creates a Rack::Builder instance with all the middleware set up and
|
||||
# an instance of this class as end point.
|
||||
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 (inst || app.new!)
|
||||
end
|
||||
|
||||
builder
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,52 +1,12 @@
|
|||
require "padrino-core/application/rendering"
|
||||
|
||||
module Middleman::CoreExtensions::Rendering
|
||||
class << self
|
||||
def registered(app)
|
||||
app.extend ClassMethods
|
||||
app.send :include, InstanceMethods
|
||||
|
||||
# Tilt-aware renderer
|
||||
app.register Padrino::Rendering
|
||||
|
||||
# Activate custom renderers
|
||||
app.register Middleman::Renderers::Sass
|
||||
app.register Middleman::Renderers::Markdown
|
||||
app.register Middleman::Renderers::ERb
|
||||
app.register Middleman::Renderers::CoffeeScript
|
||||
app.register Middleman::Renderers::Liquid
|
||||
end
|
||||
alias :included :registered
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
def extensionless_path(file)
|
||||
@_extensionless_path_cache ||= {}
|
||||
|
||||
if @_extensionless_path_cache.has_key?(file)
|
||||
@_extensionless_path_cache[file]
|
||||
else
|
||||
path = file.dup
|
||||
end_of_the_line = false
|
||||
while !end_of_the_line
|
||||
file_extension = File.extname(path)
|
||||
|
||||
if ::Tilt.mappings.has_key?(file_extension.gsub(/^\./, ""))
|
||||
path = path.sub(file_extension, "")
|
||||
else
|
||||
end_of_the_line = true
|
||||
end
|
||||
end
|
||||
|
||||
@_extensionless_path_cache[file] = path
|
||||
path
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
module InstanceMethods
|
||||
def extensionless_path(path)
|
||||
settings.extensionless_path(path)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -21,7 +21,7 @@ module Middleman::CoreExtensions::Sprockets
|
|||
end
|
||||
|
||||
app.after_configuration do
|
||||
js_env = Middleman::CoreExtensions::Sprockets::JavascriptEnvironment.new(app)
|
||||
js_env = Middleman::CoreExtensions::Sprockets::JavascriptEnvironment.new(self)
|
||||
|
||||
vendor_dir = File.join("vendor", "assets", "javascripts")
|
||||
gems_with_js = ::Middleman.rubygems_latest_specs.select do |spec|
|
||||
|
@ -38,13 +38,13 @@ module Middleman::CoreExtensions::Sprockets
|
|||
end
|
||||
|
||||
# add paths to js_env (vendor/assets/javascripts)
|
||||
app.map "/#{app.js_dir}" do
|
||||
app.map "/#{self.js_dir}" do
|
||||
run js_env
|
||||
end
|
||||
end
|
||||
|
||||
app.after_compass_config do
|
||||
css_env = Middleman::CoreExtensions::Sprockets::StylesheetEnvironment.new(app)
|
||||
css_env = Middleman::CoreExtensions::Sprockets::StylesheetEnvironment.new(self)
|
||||
|
||||
vendor_dir = File.join("vendor", "assets", "stylesheets")
|
||||
gems_with_css = ::Middleman.rubygems_latest_specs.select do |spec|
|
||||
|
@ -60,7 +60,7 @@ module Middleman::CoreExtensions::Sprockets
|
|||
css_env.append_path File.join(spec.full_gem_path, app_dir)
|
||||
end
|
||||
|
||||
app.map "/#{app.css_dir}" do
|
||||
app.map "/#{self.css_dir}" do
|
||||
run css_env
|
||||
end
|
||||
end
|
||||
|
@ -96,7 +96,7 @@ module Middleman::CoreExtensions::Sprockets
|
|||
# Disable css
|
||||
# unregister_processor "text/css", ::Sprockets::DirectiveProcessor
|
||||
|
||||
self.js_compressor = app.settings.js_compressor
|
||||
self.js_compressor = app.js_compressor
|
||||
|
||||
# configure search paths
|
||||
append_path app.js_dir
|
||||
|
@ -115,7 +115,7 @@ module Middleman::CoreExtensions::Sprockets
|
|||
# Disable js
|
||||
# unregister_processor "application/javascript", ::Sprockets::DirectiveProcessor
|
||||
|
||||
self.css_compressor = app.settings.css_compressor
|
||||
self.css_compressor = app.css_compressor
|
||||
|
||||
# configure search paths
|
||||
append_path app.css_dir
|
||||
|
|
|
@ -78,14 +78,15 @@ module Guard
|
|||
|
||||
def server_start
|
||||
app = ::Middleman.server
|
||||
app.set :environment, (@options[:environment] || "development").to_sym
|
||||
app.set :logging, @options.has_key?(:debug) && (@options[:debug] == "true")
|
||||
|
||||
# Quiet down Guard
|
||||
# ENV['GUARD_ENV'] = 'test' if @options[:debug] == "true"
|
||||
|
||||
@app = app.new!
|
||||
app_rack = app.build_new(@app)
|
||||
@app = app.new
|
||||
@app.set :environment, (@options[:environment] || "development").to_sym
|
||||
@app.set :logging, @options.has_key?(:debug) && (@options[:debug] == "true")
|
||||
|
||||
app_rack = ::Middleman::Base.app
|
||||
app_rack.run @app
|
||||
|
||||
@server_job = fork do
|
||||
opts = @options.dup
|
||||
|
|
|
@ -1,8 +0,0 @@
|
|||
module Middleman::Renderers::CoffeeScript
|
||||
class << self
|
||||
def registered(app)
|
||||
require "coffee_script"
|
||||
end
|
||||
alias :included :registered
|
||||
end
|
||||
end
|
|
@ -3,32 +3,27 @@ require "tilt"
|
|||
module Middleman::Renderers::ERb
|
||||
class << self
|
||||
def registered(app)
|
||||
app.extend ClassMethods
|
||||
app.send :include, InstanceMethods
|
||||
|
||||
app.set :erb_engine, :erb
|
||||
|
||||
if !app.respond_to? :erb_engine_prefix
|
||||
app.set :erb_engine_prefix, ::Tilt
|
||||
end
|
||||
app.set :erb_engine_prefix, ::Tilt
|
||||
|
||||
app.after_configuration do
|
||||
engine = app.settings.erb_engine
|
||||
|
||||
if engine.is_a? Symbol
|
||||
engine = app.erb_tilt_template_from_symbol(engine)
|
||||
if erb_engine.is_a? Symbol
|
||||
erb_engine = erb_tilt_template_from_symbol(erb_engine)
|
||||
end
|
||||
|
||||
::Tilt.prefer(engine)
|
||||
::Tilt.prefer(erb_engine)
|
||||
end
|
||||
end
|
||||
alias :included :registered
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
module InstanceMethods
|
||||
def erb_tilt_template_from_symbol(engine)
|
||||
engine = engine.to_s
|
||||
engine = engine == "erb" ? "ERB" : engine.camelize
|
||||
settings.erb_engine_prefix.const_get("#{engine}Template")
|
||||
erb_engine_prefix.const_get("#{engine}Template")
|
||||
end
|
||||
end
|
||||
end
|
|
@ -7,10 +7,7 @@ module Middleman::Renderers::Liquid
|
|||
require "liquid"
|
||||
|
||||
app.after_configuration do
|
||||
full_path = app.views
|
||||
full_path = File.join(app.root, app.views) unless app.views.include?(app.root)
|
||||
|
||||
Liquid::Template.file_system = Liquid::LocalFileSystem.new(full_path)
|
||||
Liquid::Template.file_system = Liquid::LocalFileSystem.new(self.source_dir)
|
||||
|
||||
app.before_processing(:liquid) do |result|
|
||||
if result && result[1] == :liquid
|
||||
|
|
|
@ -1,34 +1,29 @@
|
|||
module Middleman::Renderers::Markdown
|
||||
class << self
|
||||
def registered(app)
|
||||
app.extend ClassMethods
|
||||
app.send :include, InstanceMethods
|
||||
|
||||
app.set :markdown_engine, nil
|
||||
|
||||
if !app.respond_to? :markdown_engine_prefix
|
||||
app.set :markdown_engine_prefix, ::Tilt
|
||||
end
|
||||
app.set :markdown_engine_prefix, ::Tilt
|
||||
|
||||
app.after_configuration do
|
||||
engine = app.settings.markdown_engine
|
||||
|
||||
unless engine.nil?
|
||||
if engine.is_a? Symbol
|
||||
engine = app.markdown_tilt_template_from_symbol(engine)
|
||||
unless markdown_engine.nil?
|
||||
if markdown_engine.is_a? Symbol
|
||||
markdown_engine = markdown_tilt_template_from_symbol(markdown_engine)
|
||||
end
|
||||
|
||||
::Tilt.prefer(engine)
|
||||
::Tilt.prefer(markdown_engine)
|
||||
end
|
||||
end
|
||||
end
|
||||
alias :included :registered
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
module InstanceMethods
|
||||
def markdown_tilt_template_from_symbol(engine)
|
||||
engine = engine.to_s
|
||||
engine = engine == "rdiscount" ? "RDiscount" : engine.camelize
|
||||
settings.markdown_engine_prefix.const_get("#{engine}Template")
|
||||
markdown_engine_prefix.const_get("#{engine}Template")
|
||||
end
|
||||
end
|
||||
end
|
|
@ -24,20 +24,16 @@ Gem::Specification.new do |s|
|
|||
s.add_dependency("thin", ["~> 1.2.11"])
|
||||
s.add_dependency("thor", ["~> 0.14.0"])
|
||||
s.add_dependency("tilt", ["~> 1.3.1"])
|
||||
s.add_dependency("maruku", ["~> 0.6.0"])
|
||||
s.add_dependency("sinatra", ["~> 1.3.1"])
|
||||
s.add_dependency("rack-test", ["~> 0.6.1"])
|
||||
s.add_dependency("uglifier", ["~> 1.1.0"])
|
||||
s.add_dependency("slim", ["~> 1.0.2"])
|
||||
s.add_dependency("haml", ["~> 3.1.0"])
|
||||
s.add_dependency("sass", ["~> 3.1.7"])
|
||||
s.add_dependency("activesupport", ["~> 3.1.0"])
|
||||
s.add_dependency("compass", ["~> 0.11.3"])
|
||||
s.add_dependency("coffee-script", ["~> 2.2.0"])
|
||||
s.add_dependency("execjs", ["~> 1.2.7"])
|
||||
s.add_dependency("sprockets", ["~> 2.0"])
|
||||
s.add_dependency("sprockets-sass", ["~> 0.3.0"])
|
||||
s.add_dependency("padrino-core", ["~> 0.10.5"])
|
||||
s.add_dependency("padrino-helpers", ["~> 0.10.5"])
|
||||
s.add_dependency("hooks", ["~> 0.2.0"])
|
||||
s.add_dependency("rb-fsevent")
|
||||
|
||||
|
|
Loading…
Reference in a new issue