Hack a way to pass Middleman context into Slim filters

This commit is contained in:
Thomas Reynolds 2012-08-16 13:48:34 -07:00
parent 8e7041994f
commit 54c055ea5e
6 changed files with 79 additions and 13 deletions

View file

@ -1,6 +1,7 @@
Master
===
* Fix Sass/Scss filter in Slim templates
* Added SMACSS template
* Give file metadata (such as frontmatter) precedence over path meta. #552
* Add `sass_assets_paths` option for arbitrary sass partial locations.

View file

@ -223,7 +223,7 @@ module Middleman
# messages, which can take a long time (minutes at full CPU)
# if the object is huge or has cyclic references, like this.
def to_s
"the Middleman application context"
"#<Middleman::Application>"
end
# Expand a path to include the index file if it's a directory

View file

@ -39,6 +39,14 @@ module Middleman
# A SassTemplate for Tilt which outputs debug messages
class SassPlusCSSFilenameTemplate < ::Tilt::SassTemplate
def initialize(*args, &block)
super
if @options.has_key?(:context)
@context = @options[:context]
end
end
# Define the expected syntax for the template
# @return [Symbol]
def syntax
@ -52,7 +60,7 @@ module Middleman
# @param [Hash] locals
# @return [String]
def evaluate(context, locals, &block)
@context = context
@context ||= context
@engine = ::Sass::Engine.new(data, sass_options)
begin
@ -62,17 +70,20 @@ module Middleman
end
end
private
# 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("."))
options.merge(:filename => eval_file, :line => line, :syntax => syntax, :css_filename => css_filename)
more_opts = { :filename => eval_file, :line => line, :syntax => syntax }
if @context.is_a?(::Middleman::Application) && file
location_of_sass_file = File.expand_path(@context.source, @context.root)
parts = basename.split('.')
parts.pop
more_opts[:css_filename] = File.join(location_of_sass_file, @context.css_dir, parts.join("."))
end
options.merge(more_opts)
end
end

View file

@ -21,6 +21,14 @@ module Middleman
:buffer => '@_out_buf',
:generator => ::Temple::Generators::StringBuffer
)
app.after_configuration do
sass_context_hack = {
:context => self
}
::Slim::EmbeddedEngine.default_options[:sass] = sass_context_hack
::Slim::EmbeddedEngine.default_options[:scss] = sass_context_hack
end
end
alias :included :registered

View file

@ -18,4 +18,52 @@ Feature: Support slim templating language
"""
And the Server is running at "empty_app"
When I go to "/slim.html"
Then I should see "<h1>Welcome to Slim</h1>"
Then I should see "<h1>Welcome to Slim</h1>"
Scenario: Rendering Scss in a Slim filter
Given an empty app
And a file named "config.rb" with:
"""
"""
And a file named "source/scss.html.slim" with:
"""
doctype 5
html lang='en'
head
meta charset="utf-8"
scss:
@import "compass";
@include global-reset;
body
h1 Welcome to Slim
"""
And a file named "source/sass.html.slim" with:
"""
doctype 5
html lang='en'
head
meta charset="utf-8"
sass:
@import "compass"
+global-reset
body
h1 Welcome to Slim
"""
And a file named "source/error.html.slim" with:
"""
doctype 5
html lang='en'
head
meta charset="utf-8"
scss:
+global-reset
body
h1 Welcome to Slim
"""
And the Server is running at "empty_app"
When I go to "/scss.html"
Then I should see "html, body, div"
When I go to "/sass.html"
Then I should see "html, body, div"
When I go to "/error.html"
Then I should see "Syntax error"

View file

@ -73,7 +73,6 @@ module Middleman
# A Compass Sass template for Tilt, adding our options in
class CompassSassTemplate < ::Middleman::Renderers::Sass::SassPlusCSSFilenameTemplate
private
def sass_options
super.merge(::Compass.configuration.to_sass_engine_options)
end
@ -81,7 +80,6 @@ module Middleman
# A Compass Scss template for Tilt, adding our options in
class CompassScssTemplate < ::Middleman::Renderers::Sass::ScssPlusCSSFilenameTemplate
private
def sass_options
super.merge(::Compass.configuration.to_sass_engine_options)
end