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)
This commit is contained in:
Kovid Goyal 2013-10-23 20:42:27 +05:30
parent 3f6c88f0ae
commit c20db472eb
2 changed files with 24 additions and 0 deletions

View File

@ -269,10 +269,15 @@
</xsl:attribute>
<xsl:choose>
<xsl:when test="(@type) = 'note'">
<xsl:attribute name="link_note"></xsl:attribute>
<sup>
<xsl:apply-templates/>
</sup>
</xsl:when>
<xsl:when test="(@type) = 'cite' and @id">
<xsl:attribute name="link_cite"><xsl:value-of select="@id"/></xsl:attribute>
<xsl:apply-templates/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates/>
</xsl:otherwise>

View File

@ -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))