lib/faml/script_parser.rb in faml-0.2.6 vs lib/faml/script_parser.rb in faml-0.2.7

- old
+ new

@@ -23,62 +23,84 @@ private def parse_script(text) if text[1] == '=' - Ast::Text.new(text[2 .. -1].strip) + create_node(Ast::Text) { |t| t.text = text[2 .. -1].strip } else - script = text[1 .. -1].lstrip - if script.empty? + node = create_node(Ast::Script) + node.script = text[1 .. -1].lstrip + if node.script.empty? syntax_error!('No Ruby code to evaluate') end - script += RubyMultiline.read(@line_parser, script) - Ast::Script.new([], script) + node.script += RubyMultiline.read(@line_parser, node.script) + node end end def parse_sanitized(text) case when text.start_with?('&==') - Ast::Text.new(text[3 .. -1].lstrip) + create_node(Ast::Text) { |t| t.text = text[3 .. -1].lstrip } when text[1] == '=' || text[1] == '~' - script = text[2 .. -1].lstrip - if script.empty? + node = create_node(Ast::Script) + node.script = text[2 .. -1].lstrip + if node.script.empty? syntax_error!('No Ruby code to evaluate') end - script += RubyMultiline.read(@line_parser, script) - Ast::Script.new([], script, true, text[1] == '~') + node.script += RubyMultiline.read(@line_parser, node.script) + node.preserve = text[1] == '~' + node else - Ast::Text.new(text[1 .. -1].strip) + create_node(Ast::Text) { |t| t.text = text[1 .. -1].strip } end end def parse_unescape(text) case when text.start_with?('!==') - Ast::Text.new(text[3 .. -1].lstrip, false) + create_node(Ast::Text) do |t| + t.text = text[3 .. -1].lstrip + t.escape_html = false + end when text[1] == '=' || text[1] == '~' - script = text[2 .. -1].lstrip - if script.empty? + node = create_node(Ast::Script) + node.escape_html = false + node.script = text[2 .. -1].lstrip + if node.script.empty? syntax_error!('No Ruby code to evaluate') end - script += RubyMultiline.read(@line_parser, script) - Ast::Script.new([], script, false, text[1] == '~') + node.script += RubyMultiline.read(@line_parser, node.script) + node.preserve = text[1] == '~' + node else - Ast::Text.new(text[1 .. -1].lstrip, false) + create_node(Ast::Text) do |t| + t.text = text[1 .. -1].lstrip + t.escape_html = false + end end end def parse_text(text) text = text.lstrip if text.empty? nil else - Ast::Text.new(text) + create_node(Ast::Text) { |t| t.text = text } end end def syntax_error!(message) raise SyntaxError.new(message, @line_parser.lineno) + end + + def create_node(klass, &block) + klass.new.tap do |node| + node.filename = @line_parser.filename + node.lineno = @line_parser.lineno + if block + block.call(node) + end + end end end end