comparison genshi/util.py @ 433:6d01e91f2a49

More API docs.
author cmlenz
date Thu, 22 Mar 2007 21:12:03 +0000
parents 5b248708bbed
children 4145dd84001e f0bb2c5ea0ff
comparison
equal deleted inserted replaced
432:3879c9ad3472 433:6d01e91f2a49
137 item.next = self.head 137 item.next = self.head
138 self.head.previous = self.head = item 138 self.head.previous = self.head = item
139 139
140 140
141 def flatten(items): 141 def flatten(items):
142 """Flattens a potentially nested sequence into a flat list: 142 """Flattens a potentially nested sequence into a flat list.
143
144 :param items: the sequence to flatten
143 145
144 >>> flatten((1, 2)) 146 >>> flatten((1, 2))
145 [1, 2] 147 [1, 2]
146 >>> flatten([1, (2, 3), 4]) 148 >>> flatten([1, (2, 3), 4])
147 [1, 2, 3, 4] 149 [1, 2, 3, 4]
157 return retval 159 return retval
158 160
159 def plaintext(text, keeplinebreaks=True): 161 def plaintext(text, keeplinebreaks=True):
160 """Returns the text as a `unicode` string with all entities and tags 162 """Returns the text as a `unicode` string with all entities and tags
161 removed. 163 removed.
164
165 >>> plaintext('<b>1 &lt; 2</b>')
166 u'1 < 2'
167
168 The `keeplinebreaks` parameter can be set to ``False`` to replace any line
169 breaks by simple spaces:
170
171 >>> plaintext('''<b>1
172 ... &lt;
173 ... 2</b>''', keeplinebreaks=False)
174 u'1 < 2'
175
176 :param text: the text to convert to plain text
177 :param keeplinebreaks: whether line breaks in the text should be kept intact
178 :return: the text with tags and entities removed
162 """ 179 """
163 text = stripentities(striptags(text)) 180 text = stripentities(striptags(text))
164 if not keeplinebreaks: 181 if not keeplinebreaks:
165 text = text.replace(u'\n', u' ') 182 text = text.replace(u'\n', u' ')
166 return text 183 return text
206 return ref 223 return ref
207 return _STRIPENTITIES_RE.sub(_replace_entity, text) 224 return _STRIPENTITIES_RE.sub(_replace_entity, text)
208 225
209 _STRIPTAGS_RE = re.compile(r'<[^>]*?>') 226 _STRIPTAGS_RE = re.compile(r'<[^>]*?>')
210 def striptags(text): 227 def striptags(text):
211 """Return a copy of the text with all XML/HTML tags removed. 228 """Return a copy of the text with any XML/HTML tags removed.
212 229
213 >>> striptags('<span>Foo</span> bar') 230 >>> striptags('<span>Foo</span> bar')
214 'Foo bar' 231 'Foo bar'
215 >>> striptags('<span class="bar">Foo</span>') 232 >>> striptags('<span class="bar">Foo</span>')
216 'Foo' 233 'Foo'
217 >>> striptags('Foo<br />') 234 >>> striptags('Foo<br />')
218 'Foo' 235 'Foo'
236
237 :param text: the string to remove tags from
238 :return: the text with tags removed
219 """ 239 """
220 return _STRIPTAGS_RE.sub('', text) 240 return _STRIPTAGS_RE.sub('', text)
Copyright (C) 2012-2017 Edgewall Software