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