|
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 | + |
2 | 22 |
|
3 | 23 | 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 |
6 | 62 |
|
7 | 63 | 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 | + """ |
8 | 69 | def role(name, rawtext, text, lineno, inliner, options={}, content=[]): |
9 | 70 | url = pattern % (text,) |
10 | 71 | node = nodes.reference(rawtext, text, refuri=url, **options) |
|
0 commit comments