Book details: Fix formatting of text when copying all book details in narrow mode

This commit is contained in:
Kovid Goyal 2023-05-09 16:32:18 +05:30
parent dcd5978707
commit 5993dca2f2
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -84,15 +84,22 @@ def copy_all(text_browser):
from html5_parser import parse from html5_parser import parse
from lxml import etree from lxml import etree
root = parse(html) root = parse(html)
for x in ('table', 'tr', 'tbody'): tables = tuple(root.iterdescendants('table'))
for tag in root.iterdescendants(x): for tag in root.iterdescendants(('table', 'tr', 'tbody')):
tag.tag = 'div' tag.tag = 'div'
for tag in root.iterdescendants('td'): parent = root
is_vertical = getattr(text_browser, 'vertical', True)
if not is_vertical:
parent = tables[1]
for tag in parent.iterdescendants('td'):
tt = etree.tostring(tag, method='text', encoding='unicode') tt = etree.tostring(tag, method='text', encoding='unicode')
tag.tag = 'span' tag.tag = 'span'
for child in tuple(tag): for child in tuple(tag):
tag.remove(child) tag.remove(child)
tag.text = tt.strip() tag.text = tt.strip()
if not is_vertical:
for tag in root.iterdescendants('td'):
tag.tag = 'div'
for tag in root.iterdescendants('a'): for tag in root.iterdescendants('a'):
tag.attrib.pop('href', None) tag.attrib.pop('href', None)
from calibre.utils.html2text import html2text from calibre.utils.html2text import html2text