Strip leading email quotation characters ('>'). Removes any combination of leading '>' interspersed with whitespace that appears *identically* in all lines of the input text. Parameters ---------- text : str Examples -------- Simple uses:: In [2]: strip_e
(text)
| 426 | |
| 427 | |
| 428 | def strip_email_quotes(text): |
| 429 | """Strip leading email quotation characters ('>'). |
| 430 | |
| 431 | Removes any combination of leading '>' interspersed with whitespace that |
| 432 | appears *identically* in all lines of the input text. |
| 433 | |
| 434 | Parameters |
| 435 | ---------- |
| 436 | text : str |
| 437 | |
| 438 | Examples |
| 439 | -------- |
| 440 | |
| 441 | Simple uses:: |
| 442 | |
| 443 | In [2]: strip_email_quotes('> > text') |
| 444 | Out[2]: 'text' |
| 445 | |
| 446 | In [3]: strip_email_quotes('> > text\\n> > more') |
| 447 | Out[3]: 'text\\nmore' |
| 448 | |
| 449 | Note how only the common prefix that appears in all lines is stripped:: |
| 450 | |
| 451 | In [4]: strip_email_quotes('> > text\\n> > more\\n> more...') |
| 452 | Out[4]: '> text\\n> more\\nmore...' |
| 453 | |
| 454 | So if any line has no quote marks ('>') , then none are stripped from any |
| 455 | of them :: |
| 456 | |
| 457 | In [5]: strip_email_quotes('> > text\\n> > more\\nlast different') |
| 458 | Out[5]: '> > text\\n> > more\\nlast different' |
| 459 | """ |
| 460 | lines = text.splitlines() |
| 461 | matches = set() |
| 462 | for line in lines: |
| 463 | prefix = re.match(r'^(\s*>[ >]*)', line) |
| 464 | if prefix: |
| 465 | matches.add(prefix.group(1)) |
| 466 | else: |
| 467 | break |
| 468 | else: |
| 469 | prefix = long_substr(list(matches)) |
| 470 | if prefix: |
| 471 | strip = len(prefix) |
| 472 | text = '\n'.join([ ln[strip:] for ln in lines]) |
| 473 | return text |
| 474 | |
| 475 | def strip_ansi(source): |
| 476 | """ |
no test coverage detected