From 6ac4b8df6cf76a39d16100953e32d651c585e07f Mon Sep 17 00:00:00 2001 From: Roland Meister Date: Mon, 11 Mar 2013 20:31:26 +0100 Subject: Code cleanup: remove redundant check; move uri formatting into branch. --- sphinx/builders/epub.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/sphinx/builders/epub.py b/sphinx/builders/epub.py index 6f8d9dae..644ada92 100644 --- a/sphinx/builders/epub.py +++ b/sphinx/builders/epub.py @@ -295,13 +295,12 @@ class EpubBuilder(StandaloneHTMLBuilder): 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) def write_doc(self, docname, doctree): """Write one document file. -- cgit v1.2.1 From 4db935fbd4182ca8d812d33b9e3bcab02412d0b7 Mon Sep 17 00:00:00 2001 From: Roland Meister Date: Mon, 11 Mar 2013 21:59:53 +0100 Subject: New 'footnote' setting for epub_show_links. --- doc/config.rst | 1 + sphinx/builders/epub.py | 35 +++++++++++++++++++++++++++++++++-- sphinx/quickstart.py | 2 +- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/doc/config.rst b/doc/config.rst index 2b8d33f2..ec4b3ae8 100644 --- a/doc/config.rst +++ b/doc/config.rst @@ -953,6 +953,7 @@ the `Dublin Core metadata `_. 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 644ada92..6b734bda 100644 --- a/sphinx/builders/epub.py +++ b/sphinx/builders/epub.py @@ -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. diff --git a/sphinx/quickstart.py b/sphinx/quickstart.py index 36919767..a4dd0d2b 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. -- cgit v1.2.1 From 5e454f00106ce5c21548a68f786ffd97e985158c Mon Sep 17 00:00:00 2001 From: Roland Meister Date: Mon, 11 Mar 2013 22:02:57 +0100 Subject: Introduce _footnote_label_template global variable for customization. --- sphinx/builders/epub.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sphinx/builders/epub.py b/sphinx/builders/epub.py index 6b734bda..3991ce13 100644 --- a/sphinx/builders/epub.py +++ b/sphinx/builders/epub.py @@ -131,6 +131,8 @@ _toctree_template = u'toctree-l%d' _link_target_template = u' [%(uri)s]' +_footnote_label_template = u'#%d' + _css_link_target_class = u'link-target' # XXX These strings should be localized according to epub_language @@ -324,7 +326,7 @@ class EpubBuilder(StandaloneHTMLBuilder): link['classes'].append(_css_link_target_class) node.parent.insert(idx, link) elif show_urls == 'footnote': - label = "#%d" % nr + label = _footnote_label_template % nr nr += 1 footnote_ref = make_footnote_ref(doc, label) node.parent.insert(idx, footnote_ref) -- cgit v1.2.1 From 923e781f518f9dbcf474af5680cdd84002e8cb32 Mon Sep 17 00:00:00 2001 From: Roland Meister Date: Mon, 11 Mar 2013 23:33:38 +0100 Subject: Add helper function footnote_spot that determins where to put new footnotes. --- sphinx/builders/epub.py | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/sphinx/builders/epub.py b/sphinx/builders/epub.py index 3991ce13..0d5ebe53 100644 --- a/sphinx/builders/epub.py +++ b/sphinx/builders/epub.py @@ -133,6 +133,8 @@ _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 @@ -308,12 +310,33 @@ class EpubBuilder(StandaloneHTMLBuilder): 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] - # XXX search place to put footnotes - bottom = doc + fn_spot, fn_idx = footnote_spot(tree) nr = 1 for node in tree.traverse(nodes.reference): uri = node.get('refuri', '') @@ -331,9 +354,10 @@ class EpubBuilder(StandaloneHTMLBuilder): footnote_ref = make_footnote_ref(doc, label) node.parent.insert(idx, footnote_ref) footnote = make_footnote(doc, label, uri) - bottom.append(footnote) + 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. -- cgit v1.2.1