mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-06-23 15:30:45 -04:00
Only wrap leading whitespace if parent has no children
This commit is contained in:
parent
4ff64c0b21
commit
644f6701c6
@ -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
|
||||
|
@ -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"> </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"> </span></p>'
|
||||
'<p> <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.':
|
||||
|
Loading…
x
Reference in New Issue
Block a user