| [414] | 1 | # -*- coding: utf-8 -*- |
|---|
| 2 | # |
|---|
| [1120] | 3 | # Copyright (C) 2006-2010 Edgewall Software |
|---|
| [414] | 4 | # All rights reserved. |
|---|
| 5 | # |
|---|
| 6 | # This software is licensed as described in the file COPYING, which |
|---|
| 7 | # you should have received as part of this distribution. The terms |
|---|
| 8 | # are also available at http://genshi.edgewall.org/wiki/License. |
|---|
| 9 | # |
|---|
| 10 | # This software consists of voluntary contributions made by many |
|---|
| 11 | # individuals. For the exact contribution history, see the revision |
|---|
| 12 | # history and logs, available at http://genshi.edgewall.org/log/. |
|---|
| 13 | |
|---|
| 14 | """Markup templating engine.""" |
|---|
| 15 | |
|---|
| 16 | from itertools import chain |
|---|
| 17 | |
|---|
| [752] | 18 | from genshi.core import Attrs, Markup, Namespace, Stream, StreamEventKind |
|---|
| [497] | 19 | from genshi.core import START, END, START_NS, END_NS, TEXT, PI, COMMENT |
|---|
| [414] | 20 | from genshi.input import XMLParser |
|---|
| [492] | 21 | from genshi.template.base import BadDirectiveError, Template, \ |
|---|
| [575] | 22 | TemplateSyntaxError, _apply_directives, \ |
|---|
| [725] | 23 | EXEC, INCLUDE, SUB |
|---|
| [497] | 24 | from genshi.template.eval import Suite |
|---|
| [499] | 25 | from genshi.template.interpolation import interpolate |
|---|
| [414] | 26 | from genshi.template.directives import * |
|---|
| [726] | 27 | from genshi.template.text import NewTextTemplate |
|---|
| [414] | 28 | |
|---|
| [517] | 29 | __all__ = ['MarkupTemplate'] |
|---|
| 30 | __docformat__ = 'restructuredtext en' |
|---|
| [497] | 31 | |
|---|
| [517] | 32 | |
|---|
| [414] | 33 | class MarkupTemplate(Template): |
|---|
| 34 | """Implementation of the template language for XML-based templates. |
|---|
| 35 | |
|---|
| 36 | >>> tmpl = MarkupTemplate('''<ul xmlns:py="http://genshi.edgewall.org/"> |
|---|
| 37 | ... <li py:for="item in items">${item}</li> |
|---|
| 38 | ... </ul>''') |
|---|
| [1076] | 39 | >>> print(tmpl.generate(items=[1, 2, 3])) |
|---|
| [414] | 40 | <ul> |
|---|
| 41 | <li>1</li><li>2</li><li>3</li> |
|---|
| 42 | </ul> |
|---|
| 43 | """ |
|---|
| [519] | 44 | |
|---|
| [954] | 45 | DIRECTIVE_NAMESPACE = 'http://genshi.edgewall.org/' |
|---|
| 46 | XINCLUDE_NAMESPACE = 'http://www.w3.org/2001/XInclude' |
|---|
| [443] | 47 | |
|---|
| [414] | 48 | directives = [('def', DefDirective), |
|---|
| 49 | ('match', MatchDirective), |
|---|
| 50 | ('when', WhenDirective), |
|---|
| 51 | ('otherwise', OtherwiseDirective), |
|---|
| 52 | ('for', ForDirective), |
|---|
| 53 | ('if', IfDirective), |
|---|
| 54 | ('choose', ChooseDirective), |
|---|
| 55 | ('with', WithDirective), |
|---|
| 56 | ('replace', ReplaceDirective), |
|---|
| 57 | ('content', ContentDirective), |
|---|
| 58 | ('attrs', AttrsDirective), |
|---|
| 59 | ('strip', StripDirective)] |
|---|
| [721] | 60 | serializer = 'xml' |
|---|
| [752] | 61 | _number_conv = Markup |
|---|
| [414] | 62 | |
|---|
| [954] | 63 | def __init__(self, source, filepath=None, filename=None, loader=None, |
|---|
| 64 | encoding=None, lookup='strict', allow_exec=True): |
|---|
| 65 | Template.__init__(self, source, filepath=filepath, filename=filename, |
|---|
| 66 | loader=loader, encoding=encoding, lookup=lookup, |
|---|
| 67 | allow_exec=allow_exec) |
|---|
| 68 | self.add_directives(self.DIRECTIVE_NAMESPACE, self) |
|---|
| 69 | |
|---|
| [831] | 70 | def _init_filters(self): |
|---|
| 71 | Template._init_filters(self) |
|---|
| [598] | 72 | # Make sure the include filter comes after the match filter |
|---|
| [1099] | 73 | self.filters.remove(self._include) |
|---|
| 74 | self.filters += [self._match, self._include] |
|---|
| [414] | 75 | |
|---|
| [456] | 76 | def _parse(self, source, encoding): |
|---|
| 77 | if not isinstance(source, Stream): |
|---|
| [526] | 78 | source = XMLParser(source, filename=self.filename, |
|---|
| [456] | 79 | encoding=encoding) |
|---|
| [954] | 80 | stream = [] |
|---|
| [414] | 81 | |
|---|
| [456] | 82 | for kind, data, pos in source: |
|---|
| 83 | |
|---|
| [954] | 84 | if kind is TEXT: |
|---|
| 85 | for kind, data, pos in interpolate(data, self.filepath, pos[1], |
|---|
| 86 | pos[2], lookup=self.lookup): |
|---|
| [414] | 87 | stream.append((kind, data, pos)) |
|---|
| 88 | |
|---|
| [954] | 89 | elif kind is PI and data[0] == 'python': |
|---|
| 90 | if not self.allow_exec: |
|---|
| 91 | raise TemplateSyntaxError('Python code blocks not allowed', |
|---|
| 92 | self.filepath, *pos[1:]) |
|---|
| 93 | try: |
|---|
| 94 | suite = Suite(data[1], self.filepath, pos[1], |
|---|
| 95 | lookup=self.lookup) |
|---|
| 96 | except SyntaxError, err: |
|---|
| 97 | raise TemplateSyntaxError(err, self.filepath, |
|---|
| 98 | pos[1] + (err.lineno or 1) - 1, |
|---|
| 99 | pos[2] + (err.offset or 0)) |
|---|
| 100 | stream.append((EXEC, suite, pos)) |
|---|
| 101 | |
|---|
| 102 | elif kind is COMMENT: |
|---|
| 103 | if not data.lstrip().startswith('!'): |
|---|
| [414] | 104 | stream.append((kind, data, pos)) |
|---|
| 105 | |
|---|
| [954] | 106 | else: |
|---|
| 107 | stream.append((kind, data, pos)) |
|---|
| 108 | |
|---|
| 109 | return stream |
|---|
| 110 | |
|---|
| 111 | def _extract_directives(self, stream, namespace, factory): |
|---|
| 112 | depth = 0 |
|---|
| 113 | dirmap = {} # temporary mapping of directives to elements |
|---|
| 114 | new_stream = [] |
|---|
| 115 | ns_prefix = {} # namespace prefixes in use |
|---|
| 116 | |
|---|
| 117 | for kind, data, pos in stream: |
|---|
| 118 | |
|---|
| 119 | if kind is START: |
|---|
| [443] | 120 | tag, attrs = data |
|---|
| [414] | 121 | directives = [] |
|---|
| 122 | strip = False |
|---|
| 123 | |
|---|
| [954] | 124 | if tag.namespace == namespace: |
|---|
| 125 | cls = factory.get_directive(tag.localname) |
|---|
| [414] | 126 | if cls is None: |
|---|
| [954] | 127 | raise BadDirectiveError(tag.localname, |
|---|
| 128 | self.filepath, pos[1]) |
|---|
| [661] | 129 | args = dict([(name.localname, value) for name, value |
|---|
| 130 | in attrs if not name.namespace]) |
|---|
| [1069] | 131 | directives.append((factory.get_directive_index(cls), cls, |
|---|
| 132 | args, ns_prefix.copy(), pos)) |
|---|
| [414] | 133 | strip = True |
|---|
| 134 | |
|---|
| [443] | 135 | new_attrs = [] |
|---|
| 136 | for name, value in attrs: |
|---|
| [954] | 137 | if name.namespace == namespace: |
|---|
| 138 | cls = factory.get_directive(name.localname) |
|---|
| [414] | 139 | if cls is None: |
|---|
| 140 | raise BadDirectiveError(name.localname, |
|---|
| 141 | self.filepath, pos[1]) |
|---|
| [954] | 142 | if type(value) is list and len(value) == 1: |
|---|
| 143 | value = value[0][1] |
|---|
| [1069] | 144 | directives.append((factory.get_directive_index(cls), |
|---|
| 145 | cls, value, ns_prefix.copy(), pos)) |
|---|
| [414] | 146 | else: |
|---|
| [443] | 147 | new_attrs.append((name, value)) |
|---|
| 148 | new_attrs = Attrs(new_attrs) |
|---|
| [414] | 149 | |
|---|
| 150 | if directives: |
|---|
| [1069] | 151 | directives.sort() |
|---|
| [954] | 152 | dirmap[(depth, tag)] = (directives, len(new_stream), |
|---|
| 153 | strip) |
|---|
| [414] | 154 | |
|---|
| [954] | 155 | new_stream.append((kind, (tag, new_attrs), pos)) |
|---|
| 156 | depth += 1 |
|---|
| 157 | |
|---|
| 158 | elif kind is END: |
|---|
| 159 | depth -= 1 |
|---|
| 160 | new_stream.append((kind, data, pos)) |
|---|
| 161 | |
|---|
| 162 | # If there have have directive attributes with the |
|---|
| 163 | # corresponding start tag, move the events inbetween into |
|---|
| 164 | # a "subprogram" |
|---|
| 165 | if (depth, data) in dirmap: |
|---|
| 166 | directives, offset, strip = dirmap.pop((depth, data)) |
|---|
| 167 | substream = new_stream[offset:] |
|---|
| 168 | if strip: |
|---|
| 169 | substream = substream[1:-1] |
|---|
| 170 | new_stream[offset:] = [ |
|---|
| 171 | (SUB, (directives, substream), pos) |
|---|
| 172 | ] |
|---|
| 173 | |
|---|
| 174 | elif kind is SUB: |
|---|
| 175 | directives, substream = data |
|---|
| 176 | substream = self._extract_directives(substream, namespace, |
|---|
| 177 | factory) |
|---|
| 178 | |
|---|
| 179 | if len(substream) == 1 and substream[0][0] is SUB: |
|---|
| 180 | added_directives, substream = substream[0][1] |
|---|
| 181 | directives += added_directives |
|---|
| 182 | |
|---|
| 183 | new_stream.append((kind, (directives, substream), pos)) |
|---|
| 184 | |
|---|
| 185 | elif kind is START_NS: |
|---|
| 186 | # Strip out the namespace declaration for template |
|---|
| 187 | # directives |
|---|
| 188 | prefix, uri = data |
|---|
| 189 | ns_prefix[prefix] = uri |
|---|
| 190 | if uri != namespace: |
|---|
| 191 | new_stream.append((kind, data, pos)) |
|---|
| 192 | |
|---|
| 193 | elif kind is END_NS: |
|---|
| 194 | uri = ns_prefix.pop(data, None) |
|---|
| 195 | if uri and uri != namespace: |
|---|
| 196 | new_stream.append((kind, data, pos)) |
|---|
| 197 | |
|---|
| 198 | else: |
|---|
| 199 | new_stream.append((kind, data, pos)) |
|---|
| 200 | |
|---|
| 201 | return new_stream |
|---|
| 202 | |
|---|
| 203 | def _extract_includes(self, stream): |
|---|
| 204 | streams = [[]] # stacked lists of events of the "compiled" template |
|---|
| 205 | prefixes = {} |
|---|
| 206 | fallbacks = [] |
|---|
| 207 | includes = [] |
|---|
| 208 | xinclude_ns = Namespace(self.XINCLUDE_NAMESPACE) |
|---|
| 209 | |
|---|
| 210 | for kind, data, pos in stream: |
|---|
| 211 | stream = streams[-1] |
|---|
| 212 | |
|---|
| 213 | if kind is START: |
|---|
| 214 | # Record any directive attributes in start tags |
|---|
| 215 | tag, attrs = data |
|---|
| 216 | if tag in xinclude_ns: |
|---|
| [443] | 217 | if tag.localname == 'include': |
|---|
| [954] | 218 | include_href = attrs.get('href') |
|---|
| [443] | 219 | if not include_href: |
|---|
| 220 | raise TemplateSyntaxError('Include misses required ' |
|---|
| [514] | 221 | 'attribute "href"', |
|---|
| 222 | self.filepath, *pos[1:]) |
|---|
| [954] | 223 | includes.append((include_href, attrs.get('parse'))) |
|---|
| [463] | 224 | streams.append([]) |
|---|
| [443] | 225 | elif tag.localname == 'fallback': |
|---|
| [704] | 226 | streams.append([]) |
|---|
| 227 | fallbacks.append(streams[-1]) |
|---|
| [443] | 228 | else: |
|---|
| [954] | 229 | stream.append((kind, (tag, attrs), pos)) |
|---|
| [443] | 230 | |
|---|
| [414] | 231 | elif kind is END: |
|---|
| [954] | 232 | if fallbacks and data == xinclude_ns['fallback']: |
|---|
| [1239] | 233 | fallback_stream = streams.pop() |
|---|
| 234 | assert fallback_stream is fallbacks[-1] |
|---|
| [954] | 235 | elif data == xinclude_ns['include']: |
|---|
| [704] | 236 | fallback = None |
|---|
| 237 | if len(fallbacks) == len(includes): |
|---|
| 238 | fallback = fallbacks.pop() |
|---|
| 239 | streams.pop() # discard anything between the include tags |
|---|
| 240 | # and the fallback element |
|---|
| [463] | 241 | stream = streams[-1] |
|---|
| [726] | 242 | href, parse = includes.pop() |
|---|
| 243 | try: |
|---|
| 244 | cls = { |
|---|
| 245 | 'xml': MarkupTemplate, |
|---|
| 246 | 'text': NewTextTemplate |
|---|
| [1103] | 247 | }.get(parse) or self.__class__ |
|---|
| [726] | 248 | except KeyError: |
|---|
| 249 | raise TemplateSyntaxError('Invalid value for "parse" ' |
|---|
| 250 | 'attribute of include', |
|---|
| 251 | self.filepath, *pos[1:]) |
|---|
| 252 | stream.append((INCLUDE, (href, cls, fallback), pos)) |
|---|
| [443] | 253 | else: |
|---|
| 254 | stream.append((kind, data, pos)) |
|---|
| 255 | |
|---|
| [954] | 256 | elif kind is START_NS and data[1] == xinclude_ns: |
|---|
| 257 | # Strip out the XInclude namespace |
|---|
| 258 | prefixes[data[0]] = data[1] |
|---|
| [414] | 259 | |
|---|
| [954] | 260 | elif kind is END_NS and data in prefixes: |
|---|
| 261 | prefixes.pop(data) |
|---|
| [497] | 262 | |
|---|
| [414] | 263 | else: |
|---|
| 264 | stream.append((kind, data, pos)) |
|---|
| 265 | |
|---|
| [463] | 266 | assert len(streams) == 1 |
|---|
| 267 | return streams[0] |
|---|
| [414] | 268 | |
|---|
| [954] | 269 | def _interpolate_attrs(self, stream): |
|---|
| 270 | for kind, data, pos in stream: |
|---|
| 271 | |
|---|
| 272 | if kind is START: |
|---|
| 273 | # Record any directive attributes in start tags |
|---|
| 274 | tag, attrs = data |
|---|
| 275 | new_attrs = [] |
|---|
| 276 | for name, value in attrs: |
|---|
| 277 | if value: |
|---|
| 278 | value = list(interpolate(value, self.filepath, pos[1], |
|---|
| 279 | pos[2], lookup=self.lookup)) |
|---|
| 280 | if len(value) == 1 and value[0][0] is TEXT: |
|---|
| 281 | value = value[0][1] |
|---|
| 282 | new_attrs.append((name, value)) |
|---|
| 283 | data = tag, Attrs(new_attrs) |
|---|
| 284 | |
|---|
| 285 | yield kind, data, pos |
|---|
| 286 | |
|---|
| [1257] | 287 | def _prepare(self, stream, inlined=None): |
|---|
| 288 | return Template._prepare( |
|---|
| 289 | self, self._extract_includes(self._interpolate_attrs(stream)), |
|---|
| 290 | inlined=inlined) |
|---|
| [954] | 291 | |
|---|
| 292 | def add_directives(self, namespace, factory): |
|---|
| 293 | """Register a custom `DirectiveFactory` for a given namespace. |
|---|
| 294 | |
|---|
| 295 | :param namespace: the namespace URI |
|---|
| 296 | :type namespace: `basestring` |
|---|
| 297 | :param factory: the directive factory to register |
|---|
| 298 | :type factory: `DirectiveFactory` |
|---|
| 299 | :since: version 0.6 |
|---|
| 300 | """ |
|---|
| 301 | assert not self._prepared, 'Too late for adding directives, ' \ |
|---|
| 302 | 'template already prepared' |
|---|
| 303 | self._stream = self._extract_directives(self._stream, namespace, |
|---|
| 304 | factory) |
|---|
| 305 | |
|---|
| [892] | 306 | def _match(self, stream, ctxt, start=0, end=None, **vars): |
|---|
| [414] | 307 | """Internal stream filter that applies any defined match templates |
|---|
| 308 | to the stream. |
|---|
| 309 | """ |
|---|
| [892] | 310 | match_templates = ctxt._match_templates |
|---|
| [414] | 311 | |
|---|
| [1149] | 312 | def _strip(stream, append): |
|---|
| [414] | 313 | depth = 1 |
|---|
| [1052] | 314 | next = stream.next |
|---|
| [414] | 315 | while 1: |
|---|
| [1052] | 316 | event = next() |
|---|
| [414] | 317 | if event[0] is START: |
|---|
| 318 | depth += 1 |
|---|
| 319 | elif event[0] is END: |
|---|
| 320 | depth -= 1 |
|---|
| 321 | if depth > 0: |
|---|
| 322 | yield event |
|---|
| 323 | else: |
|---|
| [1052] | 324 | append(event) |
|---|
| [414] | 325 | break |
|---|
| 326 | |
|---|
| 327 | for event in stream: |
|---|
| 328 | |
|---|
| [1011] | 329 | # We (currently) only care about start and end events for matching |
|---|
| [414] | 330 | # We might care about namespace events in the future, though |
|---|
| [1011] | 331 | if not match_templates or (event[0] is not START and |
|---|
| 332 | event[0] is not END): |
|---|
| [414] | 333 | yield event |
|---|
| 334 | continue |
|---|
| 335 | |
|---|
| [718] | 336 | for idx, (test, path, template, hints, namespaces, directives) \ |
|---|
| 337 | in enumerate(match_templates): |
|---|
| [892] | 338 | if idx < start or end is not None and idx >= end: |
|---|
| [884] | 339 | continue |
|---|
| [414] | 340 | |
|---|
| 341 | if test(event, namespaces, ctxt) is True: |
|---|
| [718] | 342 | if 'match_once' in hints: |
|---|
| 343 | del match_templates[idx] |
|---|
| 344 | idx -= 1 |
|---|
| [414] | 345 | |
|---|
| 346 | # Let the remaining match templates know about the event so |
|---|
| 347 | # they get a chance to update their internal state |
|---|
| 348 | for test in [mt[0] for mt in match_templates[idx + 1:]]: |
|---|
| 349 | test(event, namespaces, ctxt, updateonly=True) |
|---|
| 350 | |
|---|
| 351 | # Consume and store all events until an end event |
|---|
| 352 | # corresponding to this start event is encountered |
|---|
| [892] | 353 | pre_end = idx + 1 |
|---|
| [810] | 354 | if 'match_once' not in hints and 'not_recursive' in hints: |
|---|
| [892] | 355 | pre_end -= 1 |
|---|
| [1149] | 356 | tail = [] |
|---|
| 357 | inner = _strip(stream, tail.append) |
|---|
| [892] | 358 | if pre_end > 0: |
|---|
| [1093] | 359 | inner = self._match(inner, ctxt, start=start, |
|---|
| 360 | end=pre_end, **vars) |
|---|
| [816] | 361 | content = self._include(chain([event], inner, tail), ctxt) |
|---|
| 362 | if 'not_buffered' not in hints: |
|---|
| 363 | content = list(content) |
|---|
| [1052] | 364 | content = Stream(content) |
|---|
| [414] | 365 | |
|---|
| 366 | # Make the select() function available in the body of the |
|---|
| 367 | # match template |
|---|
| [897] | 368 | selected = [False] |
|---|
| [414] | 369 | def select(path): |
|---|
| [897] | 370 | selected[0] = True |
|---|
| [1052] | 371 | return content.select(path, namespaces, ctxt) |
|---|
| [816] | 372 | vars = dict(select=select) |
|---|
| [414] | 373 | |
|---|
| 374 | # Recursively process the output |
|---|
| [816] | 375 | template = _apply_directives(template, directives, ctxt, |
|---|
| [1036] | 376 | vars) |
|---|
| [1015] | 377 | for event in self._match(self._flatten(template, ctxt, |
|---|
| 378 | **vars), |
|---|
| 379 | ctxt, start=idx + 1, **vars): |
|---|
| [414] | 380 | yield event |
|---|
| 381 | |
|---|
| [897] | 382 | # If the match template did not actually call select to |
|---|
| 383 | # consume the matched stream, the original events need to |
|---|
| 384 | # be consumed here or they'll get appended to the output |
|---|
| 385 | if not selected[0]: |
|---|
| 386 | for event in content: |
|---|
| 387 | pass |
|---|
| 388 | |
|---|
| [1170] | 389 | # Let this match template and the remaining match |
|---|
| 390 | # templates know about the last event in the |
|---|
| 391 | # matched content, so they can update their |
|---|
| [897] | 392 | # internal state accordingly |
|---|
| [1170] | 393 | for test in [mt[0] for mt in match_templates[idx:]]: |
|---|
| [897] | 394 | test(tail[0], namespaces, ctxt, updateonly=True) |
|---|
| 395 | |
|---|
| [414] | 396 | break |
|---|
| 397 | |
|---|
| 398 | else: # no matches |
|---|
| 399 | yield event |
|---|