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