def populate_anchors(self, name): if name not in self.anchor_cache: from calibre.ebooks.oeb.base import XHTML_NS root = self.container.parsed(name) ac = self.anchor_cache[name] = [] for item in set(root.xpath('//*[@id]')) | set(root.xpath('//h:a[@name]', namespaces={'h':XHTML_NS})): frag = item.get('id', None) or item.get('name') text = lead_text(item, num_words=4) ac.append((text, frag)) ac.sort(key=lambda text_frag: primary_sort_key(text_frag[0])) self.anchor_names.model().set_names(self.anchor_cache[name]) self.update_target()