Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a custom Sphinx extension for linking to API documentation.
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
Showing
2 changed files
with
42 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |