comparison markup/filters.py @ 14:c7d33e0c9839 trunk

The `<py:match>` directive now protects itself against simple infinite recursion (see MatchDirective), while still allowing recursion in general.
author cmlenz
date Tue, 13 Jun 2006 17:56:42 +0000
parents f9001cd6785b
children b3edbde541c4
comparison
equal deleted inserted replaced
13:f9001cd6785b 14:c7d33e0c9839
20 import re 20 import re
21 21
22 from markup.core import Attributes, Markup, Stream 22 from markup.core import Attributes, Markup, Stream
23 from markup.path import Path 23 from markup.path import Path
24 24
25 __all__ = ['EvalFilter', 'IncludeFilter', 'MatchFilter', 'WhitespaceFilter', 25 __all__ = ['EvalFilter', 'IncludeFilter', 'WhitespaceFilter', 'HTMLSanitizer']
26 'HTMLSanitizer']
27 26
28 27
29 class EvalFilter(object): 28 class EvalFilter(object):
30 """Responsible for evaluating expressions in a template.""" 29 """Responsible for evaluating expressions in a template."""
31 30
139 yield ikind, idata, ipos 138 yield ikind, idata, ipos
140 139
141 # If the included template defines any filters added at 140 # If the included template defines any filters added at
142 # runtime (such as py:match templates), those need to be 141 # runtime (such as py:match templates), those need to be
143 # applied to the including template, too. 142 # applied to the including template, too.
144 filters = template.filters + template._included_filters 143 filters = template._included_filters + template.filters
145 for filter_ in filters: 144 for filter_ in filters:
146 stream = filter_(stream, ctxt) 145 stream = filter_(stream, ctxt)
147 146
148 # Runtime filters included need to be propagated up 147 # Runtime filters included need to be propagated up
149 self.template._included_filters += filters 148 self.template._included_filters += filters
179 # process 178 # process
180 return 179 return
181 180
182 for event in self(stream, ctxt, ns_prefixes=ns_prefixes): 181 for event in self(stream, ctxt, ns_prefixes=ns_prefixes):
183 yield event 182 yield event
184
185
186 class MatchFilter(object):
187 """A filter that delegates to a given handler function when the input stream
188 matches some path expression.
189 """
190
191 def __init__(self, path, handler):
192 self.path = Path(path)
193 self.handler = handler
194
195 def __call__(self, stream, ctxt=None):
196 from markup.template import Template
197
198 test = self.path.test()
199 for kind, data, pos in stream:
200 result = test(kind, data, pos)
201 if result is True:
202 content = [(kind, data, pos)]
203 depth = 1
204 while depth > 0:
205 ev = stream.next()
206 if ev[0] is Stream.START:
207 depth += 1
208 elif ev[0] is Stream.END:
209 depth -= 1
210 content.append(ev)
211 test(*ev)
212
213 yield (Template.SUB,
214 ([lambda stream, ctxt: self.handler(content, ctxt)], []),
215 pos)
216 else:
217 yield kind, data, pos
218 183
219 184
220 class WhitespaceFilter(object): 185 class WhitespaceFilter(object):
221 """A filter that removes extraneous white space from the stream. 186 """A filter that removes extraneous white space from the stream.
222 187
Copyright (C) 2012-2017 Edgewall Software