Skip to content

Commit d155d43

Browse files
committed
✨ moban extensions that are specific to this repo. related to: moremoban/moban#23
1 parent c8a2674 commit d155d43

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

plugins/github.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import re
2+
from moban.extensions import JinjaFilter
3+
4+
5+
GITHUB_REF_PATTERN = "`([^`]*?#[0-9]+)`"
6+
ISSUE = "^.*?" + GITHUB_REF_PATTERN + ".*?$"
7+
SAME_PROJ_FULL_ISSUE = "`#{3} <https://github.com/{0}/{1}/{2}/{3}>`_"
8+
DIFF_PROJ_FULL_ISSUE = "`{1}#{3} <https://github.com/{0}/{1}/{2}/{3}>`_"
9+
PULL_REQUEST = "PR"
10+
PULL = "pull"
11+
ISSUES = "issues"
12+
13+
14+
@JinjaFilter('github_expand')
15+
def github_expand(line, name, organisation):
16+
result = re.match(ISSUE, line)
17+
if result:
18+
github_thing = result.group(1)
19+
tokens = github_thing.split("#")
20+
if len(tokens) == 4:
21+
if tokens[2] == PULL_REQUEST:
22+
tokens[2] = PULL
23+
else:
24+
tokens[2] = ISSUES
25+
elif len(tokens) == 3:
26+
if tokens[1] == PULL_REQUEST:
27+
tokens = [organisation, tokens[0], PULL, tokens[2]]
28+
else:
29+
tokens = [organisation, tokens[0], ISSUES, tokens[2]]
30+
elif len(tokens) == 2:
31+
if tokens[0] == PULL_REQUEST:
32+
tokens = [organisation, name, PULL] + tokens[1:]
33+
elif tokens[0] != "":
34+
tokens = [organisation, tokens[0], ISSUES] + tokens[1:]
35+
else:
36+
tokens = [organisation, name, ISSUES] + tokens[1:]
37+
if tokens[1] != name:
38+
reference = DIFF_PROJ_FULL_ISSUE.format(*tokens)
39+
else:
40+
reference = SAME_PROJ_FULL_ISSUE.format(*tokens)
41+
return re.sub(GITHUB_REF_PATTERN, reference, line)
42+
else:
43+
return line

plugins/text.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import re
2+
from moban.extensions import JinjaFilter
3+
4+
5+
@JinjaFilter('split_length')
6+
def split_length(input_line, length):
7+
start = 0
8+
limit = length
9+
line = re.sub("\s+", " ", input_line)
10+
line_length = len(line)
11+
if line_length <= length:
12+
yield line
13+
else:
14+
while True:
15+
if " " in line[start:start + limit]:
16+
# go back and find a space
17+
while limit > 0 and line[start + limit] != " ":
18+
limit -= 1
19+
else:
20+
# full whole line is single unit
21+
# so go forward find a space
22+
while (
23+
(start + limit) < len(line) and line[start + limit] != " "
24+
):
25+
limit += 1
26+
27+
yield line[start:start + limit]
28+
start = start + limit + 1
29+
limit = length
30+
if len(line[start:]) < length or start + limit >= len(line):
31+
break
32+
33+
yield line[start:]

0 commit comments

Comments
 (0)