From 28c1cb4711b1f957591cbfc4f29287334f3bebcb Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 24 Mar 2025 11:39:51 +0530 Subject: [PATCH] KEPUB Output: Include leading whitespace in kobo sentence span tags. This prevents unsightly breaks when highlighting across formatted text on the Kobo. Fixes #2103926 [Highlighting space after italicized words on Kobo device](https://bugs.launchpad.net/calibre/+bug/2103926) --- src/calibre/ebooks/oeb/polish/kepubify.py | 10 ++++++---- src/calibre/ebooks/oeb/polish/tests/kepubify.py | 16 ++++++++-------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/calibre/ebooks/oeb/polish/kepubify.py b/src/calibre/ebooks/oeb/polish/kepubify.py index aa1b811a42..185c596908 100644 --- a/src/calibre/ebooks/oeb/polish/kepubify.py +++ b/src/calibre/ebooks/oeb/polish/kepubify.py @@ -180,14 +180,16 @@ def add_kobo_spans(inner, root_lang): ws = None if num := len(text) - len(stripped): ws = text[:num] + before = None if stripped else ws if at: - parent[at-1].tail = ws + parent[at-1].tail = before else: - parent.text = ws + parent.text = before if stripped: - for pos, sz in sentence_positions(stripped, lang): + text = (ws + stripped) if ws else stripped + for pos, sz in sentence_positions(text, lang): s = kobo_span(parent) - s.text = stripped[pos:pos+sz] + s.text = text[pos:pos+sz] parent.insert(at, s) at += 1 diff --git a/src/calibre/ebooks/oeb/polish/tests/kepubify.py b/src/calibre/ebooks/oeb/polish/tests/kepubify.py index bca3e32d78..8e42d85f2e 100644 --- a/src/calibre/ebooks/oeb/polish/tests/kepubify.py +++ b/src/calibre/ebooks/oeb/polish/tests/kepubify.py @@ -68,8 +68,8 @@ div#book-inner {{ margin-top: 0; margin-bottom: 0; }} with tail': - '
Script: with tail
', + '
Script: with tail
', '
Svg: mouse no tail': '
Svg: mouse' - ' no tail
', + ' no tail
', # encoding quirks '

A\xa0nbsp; ':