Cleanup renderers

This commit is contained in:
Thomas Reynolds 2012-04-26 16:15:35 -07:00
parent 2297100d9e
commit 874ebc13c9
5 changed files with 202 additions and 170 deletions

View file

@ -1,25 +1,30 @@
# Require gem module Middleman
require "haml" module Renderers
# Haml Renderer # Haml Renderer
module Middleman::Renderers::Haml module Haml
# Setup extension # Setup extension
class << self class << self
# Once registered # Once registered
def registered(app) def registered(app)
# Add haml helpers to context # Require gem
app.send :include, ::Haml::Helpers require "haml"
app.before_configuration do app.before_configuration do
template_extensions :haml => :html template_extensions :haml => :html
end end
# Setup haml helper paths # Add haml helpers to context
app.ready do app.send :include, ::Haml::Helpers
init_haml_helpers
# Setup haml helper paths
app.ready do
init_haml_helpers
end
end
alias :included :registered
end end
end end
alias :included :registered
end end
end end

View file

@ -1,35 +1,40 @@
# Liquid Renderer module Middleman
module Middleman::Renderers::Liquid module Renderers
# Setup extension # Liquid Renderer
class << self module Liquid
# Once registerd # Setup extension
def registered(app) class << self
# Liquid is not included in the default gems,
# but we'll support it if available.
begin
# Require Gem # Once registerd
require "liquid" def registered(app)
# Liquid is not included in the default gems,
# but we'll support it if available.
begin
# Require Gem
require "liquid"
app.before_configuration do app.before_configuration do
template_extensions :liquid => :html template_extensions :liquid => :html
end end
# After config, setup liquid partial paths # After config, setup liquid partial paths
app.after_configuration do app.after_configuration do
Liquid::Template.file_system = Liquid::LocalFileSystem.new(source_dir) ::Liquid::Template.file_system = ::Liquid::LocalFileSystem.new(source_dir)
# Convert data object into a hash for liquid # Convert data object into a hash for liquid
sitemap.provides_metadata %r{\.liquid$} do |path| sitemap.provides_metadata %r{\.liquid$} do |path|
{ :locals => { :data => data.to_h } } { :locals => { :data => data.to_h } }
end
end
rescue LoadError
end end
end end
rescue LoadError
alias :included :registered
end end
end end
alias :included :registered
end end
end end

View file

@ -1,50 +1,57 @@
# Markdown renderer module Middleman
module Middleman::Renderers::Markdown module Renderers
# Setup extension # Markdown renderer
class << self module Markdown
# Once registered # Setup extension
def registered(app) class << self
# Require redcarpet gem
require "redcarpet"
# Forcably disable Redcarpet1 support. # Once registered
# Tilt defaults to this if available, but the compat def registered(app)
# layer disables extensions. # Set our preference for a markdown engine
Object.send(:remove_const, :RedcarpetCompat) if defined? ::RedcarpetCompat # TODO: Find a JRuby-compatible version
app.set :markdown_engine, :redcarpet
app.set :markdown_engine_prefix, ::Tilt
# Set our preference for a markdown engine app.before_configuration do
app.set :markdown_engine, :redcarpet template_extensions :markdown => :html,
app.set :markdown_engine_prefix, ::Tilt :mdown => :html,
:md => :html,
app.before_configuration do :mkd => :html,
template_extensions :markdown => :html, :mkdn => :html
:mdown => :html,
:md => :html,
:mkd => :html,
:mkdn => :html
end
# Once configuration is parsed
app.after_configuration do
# Look for the user's preferred engine
unless markdown_engine.nil?
# Map symbols to classes
if markdown_engine.is_a? Symbol
engine = markdown_engine.to_s
engine = engine == "rdiscount" ? "RDiscount" : engine.camelize
markdown_engine = markdown_engine_prefix.const_get("#{engine}Template")
end end
# Tell tilt to use that engine # Once configuration is parsed
::Tilt.prefer(markdown_engine) app.after_configuration do
# Look for the user's preferred engine
unless markdown_engine.nil?
# Map symbols to classes
markdown_engine_klass = if markdown_engine.is_a? Symbol
engine = markdown_engine.to_s
engine = engine == "rdiscount" ? "RDiscount" : engine.camelize
markdown_engine_prefix.const_get("#{engine}Template")
else
markdown_engine_prefix
end
# Tell tilt to use that engine
::Tilt.prefer(markdown_engine_klass)
if markdown_engine == :redcarpet
# Forcably disable Redcarpet1 support.
# Tilt defaults to this if available, but the compat
# layer disables extensions.
Object.send(:remove_const, :RedcarpetCompat) if defined? ::RedcarpetCompat
end
end
end
end end
alias :included :registered
end end
end end
alias :included :registered
end end
end end

View file

