| 1 |
"""cloud_sptheme.ext.issue_tracker - added ":issue:" text role to link to issue tracker""" |
| 2 |
#=========================================================================== |
| 3 |
# imports |
| 4 |
#=========================================================================== |
| 5 |
#core |
| 6 |
import re |
| 7 |
#site |
| 8 |
from docutils import nodes |
| 9 |
from docutils.parsers.rst.roles import set_classes |
| 10 |
#pkg |
| 11 |
from cloud_sptheme import __version__ |
| 12 |
#local |
| 13 |
|
| 14 |
#=========================================================================== |
| 15 |
# issue role |
| 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 |
# causes :issue:`xx` to be replaced with label instead of url. |
| 33 |
return None |
| 34 |
|
| 35 |
elif template.startswith("bb:"): |
| 36 |
# parse "bb:<project>", and replace with bitbucket url |
| 37 |
project = template[3:].strip("/") |
| 38 |
return "https://bitbucket.org/" + project + "/issues/{issue}" |
| 39 |
|
| 40 |
elif template.startswith("gh:"): |
| 41 |
# parse "gh:<project>", and replace with github url |
| 42 |
project = template[3:].strip("/") |
| 43 |
return "https://github.com/" + project + "/issues/{issue}" |
| 44 |
|
| 45 |
else: |
| 46 |
# assume it contains {issue} and possibly {title} |
| 47 |
return template |
| 48 |
|
| 49 |
# pattern allows inside :issue: text roles |
| 50 |
issue_re = re.compile(""" |
| 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 |
#NOTE: |
| 67 |
# name - role name in doc, should be 'issue' |
| 68 |
# rawtext - text of entire node |
| 69 |
# text - contents of role |
| 70 |
# lineno |
| 71 |
# inliner - ??? |
| 72 |
# options - ??? |
| 73 |
# content - ??? |
| 74 |
# returns (nodes, messages) |
| 75 |
|
| 76 |
# extract title & issue number from text |
| 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 |
# get url template from config, resolve aliases |
| 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 |
# generate replacement node |
| 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 |
# init |
| 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 |
# identifies the version of our extension |
| 115 |
return {'version': __version__} |
| 116 |
|
| 117 |
#=========================================================================== |
| 118 |
# eof |
| 119 |
#=========================================================================== |