matplotlib
167 строк · 5.2 Кб
1"""
2Define text roles for GitHub.
3
4* ghissue - Issue
5* ghpull - Pull Request
6* ghuser - User
7
8Adapted from bitbucket example here:
9https://bitbucket.org/birkenfeld/sphinx-contrib/src/tip/bitbucket/sphinxcontrib/bitbucket.py
10
11Authors
12-------
13
14* Doug Hellmann
15* Min RK
16"""
17#
18# Original Copyright (c) 2010 Doug Hellmann. All rights reserved.
19#
20
21from docutils import nodes, utils
22from docutils.parsers.rst.roles import set_classes
23
24
25def make_link_node(rawtext, app, type, slug, options):
26"""
27Create a link to a github resource.
28
29:param rawtext: Text being replaced with link node.
30:param app: Sphinx application context
31:param type: Link type (issues, changeset, etc.)
32:param slug: ID of the thing to link to
33:param options: Options dictionary passed to role func.
34"""
35
36try:
37base = app.config.github_project_url
38if not base:
39raise AttributeError
40if not base.endswith('/'):
41base += '/'
42except AttributeError as err:
43raise ValueError(
44f'github_project_url configuration value is not set '
45f'({err})') from err
46
47ref = base + type + '/' + slug + '/'
48set_classes(options)
49prefix = "#"
50if type == 'pull':
51prefix = "PR " + prefix
52node = nodes.reference(rawtext, prefix + utils.unescape(slug), refuri=ref,
53**options)
54return node
55
56
57def ghissue_role(name, rawtext, text, lineno, inliner, options={}, content=[]):
58"""
59Link to a GitHub issue.
60
61Returns 2 part tuple containing list of nodes to insert into the
62document and a list of system messages. Both are allowed to be
63empty.
64
65:param name: The role name used in the document.
66:param rawtext: The entire markup snippet, with role.
67:param text: The text marked with the role.
68:param lineno: The line number where rawtext appears in the input.
69:param inliner: The inliner instance that called us.
70:param options: Directive options for customization.
71:param content: The directive content for customization.
72"""
73
74try:
75issue_num = int(text)
76if issue_num <= 0:
77raise ValueError
78except ValueError:
79msg = inliner.reporter.error(
80'GitHub issue number must be a number greater than or equal to 1; '
81'"%s" is invalid.' % text, line=lineno)
82prb = inliner.problematic(rawtext, rawtext, msg)
83return [prb], [msg]
84app = inliner.document.settings.env.app
85if 'pull' in name.lower():
86category = 'pull'
87elif 'issue' in name.lower():
88category = 'issues'
89else:
90msg = inliner.reporter.error(
91'GitHub roles include "ghpull" and "ghissue", '
92'"%s" is invalid.' % name, line=lineno)
93prb = inliner.problematic(rawtext, rawtext, msg)
94return [prb], [msg]
95node = make_link_node(rawtext, app, category, str(issue_num), options)
96return [node], []
97
98
99def ghuser_role(name, rawtext, text, lineno, inliner, options={}, content=[]):
100"""
101Link to a GitHub user.
102
103Returns 2 part tuple containing list of nodes to insert into the
104document and a list of system messages. Both are allowed to be
105empty.
106
107:param name: The role name used in the document.
108:param rawtext: The entire markup snippet, with role.
109:param text: The text marked with the role.
110:param lineno: The line number where rawtext appears in the input.
111:param inliner: The inliner instance that called us.
112:param options: Directive options for customization.
113:param content: The directive content for customization.
114"""
115ref = 'https://www.github.com/' + text
116node = nodes.reference(rawtext, text, refuri=ref, **options)
117return [node], []
118
119
120def ghcommit_role(
121name, rawtext, text, lineno, inliner, options={}, content=[]):
122"""
123Link to a GitHub commit.
124
125Returns 2 part tuple containing list of nodes to insert into the
126document and a list of system messages. Both are allowed to be
127empty.
128
129:param name: The role name used in the document.
130:param rawtext: The entire markup snippet, with role.
131:param text: The text marked with the role.
132:param lineno: The line number where rawtext appears in the input.
133:param inliner: The inliner instance that called us.
134:param options: Directive options for customization.
135:param content: The directive content for customization.
136"""
137app = inliner.document.settings.env.app
138try:
139base = app.config.github_project_url
140if not base:
141raise AttributeError
142if not base.endswith('/'):
143base += '/'
144except AttributeError as err:
145raise ValueError(
146f'github_project_url configuration value is not set '
147f'({err})') from err
148
149ref = base + text
150node = nodes.reference(rawtext, text[:6], refuri=ref, **options)
151return [node], []
152
153
154def setup(app):
155"""
156Install the plugin.
157
158:param app: Sphinx application context.
159"""
160app.add_role('ghissue', ghissue_role)
161app.add_role('ghpull', ghissue_role)
162app.add_role('ghuser', ghuser_role)
163app.add_role('ghcommit', ghcommit_role)
164app.add_config_value('github_project_url', None, 'env')
165
166metadata = {'parallel_read_safe': True, 'parallel_write_safe': True}
167return metadata
168