forked from leanprover-community/mathlib-port-status
-
Notifications
You must be signed in to change notification settings - Fork 0
/
htmlify_comment.py
40 lines (37 loc) · 1.55 KB
/
htmlify_comment.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
import re
from markupsafe import Markup
from typing import Optional
import pycmarkgfm
def htmlify_text(s: Optional[str], default_repo='leanprover-community/mathlib') -> Markup:
"""Add links to a comment from the yaml file"""
if s is None:
return Markup()
assert isinstance(s, str), f"got {type(s)}"
def repl_func(m):
if m.group(1) is not None:
repo = m.group(1)
pull = m.group(2)
repo = {
'mathlib4': 'leanprover-community/mathlib4',
'mathlib': 'leanprover-community/mathlib',
'mathlib3': 'leanprover-community/mathlib',
'': default_repo,
'lean4': 'leanprover/lean4',
'lean': 'leanprover-community/lean',
}.get(repo, repo)
return f'[{m.group(0)}](https://github.com/{repo}/pull/{pull})'
elif m.group(3) is not None:
return f'[{m.group(0)[:8]}](https://github.com/leanprover-community/mathlib/commit/{m.group(3)})'
elif m.group(4) is not None:
return f'[{m.group(0)}](https://github.com/{m.group(4)})'
else:
return m.group(0)
# TODO: no easy way to build this as an extension for pycmarkgfm
hacked_links = re.sub(
r'(?:([-_a-zA-Z0-9/]*)#([0-9]+))|([0-9a-f]{40})|@([-a-z0-9A-Z_]+)|.*?',
repl_func, s)
return Markup(pycmarkgfm.gfm_to_html(hacked_links))
def htmlify_comment(s: Optional[str], **kwargs) -> Markup:
m = htmlify_text(s, **kwargs)
m = re.sub(r'^<p>(.*)</p>', r'\1', str(m))
return Markup(m)