<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -1,7 +1,39 @@
 #!/usr/local/bin/python
 import cgi, re, itertools
+context = 2
+
+class TempException(Exception):
+    def __init__(self, val, pos, tmpl):
+        Exception.__init__(self, val)
+        self.val, self.pos, self.tmpl = val, pos, tmpl
+        self.lineNo, self.context = self._getLines(tmpl.txt, pos, context)
+
+    def _getLines(self, txt, pos, context):
+        lines = txt.splitlines(True)
+        cur = 0
+        for i, l in enumerate(lines):
+            cur += len(l)
+            if cur &gt; pos:
+                break
+        if i &lt; context:
+            startc = 0
+        else:
+            startc = i - context
+        if i &gt; (len(lines)-context):
+            endc = len(lines)
+        else:
+            endc = i + context + 1
+        return i + 1, [j.strip() for j in lines[startc:endc]]
+
+    def __str__(self):
+        ret = [
+            &quot;TempException: %s&quot;%self.val,
+            &quot;\tContext: line %s in %s:&quot;%(self.lineNo, self.tmpl.name),
+        ]
+        ret.extend([&quot;\t\t&quot; + i for i in self.context])
+        return &quot;\n&quot;.join(ret)
+
 
-class TempException(Exception): pass
 
 def escape(s):
     &quot;&quot;&quot;
@@ -16,19 +48,19 @@ def escape(s):
     return s
 
 
-def _compile(expr):
+def _compile(expr, pos, tmpl):
         try:
             return compile(expr, &quot;&lt;string&gt;&quot;, &quot;eval&quot;)
         except SyntaxError, value:
-            s = 'Invalid expression in template: &quot;%s&quot;'%(expr)
-            raise TempException(s)
+            s = 'Invalid expression: &quot;%s&quot;'%(expr)
+            raise TempException(s, pos, tmpl)
 
 
 class _Expression:
-    def __init__(self, expr, flavor, pos, txt):
+    def __init__(self, expr, flavor, pos, tmpl):
         self.expr, self.flavor = expr, flavor
-        self.pos, self.txt = pos, txt
-        self._ecache = _compile(expr)
+        self.pos, self.tmpl = pos, tmpl
+        self._ecache = _compile(expr, pos, tmpl)
 
     def __call__(self, ns):
         try:
@@ -37,7 +69,7 @@ class _Expression:
                 ret = ret(ns)
         except NameError, value:
             s = 'NameError: &quot;%s&quot;'%value
-            raise TempException(s)
+            raise TempException(s, self.pos, self.tmpl)
         if self.flavor == &quot;@&quot;:
             if not getattr(ret, &quot;_cubictemp_unescaped&quot;, 0):
                 return escape(str(ret))
@@ -63,10 +95,10 @@ class _Block(list):
 
 
 class _Iterable(list):
-    def __init__(self, iterable, varname, pos, txt):
+    def __init__(self, iterable, varname, pos, tmpl):
         self.iterable, self.varname = iterable, varname
-        self.pos, self.txt = pos, txt
-        self._ecache = _compile(iterable)
+        self.pos, self.tmpl = pos, tmpl
+        self._ecache = _compile(iterable, pos, tmpl)
         self.ns = {}
 
     def __call__(self, ns):
@@ -76,11 +108,12 @@ class _Iterable(list):
             loopIter = eval(self._ecache, {}, ns)
         except NameError, value:
             s = 'NameError: &quot;%s&quot;'%value
-            raise TempException(s)
+            raise TempException(s, self.pos, self.tmpl)
         try:
             loopIter = iter(loopIter)
         except TypeError:
-            raise TempException(&quot;Can not iterate over %s&quot;%self.iterable)
+            s = &quot;Can not iterate over %s&quot;%self.iterable
+            raise TempException(s, self.pos, self.tmpl)
         s = []
         for i in loopIter:
             ns[self.varname] = i
@@ -95,7 +128,7 @@ class Temp:
         (&lt;!--\(\s*               
             (
                     for\s+(?P&lt;varName&gt;\w+)\s+in\s+(?P&lt;iterable&gt;.+)
-                |   block\s+(?P&lt;blockName&gt;\w+)
+                |   block\s+ (?P&lt;process&gt;\|)? (?P&lt;blockName&gt;\w+)
             )
         \s*\)(--&gt;)?) | 
         # The end of a tag
@@ -104,6 +137,7 @@ class Temp:
         ((?P&lt;flavor&gt;@|\$)!(?P&lt;expr&gt;.+?)!(?P=flavor))
     &quot;&quot;&quot;
     _reParts = re.compile(_bStart, re.X|re.M)
+    name = &quot;&lt;string&gt;&quot;
     def __init__(self, txt, **nsDict):
         self.nsDict, self.txt = nsDict, txt
         self.block = _Block()
@@ -121,13 +155,13 @@ class Temp:
                     b = _Block()
                     parent.ns[g[&quot;blockName&quot;]] = b
                 else:
-                    b = _Iterable(g[&quot;iterable&quot;], g[&quot;varName&quot;], pos, txt)
+                    b = _Iterable(g[&quot;iterable&quot;], g[&quot;varName&quot;], pos, self)
                     parent.append(b)
                 stack.append(b)
             elif g[&quot;end&quot;]:
                 stack.pop()
             elif g[&quot;expr&quot;]:
