1"""Creates links that allows substituting the current release
2within the title or target."""
3
4import os.path
5
6from docutils import nodes
7from sphinx.util.nodes import split_explicit_title
8
9
10def releaseref_role(name, rawtext, text, lineno, inliner, options={}, content=[]):
11    config = inliner.document.settings.env.config
12    text = text.replace("|version|", config.version)
13    text = text.replace("|release|", config.release)
14
15    has_explicit_title, title, target = split_explicit_title(text)
16    if not has_explicit_title:
17        title = os.path.basename(target)
18
19    node = nodes.reference(rawtext, title, refuri=target, **options)
20
21    return [node], []
22
23
24def setup(app):
25    app.add_role("releaseref", releaseref_role)
26
27
28