Convert renderers into first-class extensions

This commit is contained in:
Thomas Reynolds 2014-07-05 13:41:59 -07:00
parent c0a6d8ac4c
commit 300ef8d8fe
13 changed files with 185 additions and 235 deletions

View file

@ -53,6 +53,9 @@ module Middleman
# Runs after the build is finished
define_hook :after_build
define_hook :before_render
define_hook :after_render
# Root project directory (overwritten in middleman build/server)
# @return [String]
def self.root
@ -166,9 +169,6 @@ module Middleman
# Basic Rack Request Handling
include Middleman::CoreExtensions::Request
# Setup custom rendering
include Middleman::CoreExtensions::Rendering
# Reference to Logger singleton
def_delegator :"::Middleman::Logger", :singleton, :logger
@ -226,7 +226,7 @@ module Middleman
if config[:autoload_sprockets]
begin
require 'middleman-sprockets'
activate :sprockets
@extensions.activate :sprockets
rescue LoadError
# It's OK if somebody is using middleman-core without middleman-sprockets
end
@ -249,6 +249,15 @@ module Middleman
::I18n.reload!
end
# Clean up missing Tilt exts
Tilt.mappings.each do |key, _|
begin
Tilt[".#{key}"]
rescue LoadError, NameError
Tilt.mappings.delete(key)
end
end
run_hook :after_configuration
config_context.execute_after_configuration_callbacks

View file

@ -1,90 +1,55 @@
require 'middleman-core/template_context'
# Rendering extension
module Middleman
module CoreExtensions
module Rendering
# Setup extension
class << self
# Once registered
def included(app)
app.define_hook :before_render
app.define_hook :after_render
::Tilt.mappings.delete('html') # WTF, Tilt?
::Tilt.mappings.delete('csv')
require 'active_support/core_ext/string/output_safety'
# Activate custom renderers
require 'middleman-core/renderers/erb'
app.send :include, Middleman::Renderers::ERb
# CoffeeScript Support
begin
require 'middleman-core/renderers/coffee_script'
app.send :include, Middleman::Renderers::CoffeeScript
rescue LoadError
end
# Haml Support
begin
require 'middleman-core/renderers/haml'
app.send :include, Middleman::Renderers::Haml
rescue LoadError
end
# Sass Support
begin
require 'middleman-core/renderers/sass'
app.send :include, Middleman::Renderers::Sass
rescue LoadError
end
# Markdown Support
require 'middleman-core/renderers/markdown'
app.send :include, Middleman::Renderers::Markdown
# Liquid Support
begin
require 'middleman-core/renderers/liquid'
Middleman::Extensions.register :liquid, Middleman::Renderers::Liquid, auto_activate: :before_configuration
rescue LoadError
end
# Slim Support
begin
require 'middleman-core/renderers/slim'
app.send :include, Middleman::Renderers::Slim
rescue LoadError
end
# Less Support
begin
require 'middleman-core/renderers/less'
app.send :include, Middleman::Renderers::Less
rescue LoadError
end
# Stylus Support
begin
require 'middleman-core/renderers/stylus'
app.send :include, Middleman::Renderers::Stylus
rescue LoadError
end
# Clean up missing Tilt exts
app.after_configuration do
Tilt.mappings.each do |key, _|
begin
Tilt[".#{key}"]
rescue LoadError, NameError
Tilt.mappings.delete(key)
end
end
end
end
end
end
end
# ERb Support
Middleman::Extensions.register :erb_renderer, auto_activate: :before_configuration do
require 'middleman-core/renderers/erb'
Middleman::Renderers::ERb
end
# CoffeeScript Support
Middleman::Extensions.register :coffee_renderer, auto_activate: :before_configuration do
require 'middleman-core/renderers/coffee_script'
Middleman::Renderers::CoffeeScript
end
# Haml Support
Middleman::Extensions.register :haml_renderer, auto_activate: :before_configuration do
require 'middleman-core/renderers/haml'
Middleman::Renderers::Haml
end
# Sass Support
Middleman::Extensions.register :sass_renderer, auto_activate: :before_configuration do
require 'middleman-core/renderers/sass'
Middleman::Renderers::Sass
end
# Markdown Support
Middleman::Extensions.register :markdown_renderer, auto_activate: :before_configuration do
require 'middleman-core/renderers/markdown'
Middleman::Renderers::Markdown
end
# Liquid Support
Middleman::Extensions.register :liquid_renderer, auto_activate: :before_configuration do
require 'middleman-core/renderers/liquid'
Middleman::Renderers::Liquid
end
# Slim Support
Middleman::Extensions.register :slim_renderer, auto_activate: :before_configuration do
require 'middleman-core/renderers/slim'
Middleman::Renderers::Slim
end
# Less Support
Middleman::Extensions.register :less_renderer, auto_activate: :before_configuration do
require 'middleman-core/renderers/less'
Middleman::Renderers::Less
end
# Stylus Support
Middleman::Extensions.register :stylus_renderer, auto_activate: :before_configuration do
require 'middleman-core/renderers/stylus'
Middleman::Renderers::Stylus
end

