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

- old
+ new

@@ -1,20 +1,21 @@ require 'strscan' +require 'faml/error' require 'faml/parser_utils' module Faml class TextCompiler - class InvalidInterpolation < StandardError + class InvalidInterpolation < Error end def initialize(escape_html: true) @escape_html = escape_html end - def compile(text, escape_html: @escape_html) + def compile(text, lineno, escape_html: @escape_html) if self.class.contains_interpolation?(text) - compile_interpolation(text, escape_html: escape_html) + compile_interpolation(text, lineno, escape_html: escape_html) else [:static, text] end end @@ -24,22 +25,22 @@ INTERPOLATION_BEGIN === text end private - def compile_interpolation(text, escape_html: @escape_html) + def compile_interpolation(text, lineno, escape_html: @escape_html) s = StringScanner.new(text) temple = [:multi] pos = s.pos while s.scan_until(INTERPOLATION_BEGIN) escapes = s[1].size pre = s.string.byteslice(pos ... (s.pos - s.matched.size)) temple << [:static, pre] << [:static, "\\" * (escapes/2)] if escapes % 2 == 0 # perform interpolation if s[2] == '#{' - temple << [:escape, escape_html, [:dynamic, find_close_brace(s)]] + temple << [:escape, escape_html, [:dynamic, find_close_brace(s, lineno)]] else var = s[2][-1] s.scan(/\w+/) var << s.matched temple << [:escape, escape_html, [:dynamic, var]] @@ -54,14 +55,14 @@ temple end INTERPOLATION_BRACE = /[\{\}]/o - def find_close_brace(scanner) + def find_close_brace(scanner, lineno) pos = scanner.pos depth = ParserUtils.balance(scanner, '{', '}') if depth != 0 - raise InvalidInterpolation.new(scanner.string) + raise InvalidInterpolation.new(scanner.string, lineno) else scanner.string.byteslice(pos ... (scanner.pos-1)) end end end