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