From 9aea6019e170b1d768423fc4ceeb2f6f50ce7e98 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 31 Oct 2022 05:41:51 +0530 Subject: [PATCH] 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) --- src/calibre/gui2/book_details.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/calibre/gui2/book_details.py b/src/calibre/gui2/book_details.py index 5f87108c3c..9dae2a4a55 100644 --- a/src/calibre/gui2/book_details.py +++ b/src/calibre/gui2/book_details.py @@ -69,8 +69,28 @@ def copy_all(text_browser): mf = getattr(text_browser, 'details', text_browser) c = QApplication.clipboard() md = QMimeData() - md.setText(mf.toPlainText()) - md.setHtml(mf.toHtml()) + html = 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)