From 1265fdb98ad8c4e0235d3b506a15d892f07bb59e Mon Sep 17 00:00:00 2001 From: Takashi Kokubun Date: Mon, 12 Oct 2015 08:29:35 +0900 Subject: [PATCH] Compile plain filter --- lib/hamlit/compiler.rb | 8 ++++++++ lib/hamlit/filter_compiler.rb | 32 +++++++++++++++++++++++++++++ lib/hamlit/filter_compiler/plain.rb | 9 ++++++++ test/haml-spec/tests.yml | 18 ++++++++-------- 4 files changed, 58 insertions(+), 9 deletions(-) create mode 100644 lib/hamlit/filter_compiler.rb create mode 100644 lib/hamlit/filter_compiler/plain.rb diff --git a/lib/hamlit/compiler.rb b/lib/hamlit/compiler.rb index c9ff0126..c9f22fa9 100644 --- a/lib/hamlit/compiler.rb +++ b/lib/hamlit/compiler.rb @@ -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]} "]] diff --git a/lib/hamlit/filter_compiler.rb b/lib/hamlit/filter_compiler.rb new file mode 100644 index 00000000..b8a081a4 --- /dev/null +++ b/lib/hamlit/filter_compiler.rb @@ -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 diff --git a/lib/hamlit/filter_compiler/plain.rb b/lib/hamlit/filter_compiler/plain.rb new file mode 100644 index 00000000..27cbb6e9 --- /dev/null +++ b/lib/hamlit/filter_compiler/plain.rb @@ -0,0 +1,9 @@ +module Hamlit + class FilterCompiler + class Plain + def compile(node) + [:static, node.value[:text].rstrip + "\n"] + end + end + end +end diff --git a/test/haml-spec/tests.yml b/test/haml-spec/tests.yml index 20020fef..bfb307b7 100644 --- a/test/haml-spec/tests.yml +++ b/test/haml-spec/tests.yml @@ -373,15 +373,15 @@ markup comments: # html: |- # hello #

-# content in a 'plain' filter: -# haml: |- -# :plain -# hello -# -# %p -# html: |- -# hello -#

+ content in a 'plain' filter: + haml: |- + :plain + hello + + %p + html: |- + hello +

# content in a 'css' filter (XHTML): # haml: |- # :css