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/compiler.rb

68 lines
1.4 KiB
Ruby
Raw Normal View History

2015-10-11 19:53:34 +09:00
require 'hamlit/doctype_compiler'
2015-10-12 13:55:49 +09:00
require 'hamlit/filters'
2015-10-11 22:51:49 +09:00
require 'hamlit/tag_compiler'
require 'hamlit/whitespace_handler'
2015-10-11 18:18:38 +09:00
2015-10-06 23:14:45 +09:00
module Hamlit
class Compiler
def initialize(options = {})
2015-10-11 22:51:49 +09:00
@doctype_compiler = DoctypeCompiler.new(options)
2015-10-12 13:55:49 +09:00
@filter_compiler = Filters.new(options)
2015-10-11 22:51:49 +09:00
@tag_compiler = TagCompiler.new(options)
@whitespace_handler = WhitespaceHandler.new
2015-10-06 23:14:45 +09:00
end
2015-10-11 11:58:52 +09:00
def call(ast)
compile(ast)
end
private
2015-10-11 11:50:22 +09:00
def compile(node)
2015-10-07 00:33:20 +09:00
case node.type
2015-10-11 11:50:22 +09:00
when :root
compile_children(node)
2015-10-12 00:37:01 +09:00
when :comment
compile_comment(node)
2015-10-11 13:05:32 +09:00
when :doctype
compile_doctype(node)
2015-10-12 08:29:35 +09:00
when :filter
compile_filter(node)
2015-10-07 00:33:20 +09:00
when :tag
2015-10-11 11:58:14 +09:00
compile_tag(node)
2015-10-11 13:05:42 +09:00
when :plain
compile_plain(node)
2015-10-07 00:33:20 +09:00
else
2015-10-11 17:02:44 +09:00
[:multi]
2015-10-07 00:33:20 +09:00
end
2015-10-06 23:14:45 +09:00
end
2015-10-11 11:50:22 +09:00
def compile_children(node)
2015-10-11 22:51:49 +09:00
@whitespace_handler.compile_children(node) { |n| compile(n) }
2015-10-11 11:50:22 +09:00
end
2015-10-11 11:58:14 +09:00
2015-10-11 13:05:32 +09:00
def compile_doctype(node)
2015-10-11 19:53:34 +09:00
@doctype_compiler.compile(node)
2015-10-11 13:05:32 +09:00
end
2015-10-12 08:29:35 +09:00
def compile_filter(node)
@filter_compiler.compile(node)
end
2015-10-12 00:37:01 +09:00
def compile_comment(node)
if node.children.empty?
return [:html, :comment, [:static, " #{node.value[:text]} "]]
end
[:html, :comment, compile_children(node)]
end
2015-10-11 11:58:14 +09:00
def compile_tag(node)
2015-10-11 18:50:01 +09:00
@tag_compiler.compile(node) { |n| compile_children(n) }
2015-10-11 13:05:42 +09:00
end
def compile_plain(node)
[:static, node.value[:text]]
2015-10-11 11:58:14 +09:00
end
2015-10-06 23:14:45 +09:00
end
end