lib/nagoro/scanner.rb in manveru-nagoro-2009.01.29 vs lib/nagoro/scanner.rb in manveru-nagoro-2009.03.21

- old
+ new

@@ -3,20 +3,22 @@ module Nagoro class Scanner < StringScanner TEXT = /[^<>]+/m DOCTYPE = /<!DOCTYPE([^>]+)>/m - TAG_START = /<([^\s>]+)/n + TAG_START = /<([^\s>]+)/ TAG_END = /<\/([^>]*)>/ - TAG_OPEN_END = /\s*>/n - TAG_CLOSING_END = /\s*\/>/n + TAG_OPEN_END = /\s*>/ + TAG_CLOSING_END = /\s*\/>/ TAG_PARAMETER = /\s*([^\s]*)=(['"])(.*?)\2/um - INSTRUCTION_START = /<\?(\S+)/n + INSTRUCTION_START = /<\?(\S+)/ INSTRUCTION_END = /(.*?)(\?>)/um - RUBY_INTERP_START = /#\{/ + RUBY_INTERP_START = /\s*#\{/m + RUBY_INTERP_TEXT = /[^\{\}]+/m + RUBY_INTERP_NEST = /\{[^\}]*\}/m RUBY_INTERP_END = /(?=\})/ def initialize(string, callback) @callback = callback super(string) @@ -32,22 +34,33 @@ def run if scan(DOCTYPE ); doctype(self[1]) elsif scan(INSTRUCTION_START); instruction(self[1]) elsif scan(TAG_END ); tag_end(self[1]) - elsif scan(RUBY_INTERP_START); ruby_interp(self.matched) + elsif scan(RUBY_INTERP_START); ruby_interp(matched) elsif scan(TAG_START ); tag_start(self[1]) - elsif scan(TEXT ); text(self.matched) + elsif scan(TEXT ); text(matched) end end def instruction(name) scan(INSTRUCTION_END) @callback.instruction(name, self[1]) end def ruby_interp(string) - string << scan_until(RUBY_INTERP_END) + done = false + + until done + if scan(RUBY_INTERP_TEXT) + string << matched + elsif scan(RUBY_INTERP_NEST) + string << matched + elsif scan(RUBY_INTERP_END) + done = true + end + end + @callback.text(string) end def tag_start(name) args = {}