From 72bc1611f7df76397b58bad8ed0898ec449ec5fe Mon Sep 17 00:00:00 2001 From: Takashi Kokubun Date: Tue, 23 Mar 2021 21:13:18 -0700 Subject: [PATCH] Support annotate_rendered_view_with_filenames Fix #180 --- lib/hamlit/ambles.rb | 20 ++++++++++++++++++++ lib/hamlit/engine.rb | 2 ++ lib/hamlit/rails_template.rb | 5 +++++ 3 files changed, 27 insertions(+) create mode 100644 lib/hamlit/ambles.rb diff --git a/lib/hamlit/ambles.rb b/lib/hamlit/ambles.rb new file mode 100644 index 00000000..3036a822 --- /dev/null +++ b/lib/hamlit/ambles.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true +module Hamlit + class Ambles < Temple::Filter + define_options :preamble, :postamble + + def initialize(*) + super + @preamble = options[:preamble] + @postamble = options[:postamble] + end + + def call(ast) + ret = [:multi] + ret << [:static, @preamble] if @preamble + ret << ast + ret << [:static, @postamble] if @postamble + ret + end + end +end diff --git a/lib/hamlit/engine.rb b/lib/hamlit/engine.rb index 069f96b5..e901c9f8 100644 --- a/lib/hamlit/engine.rb +++ b/lib/hamlit/engine.rb @@ -6,6 +6,7 @@ require 'hamlit/escapable' require 'hamlit/force_escapable' require 'hamlit/dynamic_merger' +require 'hamlit/ambles' module Hamlit class Engine < Temple::Engine @@ -33,6 +34,7 @@ class Engine < Temple::Engine filter :MultiFlattener filter :StaticMerger use DynamicMerger + use Ambles use :Generator, -> { options[:generator] } end end diff --git a/lib/hamlit/rails_template.rb b/lib/hamlit/rails_template.rb index e1723c44..2d1a3843 100644 --- a/lib/hamlit/rails_template.rb +++ b/lib/hamlit/rails_template.rb @@ -33,6 +33,11 @@ def call(template, source = nil) options = options.merge(format: :xhtml) end + if ActionView::Base.try(:annotate_rendered_view_with_filenames) && template.format == :html + options[:preamble] = "\n" + options[:postamble] = "\n" + end + Engine.new(options).call(source) end