Add tests for Slim inline filters. Refactor similar feature in Haml filters. Closes #1542

This commit is contained in:
Thomas Reynolds 2015-06-16 16:47:42 -07:00
parent 7383f67874
commit 1efe6a27c5
10 changed files with 107 additions and 34 deletions

View file

@ -0,0 +1,42 @@
Feature: Markdown support in Slim (Kramdown)
In order to test support of the Slim markdown filter
Scenario: Markdown filter in Slim works (with Kramdown)
Given a fixture app "markdown-in-slim-app"
And a file named "config.rb" with:
"""
set :markdown_engine, :kramdown
activate :directory_indexes
"""
And a file named "source/markdown_filter.html.slim" with:
"""
markdown:
# H1
paragraph
"""
Given the Server is running at "markdown-in-slim-app"
When I go to "/markdown_filter/"
Then I should see ">H1</h1>"
Then I should see "<p>paragraph</p>"
Scenario: Markdown filter in Slim uses our link_to and image_tag helpers (with Kramdown)
Given a fixture app "markdown-in-slim-app"
And a file named "config.rb" with:
"""
set :markdown_engine, :kramdown
activate :directory_indexes
"""
And a file named "source/link_and_image.html.slim" with:
"""
markdown:
[A link](/link_target.html)
![image](blank.gif){: srcset="image_2x.jpg 2x"}
"""
Given the Server is running at "markdown-in-slim-app"
When I go to "/link_and_image/"
Then I should see "/link_target/"
Then I should see "/images/image_2x.jpg 2x"
Then I should see 'src="/images/blank.gif"'

Binary file not shown.

After

Width:  |  Height:  |  Size: 43 B

View file

@ -0,0 +1,4 @@
---
layout: false
---
Hello World

View file

@ -53,6 +53,7 @@ module Middleman
extension = File.extname(path)
options = opts.merge(options_for_ext(extension))
options[:outvar] ||= '@_out_buf'
options[:context] = context
options.delete(:layout)
# Overwrite with frontmatter options
@ -66,9 +67,10 @@ module Middleman
end
# Read compiled template from disk or cache
template = cache.fetch(:compiled_template, extension, options, body) do
::Tilt.new(path, 1, options) { body }
end
template = ::Tilt.new(path, 1, options) { body }
# template = cache.fetch(:compiled_template, extension, options, body) do
# ::Tilt.new(path, 1, options) { body }
# end
# Render using Tilt
content = ::Middleman::Util.instrument 'render.tilt', path: path do

View file

@ -20,29 +20,45 @@ module Middleman
# thus making it impossible to pass our Middleman instance
# in. So we have to resort to heavy hackery :(
class HamlTemplate < ::Tilt::HamlTemplate
def initialize(*args, &block)
super
@context = @options[:context] if @options.key?(:context)
end
def prepare
end
def evaluate(scope, locals, &block)
::Middleman::Renderers::Haml.last_haml_scope = scope
options = @options.merge(filename: eval_file, line: line)
options = @options.merge(filename: eval_file, line: line, context: @context || scope)
@engine = ::Haml::Engine.new(data, options)
output = @engine.render(scope, locals, &block)
::Middleman::Renderers::Haml.last_haml_scope = nil
output
end
end
# Haml Renderer
class Haml < ::Middleman::Extension
cattr_accessor :last_haml_scope
def initialize(app, options={}, &block)
super
::Haml::Options.defaults[:context] = nil
::Haml::Options.send :attr_accessor, :context
[::Haml::Filters::Sass, ::Haml::Filters::Scss, ::Haml::Filters::Markdown].each do |f|
f.class_exec do
def self.render_with_options(text, compiler_options)
modified_options = options.dup
modified_options[:context] = compiler_options[:context]
text = template_class.new(nil, 1, modified_options) {text}.render
super(text, compiler_options)
end
end
end
::Tilt.prefer(::Middleman::Renderers::HamlTemplate, :haml)
# Add haml helpers to context

View file

@ -4,10 +4,16 @@ module Middleman
module Renderers
# Our own Kramdown Tilt template that simply uses our custom renderer.
class KramdownTemplate < ::Tilt::KramdownTemplate
def evaluate(scope, *)
@output ||= begin
MiddlemanKramdownHTML.scope = (defined?(::Middleman::Renderers::Haml) && ::Middleman::Renderers::Haml.last_haml_scope) ? ::Middleman::Renderers::Haml.last_haml_scope : scope
def initialize(*args, &block)
super
@context = @options[:context] if @options.key?(:context)
end
def evaluate(context, *)
MiddlemanKramdownHTML.scope = @context || context
@output ||= begin
output, warnings = MiddlemanKramdownHTML.convert(@engine.root, @engine.options)
@engine.warnings.concat(warnings)
output
@ -38,6 +44,7 @@ module Middleman
attr = el.attr.dup
link = attr.delete('href')
scope.link_to(content, link, attr)
end
end

View file

@ -10,6 +10,12 @@ module Middleman
escape_html: :filter_html
}
def initialize(*args, &block)
super
@context = @options[:context] if @options.key?(:context)
end
# Overwrite built-in Tilt version.
# Don't overload :renderer option with smartypants
# Support renderer-level options
@ -40,11 +46,7 @@ module Middleman
def evaluate(scope, _)
@output ||= begin
if defined?(::Middleman::Renderers::Haml)
MiddlemanRedcarpetHTML.scope = ::Middleman::Renderers::Haml.last_haml_scope || scope
else
MiddlemanRedcarpetHTML.scope = scope
end
MiddlemanRedcarpetHTML.scope = @context || scope
@engine.render(data)
end

View file

@ -94,11 +94,7 @@ module Middleman
# Change Sass path, for url functions, to the build folder if we're building
# @return [Hash]
def sass_options
ctx = if defined?(::Middleman::Renderers::Haml)
::Middleman::Renderers::Haml.last_haml_scope || @context
else
@context
end
ctx = @context
more_opts = {
load_paths: ctx.config[:sass_assets_paths],

View file

@ -7,9 +7,24 @@ module SafeTemplate
end
end
class Slim::Template
class ::Slim::Template
include SafeTemplate
def initialize(file, line, opts, &block)
if opts.key?(:context)
context_hack = {
context: opts[:context]
}
::Slim::Embedded::SassEngine.disable_option_validator!
%w(sass scss markdown).each do |engine|
::Slim::Embedded.options[engine.to_sym] = context_hack
end
end
super
end
def precompiled_preamble(locals)
"__in_slim_template = true\n" << super
end
@ -32,17 +47,6 @@ module Middleman
disable_escape: true
)
end
def after_configuration
context_hack = {
context: app.template_context_class.new(app)
}
::Slim::Embedded::SassEngine.disable_option_validator!
%w(sass scss markdown).each do |engine|
::Slim::Embedded.options[engine.to_sym] = context_hack
end
end
end
end
end