File: issue_role.py

package info (click to toggle)
flufl.enum 7.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 184 kB
  • sloc: python: 551; makefile: 9
file content (45 lines) | stat: -rw-r--r-- 1,286 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from docutils import nodes

from sphinx.application import Sphinx
from sphinx.roles import ReferenceRole
from sphinx.util.typing import ExtensionMetadata


BASE_URL = 'https://gitlab.com/warsaw/flufl.enum/-/issues/'


class IssueRole(ReferenceRole):
    """A role to hyperlink GitLab issues.

    Use like this: :GL:`16`
    """
    def run(self) -> tuple[list[nodes.Node], list[nodes.system_message]]:
        try:
            issue_number = int(self.target)
        except ValueError:
            message = self.inliner.reporter.error(f'Role target must be an integer :GL:{self.target}')
            problem = self.inliner.problematic(self.rawtext, self.rawtext, message)
            return [problem], [message]

        issue_uri = BASE_URL + self.target
        title = self.title if self.has_explicit_title else f'GL#{self.target}'

        return [
            nodes.reference(
                '', title,
                internal=True,
                refuri=issue_uri,
                classes=['issue'],
                _title_tuple=(issue_number,)
            )
        ], []


def setup(app: Sphinx) -> ExtensionMetadata:
    app.add_role('GL', IssueRole())

    return {
        'version': '0.1',
        'parallel_read_safe': True,
        'parallel_write_safe': True,
    }