Skip to content

Commit

Permalink
Edit book: Table of Contents tool: Allow using the title attribute on…
Browse files Browse the repository at this point in the history
… headings tags to get the text for table of contents entries
  • Loading branch information
kovidgoyal committed Apr 18, 2022
1 parent a46806c commit db75fe0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
8 changes: 5 additions & 3 deletions src/calibre/ebooks/oeb/polish/toc.py
Expand Up @@ -362,8 +362,10 @@ def ensure_id(elem, all_ids):
return True, elem.get('id')


def elem_to_toc_text(elem):
def elem_to_toc_text(elem, prefer_title=False):
text = xml2text(elem).strip()
if prefer_title:
text = elem.get('title', '').strip() or text
if not text:
text = elem.get('title', '')
if not text:
Expand Down Expand Up @@ -398,7 +400,7 @@ def item_at_top(elem):
return True


def from_xpaths(container, xpaths):
def from_xpaths(container, xpaths, prefer_title=False):
'''
Generate a Table of Contents from a list of XPath expressions. Each
expression in the list corresponds to a level of the generate ToC. For
Expand Down Expand Up @@ -450,7 +452,7 @@ def process_node(node):
lvl = item_level_map.get(item, None)
if lvl is None:
continue
text = elem_to_toc_text(item)
text = elem_to_toc_text(item, prefer_title)
parent = parent_for_level(lvl)
if item_at_top(item):
dirtied, elem_id = False, None
Expand Down
45 changes: 32 additions & 13 deletions src/calibre/gui2/toc/main.py
Expand Up @@ -23,7 +23,7 @@
TOC, add_id, commit_toc, from_files, from_links, from_xpaths, get_toc
)
from calibre.gui2 import (
Application, error_dialog, info_dialog, question_dialog, set_app_uid
Application, error_dialog, info_dialog, set_app_uid
)
from calibre.gui2.convert.xpath_wizard import XPathEdit
from calibre.gui2.progress_indicator import ProgressIndicator
Expand Down Expand Up @@ -140,7 +140,7 @@ class ItemView(QStackedWidget): # {{{
delete_item = pyqtSignal()
flatten_item = pyqtSignal()
go_to_root = pyqtSignal()
create_from_xpath = pyqtSignal(object, object)
create_from_xpath = pyqtSignal(object, object, object)
create_from_links = pyqtSignal()
create_from_files = pyqtSignal()
flatten_toc = pyqtSignal()
Expand Down Expand Up @@ -314,23 +314,42 @@ def __init__(self, parent, prefs):
self.w2.setWordWrap(True)
l.addWidget(la, l.rowCount(), 0, 1, 2)

def ask_if_duplicates_should_be_removed(self):
return not question_dialog(self, _('Remove duplicates'), _(
'Should headings with the same text at the same level be included?'),
yes_text=_('&Include duplicates'), no_text=_('&Remove duplicates'))
def headings_question(self, xpaths):
from calibre.gui2.widgets2 import Dialog

class D(Dialog):
def __init__(self, parent):
super().__init__(_('Configure ToC generation'), 'configure-toc-from-headings', parent=parent)

def setup_ui(s):
s.l = l = QVBoxLayout(s)
s.remove_duplicates_cb = rd = QCheckBox(_('Remove &duplicated headings at the same ToC level'))
l.addWidget(rd)
rd.setChecked(bool(self.prefs.get('toc_from_headings_remove_duplicates', True)))
s.prefer_title_cb = pt = QCheckBox(_('Use the &title attribute for ToC text'))
l.addWidget(pt)
pt.setToolTip(textwrap.fill(_(
'When a heading tag has the "title" attribute use its contents as the text for the ToC entry,'
' instead of the the text inside the heading tag itself.')))
pt.setChecked(bool(self.prefs.get('toc_from_headings_prefer_title')))
l.addWidget(s.bb)

d = D(self)
if d.exec_() == QDialog.DialogCode.Accepted:
self.create_from_xpath.emit(xpaths, d.remove_duplicates_cb.isChecked(), d.prefer_title_cb.isChecked())
self.prefs.set('toc_from_headings_remove_duplicates', d.remove_duplicates_cb.isChecked())
self.prefs.set('toc_from_headings_prefer_title', d.prefer_title_cb.isChecked())

def create_from_major_headings(self):
self.create_from_xpath.emit(['//h:h%d'%i for i in range(1, 4)],
self.ask_if_duplicates_should_be_removed())
self.headings_question(['//h:h%d'%i for i in range(1, 4)])

def create_from_all_headings(self):
self.create_from_xpath.emit(['//h:h%d'%i for i in range(1, 7)],
self.ask_if_duplicates_should_be_removed())
self.headings_question(['//h:h%d'%i for i in range(1, 7)])

def create_from_user_xpath(self):
d = XPathDialog(self, self.prefs)
if d.exec() == QDialog.DialogCode.Accepted and d.xpaths:
self.create_from_xpath.emit(d.xpaths, d.remove_duplicates_cb.isChecked())
self.create_from_xpath.emit(d.xpaths, d.remove_duplicates_cb.isChecked(), False)

def hide_azw3_warning(self):
self.w1.setVisible(False), self.w2.setVisible(False)
Expand Down Expand Up @@ -945,8 +964,8 @@ def process_node(root, tocparent, added):
process_node(self.root, toc, nodes)
self.highlight_item(nodes[0])

def create_from_xpath(self, xpaths, remove_duplicates=True):
toc = from_xpaths(self.ebook, xpaths)
def create_from_xpath(self, xpaths, remove_duplicates=True, prefer_title=False):
toc = from_xpaths(self.ebook, xpaths, prefer_title=prefer_title)
if len(toc) == 0:
return error_dialog(self, _('No items found'),
_('No items were found that could be added to the Table of Contents.'), show=True)
Expand Down

0 comments on commit db75fe0

Please sign in to comment.