From c20db472ebf972583127dfee1d869e14cfd44b9b Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 23 Oct 2013 20:42:27 +0530 Subject: [PATCH] FB2 Input: Add support for note and cite back references FB2 Input: Add support for note and cite back references. Link pairs of type="note" and type="cite" now automatically generate the correct back reference. Fixes #1243714 [Fb2 return link is lost when converted to mobi or epub](https://bugs.launchpad.net/calibre/+bug/1243714) --- resources/templates/fb2.xsl | 5 +++++ .../ebooks/conversion/plugins/fb2_input.py | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/resources/templates/fb2.xsl b/resources/templates/fb2.xsl index 474c7c0045..83bb9c7f10 100644 --- a/resources/templates/fb2.xsl +++ b/resources/templates/fb2.xsl @@ -269,10 +269,15 @@ + + + + + diff --git a/src/calibre/ebooks/conversion/plugins/fb2_input.py b/src/calibre/ebooks/conversion/plugins/fb2_input.py index f47a63b15f..784b13cfc8 100644 --- a/src/calibre/ebooks/conversion/plugins/fb2_input.py +++ b/src/calibre/ebooks/conversion/plugins/fb2_input.py @@ -87,6 +87,25 @@ class FB2Input(InputFormatPlugin): transform = etree.XSLT(styledoc) result = transform(doc) + + # Handle links of type note and cite + notes = {a.get('href')[1:]: a for a in result.xpath('//a[@link_note and @href]') if a.get('href').startswith('#')} + cites = {a.get('link_cite'): a for a in result.xpath('//a[@link_cite]') if not a.get('href', '')} + all_ids = {x for x in result.xpath('//*/@id')} + for cite, a in cites.iteritems(): + note = notes.get(cite, None) + if note: + c = 1 + while 'cite%d' % c in all_ids: + c += 1 + if not note.get('id', None): + note.set('id', 'cite%d' % c) + all_ids.add(note.get('id')) + a.set('href', '#%s' % note.get('id')) + for x in result.xpath('//*[@link_note or @link_cite]'): + x.attrib.pop('link_note', None) + x.attrib.pop('link_cite', None) + for img in result.xpath('//img[@src]'): src = img.get('src') img.set('src', self.binary_map.get(src, src))