From bc9f2579bff1b8dd2ebb2f6d34931c3f23f75cd2 Mon Sep 17 00:00:00 2001 From: Takashi Kokubun Date: Sun, 11 Oct 2015 18:50:01 +0900 Subject: [PATCH] Refactor TagCompiler --- lib/hamlit/compiler.rb | 4 ++-- lib/hamlit/tag_compiler.rb | 22 +++++++++++++++++++--- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/lib/hamlit/compiler.rb b/lib/hamlit/compiler.rb index a86fb27c..3677ca7c 100644 --- a/lib/hamlit/compiler.rb +++ b/lib/hamlit/compiler.rb @@ -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) diff --git a/lib/hamlit/tag_compiler.rb b/lib/hamlit/tag_compiler.rb index 51b26c0a..48d77196 100644 --- a/lib/hamlit/tag_compiler.rb +++ b/lib/hamlit/tag_compiler.rb @@ -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