View file

@ -1,7 +1,7 @@
module Middleman
class ExtensionManager
extend Forwardable
def_delegator :"::Middleman::Logger", :singleton, :logger
def_delegator :@app, :logger
def_delegators :@activated, :[]
def initialize(app)
@ -32,7 +32,13 @@ module Middleman
# @yield [Middleman::Configuration::ConfigurationManager] Extension options that can be modified before the extension is initialized.
# @return [void]
def activate(ext_name, options={}, &block)
extension = ::Middleman::Extensions.load(ext_name)
begin
extension = ::Middleman::Extensions.load(ext_name)
rescue LoadError => e
logger.debug "== Failed Activation `#{ext_name}` : #{e.message}"
return
end
logger.debug "== Activating: #{ext_name}"
if extension.supports_multiple_instances?

View file

@ -1,6 +1,9 @@
require 'tilt'
require 'active_support/core_ext/string/output_safety'
::Tilt.mappings.delete('html') # WTF, Tilt?
::Tilt.mappings.delete('csv')
module Middleman
class FileRenderer
extend Forwardable

View file

@ -94,6 +94,12 @@ module Middleman
def new_app
opts = @options.dup
::Middleman::Logger.singleton(
opts[:debug] ? 0 : 1,
opts[:instrumenting] || false
)
server = ::Middleman::Application.server
# Add in the meta pages application
@ -103,11 +109,6 @@ module Middleman
end
@app = server.inst do
::Middleman::Logger.singleton(
opts[:debug] ? 0 : 1,
opts[:instrumenting] || false
)
config[:environment] = opts[:environment].to_sym if opts[:environment]
end
end

View file

@ -4,20 +4,18 @@ require 'coffee_script'
module Middleman
module Renderers
# CoffeeScript Renderer
module CoffeeScript
class CoffeeScript < ::Middleman::Extension
# Setup extension
class << self
# Once registered
def registered(app)
# Tell Tilt to use it as well (for inline scss blocks)
::Tilt.register 'coffee', DebuggingCoffeeScriptTemplate
::Tilt.prefer(DebuggingCoffeeScriptTemplate)
def initialize(app, options={}, &block)
super
app.before_configuration do
DebuggingCoffeeScriptTemplate.middleman_app = self
end
# Tell Tilt to use it as well (for inline scss blocks)
::Tilt.register 'coffee', DebuggingCoffeeScriptTemplate
::Tilt.prefer(DebuggingCoffeeScriptTemplate)
app.before_configuration do
DebuggingCoffeeScriptTemplate.middleman_app = self
end
alias_method :included, :registered
end
# A Template for Tilt which outputs debug messages

View file

@ -1,17 +1,9 @@
# ERb renderer
module Middleman
module Renderers
module ERb
# Setup extension
class << self
# once registered
def registered(app)
# After config
app.after_configuration do
::Tilt.prefer(Template, :erb)
end
end
alias_method :included, :registered
class ERb < ::Middleman::Extension
def after_configuration
::Tilt.prefer(Template, :erb)
end
class Template < ::Tilt::ErubisTemplate

View file

@ -37,19 +37,16 @@ module Middleman
end
# Haml Renderer
module Haml
mattr_accessor :last_haml_scope
class Haml < ::Middleman::Extension
cattr_accessor :last_haml_scope
# Setup extension
class << self
# Once registered
def registered(_)
::Tilt.prefer(::Middleman::Renderers::HamlTemplate, :haml)
def initialize(app, options={}, &block)
super
# Add haml helpers to context
::Middleman::TemplateContext.send :include, ::Haml::Helpers
end
alias_method :included, :registered
::Tilt.prefer(::Middleman::Renderers::HamlTemplate, :haml)
# Add haml helpers to context
::Middleman::TemplateContext.send :include, ::Haml::Helpers
end
end
end

