xref: /libuv/docs/src/sphinx-plugins/manpage.py (revision 707dd7f1)
1# encoding: utf-8
2
3#
4# Copyright (c) 2013 Dariusz Dwornikowski.  All rights reserved.
5#
6# Adapted from https://github.com/tdi/sphinxcontrib-manpage
7# License: Apache 2
8#
9
10
11import re
12
13from docutils import nodes, utils
14from docutils.parsers.rst.roles import set_classes
15from string import Template
16
17
18def make_link_node(rawtext, app, name, manpage_num, options):
19    ref = app.config.man_url_regex
20    if not ref:
21        ref = "https://man7.org/linux/man-pages/man%s/%s.%s.html" %(manpage_num, name, manpage_num)
22    else:
23        s = Template(ref)
24        ref = s.substitute(num=manpage_num, topic=name)
25    set_classes(options)
26    node = nodes.reference(rawtext, "%s(%s)" % (name, manpage_num), refuri=ref, **options)
27    return node
28
29
30def man_role(name, rawtext, text, lineno, inliner, options={}, content=[]):
31    app = inliner.document.settings.env.app
32    p = re.compile("([a-zA-Z0-9_\.-_]+)\((\d)\)")
33    m = p.match(text)
34
35    manpage_num = m.group(2)
36    name = m.group(1)
37    node = make_link_node(rawtext, app, name, manpage_num, options)
38    return [node], []
39
40
41def setup(app):
42    app.add_role('man', man_role)
43    app.add_config_value('man_url_regex', None, 'env')
44    return
45
46