From 1efe6a27c55f92f5a1ebdf5c7889490c65bbf24a Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Tue, 16 Jun 2015 16:47:42 -0700 Subject: [PATCH] Add tests for Slim inline filters. Refactor similar feature in Haml filters. Closes #1542 --- .../markdown_kramdown_in_slim.feature | 42 ++++++++++++++++++ .../fixtures/markdown-in-slim-app/config.rb | 0 .../source/images/blank.gif | Bin 0 -> 43 bytes .../source/link_target.html.markdown | 4 ++ .../lib/middleman-core/file_renderer.rb | 8 ++-- .../lib/middleman-core/renderers/haml.rb | 28 +++++++++--- .../lib/middleman-core/renderers/kramdown.rb | 13 ++++-- .../lib/middleman-core/renderers/redcarpet.rb | 12 ++--- .../lib/middleman-core/renderers/sass.rb | 6 +-- .../lib/middleman-core/renderers/slim.rb | 28 +++++++----- 10 files changed, 107 insertions(+), 34 deletions(-) create mode 100644 middleman-core/features/markdown_kramdown_in_slim.feature create mode 100644 middleman-core/fixtures/markdown-in-slim-app/config.rb create mode 100755 middleman-core/fixtures/markdown-in-slim-app/source/images/blank.gif create mode 100755 middleman-core/fixtures/markdown-in-slim-app/source/link_target.html.markdown diff --git a/middleman-core/features/markdown_kramdown_in_slim.feature b/middleman-core/features/markdown_kramdown_in_slim.feature new file mode 100644 index 00000000..9e58a461 --- /dev/null +++ b/middleman-core/features/markdown_kramdown_in_slim.feature @@ -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" + Then I should see "

paragraph

" + + + 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"' diff --git a/middleman-core/fixtures/markdown-in-slim-app/config.rb b/middleman-core/fixtures/markdown-in-slim-app/config.rb new file mode 100644 index 00000000..e69de29b diff --git a/middleman-core/fixtures/markdown-in-slim-app/source/images/blank.gif b/middleman-core/fixtures/markdown-in-slim-app/source/images/blank.gif new file mode 100755 index 0000000000000000000000000000000000000000..2498f1aac58dab923f0fd99b1c8ee6b8c53c7158 GIT binary patch literal 43 scmZ?wbhEHbWMp7uXkcKtd-pB_1B2pE79h#MpaUX6G7L;iE{qJ;0KEqWk^lez literal 0 HcmV?d00001 diff --git a/middleman-core/fixtures/markdown-in-slim-app/source/link_target.html.markdown b/middleman-core/fixtures/markdown-in-slim-app/source/link_target.html.markdown new file mode 100755 index 00000000..4f970c47 --- /dev/null +++ b/middleman-core/fixtures/markdown-in-slim-app/source/link_target.html.markdown @@ -0,0 +1,4 @@ +--- +layout: false +--- +Hello World \ No newline at end of file diff --git a/middleman-core/lib/middleman-core/file_renderer.rb b/middleman-core/lib/middleman-core/file_renderer.rb index f9087004..796587d2 100644 --- a/middleman-core/lib/middleman-core/file_renderer.rb +++ b/middleman-core/lib/middleman-core/file_renderer.rb @@ -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 diff --git a/middleman-core/lib/middleman-core/renderers/haml.rb b/middleman-core/lib/middleman-core/renderers/haml.rb index 8f98ddc6..7e2b6fbb 100644 --- a/middleman-core/lib/middleman-core/renderers/haml.rb +++ b/middleman-core/lib/middleman-core/renderers/haml.rb @@ -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 diff --git a/middleman-core/lib/middleman-core/renderers/kramdown.rb b/middleman-core/lib/middleman-core/renderers/kramdown.rb index c93d254d..86317560 100644 --- a/middleman-core/lib/middleman-core/renderers/kramdown.rb +++ b/middleman-core/lib/middleman-core/renderers/kramdown.rb @@ -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 diff --git a/middleman-core/lib/middleman-core/renderers/redcarpet.rb b/middleman-core/lib/middleman-core/renderers/redcarpet.rb index 0e141969..545a3c87 100644 --- a/middleman-core/lib/middleman-core/renderers/redcarpet.rb +++ b/middleman-core/lib/middleman-core/renderers/redcarpet.rb @@ -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 diff --git a/middleman-core/lib/middleman-core/renderers/sass.rb b/middleman-core/lib/middleman-core/renderers/sass.rb index f796992d..ff449f4b 100644 --- a/middleman-core/lib/middleman-core/renderers/sass.rb +++ b/middleman-core/lib/middleman-core/renderers/sass.rb @@ -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], diff --git a/middleman-core/lib/middleman-core/renderers/slim.rb b/middleman-core/lib/middleman-core/renderers/slim.rb index 21277fe4..16640f4a 100644 --- a/middleman-core/lib/middleman-core/renderers/slim.rb +++ b/middleman-core/lib/middleman-core/renderers/slim.rb @@ -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