Show More
@@ -12,6 +12,22 b' import util, config, templatefilters' | |||||
12 | path = ['templates', '../templates'] |
|
12 | path = ['templates', '../templates'] | |
13 | stringify = templatefilters.stringify |
|
13 | stringify = templatefilters.stringify | |
14 |
|
14 | |||
|
15 | def _flatten(thing): | |||
|
16 | '''yield a single stream from a possibly nested set of iterators''' | |||
|
17 | if isinstance(thing, str): | |||
|
18 | yield thing | |||
|
19 | elif not hasattr(thing, '__iter__'): | |||
|
20 | yield str(thing) | |||
|
21 | elif thing is not None: | |||
|
22 | for i in thing: | |||
|
23 | if isinstance(i, str): | |||
|
24 | yield i | |||
|
25 | elif not hasattr(i, '__iter__'): | |||
|
26 | yield str(i) | |||
|
27 | elif i is not None: | |||
|
28 | for j in _flatten(i): | |||
|
29 | yield j | |||
|
30 | ||||
15 | def parsestring(s, quoted=True): |
|
31 | def parsestring(s, quoted=True): | |
16 | '''parse a string using simple c-like syntax. |
|
32 | '''parse a string using simple c-like syntax. | |
17 | string must be in quotes if quoted is True.''' |
|
33 | string must be in quotes if quoted is True.''' | |
@@ -53,22 +69,7 b' class engine(object):' | |||||
53 | added elements for use during expansion. Is a generator.''' |
|
69 | added elements for use during expansion. Is a generator.''' | |
54 | if t not in self._cache: |
|
70 | if t not in self._cache: | |
55 | self._cache[t] = self._parse(self._loader(t)) |
|
71 | self._cache[t] = self._parse(self._loader(t)) | |
56 | parsed = self._cache[t] |
|
72 | return _flatten(self._process(self._cache[t], mapping)) | |
57 | iters = [self._process(parsed, mapping)] |
|
|||
58 | while iters: |
|
|||
59 | try: |
|
|||
60 | item = iters[0].next() |
|
|||
61 | except StopIteration: |
|
|||
62 | iters.pop(0) |
|
|||
63 | continue |
|
|||
64 | if isinstance(item, str): |
|
|||
65 | yield item |
|
|||
66 | elif item is None: |
|
|||
67 | yield '' |
|
|||
68 | elif hasattr(item, '__iter__'): |
|
|||
69 | iters.insert(0, iter(item)) |
|
|||
70 | else: |
|
|||
71 | yield str(item) |
|
|||
72 |
|
73 | |||
73 | def _parse(self, tmpl): |
|
74 | def _parse(self, tmpl): | |
74 | '''preparse a template''' |
|
75 | '''preparse a template''' |
General Comments 0
You need to be logged in to leave comments.
Login now