def parse(body)
rx = @glossary.directive
stack = [Template.new(self)]
stack[0].parent = self
last = stack.last
modifier = nil
closer = nil
while (m = rx.match(body))
pre = m.pre_match
command = m[1..-1].compact.first.strip.gsub(/\s+/,' ')
body = m.post_match
if (pre && pre.length > 0)
stack.last.add TextCommand.new(pre)
end
next if modifier && @glossary.modifies?(modifier,last,command)
if closer and @glossary.modifies?(closer,last,command)
cmd = stack.pop
last = stack.last
last.add(cmd)
modifier = last.class.modifier
closer = last.class.closer
next
end
cmd = @glossary.lookup(command)
if cmd.is_a?(StackableCommand)
modifier = cmd.class.modifier
closer = cmd.class.closer
stack.push cmd
last = cmd
else
last.add cmd
end
end
stack.last.add TextCommand.new(body) if body && body.length > 0
if (stack.length > 1)
raise ArgumentError, 'Mismatched command closures in template'
end
stack[0]
end