View file

@ -3,24 +3,20 @@ require 'less'
module Middleman
module Renderers
# Sass renderer
module Less
# Setup extension
class << self
# Once registered
def registered(app)
# Default less options
app.config.define_setting :less, {}, 'LESS compiler options'
class Less < ::Middleman::Extension
def initialize(app, options={}, &block)
super
app.after_configuration do
::Less.paths << File.join(source_dir, config[:css_dir])
end
# Default less options
app.config.define_setting :less, {}, 'LESS compiler options'
# Tell Tilt to use it as well (for inline sass blocks)
::Tilt.register 'less', LocalLoadingLessTemplate
::Tilt.prefer(LocalLoadingLessTemplate)
app.after_configuration do
::Less.paths << File.join(source_dir, config[:css_dir])
end
alias_method :included, :registered
# Tell Tilt to use it as well (for inline sass blocks)
::Tilt.register 'less', LocalLoadingLessTemplate
::Tilt.prefer(LocalLoadingLessTemplate)
end
# A SassTemplate for Tilt which outputs debug messages

View file

@ -1,51 +1,48 @@
module Middleman
module Renderers
# Markdown renderer
module Markdown
# Setup extension
class << self
# Once registered
def registered(app)
# Set our preference for a markdown engine
app.config.define_setting :markdown_engine, :kramdown, 'Preferred markdown engine'
app.config.define_setting :markdown_engine_prefix, ::Tilt, 'The parent module for markdown template engines'
class Markdown < ::Middleman::Extension
# Once registered
def initialize(app, options={}, &block)
super
# Once configuration is parsed
app.after_configuration do
markdown_exts = %w(markdown mdown md mkd mkdn)
# Set our preference for a markdown engine
app.config.define_setting :markdown_engine, :kramdown, 'Preferred markdown engine'
app.config.define_setting :markdown_engine_prefix, ::Tilt, 'The parent module for markdown template engines'
end
begin
# Look for the user's preferred engine
if config[:markdown_engine] == :redcarpet
require 'middleman-core/renderers/redcarpet'
::Tilt.prefer(::Middleman::Renderers::RedcarpetTemplate, *markdown_exts)
elsif config[:markdown_engine] == :kramdown
require 'middleman-core/renderers/kramdown'
::Tilt.prefer(::Middleman::Renderers::KramdownTemplate, *markdown_exts)
elsif config[:markdown_engine]
# Map symbols to classes
markdown_engine_klass = if config[:markdown_engine].is_a? Symbol
engine = config[:markdown_engine].to_s
engine = engine == 'rdiscount' ? 'RDiscount' : engine.camelize
config[:markdown_engine_prefix].const_get("#{engine}Template")
else
config[:markdown_engine_prefix]
end
# Once configuration is parsed
def after_configuration
markdown_exts = %w(markdown mdown md mkd mkdn)
# Tell tilt to use that engine
::Tilt.prefer(markdown_engine_klass, *markdown_exts)
end
rescue LoadError
# If they just left it at the default engine and don't happen to have it,
# then they're using middleman-core bare and we shouldn't bother them.
if config.setting(:markdown_engine).value_set?
logger.warn "Requested Markdown engine (#{config[:markdown_engine]}) not found. Maybe the gem needs to be installed and required?"
end
begin
# Look for the user's preferred engine
if app.config[:markdown_engine] == :redcarpet
require 'middleman-core/renderers/redcarpet'
::Tilt.prefer(::Middleman::Renderers::RedcarpetTemplate, *markdown_exts)
elsif app.config[:markdown_engine] == :kramdown
require 'middleman-core/renderers/kramdown'
::Tilt.prefer(::Middleman::Renderers::KramdownTemplate, *markdown_exts)
elsif app.config[:markdown_engine]
# Map symbols to classes
markdown_engine_klass = if app.config[:markdown_engine].is_a? Symbol
engine = app.config[:markdown_engine].to_s
engine = engine == 'rdiscount' ? 'RDiscount' : engine.camelize
app.config[:markdown_engine_prefix].const_get("#{engine}Template")
else
app.config[:markdown_engine_prefix]
end
# Tell tilt to use that engine
::Tilt.prefer(markdown_engine_klass, *markdown_exts)
end
rescue LoadError
# If they just left it at the default engine and don't happen to have it,
# then they're using middleman-core bare and we shouldn't bother them.
if config.setting(:markdown_engine).value_set?
logger.warn "Requested Markdown engine (#{app.config[:markdown_engine]}) not found. Maybe the gem needs to be installed and required?"
end
end
alias_method :included, :registered
end
end
end

