From 644f6701c6ee6a96d62c72c95d3ccf05fae70df8 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 23 Feb 2025 13:43:15 +0530 Subject: [PATCH] Only wrap leading whitespace if parent has no children --- src/calibre/ebooks/oeb/polish/kepubify.py | 21 +++++++++++-------- .../ebooks/oeb/polish/tests/kepubify.py | 5 +++-- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/calibre/ebooks/oeb/polish/kepubify.py b/src/calibre/ebooks/oeb/polish/kepubify.py index 1cda24b25c..d43ec67a67 100644 --- a/src/calibre/ebooks/oeb/polish/kepubify.py +++ b/src/calibre/ebooks/oeb/polish/kepubify.py @@ -164,23 +164,26 @@ def add_kobo_spans(inner, root_lang): paranum += 1 segnum = 0 increment_next_para = False - stripped = leading_whitespace_pat.sub('', text) or text - ws = None - if num := len(text) - len(stripped): - ws = text[:num] try: at = 0 if after_child is None else parent.index(after_child) + 1 except ValueError: # wrapped child at = parent.index(after_child.getparent()) + 1 + stripped = leading_whitespace_pat.sub('', text) + if not at and not stripped and not len(parent): + stripped = text + ws = None + if num := len(text) - len(stripped): + ws = text[:num] if at: parent[at-1].tail = ws else: parent.text = ws - for pos, sz in sentence_positions(stripped, lang): - s = kobo_span(parent) - s.text = stripped[pos:pos+sz] - parent.insert(at, s) - at += 1 + if stripped: + for pos, sz in sentence_positions(stripped, lang): + s = kobo_span(parent) + s.text = stripped[pos:pos+sz] + parent.insert(at, s) + at += 1 def wrap_child(child: etree.Element) -> etree.Element: nonlocal increment_next_para, paranum, segnum diff --git a/src/calibre/ebooks/oeb/polish/tests/kepubify.py b/src/calibre/ebooks/oeb/polish/tests/kepubify.py index 11d51f9b65..a442ed56b5 100644 --- a/src/calibre/ebooks/oeb/polish/tests/kepubify.py +++ b/src/calibre/ebooks/oeb/polish/tests/kepubify.py @@ -59,8 +59,9 @@ div#book-inner {{ margin-top: 0; margin-bottom: 0; }}