Skip to content

Commit

Permalink
Improve Haml 6 support (#2590)
Browse files Browse the repository at this point in the history
  • Loading branch information
k0kubun committed Feb 6, 2023
1 parent a44adc4 commit d1737ac
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 15 deletions.
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
PATH
remote: middleman-cli
specs:
middleman-cli (4.4.2)
middleman-cli (4.4.3)
thor (>= 0.17.0, < 2.0)

PATH
remote: middleman-core
specs:
middleman-core (4.4.2)
middleman-core (4.4.3)
activesupport (>= 6.1, < 7.1)
addressable (~> 2.4)
backports (~> 3.6)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
= content_for :from_template do
= "I am the yielded content haml <s>with html tags</s>"
- content_for :from_template do
!= "I am the yielded content haml <s>with html tags</s>"

%p I am in the template
49 changes: 39 additions & 10 deletions middleman-core/lib/middleman-core/renderers/haml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,18 @@ def initialize(*args, &block)
def prepare; end

def evaluate(scope, locals, &block)
options = {}.merge!(@options).merge!(filename: eval_file, line: line, context: @context || scope)
options = {}.merge!(@options).merge!(context: @context || scope)
if options.include?(:outvar)
options[:buffer] = options.delete(:outvar)
options[:save_buffer] = true
end
@engine = ::Haml::Engine.new(data, options)
if Object.const_defined?('::Haml::Template') # haml 6+
@engine = ::Haml::Template.new(eval_file, line, options) { data }
else
options[:filename] = eval_file
options[:line] = line
@engine = ::Haml::Engine.new(data, options)
end
output = @engine.render(scope, locals, &block)

output
Expand All @@ -46,21 +52,44 @@ class Haml < ::Middleman::Extension
def initialize(app, options={}, &block)
super

::Haml::Options.defaults[:context] = nil
::Haml::Options.send :attr_accessor, :context
if Object.const_defined?('::Haml::Options') # Haml 5 and older
::Haml::Options.defaults[:context] = nil
::Haml::Options.send :attr_accessor, :context
else # Haml 6+
::Haml::Engine.define_options context: nil
end
if defined?(::Haml::TempleEngine)
::Haml::TempleEngine.define_options context: nil
end

# rubocop:disable NestedMethodDefinition
[::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)
if respond_to?(:template_class) # Haml 5 and older
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
else # Haml 6+
def initialize(options = {})
super
@context = options[:context]
end

def compile_with_tilt(node, name, indent_width: 0)
options = { context: @context }
source = node.value[:text]
result = ::Tilt["t.#{name}"].new(nil, 1, options) { source }.render

temple = [:multi, [:static, result.gsub(/^/, ' ' * indent_width)]]
source.lines.size.times do
temple << [:newline]
end
temple
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion middleman/middleman.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Gem::Specification.new do |s|

s.add_dependency('middleman-core', Middleman::VERSION)
s.add_dependency('middleman-cli', Middleman::VERSION)
s.add_dependency('haml', ['>= 4.0.5', '< 6.0'])
s.add_dependency('haml', ['>= 4.0.5'])
s.add_dependency('coffee-script', ['~> 2.2'])
s.add_dependency('kramdown', ['>= 2.3.0'])
end

0 comments on commit d1737ac

Please sign in to comment.