Changeset 8 for formatflowed
- Timestamp:
- 09/05/05 13:20:18 (7 years ago)
- Files:
-
- 1 modified
-
formatflowed/trunk/formatflowed.py (modified) (14 diffs)
Legend:
- Unmodified
- Added
- Removed
-
formatflowed/trunk/formatflowed.py
r7 r8 16 16 import textwrap 17 17 18 __all__ = ['FormatFlowedDecoder', 'decode', 'convertToWrapped'] 18 __all__ = [ 19 'PARAGRAPH', 20 'FIXED', 21 'SIGNATURE_SEPARATOR', 22 'FormatFlowedDecoder', 23 'decode', 24 'convertToWrapped' 25 ] 26 27 # Constants denoting the various text chunk types recognized by format=flowed 28 PARAGRAPH, FIXED, SIGNATURE_SEPARATOR = range(3) 19 29 20 30 class FormatFlowedDecoder: … … 114 124 tuples. information is a dictionary with the following fields: 115 125 type 116 One of 'paragraph', 'fixed', 'signature-separator'126 One of PARAGRAPH, FIXED, SIGNATURE_SEPARATOR 117 127 quotedepth 118 128 Number of quotemarks found on the text chunk … … 120 130 chunk is a unicode string. All text is unwrapped and without any 121 131 quotemarks; when displaying these chunks, the appropriate quotemarks 122 should be added again, and chunks of type 'paragraph'should be123 displayed wrapped. Chunks of type 'fixed'should be displayed132 should be added again, and chunks of type PARAGRAPH should be 133 displayed wrapped. Chunks of type FIXED should be displayed 124 134 unwrapped. 125 135 … … 141 151 ... "Lewis Carroll"))) 142 152 >>> list(result) == [ 143 ... ({'quotedepth': 2, 'type': 'paragraph'},153 ... ({'quotedepth': 2, 'type': PARAGRAPH}, 144 154 ... u"`Take some more tea,' the March Hare said to Alice, " 145 155 ... u"very earnestly."), 146 ... ({'quotedepth': 1, 'type': 'fixed'}, u""),147 ... ({'quotedepth': 1, 'type': 'paragraph'},156 ... ({'quotedepth': 1, 'type': FIXED}, u""), 157 ... ({'quotedepth': 1, 'type': PARAGRAPH}, 148 158 ... u"`I've had nothing yet,' Alice replied in an offended " 149 159 ... u"tone, `so I can't take more.'"), 150 ... ({'quotedepth': 0, 'type': 'fixed'}, u""),151 ... ({'quotedepth': 0, 'type': 'paragraph'},160 ... ({'quotedepth': 0, 'type': FIXED}, u""), 161 ... ({'quotedepth': 0, 'type': PARAGRAPH}, 152 162 ... u"`You mean you can't take less,' said the Hatter: `it's " 153 163 ... u"very easy to take more than nothing.'"), 154 ... ({'quotedepth': 0, 'type': 'fixed'}, u""),155 ... ({'quotedepth': 0, 'type': 'signature-separator'}, u"-- "),156 ... ({'quotedepth': 0, 'type': 'fixed'}, u"Lewis Carroll")164 ... ({'quotedepth': 0, 'type': FIXED}, u""), 165 ... ({'quotedepth': 0, 'type': SIGNATURE_SEPARATOR}, u"-- "), 166 ... ({'quotedepth': 0, 'type': FIXED}, u"Lewis Carroll") 157 167 ... ] 158 168 True … … 169 179 ... "Depth zero paragraph with fixed line."))) 170 180 >>> list(result) == [ 171 ... ({'quotedepth': 1, 'type': 'paragraph'},181 ... ({'quotedepth': 1, 'type': PARAGRAPH}, 172 182 ... u"Depth one paragraph with flow space. "), 173 ... ({'quotedepth': 2, 'type': 'paragraph'},183 ... ({'quotedepth': 2, 'type': PARAGRAPH}, 174 184 ... u"Depth two paragraph with flow space. "), 175 ... ({'quotedepth': 0, 'type': 'fixed'},185 ... ({'quotedepth': 0, 'type': FIXED}, 176 186 ... u"Depth zero paragraph with fixed line.")] 177 187 True … … 183 193 ... "-- "))) 184 194 >>> list(result) == [ 185 ... ({'quotedepth': 0, 'type': 'paragraph'},195 ... ({'quotedepth': 0, 'type': PARAGRAPH}, 186 196 ... u"A paragraph with flow space. "), 187 ... ({'quotedepth': 0, 'type': 'signature-separator'}, u"-- ")]197 ... ({'quotedepth': 0, 'type': SIGNATURE_SEPARATOR}, u"-- ")] 188 198 True 189 199 … … 193 203 ... "A paragraph with flow space. ",))) 194 204 >>> list(result) == [ 195 ... ({'quotedepth': 0, 'type': 'paragraph'},205 ... ({'quotedepth': 0, 'type': PARAGRAPH}, 196 206 ... u"A paragraph with flow space. ")] 197 207 True … … 206 216 ... "break across the paragraph."))) 207 217 >>> list(result) == [ 208 ... ({'quotedepth': 0, 'type': 'paragraph'},218 ... ({'quotedepth': 0, 'type': PARAGRAPH}, 209 219 ... u'Contrived example with a word-break across the ' 210 220 ... u'paragraph.')] … … 222 232 ... "\xf7K"))) 223 233 >>> list(result) == [ 224 ... ({'quotedepth': 1, 'type': 'paragraph'},234 ... ({'quotedepth': 1, 'type': PARAGRAPH}, 225 235 ... u'This is a quoted paragraph encoded in cp037.')] 226 236 True … … 228 238 """ 229 239 para = u'' 230 pinfo = {'type': 'paragraph'}240 pinfo = {'type': PARAGRAPH} 231 241 for line in flowed.split('\r\n'): 232 242 line = line.decode(self.character_set) … … 238 248 # exception case: flowed line followed by sig-sep 239 249 yield (pinfo, para) 240 pinfo = {'type': 'paragraph'}250 pinfo = {'type': PARAGRAPH} 241 251 para = u'' 242 yield ({'type': 'signature-separator',252 yield ({'type': SIGNATURE_SEPARATOR, 243 253 'quotedepth': quotedepth}, line) 244 254 continue … … 248 258 # exception case: flowed line followed by quotedepth change 249 259 yield (pinfo, para) 250 pinfo = {'type': 'paragraph'}260 pinfo = {'type': PARAGRAPH} 251 261 para = u'' 252 262 para += self._stripflow(line) … … 259 269 # exception case: flowed line followed by quotedepth change 260 270 yield (pinfo, para) 261 pinfo = {'type': 'paragraph'}271 pinfo = {'type': PARAGRAPH} 262 272 para = u'' 263 273 else: 264 274 yield (pinfo, para + line) 265 pinfo = {'type': 'paragraph'}275 pinfo = {'type': PARAGRAPH} 266 276 para = u'' 267 277 continue 268 yield ({'type': 'fixed', 'quotedepth': quotedepth}, line)278 yield ({'type': FIXED, 'quotedepth': quotedepth}, line) 269 279 270 280 if para: … … 349 359 if quotemarker and quote[-1] != ' ': 350 360 quotemarker += ' ' 351 if type == 'fixed'and not wrap_fixed:361 if type == FIXED and not wrap_fixed: 352 362 result.append(quotemarker + chunk) 353 elif not chunk or type == 'signature-separator':363 elif not chunk or type == SIGNATURE_SEPARATOR: 354 364 result.append(quotemarker + chunk) 355 365 else:
