comparison genshi/util.py @ 820:1837f39efd6f experimental-inline

Sync (old) experimental inline branch with trunk@1027.
author cmlenz
date Wed, 11 Mar 2009 17:51:06 +0000
parents 0742f421caba
children 09cc3627654c
comparison
equal deleted inserted replaced
500:0742f421caba 820:1837f39efd6f
150 >>> flatten([1, (2, [3, 4]), 5]) 150 >>> flatten([1, (2, [3, 4]), 5])
151 [1, 2, 3, 4, 5] 151 [1, 2, 3, 4, 5]
152 """ 152 """
153 retval = [] 153 retval = []
154 for item in items: 154 for item in items:
155 if isinstance(item, (list, tuple)): 155 if isinstance(item, (frozenset, list, set, tuple)):
156 retval += flatten(item) 156 retval += flatten(item)
157 else: 157 else:
158 retval.append(item) 158 retval.append(item)
159 return retval 159 return retval
160 160
196 >>> stripentities('…') 196 >>> stripentities('…')
197 u'\u2026' 197 u'\u2026'
198 198
199 If the `keepxmlentities` parameter is provided and is a truth value, the 199 If the `keepxmlentities` parameter is provided and is a truth value, the
200 core XML entities (&, ', >, < and ") are left intact. 200 core XML entities (&, ', >, < and ") are left intact.
201 201
202 >>> stripentities('1 < 2 …', keepxmlentities=True) 202 >>> stripentities('1 < 2 …', keepxmlentities=True)
203 u'1 < 2 \u2026' 203 u'1 < 2 \u2026'
204 """ 204 """
205 def _replace_entity(match): 205 def _replace_entity(match):
206 if match.group(1): # numeric entity 206 if match.group(1): # numeric entity
221 return u'&%s;' % ref 221 return u'&%s;' % ref
222 else: 222 else:
223 return ref 223 return ref
224 return _STRIPENTITIES_RE.sub(_replace_entity, text) 224 return _STRIPENTITIES_RE.sub(_replace_entity, text)
225 225
226 _STRIPTAGS_RE = re.compile(r'<[^>]*?>') 226 _STRIPTAGS_RE = re.compile(r'(<!--.*?-->|<[^>]*>)')
227 def striptags(text): 227 def striptags(text):
228 """Return a copy of the text with any XML/HTML tags removed. 228 """Return a copy of the text with any XML/HTML tags removed.
229 229
230 >>> striptags('<span>Foo</span> bar') 230 >>> striptags('<span>Foo</span> bar')
231 'Foo bar' 231 'Foo bar'
232 >>> striptags('<span class="bar">Foo</span>') 232 >>> striptags('<span class="bar">Foo</span>')
233 'Foo' 233 'Foo'
234 >>> striptags('Foo<br />') 234 >>> striptags('Foo<br />')
235 'Foo' 235 'Foo'
236 236
237 HTML/XML comments are stripped, too:
238
239 >>> striptags('<!-- <blub>hehe</blah> -->test')
240 'test'
241
237 :param text: the string to remove tags from 242 :param text: the string to remove tags from
238 :return: the text with tags removed 243 :return: the text with tags removed
239 """ 244 """
240 return _STRIPTAGS_RE.sub('', text) 245 return _STRIPTAGS_RE.sub('', text)
Copyright (C) 2012-2017 Edgewall Software