annotate markup/filters.py @ 187:29a19af7d17a stable-0.2.x 0.2.0

Prepare [milestone:0.2] release.
author cmlenz
date Tue, 22 Aug 2006 15:28:35 +0000
parents 41db0260ebb1
children
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
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
16 try:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
17 frozenset
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
18 except NameError:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
19 from sets import ImmutableSet as frozenset
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
20 import re
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
21
182
41db0260ebb1 Renamed `Attributes` to `Attrs` to reduce the verbosity.
cmlenz
parents: 145
diff changeset
22 from markup.core import Attrs, Namespace, stripentities
145
56d534eb53f9 * Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents: 142
diff changeset
23 from markup.core import END, END_NS, START, START_NS
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
24
123
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
25 __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
26
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
27
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
28 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
29 """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
30 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
31 """
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
32
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
33 _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
34 '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
35 '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
36 '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
37 '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
38 '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
39 '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
40 '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
41 'ul', 'var'])
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
42
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
43 _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
44 '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
45 '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
46 '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
47 '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
48 '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
49 '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
50 '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
51 '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
52 '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
53 '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
54 _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
55 'src'])
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
56 _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
57
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
58 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
59 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
60
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
61 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
62 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
63 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
64 continue
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
65 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
66 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
67 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
68 continue
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
69
182
41db0260ebb1 Renamed `Attributes` to `Attrs` to reduce the verbosity.
cmlenz
parents: 145
diff changeset
70 new_attrib = Attrs()
123
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
71 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
72 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
73 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
74 continue
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
75 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
76 # 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
77 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
78 continue
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
79 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
80 # 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
81 decls = []
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
82 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
83 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
84 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
85 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
86 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
87 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
88 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
89 break
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
90 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
91 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
92 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
93 continue
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
94 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
95 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
96
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
97 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
98
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
99 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
100 tag = data
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
101 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
102 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
103 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
104 else:
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
105 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
106
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
107 else:
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
108 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
109 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
110
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
111 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
112 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
113 return None
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 113
diff changeset
114 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
115 return ''.join(chars).lower()
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
116
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
117
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
118 class IncludeFilter(object):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
119 """Template filter providing (very) basic XInclude support
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
120 (see http://www.w3.org/TR/xinclude/) in templates.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
121 """
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
122
18
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
123 NAMESPACE = Namespace('http://www.w3.org/2001/XInclude')
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
124
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
125 def __init__(self, loader):
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
126 """Initialize the filter.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
127
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
128 @param loader: the `TemplateLoader` to use for resolving references to
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
129 external template files
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
130 """
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
131 self.loader = loader
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
132
142
b49cc51999b9 Minor cleanup in XInclude filter.
cmlenz
parents: 123
diff changeset
133 def __call__(self, stream, ctxt=None):
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
134 """Filter the stream, processing any XInclude directives it may
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
135 contain.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
136
142
b49cc51999b9 Minor cleanup in XInclude filter.
cmlenz
parents: 123
diff changeset
137 @param stream: the markup event stream to filter
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
138 @param ctxt: the template context
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
139 """
145
56d534eb53f9 * Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents: 142
diff changeset
140 from markup.template import TemplateError, TemplateNotFound
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
141
142
b49cc51999b9 Minor cleanup in XInclude filter.
cmlenz
parents: 123
diff changeset
142 ns_prefixes = []
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
143 in_fallback = False
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
144 include_href, fallback_stream = None, None
69
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
145 namespace = self.NAMESPACE
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
146
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
147 for kind, data, pos in stream:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
148
69
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
149 if kind is START and not in_fallback and data[0] in namespace:
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
150 tag, attrib = data
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
151 if tag.localname == 'include':
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
152 include_href = attrib.get('href')
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
153 elif tag.localname == 'fallback':
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
154 in_fallback = True
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
155 fallback_stream = []
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
156
69
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
157 elif kind is END and data in namespace:
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
158 if data.localname == 'include':
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
159 try:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
160 if not include_href:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
161 raise TemplateError('Include misses required '
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
162 'attribute "href"')
21
eca77129518a * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents: 18
diff changeset
163 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
164 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
165 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
166 yield event
13
bf9de5a4c896 Match directives should now also be applied when included indirectly.
cmlenz
parents: 12
diff changeset
167
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
168 except TemplateNotFound:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
169 if fallback_stream is None:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
170 raise
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
171 for event in fallback_stream:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
172 yield event
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
173
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
174 include_href = None
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
175 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
176
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
177 elif data.localname == 'fallback':
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
178 in_fallback = False
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
179
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
180 elif in_fallback:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
181 fallback_stream.append((kind, data, pos))
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
182
69
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
183 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
184 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
185
69
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
186 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
187 ns_prefixes.pop()
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
188
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
189 else:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
190 yield kind, data, pos
Copyright (C) 2012-2017 Edgewall Software