| 1 |
"""cloud_sptheme.ext.issue_tracker - added ":issue:" text role to link to issue tracker""" |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
import re |
| 7 |
|
| 8 |
from docutils import nodes |
| 9 |
from docutils.parsers.rst.roles import set_classes |
| 10 |
|
| 11 |
from cloud_sptheme import __version__ |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
def make_error(inliner, rawtext, line, value): |
| 19 |
"generate error node and msg" |
| 20 |
msg = inliner.reporter.error(value, line=line) |
| 21 |
node = inliner.problematic(rawtext, rawtext, msg) |
| 22 |
return [node], [msg] |
| 23 |
|
| 24 |
def get_issue_tracker_title(config): |
| 25 |
"retrieve issue_tracker_title template" |
| 26 |
return getattr(config, "issue_tracker_title", None) or "issue {issue}" |
| 27 |
|
| 28 |
def get_issue_tracker_url(config): |
| 29 |
"retrieve issue_tracker_url template, replacing aliases" |
| 30 |
template = getattr(config, "issue_tracker_url", None) |
| 31 |
if not template: |
| 32 |
|
| 33 |
return None |
| 34 |
|
| 35 |
elif template.startswith("bb:"): |
| 36 |
|
| 37 |
project = template[3:].strip("/") |
| 38 |
return "https://bitbucket.org/" + project + "/issues/{issue}" |
| 39 |
|
| 40 |
elif template.startswith("gh:"): |
| 41 |
|
| 42 |
project = template[3:].strip("/") |
| 43 |
return "https://github.com/" + project + "/issues/{issue}" |
| 44 |
|
| 45 |
else: |
| 46 |
|
| 47 |
return template |
| 48 |
|
| 49 |
|
| •50 |
issue_re = re.compile(r""" |
| 51 |
^ |
| 52 |
(?: |
| 53 |
(?P<title>[^<]+) |
| 54 |
\s* |
| 55 |
< |
| 56 |
(?P<issue1>\d+) |
| 57 |
> |
| 58 |
| |
| 59 |
(?P<issue2>\d+) |
| 60 |
) |
| 61 |
$ |
| 62 |
""", re.X) |
| 63 |
|
| 64 |
def issue_role(name, rawtext, text, line, inliner, options={}, content=[]): |
| 65 |
"generate link to an issue" |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
m = issue_re.match(text) |
| 78 |
if m: |
| 79 |
issue = int(m.group("issue1") or m.group("issue2")) |
| 80 |
title = m.group("title") |
| 81 |
else: |
| 82 |
return make_error(inliner, rawtext, line, |
| 83 |
"Invalid issue identifier: %r" % (text,)) |
| 84 |
|
| 85 |
|
| 86 |
config = inliner.document.settings.env.app.config |
| 87 |
url_template = get_issue_tracker_url(config) |
| 88 |
title_template = get_issue_tracker_title(config) |
| 89 |
|
| 90 |
|
| 91 |
if not title: |
| 92 |
title = title_template.format(issue=issue) |
| 93 |
set_classes(options) |
| 94 |
clist = options.setdefault('classes',[]) |
| 95 |
clist.append("issue") |
| 96 |
if url_template: |
| 97 |
url = url_template.format(issue=issue, title=title) |
| 98 |
node = nodes.reference(rawtext, title, refuri=url, **options) |
| 99 |
else: |
| 100 |
node = nodes.emphasis(rawtext, title, **options) |
| 101 |
return [node], [] |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
def setup(app): |
| 108 |
"install plugin" |
| 109 |
|
| 110 |
app.add_config_value('issue_tracker_url', None, 'env') |
| 111 |
app.add_config_value('issue_tracker_title', None, 'env') |
| 112 |
app.add_role('issue', issue_role) |
| 113 |
|
| 114 |
|
| 115 |
return {'version': __version__} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|