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

Compile plain filter

This commit is contained in:
Takashi Kokubun 2015-10-12 08:29:35 +09:00
parent 4fff10c075
commit 1265fdb98a
4 changed files with 58 additions and 9 deletions

View file

@ -1,4 +1,5 @@
require 'hamlit/doctype_compiler'
require 'hamlit/filter_compiler'
require 'hamlit/tag_compiler'
require 'hamlit/whitespace_handler'
@ -6,6 +7,7 @@ module Hamlit
class Compiler
def initialize(options = {})
@doctype_compiler = DoctypeCompiler.new(options)
@filter_compiler = FilterCompiler.new
@tag_compiler = TagCompiler.new(options)
@whitespace_handler = WhitespaceHandler.new
end
@ -24,6 +26,8 @@ module Hamlit
compile_comment(node)
when :doctype
compile_doctype(node)
when :filter
compile_filter(node)
when :tag
compile_tag(node)
when :plain
@ -41,6 +45,10 @@ module Hamlit
@doctype_compiler.compile(node)
end
def compile_filter(node)
@filter_compiler.compile(node)
end
def compile_comment(node)
if node.children.empty?
return [:html, :comment, [:static, " #{node.value[:text]} "]]

View file

@ -0,0 +1,32 @@
require 'hamlit/filter_compiler/plain'
module Hamlit
class FilterCompiler
@registered = {}
class << self
attr_reader :registered
private
def register(name, compiler)
registered[name] = compiler.new
end
end
register :plain, Plain
def compile(node)
find_compiler(node.value[:name]).compile(node)
end
private
def find_compiler(name)
compiler = FilterCompiler.registered[name.to_sym]
raise "FilterCompiler for '#{name}' was not found" unless compiler
compiler
end
end
end

View file

@ -0,0 +1,9 @@
module Hamlit
class FilterCompiler
class Plain
def compile(node)
[:static, node.value[:text].rstrip + "\n"]
end
end
end
end

View file

@ -373,15 +373,15 @@ markup comments:
# html: |-
# hello&#x000A;
# <p></p>
# content in a 'plain' filter:
# haml: |-
# :plain
# hello
#
# %p
# html: |-
# hello
# <p></p>
content in a 'plain' filter:
haml: |-
:plain
hello
%p
html: |-
hello
<p></p>
# content in a 'css' filter (XHTML):
# haml: |-
# :css