@ -1,81 +1,88 @@
# Pull in gems # Pull in gems
require "sprockets" require "sprockets"
require "sprockets-sass" require "sprockets-sass"
require "sass"
# Stick with Compass' asset functions module Middleman
Sprockets::Sass.add_sass_functions = false module Renderers
# Sass renderer # Sass renderer
module Middleman::Renderers::Sass module Sass
# Setup extension # Setup extension
class << self class << self
# Once registered # Once registered
def registered(app) def registered(app)
# Default sass options require "sass"
app.set :sass, {}
app.before_configuration do # Stick with Compass' asset functions
template_extensions :scss => :css, ::Sprockets::Sass.add_sass_functions = false
:sass => :css
# Default sass options
app.set :sass, {}
app.before_configuration do
template_extensions :scss => :css,
:sass => :css
end
# Tell Sprockets to use our custom Sass template
::Sprockets.register_engine ".sass", SassPlusCSSFilenameTemplate
# Tell Tilt to use it as well (for inline sass blocks)
::Tilt.register 'sass', SassPlusCSSFilenameTemplate
::Tilt.prefer(SassPlusCSSFilenameTemplate)
# Tell Sprockets to use our custom Scss template
::Sprockets.register_engine ".scss", ScssPlusCSSFilenameTemplate
# Tell Tilt to use it as well (for inline scss blocks)
::Tilt.register 'scss', ScssPlusCSSFilenameTemplate
::Tilt.prefer(ScssPlusCSSFilenameTemplate)
end
alias :included :registered
end end
end
alias :included :registered # A SassTemplate for Sprockets/Tilt which outputs debug messages
end class SassPlusCSSFilenameTemplate < ::Sprockets::Sass::SassTemplate
# A SassTemplate for Sprockets/Tilt which outputs debug messages # Add exception messaging
class SassPlusCSSFilenameTemplate < ::Sprockets::Sass::SassTemplate # @param [Class] context
# @param [Hash] locals
# @return [String]
def evaluate(context, locals, &block)
begin
super
rescue Sass::SyntaxError => e
Sass::SyntaxError.exception_to_css(e, :full_exception => true)
end
end
# Add exception messaging protected
# @param [Class] context # Change Sass path, for url functions, to the build folder if we're building
# @param [Hash] locals # @return [Hash]
# @return [String] def sass_options
def evaluate(context, locals, &block) location_of_sass_file = File.expand_path(@context.source, @context.root)
begin
super parts = basename.split('.')
rescue Sass::SyntaxError => e parts.pop
Sass::SyntaxError.exception_to_css(e, :full_exception => true) css_filename = File.join(location_of_sass_file, @context.css_dir, parts.join("."))
super.merge(:css_filename => css_filename)
end
end end
end
protected # SCSS version of the above template
# Change Sass path, for url functions, to the build folder if we're building class ScssPlusCSSFilenameTemplate < SassPlusCSSFilenameTemplate
# @return [Hash]
def sass_options
location_of_sass_file = File.expand_path(@context.source, @context.root)
parts = basename.split('.') # Define the expected syntax for the template
parts.pop # @return [Symbol]
css_filename = File.join(location_of_sass_file, @context.css_dir, parts.join(".")) def syntax
:scss
end
end
super.merge(:css_filename => css_filename)
end end
end end
# Tell Sprockets to use our custom Sass template
::Sprockets.register_engine ".sass", SassPlusCSSFilenameTemplate
# Tell Tilt to use it as well (for inline sass blocks)
::Tilt.register 'sass', SassPlusCSSFilenameTemplate
::Tilt.prefer(SassPlusCSSFilenameTemplate)
# SCSS version of the above template
class ScssPlusCSSFilenameTemplate < SassPlusCSSFilenameTemplate
# Define the expected syntax for the template
# @return [Symbol]
def syntax
:scss
end
end
# Tell Sprockets to use our custom Scss template
::Sprockets.register_engine ".scss", ScssPlusCSSFilenameTemplate
# Tell Tilt to use it as well (for inline scss blocks)
::Tilt.register 'scss', ScssPlusCSSFilenameTemplate
::Tilt.prefer(ScssPlusCSSFilenameTemplate)
end end

View file

@ -1,27 +1,35 @@
# Slim renderer module Middleman
module Middleman::Renderers::Slim module Renderers
# Setup extension # Slim renderer
class << self module Slim
# Once registered # Setup extension
def registered(app) class << self
# Slim is not included in the default gems,
# but we'll support it if available.
begin
# Load gem
require "slim"
app.before_configuration do # Once registered
template_extensions :slim => :html def registered(app)
# Slim is not included in the default gems,
# but we'll support it if available.
begin
# Load gem
require "slim"
app.before_configuration do
template_extensions :slim => :html
end
# Setup Slim options to work with partials
::Slim::Engine.set_default_options(
:buffer => '@_out_buf',
:generator => ::Temple::Generators::StringBuffer
)
rescue LoadError
end
end end
# Setup Slim options to work with partials alias :included :registered
Slim::Engine.set_default_options(:buffer => '@_out_buf', :generator => Temple::Generators::StringBuffer) if defined?(Slim)
rescue LoadError
end end
end end
alias :included :registered
end end
end end