New 'footnote' setting for epub_show_links.

This commit is contained in:
Roland Meister 2013-03-11 21:59:53 +01:00
parent 1eb07f1e96
commit 234c0c0c75
3 changed files with 35 additions and 3 deletions

View File

@ -953,6 +953,7 @@ the `Dublin Core metadata <http://dublincore.org/>`_.
settings can have the following values:
* ``'inline'`` -- display URLs inline in parentheses (default)
* ``'footnote'`` -- display URLs in footnotes
* ``'no'`` -- do not display URLs
.. versionadded:: 1.2

View File

@ -287,10 +287,32 @@ class EpubBuilder(StandaloneHTMLBuilder):
node.attributes['ids'] = newids
def add_visible_links(self, tree, show_urls='inline'):
"""Append visible link targets after external links"""
"""Add visible link targets for external links"""
def make_footnote_ref(doc, label):
"""Create a footnote_reference node with children"""
footnote_ref = nodes.footnote_reference('[#]_')
footnote_ref.append(nodes.Text(label))
doc.note_autofootnote_ref(footnote_ref)
return footnote_ref
def make_footnote(doc, label, uri):
"""Create a footnote node with children"""
footnote = nodes.footnote(uri)
para = nodes.paragraph()
para.append(nodes.Text(uri))
footnote.append(para)
footnote.insert(0, nodes.label('', label))
doc.note_autofootnote(footnote)
return footnote
if show_urls == 'no':
return
if show_urls == 'footnote':
doc = tree.traverse(nodes.document)[0]
# XXX search place to put footnotes
bottom = doc
nr = 1
for node in tree.traverse(nodes.reference):
uri = node.get('refuri', '')
if (uri.startswith('http:') or uri.startswith('https:') or
@ -301,6 +323,15 @@ class EpubBuilder(StandaloneHTMLBuilder):
link = nodes.inline(uri, uri)
link['classes'].append(_css_link_target_class)
node.parent.insert(idx, link)
elif show_urls == 'footnote':
label = "#%d" % nr
nr += 1
footnote_ref = make_footnote_ref(doc, label)
node.parent.insert(idx, footnote_ref)
footnote = make_footnote(doc, label, uri)
bottom.append(footnote)
footnote_ref['refid'] = footnote['ids'][0]
footnote.add_backref(footnote_ref['ids'][0])
def write_doc(self, docname, doctree):
"""Write one document file.

View File

@ -344,7 +344,7 @@ epub_copyright = u'%(copyright_str)s'
# Scale large images.
#epub_max_image_width = 0
# If 'no', URL addresses will not be shown.
# How to display URL addresses: 'footnote', 'no', or 'inline'.
#epub_show_urls = 'inline'
# If false, no index is generated.