# frozen_string_literal: true require 'temple' require 'haml/escapable' require 'haml/generator' module Haml class TempleEngine < Temple::Engine define_options( attr_wrapper: "'", autoclose: %w(area base basefont br col command embed frame hr img input isindex keygen link menuitem meta param source track wbr), encoding: nil, escape_attrs: true, escape_html: false, escape_filter_interpolations: nil, filename: '(haml)', format: :html5, hyphenate_data_attrs: true, line: 1, mime_type: 'text/html', preserve: %w(textarea pre code), remove_whitespace: false, suppress_eval: false, cdata: false, parser_class: ::Haml::Parser, compiler_class: ::Haml::Compiler, trace: false, filters: {}, ) use :Parser, -> { options[:parser_class] } use :Compiler, -> { options[:compiler_class] } use Escapable filter :ControlFlow filter :MultiFlattener filter :StaticMerger use Generator def compile(template) initialize_encoding(template, options[:encoding]) @precompiled = call(template) end # The source code that is evaluated to produce the Haml document. # # This is automatically converted to the correct encoding # (see {file:REFERENCE.md#encodings the `:encoding` option}). # # @return [String] def precompiled encoding = Encoding.find(@encoding || '') return @precompiled.dup.force_encoding(encoding) if encoding == Encoding::ASCII_8BIT return @precompiled.encode(encoding) end def precompiled_with_return_value "#{precompiled};#{precompiled_method_return_value}".dup end # The source code that is evaluated to produce the Haml document. # # This is automatically converted to the correct encoding # (see {file:REFERENCE.md#encodings the `:encoding` option}). # # @return [String] def precompiled_with_ambles(local_names, after_preamble: '', before_postamble: '') preamble = <