| 1 |
|
| 2 |
import functools |
| 3 |
import re |
| 4 |
|
| 5 |
import docutils.nodes |
| 6 |
import docutils.utils |
| 7 |
|
| 8 |
|
| 9 |
CUSTOM_RE = re.compile('(.*) <(.*)>') |
| 10 |
|
| 11 |
|
| 12 |
def role(config, role, rawtext, text, lineno, inliner, options={}, content=[]): |
| 13 |
template = 'https://docs.ansible.com/ansible/latest/modules/%s_module.html' |
| 14 |
|
| 15 |
match = CUSTOM_RE.match(text) |
| 16 |
if match: # "custom text <real link>" |
| 17 |
title = match.group(1) |
| 18 |
text = match.group(2) |
| 19 |
elif text.startswith('~'): # brief |
| 20 |
text = text[1:] |
| 21 |
title = config.get('brief', '%s') % ( |
| 22 |
docutils.utils.unescape(text), |
| 23 |
) |
| 24 |
else: |
| 25 |
title = config.get('text', '%s') % ( |
| 26 |
docutils.utils.unescape(text), |
| 27 |
) |
| 28 |
|
| 29 |
node = docutils.nodes.reference( |
| 30 |
rawsource=rawtext, |
| 31 |
text=title, |
| 32 |
refuri=config['url'] % (text,), |
| 33 |
**options |
| 34 |
) |
| 35 |
|
| 36 |
return [node], [] |
| 37 |
|
| 38 |
|
| 39 |
def setup(app): |
| 40 |
for name, info in app.config._raw_config['domainrefs'].items(): |
| 41 |
app.add_role(name, functools.partial(role, info)) |