annotate markup/filters.py @ 143:ef761afcedff

CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
author cmlenz
date Fri, 11 Aug 2006 14:08:13 +0000
parents b49cc51999b9
children 56d534eb53f9
rev   line source
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
1 # -*- coding: utf-8 -*-
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
2 #
66
822089ae65ce Switch copyright to Edgewall and URLs to markup.edgewall.org.
cmlenz
parents: 54
diff changeset
3 # Copyright (C) 2006 Edgewall Software
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
4 # All rights reserved.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
5 #
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
6 # This software is licensed as described in the file COPYING, which
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
7 # you should have received as part of this distribution. The terms
66
822089ae65ce Switch copyright to Edgewall and URLs to markup.edgewall.org.
cmlenz
parents: 54
diff changeset
8 # are also available at http://markup.edgewall.org/wiki/License.
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
9 #
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
10 # This software consists of voluntary contributions made by many
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
11 # individuals. For the exact contribution history, see the revision
66
822089ae65ce Switch copyright to Edgewall and URLs to markup.edgewall.org.
cmlenz
parents: 54
diff changeset
12 # history and logs, available at http://markup.edgewall.org/log/.
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
13
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
14 """Implementation of a number of stream filters."""
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
15
92
3b75c6730b29 More performance improvements... this time for whitespace normalization and template loops.
cmlenz
parents: 69
diff changeset
16 from itertools import chain
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
17 try:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
18 frozenset
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
19 except NameError:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
20 from sets import ImmutableSet as frozenset
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
21 import re
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
22
113
e815c2c07572 Removed the `sanitize()` method from the `Markup` class, and migrate the existing unit tests to `markup.tests.filters`. Provide a `Stream.filter()` method instead which can be used to conveniently apply a filter to a stream.
cmlenz
parents: 93
diff changeset
23 from markup.core import Attributes, Markup, Namespace, escape, stripentities
69
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
24 from markup.core import END, END_NS, START, START_NS, TEXT
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
25 from markup.path import Path
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
26
123
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
27 __all__ = ['HTMLSanitizer', 'IncludeFilter']
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
28
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
29
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
30 class HTMLSanitizer(object):
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
31 """A filter that removes potentially dangerous HTML tags and attributes
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
32 from the stream.
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
33 """
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
34
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
35 _SAFE_TAGS = frozenset(['a', 'abbr', 'acronym', 'address', 'area', 'b',
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
36 'big', 'blockquote', 'br', 'button', 'caption', 'center', 'cite',
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
37 'code', 'col', 'colgroup', 'dd', 'del', 'dfn', 'dir', 'div', 'dl', 'dt',
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
38 'em', 'fieldset', 'font', 'form', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
39 'hr', 'i', 'img', 'input', 'ins', 'kbd', 'label', 'legend', 'li', 'map',
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
40 'menu', 'ol', 'optgroup', 'option', 'p', 'pre', 'q', 's', 'samp',
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
41 'select', 'small', 'span', 'strike', 'strong', 'sub', 'sup', 'table',
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
42 'tbody', 'td', 'textarea', 'tfoot', 'th', 'thead', 'tr', 'tt', 'u',
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
43 'ul', 'var'])
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
44
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
45 _SAFE_ATTRS = frozenset(['abbr', 'accept', 'accept-charset', 'accesskey',
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
46 'action', 'align', 'alt', 'axis', 'bgcolor', 'border', 'cellpadding',
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
47 'cellspacing', 'char', 'charoff', 'charset', 'checked', 'cite', 'class',
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
48 'clear', 'cols', 'colspan', 'color', 'compact', 'coords', 'datetime',
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
49 'dir', 'disabled', 'enctype', 'for', 'frame', 'headers', 'height',
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
50 'href', 'hreflang', 'hspace', 'id', 'ismap', 'label', 'lang',
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
51 'longdesc', 'maxlength', 'media', 'method', 'multiple', 'name',
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
52 'nohref', 'noshade', 'nowrap', 'prompt', 'readonly', 'rel', 'rev',
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
53 'rows', 'rowspan', 'rules', 'scope', 'selected', 'shape', 'size',
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
54 'span', 'src', 'start', 'style', 'summary', 'tabindex', 'target',
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
55 'title', 'type', 'usemap', 'valign', 'value', 'vspace', 'width'])
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
56 _URI_ATTRS = frozenset(['action', 'background', 'dynsrc', 'href', 'lowsrc',
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
57 'src'])
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
58 _SAFE_SCHEMES = frozenset(['file', 'ftp', 'http', 'https', 'mailto', None])
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
59
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
60 def __call__(self, stream, ctxt=None):
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
61 waiting_for = None
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
62
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
63 for kind, data, pos in stream:
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
64 if kind is START:
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
65 if waiting_for:
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
66 continue
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
67 tag, attrib = data
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
68 if tag not in self._SAFE_TAGS:
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
69 waiting_for = tag
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
70 continue
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
71
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
72 new_attrib = Attributes()
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
73 for attr, value in attrib:
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
74 value = stripentities(value)
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
75 if attr not in self._SAFE_ATTRS:
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
76 continue
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
77 elif attr in self._URI_ATTRS:
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
78 # Don't allow URI schemes such as "javascript:"
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
79 if self._get_scheme(value) not in self._SAFE_SCHEMES:
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
80 continue
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
81 elif attr == 'style':
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
82 # Remove dangerous CSS declarations from inline styles
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
83 decls = []
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
84 for decl in filter(None, value.split(';')):
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
85 is_evil = False
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
86 if 'expression' in decl:
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
87 is_evil = True
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
88 for m in re.finditer(r'url\s*\(([^)]+)', decl):
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
89 if self._get_scheme(m.group(1)) not in self._SAFE_SCHEMES:
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
90 is_evil = True
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
91 break
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
92 if not is_evil:
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
93 decls.append(decl.strip())
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
94 if not decls:
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
95 continue
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
96 value = '; '.join(decls)
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
97 new_attrib.append((attr, value))
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
98
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
99 yield kind, (tag, new_attrib), pos
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
100
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
101 elif kind is END:
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
102 tag = data
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
103 if waiting_for:
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
104 if waiting_for == tag:
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
105 waiting_for = None
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
106 else:
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
107 yield kind, data, pos
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
108
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
109 else:
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
110 if not waiting_for:
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
111 yield kind, data, pos
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
112
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
113 def _get_scheme(self, text):
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
114 if ':' not in text:
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
115 return None
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
116 chars = [char for char in text.split(':', 1)[0] if char.isalnum()]
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
117 return ''.join(chars).lower()
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
118
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
119
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
120 class IncludeFilter(object):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
121 """Template filter providing (very) basic XInclude support
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
122 (see http://www.w3.org/TR/xinclude/) in templates.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
123 """
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
124
18
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
125 NAMESPACE = Namespace('http://www.w3.org/2001/XInclude')
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
126
17
ad63ad459524 Refactoring to address #6: all match templates are now processed by a single filter, which means that match templates added by included templates are properly applied. A side effect of this refactoring is that `Context` objects may not be reused across multiple template processing runs.
cmlenz
parents: 15
diff changeset
127 def __init__(self, loader):
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
128 """Initialize the filter.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
129
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
130 @param loader: the `TemplateLoader` to use for resolving references to
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
131 external template files
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
132 """
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
133 self.loader = loader
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
134
142
b49cc51999b9 Minor cleanup in XInclude filter.
cmlenz
parents: 123
diff changeset
135 def __call__(self, stream, ctxt=None):
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
136 """Filter the stream, processing any XInclude directives it may
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
137 contain.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
138
142
b49cc51999b9 Minor cleanup in XInclude filter.
cmlenz
parents: 123
diff changeset
139 @param stream: the markup event stream to filter
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
140 @param ctxt: the template context
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
141 """
12
87238328a71d Make the XInclude filter track namespace context, to enable it to omit `END_NS` events for the XInclude namespace.
cmlenz
parents: 10
diff changeset
142 from markup.template import Template, TemplateError, TemplateNotFound
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
143
142
b49cc51999b9 Minor cleanup in XInclude filter.
cmlenz
parents: 123
diff changeset
144 ns_prefixes = []
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
145 in_fallback = False
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
146 include_href, fallback_stream = None, None
69
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
147 namespace = self.NAMESPACE
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
148
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
149 for kind, data, pos in stream:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
150
69
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
151 if kind is START and not in_fallback and data[0] in namespace:
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
152 tag, attrib = data
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
153 if tag.localname == 'include':
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
154 include_href = attrib.get('href')
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
155 elif tag.localname == 'fallback':
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
156 in_fallback = True
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
157 fallback_stream = []
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
158
69
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
159 elif kind is END and data in namespace:
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
160 if data.localname == 'include':
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
161 try:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
162 if not include_href:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
163 raise TemplateError('Include misses required '
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
164 'attribute "href"')
21
eca77129518a * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents: 18
diff changeset
165 template = self.loader.load(include_href,
eca77129518a * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents: 18
diff changeset
166 relative_to=pos[0])
17
ad63ad459524 Refactoring to address #6: all match templates are now processed by a single filter, which means that match templates added by included templates are properly applied. A side effect of this refactoring is that `Context` objects may not be reused across multiple template processing runs.
cmlenz
parents: 15
diff changeset
167 for event in template.generate(ctxt):
ad63ad459524 Refactoring to address #6: all match templates are now processed by a single filter, which means that match templates added by included templates are properly applied. A side effect of this refactoring is that `Context` objects may not be reused across multiple template processing runs.
cmlenz
parents: 15
diff changeset
168 yield event
13
bf9de5a4c896 Match directives should now also be applied when included indirectly.
cmlenz
parents: 12
diff changeset
169
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
170 except TemplateNotFound:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
171 if fallback_stream is None:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
172 raise
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
173 for event in fallback_stream:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
174 yield event
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
175
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
176 include_href = None
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
177 fallback_stream = None
17
ad63ad459524 Refactoring to address #6: all match templates are now processed by a single filter, which means that match templates added by included templates are properly applied. A side effect of this refactoring is that `Context` objects may not be reused across multiple template processing runs.
cmlenz
parents: 15
diff changeset
178
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
179 elif data.localname == 'fallback':
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
180 in_fallback = False
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
181
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
182 elif in_fallback:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
183 fallback_stream.append((kind, data, pos))
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
184
69
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
185 elif kind is START_NS and data[1] == namespace:
12
87238328a71d Make the XInclude filter track namespace context, to enable it to omit `END_NS` events for the XInclude namespace.
cmlenz
parents: 10
diff changeset
186 ns_prefixes.append(data[0])
87238328a71d Make the XInclude filter track namespace context, to enable it to omit `END_NS` events for the XInclude namespace.
cmlenz
parents: 10
diff changeset
187
69
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
188 elif kind is END_NS and data in ns_prefixes:
12
87238328a71d Make the XInclude filter track namespace context, to enable it to omit `END_NS` events for the XInclude namespace.
cmlenz
parents: 10
diff changeset
189 ns_prefixes.pop()
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
190
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
191 else:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
192 yield kind, data, pos
Copyright (C) 2012-2017 Edgewall Software