feature api, front matter and padrino rendering are core

This commit is contained in:
Thomas Reynolds 2011-07-06 10:15:31 -07:00
parent 7a35c1f48e
commit 397cd0e30b
6 changed files with 189 additions and 175 deletions

View file

@ -73,14 +73,69 @@ module Middleman
end
module CoreExtensions
# Custom Feature API
autoload :Features, "middleman/core_extensions/features"
# DefaultHelpers are the built-in dynamic template helpers.
autoload :DefaultHelpers, "middleman/core_extensions/default_helpers"
# Data looks at the data/ folder for YAML files and makes them available
# to dynamic requests.
autoload :Data, "middleman/core_extensions/data"
# Parse YAML from templates
autoload :FrontMatter, "middleman/core_extensions/front_matter"
# Extended version of Padrino's rendering
autoload :Rendering, "middleman/core_extensions/rendering"
end
# Features API
autoload :Features, "middleman/features"
module Features
# RelativeAssets allow any asset path in dynamic templates to be either
# relative to the root of the project or use an absolute URL.
autoload :RelativeAssets, "middleman/features/relative_assets"
# AssetHost allows you to setup multiple domains to host your static assets.
# Calls to asset paths in dynamic templates will then rotate through each of
# the asset servers to better spread the load.
autoload :AssetHost, "middleman/features/asset_host"
# CacheBuster adds a query string to assets in dynamic templates to avoid
# browser caches failing to update to your new content.
autoload :CacheBuster, "middleman/features/cache_buster"
# AutomaticImageSizes inspects the images used in your dynamic templates and
# automatically adds width and height attributes to their HTML elements.
autoload :AutomaticImageSizes, "middleman/features/automatic_image_sizes"
# UglyHaml enables the non-indented output format from Haml templates. Useful
# for somewhat obfuscating the output and hiding the fact that you're using Haml.
autoload :UglyHaml, "middleman/features/ugly_haml"
# MinifyCss uses the YUI compressor to shrink CSS files
autoload :MinifyCss, "middleman/features/minify_css"
# MinifyJavascript uses the YUI compressor to shrink JS files
autoload :MinifyJavascript, "middleman/features/minify_javascript"
# CodeRay is a syntax highlighter.
autoload :CodeRay, "middleman/features/code_ray"
# Lorem provides a handful of helpful prototyping methods to generate words,
# paragraphs, fake images, names and email addresses.
autoload :Lorem, "middleman/features/lorem"
# Treat project as a blog
autoload :Blog, "middleman/features/blog"
# Proxy web services requests in dev mode only
autoload :Proxy, "middleman/features/proxy"
# Automatically resize images for mobile devises
# autoload :TinySrc, "middleman/features/tiny_src"
# LiveReload will auto-reload browsers with the live reload extension installed after changes
# Currently disabled and untested.
# autoload :LiveReload, "middleman/features/live_reload"
end
end

View file

@ -0,0 +1,95 @@
# Middleman provides an extension API which allows you to hook into the
# lifecycle of a page request, or static build, and manipulate the output.
# Internal to Middleman, these extensions are called "features," but we use
# the exact same API as is made available to the public.
#
# A Middleman extension looks like this:
#
# module MyExtension
# class << self
# def registered(app)
# # My Code
# end
# end
# end
#
# In your `config.rb`, you must load your extension (if it is not defined in
# that file) and call `activate`.
#
# require "my_extension"
# activate MyExtension
#
# This will call the `registered` method in your extension and provide you
# with the `app` parameter which is a Middleman::Server context. From here
# you can choose to respond to requests for certain paths or simply attach
# Rack middleware to the stack.
#
# The built-in features cover a wide range of functions. Some provide helper
# methods to use in your views. Some modify the output on-the-fly. And some
# apply computationally-intensive changes to your final build files.
module Middleman::CoreExtensions::Features
# The Feature API is itself a Feature. Mind blowing!
class << self
def registered(app)
app.extend ClassMethods
end
alias :included :registered
end
module ClassMethods
# This method is available in the project's `config.rb`.
# It takes a underscore-separated symbol, finds the appropriate
# feature module and includes it.
#
# activate :lorem
#
# Alternatively, you can pass in a Middleman feature module directly.
#
# activate MyFeatureModule
def activate(feature)
feature = feature.to_s if feature.is_a? Symbol
if feature.is_a? String
feature = feature.camelize
feature = Middleman::Features.const_get(feature)
end
register feature
end
# Deprecated API. Please use `activate` instead.
def enable(feature_name)
$stderr.puts "Warning: Feature activation has been renamed from enable to activate"
activate(feature_name)
super(feature_name)
end
# Add a block/proc to be run after features have been setup
def after_feature_init(&block)
@run_after_features ||= []
@run_after_features << block
end
# Load features before starting server
def new
# 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
# Add in defaults
default_extensions.each do |ext|
activate ext
end
@run_after_features.each { |block| class_eval(&block) }
super
end
end
end

