Skip to content

Commit 1f93817

Browse files
authored
Merge pull request altMITgcm#7 from jahn/link-roles
allow explicit titles in filelink role
2 parents 1328757 + efe6b98 commit 1f93817

File tree

1 file changed

+64
-3
lines changed

1 file changed

+64
-3
lines changed

doc/_extensions/mitgcm.py

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,71 @@
1-
from docutils import nodes
1+
"""
2+
Sphinx extension for the MITgcm documentation
3+
4+
Provides two custom roles:
5+
6+
:filelink:`path/to/file`
7+
inserts a link to the file on github. The full path will be shown unless an
8+
explicit title is given as in :filelink:`title <path/to/file>` or the path is
9+
prefixed with a tilde, :filelink:`~path/to/file`, in which case only the last
10+
path component (file) is shown.
11+
12+
:varlink:`identifier`
13+
inserts a link to a code search based on lxr.
14+
15+
Slightly modified from http://protips.readthedocs.io/link-roles.html.
16+
"""
17+
18+
from docutils import nodes, utils
19+
20+
from sphinx.util.nodes import split_explicit_title
21+
222

323
def setup(app):
4-
app.add_role('filelink', autolink('https://github.com/altMITgcm/MITgcm/blob/master/%s'))
5-
app.add_role('varlink', autolink('http://mitgcm.org/lxr/ident/MITgcm?_i=%s'))
24+
app.add_role(
25+
'filelink',
26+
filelink('https://github.com/altMITgcm/MITgcm/blob/master/%s'))
27+
app.add_role(
28+
'varlink',
29+
autolink('http://mitgcm.org/lxr/ident/MITgcm?_i=%s'))
30+
31+
def filelink(pattern):
32+
"""
33+
Return a role processor for external links to files based on a URL pattern.
34+
35+
%s in *pattern* will be replaced by the role text, a file path, with two
36+
modifications:
37+
38+
- an explicit title can be specified as for internal sphinx cross
39+
references: :role:`title <target>` will display *title* but link to a URL
40+
formed from *target*.
41+
- if the role text is prefixed with ~, only the last path component will be
42+
displayed: :role:`~path/to/file` will display *file* but link to
43+
path/to/file.
44+
"""
45+
def role(name, rawtext, text, lineno, inliner, options={}, content=[]):
46+
# extract explicit title if provided
47+
text = utils.unescape(text)
48+
has_explicit_title, title, part = split_explicit_title(text)
49+
# if not, check for initial tilde
50+
if not has_explicit_title and part[:1] == '~':
51+
# remove it
52+
part = part[1:]
53+
# and extract last path component
54+
title = part.split('/')[-1]
55+
# form link target
56+
url = pattern % (part,)
57+
# make link node
58+
node = nodes.reference(rawtext, title, refuri=url, **options)
59+
# and return it (no messages)
60+
return [node], []
61+
return role
662

763
def autolink(pattern):
64+
"""
65+
Return a role processor for external links based on a URL pattern.
66+
67+
%s in *pattern* will be replaced by the role text.
68+
"""
869
def role(name, rawtext, text, lineno, inliner, options={}, content=[]):
970
url = pattern % (text,)
1071
node = nodes.reference(rawtext, text, refuri=url, **options)

0 commit comments

Comments
 (0)