Skip to content

Commit

Permalink
add doi_role extension
Browse files Browse the repository at this point in the history
  • Loading branch information
sciunto committed Aug 21, 2018
1 parent 95628b5 commit 28c9782
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
52 changes: 52 additions & 0 deletions doc/ext/doi_role.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# -*- coding: utf-8 -*-
"""
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 doi_role(typ, rawtext, text, lineno, inliner, options={}, content=[]):
text = utils.unescape(text)
has_explicit_title, title, part = split_explicit_title(text)
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 arxiv_role(typ, rawtext, text, lineno, inliner, options={}, content=[]):
text = utils.unescape(text)
has_explicit_title, title, part = split_explicit_title(text)
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], []


def setup_link_role(app):
app.add_role('doi', doi_role)
app.add_role('DOI', doi_role)
app.add_role('arXiv', arxiv_role)
app.add_role('arxiv', arxiv_role)


def setup(app):
app.connect('builder-inited', setup_link_role)
return {'version': '0.1', 'parallel_read_safe': True}
2 changes: 2 additions & 0 deletions doc/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
extensions = ['sphinx.ext.autodoc',
'sphinx.ext.mathjax',
'numpydoc',
'doi_role',
'sphinx.ext.autosummary',
'sphinx.ext.intersphinx',
'sphinx.ext.linkcode',
Expand All @@ -38,6 +39,7 @@

autosummary_generate = True


#------------------------------------------------------------------------
# Sphinx-gallery configuration
#------------------------------------------------------------------------
Expand Down

0 comments on commit 28c9782

Please sign in to comment.