mirror of
https://github.com/middleman/middleman.git
synced 2022-11-09 12:20:27 -05:00
Add tests for Slim inline filters. Refactor similar feature in Haml filters. Closes #1542
This commit is contained in:
parent
7383f67874
commit
1efe6a27c5
10 changed files with 107 additions and 34 deletions
42
middleman-core/features/markdown_kramdown_in_slim.feature
Normal file
42
middleman-core/features/markdown_kramdown_in_slim.feature
Normal 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"'
|
0
middleman-core/fixtures/markdown-in-slim-app/config.rb
Normal file
0
middleman-core/fixtures/markdown-in-slim-app/config.rb
Normal file
BIN
middleman-core/fixtures/markdown-in-slim-app/source/images/blank.gif
Executable file
BIN
middleman-core/fixtures/markdown-in-slim-app/source/images/blank.gif
Executable file
Binary file not shown.
After Width: | Height: | Size: 43 B |
|
@ -0,0 +1,4 @@
|
||||||
|
---
|
||||||
|
layout: false
|
||||||
|
---
|
||||||
|
Hello World
|
|
@ -53,6 +53,7 @@ module Middleman
|
||||||
extension = File.extname(path)
|
extension = File.extname(path)
|
||||||
options = opts.merge(options_for_ext(extension))
|
options = opts.merge(options_for_ext(extension))
|
||||||
options[:outvar] ||= '@_out_buf'
|
options[:outvar] ||= '@_out_buf'
|
||||||
|
options[:context] = context
|
||||||
options.delete(:layout)
|
options.delete(:layout)
|
||||||
|
|
||||||
# Overwrite with frontmatter options
|
# Overwrite with frontmatter options
|
||||||
|
@ -66,9 +67,10 @@ module Middleman
|
||||||
end
|
end
|
||||||
|
|
||||||
# Read compiled template from disk or cache
|
# Read compiled template from disk or cache
|
||||||
template = cache.fetch(:compiled_template, extension, options, body) do
|
template = ::Tilt.new(path, 1, options) { body }
|
||||||
::Tilt.new(path, 1, options) { body }
|
# template = cache.fetch(:compiled_template, extension, options, body) do
|
||||||
end
|
# ::Tilt.new(path, 1, options) { body }
|
||||||
|
# end
|
||||||
|
|
||||||
# Render using Tilt
|
# Render using Tilt
|
||||||
content = ::Middleman::Util.instrument 'render.tilt', path: path do
|
content = ::Middleman::Util.instrument 'render.tilt', path: path do
|
||||||
|
|
|
@ -20,29 +20,45 @@ module Middleman
|
||||||
# thus making it impossible to pass our Middleman instance
|
# thus making it impossible to pass our Middleman instance
|
||||||
# in. So we have to resort to heavy hackery :(
|
# in. So we have to resort to heavy hackery :(
|
||||||
class HamlTemplate < ::Tilt::HamlTemplate
|
class HamlTemplate < ::Tilt::HamlTemplate
|
||||||
|
def initialize(*args, &block)
|
||||||
|
super
|
||||||
|
|
||||||
|
@context = @options[:context] if @options.key?(:context)
|
||||||
|
end
|
||||||
|
|
||||||
def prepare
|
def prepare
|
||||||
end
|
end
|
||||||
|
|
||||||
def evaluate(scope, locals, &block)
|
def evaluate(scope, locals, &block)
|
||||||
::Middleman::Renderers::Haml.last_haml_scope = scope
|
options = @options.merge(filename: eval_file, line: line, context: @context || scope)
|
||||||
|
|
||||||
options = @options.merge(filename: eval_file, line: line)
|
|
||||||
@engine = ::Haml::Engine.new(data, options)
|
@engine = ::Haml::Engine.new(data, options)
|
||||||
output = @engine.render(scope, locals, &block)
|
output = @engine.render(scope, locals, &block)
|
||||||
|
|
||||||
::Middleman::Renderers::Haml.last_haml_scope = nil
|
|
||||||
|
|
||||||
output
|
output
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Haml Renderer
|
# Haml Renderer
|
||||||
class Haml < ::Middleman::Extension
|
class Haml < ::Middleman::Extension
|
||||||
cattr_accessor :last_haml_scope
|
|
||||||
|
|
||||||
def initialize(app, options={}, &block)
|
def initialize(app, options={}, &block)
|
||||||
super
|
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)
|
::Tilt.prefer(::Middleman::Renderers::HamlTemplate, :haml)
|
||||||
|
|
||||||
# Add haml helpers to context
|
# Add haml helpers to context
|
||||||
|
|
|
@ -4,10 +4,16 @@ module Middleman
|
||||||
module Renderers
|
module Renderers
|
||||||
# Our own Kramdown Tilt template that simply uses our custom renderer.
|
# Our own Kramdown Tilt template that simply uses our custom renderer.
|
||||||
class KramdownTemplate < ::Tilt::KramdownTemplate
|
class KramdownTemplate < ::Tilt::KramdownTemplate
|
||||||
def evaluate(scope, *)
|
def initialize(*args, &block)
|
||||||
@output ||= begin
|
super
|
||||||
MiddlemanKramdownHTML.scope = (defined?(::Middleman::Renderers::Haml) && ::Middleman::Renderers::Haml.last_haml_scope) ? ::Middleman::Renderers::Haml.last_haml_scope : scope
|
|
||||||
|
|
||||||
|
@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)
|
output, warnings = MiddlemanKramdownHTML.convert(@engine.root, @engine.options)
|
||||||
@engine.warnings.concat(warnings)
|
@engine.warnings.concat(warnings)
|
||||||
output
|
output
|
||||||
|
@ -38,6 +44,7 @@ module Middleman
|
||||||
|
|
||||||
attr = el.attr.dup
|
attr = el.attr.dup
|
||||||
link = attr.delete('href')
|
link = attr.delete('href')
|
||||||
|
|
||||||
scope.link_to(content, link, attr)
|
scope.link_to(content, link, attr)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -10,6 +10,12 @@ module Middleman
|
||||||
escape_html: :filter_html
|
escape_html: :filter_html
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def initialize(*args, &block)
|
||||||
|
super
|
||||||
|
|
||||||
|
@context = @options[:context] if @options.key?(:context)
|
||||||
|
end
|
||||||
|
|
||||||
# Overwrite built-in Tilt version.
|
# Overwrite built-in Tilt version.
|
||||||
# Don't overload :renderer option with smartypants
|
# Don't overload :renderer option with smartypants
|
||||||
# Support renderer-level options
|
# Support renderer-level options
|
||||||
|
@ -40,11 +46,7 @@ module Middleman
|
||||||
|
|
||||||
def evaluate(scope, _)
|
def evaluate(scope, _)
|
||||||
@output ||= begin
|
@output ||= begin
|
||||||
if defined?(::Middleman::Renderers::Haml)
|
MiddlemanRedcarpetHTML.scope = @context || scope
|
||||||
MiddlemanRedcarpetHTML.scope = ::Middleman::Renderers::Haml.last_haml_scope || scope
|
|
||||||
else
|
|
||||||
MiddlemanRedcarpetHTML.scope = scope
|
|
||||||
end
|
|
||||||
|
|
||||||
@engine.render(data)
|
@engine.render(data)
|
||||||
end
|
end
|
||||||
|
|
|
@ -94,11 +94,7 @@ module Middleman
|
||||||
# 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
|
||||||
ctx = if defined?(::Middleman::Renderers::Haml)
|
ctx = @context
|
||||||
::Middleman::Renderers::Haml.last_haml_scope || @context
|
|
||||||
else
|
|
||||||
@context
|
|
||||||
end
|
|
||||||
|
|
||||||
more_opts = {
|
more_opts = {
|
||||||
load_paths: ctx.config[:sass_assets_paths],
|
load_paths: ctx.config[:sass_assets_paths],
|
||||||
|
|
|
@ -7,9 +7,24 @@ module SafeTemplate
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Slim::Template
|
class ::Slim::Template
|
||||||
include SafeTemplate
|
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)
|
def precompiled_preamble(locals)
|
||||||
"__in_slim_template = true\n" << super
|
"__in_slim_template = true\n" << super
|
||||||
end
|
end
|
||||||
|
@ -32,17 +47,6 @@ module Middleman
|
||||||
disable_escape: true
|
disable_escape: true
|
||||||
)
|
)
|
||||||
end
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue