Skip to content

Commit

Permalink
Add a custom Sphinx extension for linking to API documentation.
Browse files Browse the repository at this point in the history
Unlike extlinks, our apilinks extension knows how to format class names
for display, so we don't need to use custom titles all the time.
  • Loading branch information
szeiger committed Dec 10, 2012
1 parent 275301e commit c736771
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/sphinx/conf.py
Expand Up @@ -17,7 +17,7 @@

# Add any Sphinx extension module names here, as strings. They can be extensions
# coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
extensions = ['sphinx.ext.extlinks', 'includecode']
extensions = ['sphinx.ext.extlinks', 'includecode', 'apilinks']

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
Expand Down Expand Up @@ -254,6 +254,10 @@
'ex': ('https://github.com/slick/slick-examples/blob/'+slick_examples_version+'/src/main/scala/scala/slick/examples/%s.scala', 'Slick example ')
}

apilinks = {
'api': 'http://slick.typesafe.com/doc/'+version+'/api/#%s'
}

rst_epilog = '''
.. include:: %(links)s
.. _Slick Examples: https://github.com/slick/slick-examples/tree/%(examples-version)s
Expand Down
37 changes: 37 additions & 0 deletions src/sphinx/exts/apilinks.py
@@ -0,0 +1,37 @@
"""
Based on sphinx.ext.extlinks,
:copyright: Copyright 2007-2011 by the Sphinx team, see AUTHORS.
:license: BSD, see LICENSE for details.
"""

from docutils import nodes, utils

from sphinx.util.nodes import split_explicit_title


def make_link_role(base_url):
def role(typ, rawtext, text, lineno, inliner, options={}, content=[]):
text = utils.unescape(text)
has_explicit_title, title, part = split_explicit_title(text)
try:
full_url = base_url % part
except (TypeError, ValueError):
inliner.reporter.warning(
'unable to expand %s apilink with base URL %r, please make '
'sure the base contains \'%%s\' exactly once'
% (typ, base_url), line=lineno)
full_url = base_url + part
if not has_explicit_title:
idents = part.split(".")
title = idents[len(idents)-1]
pnode = nodes.reference(title, title, internal=False, refuri=full_url)
return [pnode], []
return role

def setup_link_roles(app):
for name, base_url in app.config.apilinks.iteritems():
app.add_role(name, make_link_role(base_url))

def setup(app):
app.add_config_value('apilinks', {}, 'env')
app.connect('builder-inited', setup_link_roles)

0 comments on commit c736771

Please sign in to comment.