From 743ba540c0e7d80efa277a7314727d917f10da9e Mon Sep 17 00:00:00 2001 From: Takashi Kokubun Date: Sat, 5 Dec 2015 23:28:55 +0900 Subject: [PATCH] Convert Haml::Parser to temple filter --- haml.gemspec | 2 +- lib/haml/parser.rb | 10 ++++++---- lib/haml/temple_engine.rb | 12 +----------- test/parser_test.rb | 4 ++-- 4 files changed, 10 insertions(+), 18 deletions(-) diff --git a/haml.gemspec b/haml.gemspec index 23f3e084..a4a7a959 100644 --- a/haml.gemspec +++ b/haml.gemspec @@ -19,7 +19,7 @@ Gem::Specification.new do |spec| spec.required_ruby_version = '>= 2.0.0' - spec.add_dependency 'temple' + spec.add_dependency 'temple', '>= 0.7.6' spec.add_dependency 'tilt' spec.add_development_dependency 'rails', '>= 4.0.0' diff --git a/lib/haml/parser.rb b/lib/haml/parser.rb index c6dfdcc9..597d588c 100644 --- a/lib/haml/parser.rb +++ b/lib/haml/parser.rb @@ -88,14 +88,18 @@ module Haml ID_KEY = 'id'.freeze CLASS_KEY = 'class'.freeze - def initialize(template, options) - @options = options + def initialize(options) + @options = options + @options = Options.new(options) unless options.is_a?(Options) + # Record the indent levels of "if" statements to validate the subsequent # elsif and else statements are indented at the appropriate level. @script_level_stack = [] @template_index = 0 @template_tabs = 0 + end + def call(template) match = template.rstrip.scan(/(([ \t]+)?(.*?))(?:\Z|\r\n|\r|\n)/m) # discard the last match which is always blank match.pop @@ -104,9 +108,7 @@ module Haml end # Append special end-of-document marker @template << Line.new(nil, '-#', '-#', @template.size, self, true) - end - def parse @root = @parent = ParseNode.new(:root) @flat = false @filter_buffer = nil diff --git a/lib/haml/temple_engine.rb b/lib/haml/temple_engine.rb index 19eae3bd..8ec01927 100644 --- a/lib/haml/temple_engine.rb +++ b/lib/haml/temple_engine.rb @@ -1,16 +1,6 @@ require 'temple' module Haml - class ParserFilter - def initialize(options = {}) - @options = Options.new(options) - end - - def call(template) - @options.parser_class.new(template, @options).parse - end - end - class CompilerFilter def initialize(options = {}) @options = Options.new(options) @@ -47,7 +37,7 @@ module Haml :trace => false ) - use ParserFilter + use :Parser, -> { options[:parser_class] } use CompilerFilter def compile(template) diff --git a/test/parser_test.rb b/test/parser_test.rb index bf3c38d4..25320812 100644 --- a/test/parser_test.rb +++ b/test/parser_test.rb @@ -165,8 +165,8 @@ module Haml def parse(haml, options = nil) options ||= Options.new - parser = Parser.new(haml, options) - parser.parse + parser = Parser.new(options) + parser.call(haml) end end end