From c6f8871130526ced11a4adec273b374092cde1e9 Mon Sep 17 00:00:00 2001 From: Robin Whittleton Date: Tue, 26 Dec 2023 12:31:11 +0100 Subject: [PATCH] Allow non-breaking spaces to tie together title blocks in draft creation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit For example, this allows “Mrs. Seacole” to be entered as “Mrs. Seacole” to be kept on the same line during title generation. --- se/commands/create_draft.py | 3 ++- se/formatting.py | 8 ++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/se/commands/create_draft.py b/se/commands/create_draft.py index 5ec53de3..8d964963 100644 --- a/se/commands/create_draft.py +++ b/se/commands/create_draft.py @@ -94,7 +94,8 @@ def _get_word_widths(string: str, target_height: int) -> list: """ words = [] - for word in reversed(string.split()): + # Forcing a split on " " means that we can use non-breaking spaces to tie together blocks (e.g. Mrs. Seacole) + for word in reversed(string.strip().split(" ")): width = 0 for char in word: diff --git a/se/formatting.py b/se/formatting.py index 9335ba77..e99cb2dc 100644 --- a/se/formatting.py +++ b/se/formatting.py @@ -1128,8 +1128,8 @@ def titlecase(text: str) -> str: # So, convert to all lowercase first. text = text.lower() - # Replace all white space except hair space with a space character - text = regex.sub(fr"[^\S{se.HAIR_SPACE}]+", " ", text) + # Replace all white space except hair spaces and non-breaking spaces with a space character + text = regex.sub(fr"[^\S{se.HAIR_SPACE}{se.NO_BREAK_SPACE}]+", " ", text) text = pip_titlecase(text) @@ -1259,6 +1259,10 @@ def titlecase(text: str) -> str: # Like `Will-o’-the-Wisp` text = regex.sub(r"(?<=-)(O’|The)-", lambda result: result.group(1).lower() + "-", text) + # Fix non-breaking spaces - we can assume that they’re intentionally used in names + # If `titlecase` is fixed we can remove this, see https://github.com/ppannuto/python-titlecase/issues/95 + text = regex.sub(fr"{se.NO_BREAK_SPACE}([a-z])", lambda result: " " + result.group(1).upper(), text) + return text def make_url_safe(text: str) -> str: