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
class << self
# Once registered
def registered(app)
# Add haml helpers to context
app.send :include, ::Haml::Helpers
app.before_configuration do # Setup extension
template_extensions :haml => :html class << self
end # Once registered
def registered(app)
# Require gem
require "haml"
app.before_configuration do
template_extensions :haml => :html
end
# Add haml helpers to context
app.send :include, ::Haml::Helpers
# Setup haml helper paths # Setup haml helper paths
app.ready do app.ready do
init_haml_helpers 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
# Liquid Renderer
module Liquid
# Setup extension # Setup extension
class << self class << self
# Once registerd # Once registerd
def registered(app) def registered(app)
# Liquid is not included in the default gems, # Liquid is not included in the default gems,
# but we'll support it if available. # but we'll support it if available.
begin begin
# Require Gem
require "liquid"
# Require Gem app.before_configuration do
require "liquid" template_extensions :liquid => :html
end
app.before_configuration do # After config, setup liquid partial paths
template_extensions :liquid => :html app.after_configuration do
end ::Liquid::Template.file_system = ::Liquid::LocalFileSystem.new(source_dir)
# After config, setup liquid partial paths
app.after_configuration do
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
class << self
# Once registered # Markdown renderer
def registered(app) module Markdown
# Require redcarpet gem
require "redcarpet" # Setup extension
class << self
# Once registered
def registered(app)
# Set our preference for a markdown engine
# TODO: Find a JRuby-compatible version
app.set :markdown_engine, :redcarpet
app.set :markdown_engine_prefix, ::Tilt
# Forcably disable Redcarpet1 support. app.before_configuration do
# Tilt defaults to this if available, but the compat template_extensions :markdown => :html,
# layer disables extensions. :mdown => :html,
Object.send(:remove_const, :RedcarpetCompat) if defined? ::RedcarpetCompat :md => :html,
:mkd => :html,
# Set our preference for a markdown engine :mkdn => :html
app.set :markdown_engine, :redcarpet end
app.set :markdown_engine_prefix, ::Tilt
# Once configuration is parsed
app.before_configuration do app.after_configuration do
template_extensions :markdown => :html,
:mdown => :html, # Look for the user's preferred engine
:md => :html, unless markdown_engine.nil?
:mkd => :html,
:mkdn => :html # Map symbols to classes
end markdown_engine_klass = if markdown_engine.is_a? Symbol
engine = markdown_engine.to_s
# Once configuration is parsed engine = engine == "rdiscount" ? "RDiscount" : engine.camelize
app.after_configuration do markdown_engine_prefix.const_get("#{engine}Template")
else
# Look for the user's preferred engine markdown_engine_prefix
unless markdown_engine.nil? end
# Map symbols to classes # Tell tilt to use that engine
if markdown_engine.is_a? Symbol ::Tilt.prefer(markdown_engine_klass)
engine = markdown_engine.to_s
engine = engine == "rdiscount" ? "RDiscount" : engine.camelize if markdown_engine == :redcarpet
markdown_engine = markdown_engine_prefix.const_get("#{engine}Template") # 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
# Tell tilt to use that engine
::Tilt.prefer(markdown_engine)
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
module Sass
# Setup extension
class << self
# Once registered
def registered(app)
require "sass"
# Sass renderer # Stick with Compass' asset functions
module Middleman::Renderers::Sass ::Sprockets::Sass.add_sass_functions = false
# Setup extension # Default sass options
class << self app.set :sass, {}
# Once registered
def registered(app)
# Default sass options
app.set :sass, {}
app.before_configuration do app.before_configuration do
template_extensions :scss => :css, template_extensions :scss => :css,
:sass => :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
end
# A SassTemplate for Sprockets/Tilt which outputs debug messages # A SassTemplate for Sprockets/Tilt which outputs debug messages
class SassPlusCSSFilenameTemplate < ::Sprockets::Sass::SassTemplate class SassPlusCSSFilenameTemplate < ::Sprockets::Sass::SassTemplate
# Add exception messaging # Add exception messaging
# @param [Class] context # @param [Class] context
# @param [Hash] locals # @param [Hash] locals
# @return [String] # @return [String]
def evaluate(context, locals, &block) def evaluate(context, locals, &block)
begin begin
super super
rescue Sass::SyntaxError => e rescue Sass::SyntaxError => e
Sass::SyntaxError.exception_to_css(e, :full_exception => true) Sass::SyntaxError.exception_to_css(e, :full_exception => true)
end
end
protected
# Change Sass path, for url functions, to the build folder if we're building
# @return [Hash]
def sass_options
location_of_sass_file = File.expand_path(@context.source, @context.root)
parts = basename.split('.')
parts.pop
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('.')
parts.pop
css_filename = File.join(location_of_sass_file, @context.css_dir, parts.join("."))
super.merge(:css_filename => css_filename)
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 # Define the expected syntax for the template
# @return [Symbol] # @return [Symbol]
def syntax def syntax
:scss :scss
end
end
end end
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
class << self
# Once registered # Slim renderer
def registered(app) module Slim
# Slim is not included in the default gems,
# but we'll support it if available. # Setup extension
begin class << self
# Load gem
require "slim" # Once registered
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 app.before_configuration do
template_extensions :slim => :html 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