1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00
haml--haml/lib/hamlit/engine.rb

72 lines
1.7 KiB
Ruby
Raw Normal View History

2015-10-06 23:14:45 +09:00
require 'temple'
require 'hamlit/parser'
2015-10-24 13:59:15 +09:00
require 'hamlit/compiler'
require 'pretty_hamlit/engine'
2015-10-06 23:14:45 +09:00
module Hamlit
class Engine < Temple::Engine
define_options(
2015-10-25 03:41:04 +09:00
generator: Temple::Generators::ArrayBuffer,
format: :html,
html_type: nil,
attr_quote: "'",
escape_html: true,
escape_attrs: true,
buffer: '_buf',
autoclose: %w(area base basefont br col command embed frame
hr img input isindex keygen link menuitem meta
param source track wbr),
filename: "",
2015-10-06 23:14:45 +09:00
)
use Parser
2015-10-24 13:59:15 +09:00
use Compiler
html :Fast
2015-10-06 23:14:45 +09:00
filter :Escapable
filter :ControlFlow
filter :MultiFlattener
filter :StaticMerger
use :Generator, -> { options[:generator] }
end
2015-10-07 00:18:41 +09:00
class HamlEngine
def initialize(template, options = {})
@template = template
@options = options
end
2015-10-11 17:58:11 +09:00
def precompiled
2015-10-24 13:59:15 +09:00
if @options[:ugly]
Engine.new(temple_options).call(@template)
else
PrettyHamlit::Engine.new(temple_options).call(@template)
end
2015-10-11 17:58:11 +09:00
end
2015-10-07 00:18:41 +09:00
def render(scope = Object.new, locals = {}, &block)
2015-10-11 15:31:13 +09:00
scope = wrap_binding(scope)
set_locals(locals, scope)
2015-10-11 17:58:11 +09:00
eval(precompiled, scope)
2015-10-11 15:31:13 +09:00
end
2015-10-13 20:45:07 +09:00
alias_method :to_html, :render
2015-10-11 15:31:13 +09:00
private
2015-10-11 21:08:57 +09:00
def temple_options
@options.dup.tap do |options|
2015-10-24 13:59:15 +09:00
options.delete(:ugly)
2015-10-11 21:08:57 +09:00
end
end
2015-10-11 15:31:13 +09:00
def wrap_binding(scope)
return scope if scope.is_a?(Binding)
scope.instance_eval { binding }
end
def set_locals(locals, scope)
set_locals = locals.map { |k, v| "#{k} = #{v.inspect}" }.join("\n")
eval(set_locals, scope)
2015-10-07 00:18:41 +09:00
end
end
2015-10-06 23:14:45 +09:00
end