|
2 | 2 | # a library that identifies quoted text in email messages
|
3 | 3 |
|
4 | 4 | from . import _internal, _patterns
|
| 5 | +from ._enums import Position |
5 | 6 |
|
6 |
| -__version__ = "0.3.1" |
| 7 | +__version__ = "0.4.0" |
7 | 8 | __all__ = ["quote", "quote_html", "unwrap", "unwrap_html"]
|
8 | 9 |
|
9 | 10 |
|
10 |
| -def quote(text: str, limit: int = 1000) -> list[tuple[bool, str]]: |
| 11 | +def quote( |
| 12 | + text: str, *, limit: int = 1000, quote_intro_line: bool = False |
| 13 | +) -> list[tuple[bool, str]]: |
11 | 14 | """
|
12 |
| - Take a plain text message as an argument, return a list of tuples. The |
13 |
| - first argument of the tuple denotes whether the text should be expanded by |
14 |
| - default. The second argument is the unmodified corresponding text. |
15 |
| -
|
16 |
| - Example: [(True, 'expanded text'), (False, '> Some quoted text')] |
17 |
| -
|
18 |
| - Unless the limit param is set to None, the text will automatically be |
19 |
| - quoted starting at the line where the limit is reached. |
| 15 | + Divide email body into quoted parts. |
| 16 | +
|
| 17 | + Args: |
| 18 | + text: Plain text message. |
| 19 | + limit: If set, the text will automatically be quoted starting at the |
| 20 | + line where the limit is reached. |
| 21 | + quote_intro_line: Whether the line introducing the quoted text ("On ... |
| 22 | + wrote:" / "Begin forwarded message:") should be part of the quoted |
| 23 | + text. |
| 24 | +
|
| 25 | + Returns: |
| 26 | + List of tuples: The first argument of the tuple denotes whether the |
| 27 | + text should be expanded by default. The second argument is the |
| 28 | + unmodified corresponding text. |
| 29 | +
|
| 30 | + Example: [(True, 'expanded text'), (False, '> Some quoted text')] |
20 | 31 | """
|
21 | 32 | lines = text.split("\n")
|
22 | 33 |
|
| 34 | + position = Position.Begin if quote_intro_line else Position.End |
23 | 35 | found = _internal.find_quote_position(
|
24 |
| - lines, _patterns.MAX_WRAP_LINES, limit |
| 36 | + lines, |
| 37 | + _patterns.MAX_WRAP_LINES, |
| 38 | + limit=limit, |
| 39 | + position=position, |
25 | 40 | )
|
26 | 41 |
|
27 |
| - if found is not None: |
28 |
| - return [ |
29 |
| - (True, "\n".join(lines[: found + 1])), |
30 |
| - (False, "\n".join(lines[found + 1 :])), |
31 |
| - ] |
| 42 | + if found is None: |
| 43 | + return [(True, text)] |
32 | 44 |
|
33 |
| - return [(True, text)] |
| 45 | + split_idx = found if quote_intro_line else found + 1 |
| 46 | + return [ |
| 47 | + (True, "\n".join(lines[:split_idx])), |
| 48 | + (False, "\n".join(lines[split_idx:])), |
| 49 | + ] |
34 | 50 |
|
35 | 51 |
|
36 |
| -def quote_html(html: str, limit: int = 1000) -> list[tuple[bool, str]]: |
| 52 | +def quote_html( |
| 53 | + html: str, *, limit: int = 1000, quote_intro_line: bool = False |
| 54 | +) -> list[tuple[bool, str]]: |
37 | 55 | """
|
38 |
| - Like quote(), but takes an HTML message as an argument. The limit param |
39 |
| - represents the maximum number of lines to traverse until quoting the rest |
40 |
| - of the markup. Lines are separated by block elements or <br>. |
| 56 | + Like quote(), but takes an HTML message as an argument. |
| 57 | +
|
| 58 | + Args: |
| 59 | + html: HTML message. |
| 60 | + limit: Maximum number of lines to traverse until quoting the rest of |
| 61 | + the markup. Lines are separated by block elements or <br>. |
| 62 | + quote_intro_line: Whether the line introducing the quoted text ("On ... |
| 63 | + wrote:" / "Begin forwarded message:") should be part of the quoted |
| 64 | + text. |
41 | 65 | """
|
42 | 66 | from . import _html
|
43 | 67 |
|
44 | 68 | tree = _html.get_html_tree(html)
|
45 | 69 |
|
46 | 70 | start_refs, end_refs, lines = _html.get_line_info(tree, limit + 1)
|
47 | 71 |
|
48 |
| - found = _internal.find_quote_position(lines, 1, limit) |
| 72 | + position = Position.Begin if quote_intro_line else Position.End |
| 73 | + found = _internal.find_quote_position( |
| 74 | + lines, 1, limit=limit, position=position |
| 75 | + ) |
49 | 76 |
|
50 | 77 | if found is None:
|
51 | 78 | # No quoting found and we're below limit. We're done.
|
52 | 79 | return [(True, _html.render_html_tree(tree))]
|
53 | 80 |
|
| 81 | + split_idx = found if quote_intro_line else found + 1 |
54 | 82 | start_tree = _html.slice_tree(
|
55 |
| - tree, start_refs, end_refs, (0, found + 1), html_copy=html |
| 83 | + tree, start_refs, end_refs, (0, split_idx), html_copy=html |
56 | 84 | )
|
57 |
| - end_tree = _html.slice_tree(tree, start_refs, end_refs, (found + 1, None)) |
| 85 | + end_tree = _html.slice_tree(tree, start_refs, end_refs, (split_idx, None)) |
58 | 86 |
|
59 | 87 | return [
|
60 | 88 | (True, _html.render_html_tree(start_tree)),
|
|
0 commit comments