View file

@ -1,7 +1,7 @@
require "yaml"
require "tilt"
module Middleman::Features::FrontMatter
module Middleman::CoreExtensions::FrontMatter
class << self
def registered(app)
app.extend ClassMethods
@ -41,20 +41,20 @@ module Middleman::Features::FrontMatter
# MARKDOWN
class RDiscountTemplate < ::Tilt::RDiscountTemplate
include Middleman::Features::FrontMatter::YamlAware
include Middleman::CoreExtensions::FrontMatter::YamlAware
end
# TEXTILE
class RedClothTemplate < ::Tilt::RedClothTemplate
include Middleman::Features::FrontMatter::YamlAware
include Middleman::CoreExtensions::FrontMatter::YamlAware
end
# ERb
class ERBTemplate < ::Tilt::ERBTemplate
include Middleman::Features::FrontMatter::YamlAware
include Middleman::CoreExtensions::FrontMatter::YamlAware
end
class ErubisTemplate < ::Tilt::ErubisTemplate
include Middleman::Features::FrontMatter::YamlAware
include Middleman::CoreExtensions::FrontMatter::YamlAware
end
end

View file

@ -0,0 +1,18 @@
require "padrino-core/application/rendering"
module Middleman::CoreExtensions::Rendering
class << self
def registered(app)
# Tilt-aware renderer
app.register Padrino::Rendering
# Activate custom renderers
app.register Middleman::Renderers::Slim
app.register Middleman::Renderers::Haml
app.register Middleman::Renderers::Sass
app.register Middleman::Renderers::Markdown
app.register Middleman::Renderers::CoffeeScript
end
alias :included :registered
end
end

View file

