-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommit.py
61 lines (44 loc) · 1.64 KB
/
commit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# -*- coding: utf-8 -*-
"""
コミット構文
=========================================
コミットIDをリンクに変換する
[commit REPOSITORY_NAME, commit-id0, commit-id-2...]
>>> text = "[commit REPOSITORY_NAME, 1234567, abcdefg]"
>>> md = markdown.Markdown(['commit'])
>>> print md.convert(text)
<a href="https://github.com/REPOSITORY_NAME/commit/1234567">1234567</a> <a href="https://github.com/REPOSITORY_NAME/commit/abcdefg">abcdefg</a>
"""
import re
from markdown.extensions import Extension
from markdown.preprocessors import Preprocessor
def replace_commit_line(line: str) -> str:
new_line: str = line
for m in re.finditer(r'\[commit (.*?)\]', line.strip()):
c = m[1].split(", ")
repo = c[0]
links: list[str] = []
for id in c[1:]:
id = id.strip()
if len(id) == 0:
continue
links.append("<a href=\"https://github.com/{0}/commit/{1}\">{1}</a>".format(repo, id))
commits: str = " ".join(links)
new_line = new_line.replace(m[0], commits)
return new_line
class CommitExtension(Extension):
def extendMarkdown(self, md, md_globals):
pre = CommitPreprocessor(md)
md.registerExtension(self)
md.preprocessors.register(pre, 'commit', 25)
class CommitPreprocessor(Preprocessor):
def __init__(self, md):
Preprocessor.__init__(self, md)
def run(self, lines):
new_lines = []
for line in lines:
new_line = replace_commit_line(line)
new_lines.append(new_line)
return new_lines
def makeExtension(**kwargs):
return CommitExtension(**kwargs)