File: doi_role.py

package info (click to toggle)
scikit-learn 1.7.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 25,748 kB
  • sloc: python: 219,120; cpp: 5,790; ansic: 846; makefile: 189; javascript: 110
file content (47 lines) | stat: -rw-r--r-- 1,666 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""
doilinks
~~~~~~~~
Extension to add links to DOIs. With this extension you can use e.g.
:doi:`10.1016/S0022-2836(05)80360-2` in your documents. This will
create a link to a DOI resolver
(``https://doi.org/10.1016/S0022-2836(05)80360-2``).
The link caption will be the raw DOI.
You can also give an explicit caption, e.g.
:doi:`Basic local alignment search tool <10.1016/S0022-2836(05)80360-2>`.

:copyright: Copyright 2015  Jon Lund Steffensen. Based on extlinks by
    the Sphinx team.
:license: BSD.
"""

from docutils import nodes, utils
from sphinx.util.nodes import split_explicit_title


def reference_role(typ, rawtext, text, lineno, inliner, options={}, content=[]):
    text = utils.unescape(text)
    has_explicit_title, title, part = split_explicit_title(text)
    if typ in ["arXiv", "arxiv"]:
        full_url = "https://arxiv.org/abs/" + part
        if not has_explicit_title:
            title = "arXiv:" + part
        pnode = nodes.reference(title, title, internal=False, refuri=full_url)
        return [pnode], []
    if typ in ["doi", "DOI"]:
        full_url = "https://doi.org/" + part
        if not has_explicit_title:
            title = "DOI:" + part
        pnode = nodes.reference(title, title, internal=False, refuri=full_url)
        return [pnode], []


def setup_link_role(app):
    app.add_role("arxiv", reference_role, override=True)
    app.add_role("arXiv", reference_role, override=True)
    app.add_role("doi", reference_role, override=True)
    app.add_role("DOI", reference_role, override=True)


def setup(app):
    app.connect("builder-inited", setup_link_role)
    return {"version": "0.1", "parallel_read_safe": True}