-                e = _Expression(g[&quot;expr&quot;], g[&quot;flavor&quot;], pos, txt)
+                e = _Expression(g[&quot;expr&quot;], g[&quot;flavor&quot;], pos, self)
                 stack[-1].append(e)
         if pos &lt; len(txt):
             stack[-1].append(_Text(txt[pos:]))
@@ -143,12 +177,6 @@ class Temp:
 
 class File(Temp):
     def __init__(self, filename, **nsDict):
-        self.filename = filename
+        self.name = filename
         data = open(filename).read()
         Temp.__init__(self, data, **nsDict)
-
-    def __str__(self):
-        try:
-            return Temp.__str__(self)
-        except TempException, val:
-            raise TempException, &quot;%s: %s&quot;%(self.filename, str(val))</diff>
      <filename>cubictemp.py</filename>
    </modified>
    <modified>
      <diff>@@ -2,13 +2,52 @@ import pylid
 import cubictemp
 
 
+class uTempException(pylid.TestCase):
+    def setUp(self):
+        self.s = cubictemp.Temp(&quot;text&quot;)
+        self.t = cubictemp.TempException(&quot;foo&quot;, 0, self.s)
+
+    def test_getLines(self):
+        txt = &quot;&quot;&quot;
+           one
+           two
+           three
+        &quot;&quot;&quot;
+        x = txt.find(&quot;one&quot;)
+        i, ctx = self.t._getLines(txt, x, 1)
+        assert i == 2
+        assert len(ctx) == 3
+        assert ctx[1].strip() == &quot;one&quot;
+
+        x = txt.find(&quot;three&quot;)
+        i, ctx = self.t._getLines(txt, x, 2)
+        assert i == 4
+        assert len(ctx) == 4
+
+    def test_format(self):
+        s = &quot;&quot;&quot;
+            &lt;!--(block foo)--&gt;
+                @!foo!@
+            &lt;!--(end)--&gt;
+            @![!@
+            &lt;!--(block bar)--&gt;
+                @!foo!@
+            &lt;!--(end)--&gt;
+        &quot;&quot;&quot;
+        self.failWith(&quot;line 5&quot;, cubictemp.Temp, s)
+
+
+
 class u_Expression(pylid.TestCase):
+    def setUp(self):
+        self.s = cubictemp.Temp(&quot;text&quot;)
+
     def test_call(self):
-        e = cubictemp._Expression(&quot;foo&quot;, &quot;@&quot;, 0, &quot;foo&quot;)
+        e = cubictemp._Expression(&quot;foo&quot;, &quot;@&quot;, 0, self.s)
         assert e(dict(foo=&quot;bar&quot;)) == &quot;bar&quot;
 
     def test_block(self):
-        e = cubictemp._Expression(&quot;foo&quot;, &quot;@&quot;, 0, &quot;foo&quot;)
+        e = cubictemp._Expression(&quot;foo&quot;, &quot;@&quot;, 0, self.s)
         t = cubictemp._Block()
         t.append(cubictemp._Text(&quot;bar&quot;))
         assert e(dict(foo=t)) == &quot;bar&quot;
@@ -18,11 +57,11 @@ class u_Expression(pylid.TestCase):
             &quot;invalid expression&quot;,
             cubictemp._Expression,
             &quot;for x&quot;, &quot;@&quot;,
-            0, &quot;foo&quot;
+            0, self.s
         )
 
     def test_namerr(self):
-        e = cubictemp._Expression(&quot;foo&quot;, &quot;@&quot;, 0, &quot;foo&quot;)
+        e = cubictemp._Expression(&quot;foo&quot;, &quot;@&quot;, 0, self.s)
         self.failWith(
             &quot;NameError&quot;,
             e,</diff>
      <filename>test/test_cubictemp.py</filename>
    </modified>
    <modified>
      <diff>@@ -20,6 +20,6 @@ Rewrite
 Block types:
 
 &lt;--(block named)
-&lt;--(block named|processor)
-&lt;--(raw|processor)
+&lt;--(block named||processor)
+&lt;--(process(i) for i in processor)
 </diff>
      <filename>todo</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>b1bac2277538da71df891cbb70ee75e2489c22fc</id>
    </parent>
  </parents>
  <author>
    <name>Aldo Cortesi</name>
    <email>aldo@nullcube.com</email>
  </author>
  <url>http://github.com/cortesi/cubictemp/commit/16a454b5a22deb5ce2a6048824d0ba5d12b5148f</url>
  <id>16a454b5a22deb5ce2a6048824d0ba5d12b5148f</id>
  <committed-date>2007-07-02T18:13:40-07:00</committed-date>
  <authored-date>2007-07-02T18:13:40-07:00</authored-date>
  <message>More informative error messages, and the beginnnings of experimental processor
support.</message>
  <tree>f0ec376c681c9bb9d31f1ebfe41b3e7b77187540</tree>
  <committer>
    <name>Aldo Cortesi</name>
    <email>aldo@nullcube.com</email>
  </committer>
</commit>
