mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Book details: Improve plain text formatting when copying to clipboard. Fixes #1993753 [Copy Book Details puts column names and values on separate lines](https://bugs.launchpad.net/calibre/+bug/1993753)
This commit is contained in:
parent
608b328f25
commit
9aea6019e1
@ -69,8 +69,28 @@ def copy_all(text_browser):
|
|||||||
mf = getattr(text_browser, 'details', text_browser)
|
mf = getattr(text_browser, 'details', text_browser)
|
||||||
c = QApplication.clipboard()
|
c = QApplication.clipboard()
|
||||||
md = QMimeData()
|
md = QMimeData()
|
||||||
md.setText(mf.toPlainText())
|
html = mf.toHtml()
|
||||||
md.setHtml(mf.toHtml())
|
md.setHtml(html)
|
||||||
|
from html5_parser import parse
|
||||||
|
from lxml import etree
|
||||||
|
root = parse(html)
|
||||||
|
for x in ('table', 'tr', 'tbody'):
|
||||||
|
for tag in root.iterdescendants(x):
|
||||||
|
tag.tag = 'div'
|
||||||
|
for tag in root.iterdescendants('td'):
|
||||||
|
tt = etree.tostring(tag, method='text', encoding='unicode')
|
||||||
|
tag.tag = 'span'
|
||||||
|
for child in tuple(tag):
|
||||||
|
tag.remove(child)
|
||||||
|
tag.text = tt.strip()
|
||||||
|
for tag in root.iterdescendants('a'):
|
||||||
|
tag.attrib.pop('href', None)
|
||||||
|
from calibre.utils.html2text import html2text
|
||||||
|
simplified_html = etree.tostring(root, encoding='unicode')
|
||||||
|
txt = html2text(simplified_html, single_line_break=True).strip()
|
||||||
|
# print(simplified_html)
|
||||||
|
# print(txt)
|
||||||
|
md.setText(txt)
|
||||||
c.setMimeData(md)
|
c.setMimeData(md)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user