changeset 248:82f2bc77ad0c trunk

* Add `MANIFEST.in` so that the generated HTML documentation is included in the source distribution. * Remove duplicate `_match` implementation. * Allow shorthand expressions to start with an underscore.
author cmlenz
date Sun, 17 Sep 2006 10:16:15 +0000
parents cf4a125b44a1
children 1561034dc11f
files MANIFEST.in genshi/template.py genshi/tests/template.py
diffstat 3 files changed, 45 insertions(+), 72 deletions(-) [+]
line wrap: on
line diff
new file mode 100644
--- /dev/null
+++ b/MANIFEST.in
@@ -0,0 +1,5 @@
+exclude doc/2000ft.graffle
+exclude doc/docutils.conf
+exclude doc/logo.lineform
+exclude doc/Makefile
+include doc/*.html
--- a/genshi/template.py
+++ b/genshi/template.py
@@ -783,7 +783,7 @@
         raise NotImplementedError
 
     _FULL_EXPR_RE = re.compile(r'(?<!\$)\$\{(.+?)\}', re.DOTALL)
-    _SHORT_EXPR_RE = re.compile(r'(?<!\$)\$([a-zA-Z][a-zA-Z0-9_\.]*)')
+    _SHORT_EXPR_RE = re.compile(r'(?<!\$)\$([a-zA-Z_][a-zA-Z0-9_\.]*)')
 
     def _interpolate(cls, text, filename=None, lineno=-1, offset=-1):
         """Parse the given string and extract expressions.
@@ -918,77 +918,6 @@
             else:
                 yield kind, data, pos
 
-    def _match(self, stream, ctxt=None, match_templates=None):
-        """Internal stream filter that applies any defined match templates
-        to the stream.
-        """
-        if match_templates is None:
-            match_templates = ctxt._match_templates
-        nsprefix = {} # mapping of namespace prefixes to URIs
-
-        tail = []
-        def _strip(stream):
-            depth = 1
-            while 1:
-                kind, data, pos = stream.next()
-                if kind is START:
-                    depth += 1
-                elif kind is END:
-                    depth -= 1
-                if depth > 0:
-                    yield kind, data, pos
-                else:
-                    tail[:] = [(kind, data, pos)]
-                    break
-
-        for kind, data, pos in stream:
-
-            # We (currently) only care about start and end events for matching
-            # We might care about namespace events in the future, though
-            if not match_templates or kind not in (START, END):
-                yield kind, data, pos
-                continue
-
-            for idx, (test, path, template, directives) in \
-                    enumerate(match_templates):
-
-                if test(kind, data, pos, nsprefix, ctxt) is True:
-
-                    # Let the remaining match templates know about the event so
-                    # they get a chance to update their internal state
-                    for test in [mt[0] for mt in match_templates[idx + 1:]]:
-                        test(kind, data, pos, nsprefix, ctxt)
-
-                    # Consume and store all events until an end event
-                    # corresponding to this start event is encountered
-                    content = [(kind, data, pos)]
-                    content += list(self._match(_strip(stream), ctxt)) + tail
-
-                    kind, data, pos = tail[0]
-                    for test in [mt[0] for mt in match_templates]:
-                        test(kind, data, pos, nsprefix, ctxt)
-
-                    # Make the select() function available in the body of the
-                    # match template
-                    def select(path):
-                        return Stream(content).select(path)
-                    ctxt.push(dict(select=select))
-
-                    # Recursively process the output
-                    template = _apply_directives(template, ctxt, directives)
-                    for event in self._match(self._eval(self._flatten(template,
-                                                                      ctxt),
-                                                        ctxt), ctxt,
-                                             match_templates[:idx] +
-                                             match_templates[idx + 1:]):
-                        yield event
-
-                    ctxt.pop()
-                    break
-
-            else: # no matches
-                yield kind, data, pos
-
 
 EXPR = Template.EXPR
 SUB = Template.SUB
--- a/genshi/tests/template.py
+++ b/genshi/tests/template.py
@@ -827,6 +827,36 @@
         self.assertEqual(Template.EXPR, parts[0][0])
         self.assertEqual('bla', parts[0][1].source)
 
+    def test_interpolate_short(self):
+        parts = list(Template._interpolate('$bla'))
+        self.assertEqual(1, len(parts))
+        self.assertEqual(Template.EXPR, parts[0][0])
+        self.assertEqual('bla', parts[0][1].source)
+
+    def test_interpolate_short_starting_with_underscore(self):
+        parts = list(Template._interpolate('$_bla'))
+        self.assertEqual(1, len(parts))
+        self.assertEqual(Template.EXPR, parts[0][0])
+        self.assertEqual('_bla', parts[0][1].source)
+
+    def test_interpolate_short_containing_underscore(self):
+        parts = list(Template._interpolate('$foo_bar'))
+        self.assertEqual(1, len(parts))
+        self.assertEqual(Template.EXPR, parts[0][0])
+        self.assertEqual('foo_bar', parts[0][1].source)
+
+    def test_interpolate_short_starting_with_dot(self):
+        parts = list(Template._interpolate('$.bla'))
+        self.assertEqual(1, len(parts))
+        self.assertEqual(Stream.TEXT, parts[0][0])
+        self.assertEqual('$.bla', parts[0][1])
+
+    def test_interpolate_short_containing_dot(self):
+        parts = list(Template._interpolate('$foo.bar'))
+        self.assertEqual(1, len(parts))
+        self.assertEqual(Template.EXPR, parts[0][0])
+        self.assertEqual('foo.bar', parts[0][1].source)
+
     def test_interpolate_mixed1(self):
         parts = list(Template._interpolate('$foo bar $baz'))
         self.assertEqual(3, len(parts))
@@ -847,6 +877,15 @@
         self.assertEqual(Stream.TEXT, parts[2][0])
         self.assertEqual(' baz', parts[2][1])
 
+    def test_interpolate_mixed1(self):
+        parts = list(Template._interpolate('$foo bar $baz'))
+        self.assertEqual(3, len(parts))
+        self.assertEqual(Template.EXPR, parts[0][0])
+        self.assertEqual('foo', parts[0][1].source)
+        self.assertEqual(Stream.TEXT, parts[1][0])
+        self.assertEqual(' bar ', parts[1][1])
+        self.assertEqual(Template.EXPR, parts[2][0])
+        self.assertEqual('baz', parts[2][1].source)
 
 class MarkupTemplateTestCase(unittest.TestCase):
     """Tests for markup template processing."""
Copyright (C) 2012-2017 Edgewall Software