Fix clickable parent Part navigation in Calibre
This commit is contained in:
+17
-1
@@ -14,14 +14,30 @@ def parse_html(content):
|
||||
return BeautifulSoup(re.sub(r'^\s*<\?xml[^>]*\?>', '', content), 'html5lib')
|
||||
|
||||
|
||||
def normalize_navigation_markup(soup):
|
||||
"""Remove only our generated wrappers around nested NAV labels.
|
||||
|
||||
Reader TOC parsers expect li > a + ol. A generated li > span > a
|
||||
retains a valid href but is interpreted as an unlinked section by Calibre.
|
||||
This also upgrades source structures cached by earlier versions.
|
||||
"""
|
||||
for wrapper in list(soup.find_all('span', attrs={'data-translation-unit': 'true'})):
|
||||
parent = wrapper.parent
|
||||
if (parent and parent.name == 'li' and wrapper.find_parent('nav')
|
||||
and parent.find('ol', recursive=False)
|
||||
and wrapper.find('a', href=True, recursive=False)):
|
||||
wrapper.unwrap()
|
||||
|
||||
|
||||
def translation_targets(soup):
|
||||
"""Choose disjoint leaf blocks; wrap mixed direct text without renaming containers.
|
||||
|
||||
A li containing p must never be extracted twice. Text before a nested list or
|
||||
inside an otherwise bare body still needs its own unit.
|
||||
"""
|
||||
normalize_navigation_markup(soup)
|
||||
for container in list(soup.find_all(list(TARGET_TAGS | {'body'}))):
|
||||
if container.find_parent(list(SKIP_TAGS)):
|
||||
if container.find_parent(list(SKIP_TAGS)) or container.find_parent('nav'):
|
||||
continue
|
||||
has_blocks = container.find(list(BLOCK_TAGS)) is not None
|
||||
if container.name != 'body' and not has_blocks:
|
||||
|
||||
Reference in New Issue
Block a user