From c437a367944008a712d6865b61a43a7ec9d017bb Mon Sep 17 00:00:00 2001 From: pagedown Date: Wed, 24 Aug 2022 13:27:12 +0800 Subject: [PATCH] Fix GitHub links in commented configuration Hyperlink GitHub related text roles: iss, pull, disc --- docs/extract-rst-targets.py | 2 +- kitty/cli.py | 15 +++++++++++++++ kitty/conf/types.py | 36 ++++++++++++++++++++++++------------ 3 files changed, 40 insertions(+), 13 deletions(-) diff --git a/docs/extract-rst-targets.py b/docs/extract-rst-targets.py index 41b39d897..cff2afc5b 100755 --- a/docs/extract-rst-targets.py +++ b/docs/extract-rst-targets.py @@ -14,7 +14,7 @@ def find_explicit_targets(text: str) -> Iterator[str]: def main() -> Dict[str, str]: - refs = {'github_discussions': 'https://github.com/kovidgoyal/kitty/discussions'} + refs = {} base = os.path.dirname(os.path.abspath(__file__)) for dirpath, dirnames, filenames in os.walk(base): if 'generated' in dirnames: diff --git a/kitty/cli.py b/kitty/cli.py index 0714029a1..8eae079e2 100644 --- a/kitty/cli.py +++ b/kitty/cli.py @@ -193,6 +193,21 @@ def ac(x: str) -> str: return ref_hyperlink(x, 'action-') +@role +def iss(x: str) -> str: + return ref_hyperlink(x, 'github-issue-') + + +@role +def pull(x: str) -> str: + return ref_hyperlink(x, 'github-pr-') + + +@role +def disc(x: str) -> str: + return ref_hyperlink(x, 'github-discussion-') + + OptionSpecSeq = List[Union[str, OptionDict]] diff --git a/kitty/conf/types.py b/kitty/conf/types.py index 31fb480c1..3a7a62955 100644 --- a/kitty/conf/types.py +++ b/kitty/conf/types.py @@ -72,7 +72,18 @@ def resolve_ref(ref: str, website_url: Callable[[str], str] = website_url) -> st href = f'actions/#{frag}' elif ref.startswith('term-') or ref.startswith('envvar-'): href = 'glossary/#' + ref - return website_url(href) + elif ref.startswith('github-'): + href = 'https://github.com/kovidgoyal/kitty' + parts = ref.split('-') + if parts[1] == 'issue': + href = f'{href}/issues/{parts[2]}' + elif parts[1] == 'pr': + href = f'{href}/pull/{parts[2]}' + elif parts[1] == 'discussion': + href = f'{href}/discussions/{parts[2]}' + if not (href.startswith('https://') or href.startswith('http://')): + href = website_url(href) + return href def remove_markup(text: str) -> str: @@ -84,27 +95,28 @@ def remove_markup(text: str) -> str: return t, q def sub(m: 'Match[str]') -> str: - if m.group(1) == 'ref': + if m.group(1) in ('ref', 'iss', 'pull', 'disc'): t, q = extract(m) + if m.group(1) == 'iss': + q = f'github-issue-{q}' + elif m.group(1) == 'pull': + q = f'github-pr-{q}' + elif m.group(1) == 'disc': + q = f'github-discussion-{q}' url = resolve_ref(q) if not url: - raise KeyError(f'Failed to resolve :ref: {q}') + raise KeyError(f'Failed to resolve :{m.group(1)}: {q}') return f'{t} <{url}>' if m.group(1) == 'doc': t, q = extract(m) return f'{t} <{website_url(q.lstrip("/"))}>' - if m.group(1) == 'ac': - q = m.group(2).split('<')[-1].rstrip('>') - return q - if m.group(1) == 'term': + if m.group(1) in ('term', 'option'): t, _ = extract(m) return t - if m.group(1) == 'option': - t, _ = extract(m) - return t - if m.group(1) == 'disc': + if m.group(1) in ('ac', 'opt'): t, q = extract(m) - return f'{t} {resolve_ref("github_discussions")}/{q}' + return f'{t} {q}' if q and q != t else t + return str(m.group(2)) return re.sub(r':([a-zA-Z0-9]+):`(.+?)`', sub, text, flags=re.DOTALL)