@ -1,119 +0,0 @@
# Middleman provides an extension API which allows you to hook into the
# lifecycle of a page request, or static build, and manipulate the output.
# Internal to Middleman, these extensions are called "features," but we use
# the exact same API as is made available to the public.
#
# A Middleman extension looks like this:
#
# module MyExtension
# class << self
# def registered(app)
# # My Code
# end
# end
# end
#
# In your `config.rb`, you must load your extension (if it is not defined in
# that file) and call `activate`.
#
# require "my_extension"
# activate MyExtension
#
# This will call the `registered` method in your extension and provide you
# with the `app` parameter which is a Middleman::Server context. From here
# you can choose to respond to requests for certain paths or simply attach
# Rack middleware to the stack.
#
# The built-in features cover a wide range of functions. Some provide helper
# methods to use in your views. Some modify the output on-the-fly. And some
# apply computationally-intensive changes to your final build files.
module Middleman::Features
# RelativeAssets allow any asset path in dynamic templates to be either
# relative to the root of the project or use an absolute URL.
autoload :RelativeAssets, "middleman/features/relative_assets"
# AssetHost allows you to setup multiple domains to host your static assets.
# Calls to asset paths in dynamic templates will then rotate through each of
# the asset servers to better spread the load.
autoload :AssetHost, "middleman/features/asset_host"
# CacheBuster adds a query string to assets in dynamic templates to avoid
# browser caches failing to update to your new content.
autoload :CacheBuster, "middleman/features/cache_buster"
# AutomaticImageSizes inspects the images used in your dynamic templates and
# automatically adds width and height attributes to their HTML elements.
autoload :AutomaticImageSizes, "middleman/features/automatic_image_sizes"
# UglyHaml enables the non-indented output format from Haml templates. Useful
# for somewhat obfuscating the output and hiding the fact that you're using Haml.
autoload :UglyHaml, "middleman/features/ugly_haml"
# MinifyCss uses the YUI compressor to shrink CSS files
autoload :MinifyCss, "middleman/features/minify_css"
# MinifyJavascript uses the YUI compressor to shrink JS files
autoload :MinifyJavascript, "middleman/features/minify_javascript"
# CodeRay is a syntax highlighter.
autoload :CodeRay, "middleman/features/code_ray"
# Lorem provides a handful of helpful prototyping methods to generate words,
# paragraphs, fake images, names and email addresses.
autoload :Lorem, "middleman/features/lorem"
# Parse YAML metadata from templates
autoload :FrontMatter, "middleman/features/front_matter"
# Treat project as a blog
autoload :Blog, "middleman/features/blog"
# Proxy web services requests in dev mode only
autoload :Proxy, "middleman/features/proxy"
# Automatically resize images for mobile devises
# autoload :TinySrc, "middleman/features/tiny_src"
# LiveReload will auto-reload browsers with the live reload extension installed after changes
# Currently disabled and untested.
# autoload :LiveReload, "middleman/features/live_reload"
# The Feature API is itself a Feature. Mind blowing!
class << self
def registered(app)
app.extend ClassMethods
end
alias :included :registered
end
module ClassMethods
# This method is available in the project's `config.rb`.
# It takes a underscore-separated symbol, finds the appropriate
# feature module and includes it.
#
# activate :lorem
#
# Alternatively, you can pass in a Middleman feature module directly.
#
# activate MyFeatureModule
def activate(feature)
feature = feature.to_s if feature.is_a? Symbol
if feature.is_a? String
feature = feature.camelize
feature = Middleman::Features.const_get(feature)
end
register feature
end
# Deprecated API. Please use `activate` instead.
def enable(feature_name)
$stderr.puts "Warning: Feature activation has been renamed from enable to activate"
activate(feature_name)
super(feature_name)
end
end
end

View file

@ -1,9 +1,6 @@
# We're riding on Sinatra, so let's include it.
require "sinatra/base"
# Use the padrino project's helpers
require "padrino-core/application/rendering"
module Middleman
class Server < Sinatra::Base
# Basic Sinatra config
@ -33,27 +30,27 @@ module Middleman
# Disable Padrino cache buster until explicitly enabled
set :asset_stamp, false
# Activate custom features
register Middleman::CoreExtensions::Features
# Setup custom rendering
register Middleman::CoreExtensions::Rendering
# Activate built-in helpers
register Middleman::CoreExtensions::DefaultHelpers
# Activate Yaml Data package
register Middleman::CoreExtensions::Data
# Activate custom features
register Middleman::Features
# Parse YAML from templates
register Middleman::CoreExtensions::FrontMatter
# Activate Webservices Proxy package
# register Middleman::Features::Proxy
register Middleman::Features::FrontMatter
# Activate Lorem helpers
register Middleman::Features::Lorem
# Tilt-aware renderer
register Padrino::Rendering
set :default_extensions, [
:lorem
]
# Override Sinatra's set to accept a block
# Specifically for the asset_host feature
def self.set(option, value=self, &block)
if block_given?
value = Proc.new { block }
@ -62,21 +59,6 @@ module Middleman
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
# Activate custom renderers
register Middleman::Renderers::Slim
register Middleman::Renderers::Haml
register Middleman::Renderers::Sass
register Middleman::Renderers::Markdown
register Middleman::Renderers::CoffeeScript
# Rack helper for adding mime-types during local preview
def self.mime(ext, type)
ext = ".#{ext}" unless ext.to_s[0] == ?.
@ -195,20 +177,3 @@ module Middleman
end
require "middleman/assets"
# The Rack App
class Middleman::Server
def self.new(*args, &block)
# 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
@@run_after_features.each { |block| class_eval(&block) }
super
end
end