Only wrap leading whitespace if parent has no children

This commit is contained in:
Kovid Goyal 2025-02-23 13:43:15 +05:30
parent 4ff64c0b21
commit 644f6701c6
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 15 additions and 11 deletions

View File

@ -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

View File

@ -59,8 +59,9 @@ div#book-inner {{ margin-top: 0; margin-bottom: 0; }}</style><script type="text/
suffix = '</div></div></body></html>'
for src, expected in {
# basics
'<p>one</p><p>\xa0</p>':
'<p><span class="koboSpan" id="kobo.1.1">one</span></p><p><span class="koboSpan" id="kobo.2.1">&#160;</span></p>',
'<p>one</p> <p>\xa0</p><p>\xa0<i>a</i></p>':
'<p><span class="koboSpan" id="kobo.1.1">one</span></p> <p><span class="koboSpan" id="kobo.2.1">&#160;</span></p>'
'<p>&#160;<i><span class="koboSpan" id="kobo.3.1">a</span></i></p>',
'<p>Simple sentences. In a single paragraph.'
'<p>A sentence <i>with <b>nested</b>, tailed</i> formatting. Another.':