Hack a way to pass Middleman context into Slim filters
This commit is contained in:
parent
8e7041994f
commit
54c055ea5e
|
@ -1,6 +1,7 @@
|
||||||
Master
|
Master
|
||||||
===
|
===
|
||||||
|
|
||||||
|
* Fix Sass/Scss filter in Slim templates
|
||||||
* Added SMACSS template
|
* Added SMACSS template
|
||||||
* Give file metadata (such as frontmatter) precedence over path meta. #552
|
* Give file metadata (such as frontmatter) precedence over path meta. #552
|
||||||
* Add `sass_assets_paths` option for arbitrary sass partial locations.
|
* Add `sass_assets_paths` option for arbitrary sass partial locations.
|
||||||
|
|
|
@ -223,7 +223,7 @@ module Middleman
|
||||||
# messages, which can take a long time (minutes at full CPU)
|
# messages, which can take a long time (minutes at full CPU)
|
||||||
# if the object is huge or has cyclic references, like this.
|
# if the object is huge or has cyclic references, like this.
|
||||||
def to_s
|
def to_s
|
||||||
"the Middleman application context"
|
"#<Middleman::Application>"
|
||||||
end
|
end
|
||||||
|
|
||||||
# Expand a path to include the index file if it's a directory
|
# Expand a path to include the index file if it's a directory
|
||||||
|
|
|
@ -39,6 +39,14 @@ module Middleman
|
||||||
# A SassTemplate for Tilt which outputs debug messages
|
# A SassTemplate for Tilt which outputs debug messages
|
||||||
class SassPlusCSSFilenameTemplate < ::Tilt::SassTemplate
|
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
|
# Define the expected syntax for the template
|
||||||
# @return [Symbol]
|
# @return [Symbol]
|
||||||
def syntax
|
def syntax
|
||||||
|
@ -52,7 +60,7 @@ module Middleman
|
||||||
# @param [Hash] locals
|
# @param [Hash] locals
|
||||||
# @return [String]
|
# @return [String]
|
||||||
def evaluate(context, locals, &block)
|
def evaluate(context, locals, &block)
|
||||||
@context = context
|
@context ||= context
|
||||||
@engine = ::Sass::Engine.new(data, sass_options)
|
@engine = ::Sass::Engine.new(data, sass_options)
|
||||||
|
|
||||||
begin
|
begin
|
||||||
|
@ -62,17 +70,20 @@ module Middleman
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
|
||||||
# Change Sass path, for url functions, to the build folder if we're building
|
# Change Sass path, for url functions, to the build folder if we're building
|
||||||
# @return [Hash]
|
# @return [Hash]
|
||||||
def sass_options
|
def sass_options
|
||||||
location_of_sass_file = File.expand_path(@context.source, @context.root)
|
more_opts = { :filename => eval_file, :line => line, :syntax => syntax }
|
||||||
|
|
||||||
parts = basename.split('.')
|
if @context.is_a?(::Middleman::Application) && file
|
||||||
parts.pop
|
location_of_sass_file = File.expand_path(@context.source, @context.root)
|
||||||
css_filename = File.join(location_of_sass_file, @context.css_dir, parts.join("."))
|
|
||||||
|
parts = basename.split('.')
|
||||||
options.merge(:filename => eval_file, :line => line, :syntax => syntax, :css_filename => css_filename)
|
parts.pop
|
||||||
|
more_opts[:css_filename] = File.join(location_of_sass_file, @context.css_dir, parts.join("."))
|
||||||
|
end
|
||||||
|
|
||||||
|
options.merge(more_opts)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -21,6 +21,14 @@ module Middleman
|
||||||
:buffer => '@_out_buf',
|
:buffer => '@_out_buf',
|
||||||
:generator => ::Temple::Generators::StringBuffer
|
: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
|
end
|
||||||
|
|
||||||
alias :included :registered
|
alias :included :registered
|
||||||
|
|
|
@ -18,4 +18,52 @@ Feature: Support slim templating language
|
||||||
"""
|
"""
|
||||||
And the Server is running at "empty_app"
|
And the Server is running at "empty_app"
|
||||||
When I go to "/slim.html"
|
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"
|
|
@ -73,7 +73,6 @@ module Middleman
|
||||||
|
|
||||||
# A Compass Sass template for Tilt, adding our options in
|
# A Compass Sass template for Tilt, adding our options in
|
||||||
class CompassSassTemplate < ::Middleman::Renderers::Sass::SassPlusCSSFilenameTemplate
|
class CompassSassTemplate < ::Middleman::Renderers::Sass::SassPlusCSSFilenameTemplate
|
||||||
private
|
|
||||||
def sass_options
|
def sass_options
|
||||||
super.merge(::Compass.configuration.to_sass_engine_options)
|
super.merge(::Compass.configuration.to_sass_engine_options)
|
||||||
end
|
end
|
||||||
|
@ -81,7 +80,6 @@ module Middleman
|
||||||
|
|
||||||
# A Compass Scss template for Tilt, adding our options in
|
# A Compass Scss template for Tilt, adding our options in
|
||||||
class CompassScssTemplate < ::Middleman::Renderers::Sass::ScssPlusCSSFilenameTemplate
|
class CompassScssTemplate < ::Middleman::Renderers::Sass::ScssPlusCSSFilenameTemplate
|
||||||
private
|
|
||||||
def sass_options
|
def sass_options
|
||||||
super.merge(::Compass.configuration.to_sass_engine_options)
|
super.merge(::Compass.configuration.to_sass_engine_options)
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue