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

Refactor TagCompiler

This commit is contained in:
Takashi Kokubun 2015-10-11 18:50:01 +09:00
parent 8eea2adf38
commit bc9f2579bf
2 changed files with 21 additions and 5 deletions

View file

@ -3,7 +3,7 @@ require 'hamlit/tag_compiler'
module Hamlit
class Compiler
def initialize(options = {})
@options = options
@tag_compiler = TagCompiler.new(options[:attr_quote])
end
def call(ast)
@ -41,7 +41,7 @@ module Hamlit
end
def compile_tag(node)
TagCompiler.compile(node) { |n| compile_children(n) }
@tag_compiler.compile(node) { |n| compile_children(n) }
end
def compile_plain(node)

View file

@ -1,14 +1,30 @@
module Hamlit
class TagCompiler
def self.compile(node)
def initialize(attr_quote)
@quote = attr_quote.inspect.freeze
end
def compile(node, &block)
attrs = compile_attributes(node)
contents = compile_contents(node, &block)
[:html, :tag, node.value[:name], attrs, contents]
end
private
def compile_attributes(node)
attrs = [:html, :attrs]
node.value[:attributes_hashes].each do |attribute_hash|
attrs << [:dynamic, "::Hamlit::AttributeBuilder.build(\"'\", #{attribute_hash})"]
attrs << [:dynamic, "::Hamlit::AttributeBuilder.build(#{@quote}, #{attribute_hash})"]
end
node.value[:attributes].each do |name, value|
attrs << [:html, :attr, name, [:static, value]]
end
[:html, :tag, node.value[:name], attrs, yield(node)]
attrs
end
def compile_contents(node, &block)
yield(node)
end
end
end