lib/utopia/trenni.rb in utopia-0.9.47 vs lib/utopia/trenni.rb in utopia-0.9.48
- old
+ new
@@ -33,10 +33,12 @@
code = parts.join
end
end
class Scanner < StringScanner
+ TEXT = /([^<#]|<(?!\?r)|#[^\{]){1,1024}/m
+
def initialize(callback, string)
@callback = callback
super(string)
end
@@ -52,21 +54,21 @@
end
end
end
def scan_text
- if scan_until(/(.*?)(?=\#\{|<\?r)/m) || scan(/(.*)\Z/m)
- @callback.text(self[1]) if self[1].size > 0
+ if scan(TEXT)
+ @callback.text(matched)
end
end
def scan_expression
if scan(/\#\{/)
- done = false
+ level = 1
code = ""
- until eos? || done
+ until eos? || level == 0
if scan(/[^"'\{\}]+/m)
code << matched
end
if scan(/"(\\"|[^"])*"/m)
@@ -75,20 +77,21 @@
if scan(/'(\\'|[^'])*'/m)
code << matched
end
- if scan(/\{[^\}]*\}/m)
+ if scan(/\{/)
code << matched
+ level += 1
end
if scan(/\}/)
- done = true
+ level -= 1
end
end
- if done
+ if level == 0
@callback.output(code)
else
raise StandardError.new "Could not find end of expression #{self}!"
end
elsif scan(/<\?r/)
@@ -112,10 +115,10 @@
end
def compile!(filename = @filename)
buffer = Buffer.new
scanner = Scanner.new(buffer, @template)
-
+
scanner.parse
@code = buffer.code
end
\ No newline at end of file