class AnchorCollector(TransformationFilter): """Collect text anchors to save indexes on a text object's annotation. """ grok.implements(ISaveEditorFilter) grok.provides(ISaveEditorFilter) grok.order(50) def prepare(self, name, text): self.entries = ITextIndexEntries(text) self.entries.clear() truncate = prepare def __call__(self, tree): for anchor in tree.xpath('//a[@class="anchor"]'): if 'name' in anchor.attrib and 'title' in anchor.attrib: name = anchor.attrib['name'].strip() title = anchor.attrib['title'].strip() if name and title: # Only collect entries with a name and a title self.entries.add(name, title) # Save back stripped values anchor.attrib['name'] = name anchor.attrib['title'] = title if 'href' in anchor.attrib: # Those should not have any href, so clean them del anchor.attrib['href']