|
45 | 45 | INDEX_NEXT_LINE_MARK = "IndexLineStart" |
46 | 46 | WRAP_END_MARK = "WrapSectionEnd" |
47 | 47 | PAGEMARK_PIN = "\x7f" # Temp char to pin page mark locations |
| 48 | +# Temp chars to replace non-breaking space are from Unicode Private Use Area, |
| 49 | +# so we know they won't appear in any of our books |
| 50 | +NBS_DICT = {"\xa0": "\ue123", "\u2007": "\ue124", "\u202f": "\ue125"} |
48 | 51 | PAGEMARK_PREFIX = "Pg" |
49 | 52 | REPLACE_END_MARK = "ReplaceEnd" |
50 | 53 | SELECTION_MARK_START = "SelectionMarkStart" |
@@ -3570,25 +3573,35 @@ def wrap_paragraph( |
3570 | 3573 | wrap_params: Wrapping parameters. |
3571 | 3574 | wrapper: TextWrapper object to perform the wrapping - re-used for efficiency. |
3572 | 3575 | """ |
| 3576 | + |
3573 | 3577 | if paragraph.startswith(" *" * 5): |
3574 | 3578 | return |
3575 | 3579 | # Remove leading/trailing space |
3576 | 3580 | paragraph = paragraph.strip() |
3577 | | - # Replace all multiple whitespace with single space |
3578 | | - paragraph = re.sub(r"\s+", " ", paragraph) |
| 3581 | + # Replace all multiple space/newline with single space |
| 3582 | + paragraph = re.sub(r"[\n ]+", " ", paragraph) |
3579 | 3583 | # Don't want pagemark pins to trap spaces around them, so... |
3580 | 3584 | # Remove space between pagemark pins |
3581 | 3585 | paragraph = re.sub(rf"(?<={PAGEMARK_PIN}) (?={PAGEMARK_PIN})", "", paragraph) |
3582 | 3586 | # Remove space after pagemark pins if space (or linestart) before |
3583 | 3587 | paragraph = re.sub(rf"(( |^){PAGEMARK_PIN}+) ", r"\1", paragraph) |
3584 | 3588 | # Remove space before pagemark pins if space (or lineend) after |
3585 | 3589 | paragraph = re.sub(rf" ({PAGEMARK_PIN}+( |$)) ", r"\1", paragraph) |
| 3590 | + # Convert non-breaking spaces to temporary non-space characters |
| 3591 | + # to stop the wrapper from putting a break there |
| 3592 | + for nbs, temp in NBS_DICT.items(): |
| 3593 | + paragraph = paragraph.replace(nbs, temp) |
3586 | 3594 |
|
3587 | 3595 | wrapper.width = wrap_params.right |
3588 | 3596 | wrapper.initial_indent = wrap_params.first * " " |
3589 | 3597 | wrapper.subsequent_indent = wrap_params.left * " " |
3590 | 3598 |
|
3591 | 3599 | wrapped = wrapper.fill(paragraph) |
| 3600 | + |
| 3601 | + # Restore non-breaking spaces |
| 3602 | + for nbs, temp in NBS_DICT.items(): |
| 3603 | + wrapped = wrapped.replace(temp, nbs) |
| 3604 | + |
3592 | 3605 | self.delete(paragraph_start, paragraph_end) |
3593 | 3606 | self.insert(paragraph_start, wrapped + "\n") |
3594 | 3607 |
|
|
0 commit comments