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
# ERb Support
Middleman::Extensions.register :erb_renderer, auto_activate: :before_configuration do
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
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)
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,11 +4,11 @@ require 'coffee_script'
module Middleman
module Renderers
# CoffeeScript Renderer
module CoffeeScript
class CoffeeScript < ::Middleman::Extension
# Setup extension
class << self
# Once registered
def registered(app)
def initialize(app, options={}, &block)
super
# Tell Tilt to use it as well (for inline scss blocks)
::Tilt.register 'coffee', DebuggingCoffeeScriptTemplate
::Tilt.prefer(DebuggingCoffeeScriptTemplate)
@ -17,8 +17,6 @@ module Middleman
DebuggingCoffeeScriptTemplate.middleman_app = self
end
end
alias_method :included, :registered
end
# A Template for Tilt which outputs debug messages
class DebuggingCoffeeScriptTemplate < ::Tilt::CoffeeScriptTemplate

View file

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

View file

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

View file

@ -3,11 +3,10 @@ require 'less'
module Middleman
module Renderers
# Sass renderer
module Less
# Setup extension
class << self
# Once registered
def registered(app)
class Less < ::Middleman::Extension
def initialize(app, options={}, &block)
super
# Default less options
app.config.define_setting :less, {}, 'LESS compiler options'
@ -20,9 +19,6 @@ module Middleman
::Tilt.prefer(LocalLoadingLessTemplate)
end
alias_method :included, :registered
end
# A SassTemplate for Tilt which outputs debug messages
class LocalLoadingLessTemplate < ::Tilt::LessTemplate
def prepare

View file

@ -1,35 +1,36 @@
module Middleman
module Renderers
# Markdown renderer
module Markdown
# Setup extension
class << self
class Markdown < ::Middleman::Extension
# Once registered
def registered(app)
def initialize(app, options={}, &block)
super
# 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
# Once configuration is parsed
app.after_configuration do
def after_configuration
markdown_exts = %w(markdown mdown md mkd mkdn)
begin
# Look for the user's preferred engine
if config[:markdown_engine] == :redcarpet
if app.config[:markdown_engine] == :redcarpet
require 'middleman-core/renderers/redcarpet'
::Tilt.prefer(::Middleman::Renderers::RedcarpetTemplate, *markdown_exts)
elsif config[:markdown_engine] == :kramdown
elsif app.config[:markdown_engine] == :kramdown
require 'middleman-core/renderers/kramdown'
::Tilt.prefer(::Middleman::Renderers::KramdownTemplate, *markdown_exts)
elsif config[:markdown_engine]
elsif app.config[:markdown_engine]
# Map symbols to classes
markdown_engine_klass = if config[:markdown_engine].is_a? Symbol
engine = config[:markdown_engine].to_s
markdown_engine_klass = if app.config[:markdown_engine].is_a? Symbol
engine = app.config[:markdown_engine].to_s
engine = engine == 'rdiscount' ? 'RDiscount' : engine.camelize
config[:markdown_engine_prefix].const_get("#{engine}Template")
app.config[:markdown_engine_prefix].const_get("#{engine}Template")
else
config[:markdown_engine_prefix]
app.config[:markdown_engine_prefix]
end
# Tell tilt to use that engine
@ -39,14 +40,10 @@ module Middleman
# 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?"
logger.warn "Requested Markdown engine (#{app.config[:markdown_engine]}) not found. Maybe the gem needs to be installed and required?"
end
end
end
end
alias_method :included, :registered
end
end
end
end

View file

@ -33,11 +33,11 @@ end
module Middleman
module Renderers
# Sass renderer
module Sass
class Sass < ::Middleman::Extension
# Setup extension
class << self
# Once registered
def registered(app)
def initialize(app, options={}, &block)
super
opts = { output_style: :nested }
opts[:line_comments] = false if ENV['TEST']
@ -59,9 +59,6 @@ module Middleman
require 'middleman-core/renderers/sass_functions'
end
alias_method :included, :registered
end
# A SassTemplate for Tilt which outputs debug messages
class SassPlusCSSFilenameTemplate < ::Tilt::SassTemplate
def initialize(*args, &block)

View file

@ -18,11 +18,9 @@ end
module Middleman
module Renderers
# Slim renderer
module Slim
class Slim < ::Middleman::Extension
# Setup extension
class << self
# Once registered
def registered(app)
def initialize(_app, _options={}, &_block)
# Setup Slim options to work with partials
::Slim::Engine.set_default_options(
buffer: '@_out_buf',
@ -30,10 +28,11 @@ module Middleman
generator: ::Temple::Generators::RailsOutputBuffer,
disable_escape: true
)
end
app.after_configuration do
def after_configuration
context_hack = {
context: template_context_class.new(self)
context: app.template_context_class.new(self)
}
::Slim::Embedded::SassEngine.disable_option_validator!
@ -42,9 +41,5 @@ module Middleman
end
end
end
alias_method :included, :registered
end
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