View file

@ -33,33 +33,30 @@ end
module Middleman
module Renderers
# Sass renderer
module Sass
class Sass < ::Middleman::Extension
# Setup extension
class << self
# Once registered
def registered(app)
opts = { output_style: :nested }
opts[:line_comments] = false if ENV['TEST']
def initialize(app, options={}, &block)
super
# Default sass options
app.config.define_setting :sass, opts, 'Sass engine options'
opts = { output_style: :nested }
opts[:line_comments] = false if ENV['TEST']
app.config.define_setting :sass_assets_paths, [], 'Paths to extra SASS/SCSS files'
# Default sass options
app.config.define_setting :sass, opts, 'Sass engine options'
# Tell Tilt to use it as well (for inline sass blocks)
::Tilt.register 'sass', SassPlusCSSFilenameTemplate
::Tilt.prefer(SassPlusCSSFilenameTemplate)
app.config.define_setting :sass_assets_paths, [], 'Paths to extra SASS/SCSS files'
# Tell Tilt to use it as well (for inline scss blocks)
::Tilt.register 'scss', ScssPlusCSSFilenameTemplate
::Tilt.prefer(ScssPlusCSSFilenameTemplate)
# Tell Tilt to use it as well (for inline sass blocks)
::Tilt.register 'sass', SassPlusCSSFilenameTemplate
::Tilt.prefer(SassPlusCSSFilenameTemplate)
::Compass::ImportOnce.activate!
# Tell Tilt to use it as well (for inline scss blocks)
::Tilt.register 'scss', ScssPlusCSSFilenameTemplate
::Tilt.prefer(ScssPlusCSSFilenameTemplate)
require 'middleman-core/renderers/sass_functions'
end
::Compass::ImportOnce.activate!
alias_method :included, :registered
require 'middleman-core/renderers/sass_functions'
end
# A SassTemplate for Tilt which outputs debug messages

View file

@ -18,32 +18,27 @@ end
module Middleman
module Renderers
# Slim renderer
module Slim
class Slim < ::Middleman::Extension
# Setup extension
class << self
# Once registered
def registered(app)
# Setup Slim options to work with partials
::Slim::Engine.set_default_options(
buffer: '@_out_buf',
use_html_safe: true,
generator: ::Temple::Generators::RailsOutputBuffer,
disable_escape: true
)
def initialize(_app, _options={}, &_block)
# Setup Slim options to work with partials
::Slim::Engine.set_default_options(
buffer: '@_out_buf',
use_html_safe: true,
generator: ::Temple::Generators::RailsOutputBuffer,
disable_escape: true
)
end
app.after_configuration do
context_hack = {
context: template_context_class.new(self)
}
def after_configuration
context_hack = {
context: app.template_context_class.new(self)
}
::Slim::Embedded::SassEngine.disable_option_validator!
%w(sass scss markdown).each do |engine|
::Slim::Embedded.default_options[engine.to_sym] = context_hack
end
end
::Slim::Embedded::SassEngine.disable_option_validator!
%w(sass scss markdown).each do |engine|
::Slim::Embedded.default_options[engine.to_sym] = context_hack
end
alias_method :included, :registered
end
end
end

View file

@ -3,17 +3,11 @@ require 'stylus/tilt'
module Middleman
module Renderers
# Sass renderer
module Stylus
# Setup extension
class << self
# Once registered
def registered(app)
# Default stylus options
app.config.define_setting :styl, {}, 'Stylus config options'
end
class Stylus < ::Middleman::Extension
def initialize(app, options={}, &block)
super
alias_method :included, :registered
app.config.define_setting :styl, {}, 'Stylus config options'
end
end
end