summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoland Meister <devnull@localhost>2013-04-03 19:06:01 +0200
committerRoland Meister <devnull@localhost>2013-04-03 19:06:01 +0200
commited51f735fc17104620f75c7832d839d6b70c303c (patch)
tree69f92a4af19789add04de9c08c62ec01884f19ba
parentb61429dfd9422591b95d9113a77f4137e73c38af (diff)
parent923e781f518f9dbcf474af5680cdd84002e8cb32 (diff)
downloadsphinx-ed51f735fc17104620f75c7832d839d6b70c303c.tar.gz
Merge with birkenfeld/sphinx
-rw-r--r--doc/config.rst1
-rw-r--r--sphinx/builders/epub.py74
-rw-r--r--sphinx/quickstart.py2
3 files changed, 67 insertions, 10 deletions
diff --git a/doc/config.rst b/doc/config.rst
index 6261e293..70adbfe0 100644
--- a/doc/config.rst
+++ b/doc/config.rst
@@ -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
diff --git a/sphinx/builders/epub.py b/sphinx/builders/epub.py
index 6f8d9dae..0d5ebe53 100644
--- a/sphinx/builders/epub.py
+++ b/sphinx/builders/epub.py
@@ -131,6 +131,10 @@ _toctree_template = u'toctree-l%d'
_link_target_template = u' [%(uri)s]'
+_footnote_label_template = u'#%d'
+
+_footnotes_rubric_name = u'Footnotes'
+
_css_link_target_class = u'link-target'
# XXX These strings should be localized according to epub_language
@@ -287,21 +291,73 @@ 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
+
+ def footnote_spot(tree):
+ """Find or create a spot to place footnotes.
+
+ The function returns the tuple (parent, index)."""
+ # The code uses the following heuristic:
+ # a) place them after the last existing footnote
+ # b) place them after an (empty) Footnotes rubric
+ # c) create an empty Footnotes rubric at the end of the document
+ fns = tree.traverse(nodes.footnote)
+ if fns:
+ fn = fns[-1]
+ return fn.parent, fn.parent.index(fn) + 1
+ for node in tree.traverse(nodes.rubric):
+ if len(node.children) == 1 and \
+ node.children[0].astext() == _footnotes_rubric_name:
+ return node.parent, node.parent.index(node) + 1
+ doc = tree.traverse(nodes.document)[0]
+ rub = nodes.rubric()
+ rub.append(nodes.Text(_footnotes_rubric_name))
+ doc.append(rub)
+ return doc, doc.index(rub) + 1
+
if show_urls == 'no':
return
-
+ if show_urls == 'footnote':
+ doc = tree.traverse(nodes.document)[0]
+ fn_spot, fn_idx = footnote_spot(tree)
+ nr = 1
for node in tree.traverse(nodes.reference):
uri = node.get('refuri', '')
if (uri.startswith('http:') or uri.startswith('https:') or
uri.startswith('ftp:')) and uri not in node.astext():
- uri = _link_target_template % {'uri': uri}
- if uri:
- idx = node.parent.index(node) + 1
- if show_urls == 'inline':
- link = nodes.inline(uri, uri)
- link['classes'].append(_css_link_target_class)
- node.parent.insert(idx, link)
+ idx = node.parent.index(node) + 1
+ if show_urls == 'inline':
+ uri = _link_target_template % {'uri': uri}
+ link = nodes.inline(uri, uri)
+ link['classes'].append(_css_link_target_class)
+ node.parent.insert(idx, link)
+ elif show_urls == 'footnote':
+ label = _footnote_label_template % nr
+ nr += 1
+ footnote_ref = make_footnote_ref(doc, label)
+ node.parent.insert(idx, footnote_ref)
+ footnote = make_footnote(doc, label, uri)
+ fn_spot.insert(fn_idx, footnote)
+ footnote_ref['refid'] = footnote['ids'][0]
+ footnote.add_backref(footnote_ref['ids'][0])
+ fn_idx += 1
def write_doc(self, docname, doctree):
"""Write one document file.
diff --git a/sphinx/quickstart.py b/sphinx/quickstart.py
index 019cd681..bbfbcb32 100644
--- a/sphinx/quickstart.py
+++ b/sphinx/quickstart.py
@@ -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.