xref: /netbsd-src/external/mit/libuv/dist/docs/src/sphinx-plugins/manpage.py (revision 5f2f42719cd62ff11fd913b40b7ce19f07c4fd25)
10e552da7Schristos# encoding: utf-8
20e552da7Schristos
30e552da7Schristos#
40e552da7Schristos# Copyright (c) 2013 Dariusz Dwornikowski.  All rights reserved.
50e552da7Schristos#
60e552da7Schristos# Adapted from https://github.com/tdi/sphinxcontrib-manpage
70e552da7Schristos# License: Apache 2
80e552da7Schristos#
90e552da7Schristos
100e552da7Schristos
110e552da7Schristosimport re
120e552da7Schristos
130e552da7Schristosfrom docutils import nodes, utils
140e552da7Schristosfrom docutils.parsers.rst.roles import set_classes
150e552da7Schristosfrom string import Template
160e552da7Schristos
170e552da7Schristos
180e552da7Schristosdef make_link_node(rawtext, app, name, manpage_num, options):
190e552da7Schristos    ref = app.config.man_url_regex
200e552da7Schristos    if not ref:
21*5f2f4271Schristos        ref = "https://man7.org/linux/man-pages/man%s/%s.%s.html" %(manpage_num, name, manpage_num)
220e552da7Schristos    else:
230e552da7Schristos        s = Template(ref)
240e552da7Schristos        ref = s.substitute(num=manpage_num, topic=name)
250e552da7Schristos    set_classes(options)
260e552da7Schristos    node = nodes.reference(rawtext, "%s(%s)" % (name, manpage_num), refuri=ref, **options)
270e552da7Schristos    return node
280e552da7Schristos
290e552da7Schristos
300e552da7Schristosdef man_role(name, rawtext, text, lineno, inliner, options={}, content=[]):
310e552da7Schristos    app = inliner.document.settings.env.app
320e552da7Schristos    p = re.compile("([a-zA-Z0-9_\.-_]+)\((\d)\)")
330e552da7Schristos    m = p.match(text)
340e552da7Schristos
350e552da7Schristos    manpage_num = m.group(2)
360e552da7Schristos    name = m.group(1)
370e552da7Schristos    node = make_link_node(rawtext, app, name, manpage_num, options)
380e552da7Schristos    return [node], []
390e552da7Schristos
400e552da7Schristos
410e552da7Schristosdef setup(app):
420e552da7Schristos    app.add_role('man', man_role)
430e552da7Schristos    app.add_config_value('man_url_regex', None, 'env')
440e552da7Schristos    return
450e552da7Schristos
46