comparison markup/core.py @ 123:93bbdcf9428b

Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
author cmlenz
date Thu, 03 Aug 2006 14:49:22 +0000
parents 88ac4c680120
children b9a0031d4bbb
comparison
equal deleted inserted replaced
122:441545dbed4a 123:93bbdcf9428b
62 self.events = events 62 self.events = events
63 63
64 def __iter__(self): 64 def __iter__(self):
65 return iter(self.events) 65 return iter(self.events)
66 66
67 def filter(self, func): 67 def filter(self, *filters):
68 """Apply a filter to the stream. 68 """Apply filters to the stream.
69 69
70 This method returns a new stream with the given filter applied. The 70 This method returns a new stream with the given filters applied. The
71 filter must be a callable that accepts the stream object as parameter, 71 filters must be callables that accept the stream object as parameter,
72 and returns the filtered stream. 72 and return the filtered stream.
73 """ 73 """
74 return Stream(func(self)) 74 stream = self
75 75 for filter_ in filters:
76 def render(self, method='xml', encoding='utf-8', filters=None, **kwargs): 76 stream = filter_(iter(stream))
77 return Stream(stream)
78
79 def render(self, method='xml', encoding='utf-8', **kwargs):
77 """Return a string representation of the stream. 80 """Return a string representation of the stream.
78 81
79 @param method: determines how the stream is serialized; can be either 82 @param method: determines how the stream is serialized; can be either
80 "xml", "xhtml", or "html", or a custom `Serializer` 83 "xml", "xhtml", or "html", or a custom `Serializer`
81 subclass 84 subclass
83 `None`, this method returns a `unicode` object 86 `None`, this method returns a `unicode` object
84 87
85 Any additional keyword arguments are passed to the serializer, and thus 88 Any additional keyword arguments are passed to the serializer, and thus
86 depend on the `method` parameter value. 89 depend on the `method` parameter value.
87 """ 90 """
88 generator = self.serialize(method=method, filters=filters, **kwargs) 91 generator = self.serialize(method=method, **kwargs)
89 output = u''.join(list(generator)) 92 output = u''.join(list(generator))
90 if encoding is not None: 93 if encoding is not None:
91 return output.encode(encoding) 94 return output.encode(encoding)
92 return output 95 return output
93 96
98 @param path: a string containing the XPath expression 101 @param path: a string containing the XPath expression
99 """ 102 """
100 from markup.path import Path 103 from markup.path import Path
101 return Path(path).select(self) 104 return Path(path).select(self)
102 105
103 def serialize(self, method='xml', filters=None, **kwargs): 106 def serialize(self, method='xml', **kwargs):
104 """Generate strings corresponding to a specific serialization of the 107 """Generate strings corresponding to a specific serialization of the
105 stream. 108 stream.
106 109
107 Unlike the `render()` method, this method is a generator that returns 110 Unlike the `render()` method, this method is a generator that returns
108 the serialized output incrementally, as opposed to returning a single 111 the serialized output incrementally, as opposed to returning a single
109 string. 112 string.
110 113
111 @param method: determines how the stream is serialized; can be either 114 @param method: determines how the stream is serialized; can be either
112 "xml", "xhtml", or "html", or a custom `Serializer` 115 "xml", "xhtml", or "html", or a custom serializer class
113 subclass 116 """
114 @param filters: list of filters to apply to the stream before
115 serialization. The default is to apply whitespace
116 reduction using `markup.filters.WhitespaceFilter`.
117 """
118 from markup.filters import WhitespaceFilter
119 from markup import output 117 from markup import output
120 cls = method 118 cls = method
121 if isinstance(method, basestring): 119 if isinstance(method, basestring):
122 cls = {'xml': output.XMLSerializer, 120 cls = {'xml': output.XMLSerializer,
123 'xhtml': output.XHTMLSerializer, 121 'xhtml': output.XHTMLSerializer,
124 'html': output.HTMLSerializer}[method] 122 'html': output.HTMLSerializer}[method]
125 else: 123 serialize = cls(**kwargs)
126 assert issubclass(cls, output.Serializer) 124 return serialize(_ensure(self))
127 serializer = cls(**kwargs)
128
129 stream = _ensure(self)
130 if filters is None:
131 filters = [WhitespaceFilter()]
132 for filter_ in filters:
133 stream = filter_(iter(stream))
134
135 return serializer.serialize(stream)
136 125
137 def __str__(self): 126 def __str__(self):
138 return self.render() 127 return self.render()
139 128
140 def __unicode__(self): 129 def __unicode__(self):
Copyright (C) 2012-2017 Edgewall Software