|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""A Sphinx extension for linking to your project's issue tracker.""" |
| 3 | +""" |
| 4 | +Copyright 2014 Steven Loria |
| 5 | +
|
| 6 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +of this software and associated documentation files (the "Software"), to deal |
| 8 | +in the Software without restriction, including without limitation the rights |
| 9 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +copies of the Software, and to permit persons to whom the Software is |
| 11 | +furnished to do so, subject to the following conditions: |
| 12 | +
|
| 13 | +The above copyright notice and this permission notice shall be included in |
| 14 | +all copies or substantial portions of the Software. |
| 15 | +
|
| 16 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | +THE SOFTWARE. |
| 23 | +""" |
| 24 | + |
| 25 | +try: |
| 26 | + from docutils import nodes, utils |
| 27 | +except ImportError: |
| 28 | + # Load lazily so that test-sphinxext does not require docutils dependency |
| 29 | + pass |
| 30 | + |
| 31 | +__version__ = '0.2.0' |
| 32 | +__author__ = 'Steven Loria' |
| 33 | +__license__ = 'MIT' |
| 34 | + |
| 35 | + |
| 36 | +def user_role(name, rawtext, text, lineno, |
| 37 | + inliner, options=None, content=None): |
| 38 | + """Sphinx role for linking to a user profile. Defaults to linking to |
| 39 | + Github profiles, but the profile URIS can be configured via the |
| 40 | + ``issues_user_uri`` config value. |
| 41 | +
|
| 42 | + Example: :: |
| 43 | +
|
| 44 | + :user:`sloria` |
| 45 | + """ |
| 46 | + options = options or {} |
| 47 | + content = content or [] |
| 48 | + username = utils.unescape(text).strip() |
| 49 | + config = inliner.document.settings.env.app.config |
| 50 | + if config.issues_user_uri: |
| 51 | + ref = config.issues_user_uri.format(user=username) |
| 52 | + else: |
| 53 | + ref = 'https://github.com/{0}'.format(username) |
| 54 | + text = '@{0}'.format(username) |
| 55 | + link = nodes.reference(text=text, refuri=ref, **options) |
| 56 | + return [link], [] |
| 57 | + |
| 58 | + |
| 59 | +def _make_issue_node(issue_no, config, options=None): |
| 60 | + options = options or {} |
| 61 | + if issue_no not in ('-', '0'): |
| 62 | + if config.issues_uri: |
| 63 | + ref = config.issues_uri.format(issue=issue_no) |
| 64 | + elif config.issues_github_path: |
| 65 | + ref = 'https://github.com/{0}/issues/{1}'.format( |
| 66 | + config.issues_github_path, issue_no |
| 67 | + ) |
| 68 | + issue_text = '#{0}'.format(issue_no) |
| 69 | + link = nodes.reference(text=issue_text, refuri=ref, **options) |
| 70 | + else: |
| 71 | + link = None |
| 72 | + return link |
| 73 | + |
| 74 | + |
| 75 | +def issue_role(name, rawtext, text, lineno, |
| 76 | + inliner, options=None, content=None): |
| 77 | + """Sphinx role for linking to an issue. Must have |
| 78 | + `issues_uri` or `issues_github_path` configured in ``conf.py``. |
| 79 | +
|
| 80 | + Examples: :: |
| 81 | +
|
| 82 | + :issue:`123` |
| 83 | + :issue:`42,45` |
| 84 | + """ |
| 85 | + options = options or {} |
| 86 | + content = content or [] |
| 87 | + issue_nos = [each.strip() for each in utils.unescape(text).split(',')] |
| 88 | + config = inliner.document.settings.env.app.config |
| 89 | + ret = [] |
| 90 | + for i, issue_no in enumerate(issue_nos): |
| 91 | + node = _make_issue_node(issue_no, config, options=options) |
| 92 | + ret.append(node) |
| 93 | + if i != len(issue_nos) - 1: |
| 94 | + sep = nodes.raw(text=', ', format='html') |
| 95 | + ret.append(sep) |
| 96 | + return ret, [] |
| 97 | + |
| 98 | + |
| 99 | +def setup(app): |
| 100 | + # Format template for issues URI |
| 101 | + # e.g. 'https://github.com/sloria/marshmallow/issues/{issue} |
| 102 | + app.add_config_value('issues_uri', default=None, rebuild='html') |
| 103 | + # Shortcut for Github, e.g. 'sloria/marshmallow' |
| 104 | + app.add_config_value('issues_github_path', default=None, rebuild='html') |
| 105 | + # Format template for user profile URI |
| 106 | + # e.g. 'https://github.com/{user}' |
| 107 | + app.add_config_value('issues_user_uri', default=None, rebuild='html') |
| 108 | + app.add_role('issue', issue_role) |
| 109 | + app.add_role('user', user_role) |
0 commit comments