From e289f4959f07c565f8b82048a51c5e9a518ca21a Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 20 Aug 2022 12:03:33 +0530 Subject: [PATCH] DRYer --- kitty/conf/types.py | 16 ++++++++-------- kitty/constants.py | 12 +++++++----- kitty/utils.py | 7 +++++-- kitty_tests/check_build.py | 27 ++++++++++++++++----------- 4 files changed, 36 insertions(+), 26 deletions(-) diff --git a/kitty/conf/types.py b/kitty/conf/types.py index 0c870cadb..00c4ee545 100644 --- a/kitty/conf/types.py +++ b/kitty/conf/types.py @@ -56,21 +56,21 @@ def resolve_ref(ref: str, website_url: Callable[[str], str] = website_url) -> st m = ref_map() href = m['ref'].get(ref, '') if href: - return website_url(href) - if ref.startswith('conf-kitty-'): - href = f'{website_url("conf")}#{ref}' + pass + elif ref.startswith('conf-kitty-'): + href = f'conf#{ref}' elif ref.startswith('conf-kitten-'): parts = ref.split('-') - href = website_url("kittens/" + parts[2] + f'#{ref}') + href = "kittens/" + parts[2] + f'/#{ref}' elif ref.startswith('at_'): base = ref.split('_', 1)[1] - href = website_url("remote-control/#at_" + base.replace('_', '-')) + href = "remote-control/#at_" + base.replace('_', '-') elif ref.startswith('action-group-'): - href = f'{website_url("actions")}#{ref}' + href = f'actions/#{ref}' elif ref.startswith('action-'): frag = ref.partition('-')[-1].replace('_', '-') - href = f'{website_url("actions")}#{frag}' - return href + href = f'actions/#{frag}' + return website_url(href) def remove_markup(text: str) -> str: diff --git a/kitty/constants.py b/kitty/constants.py index 2fca921f0..521c22cf5 100644 --- a/kitty/constants.py +++ b/kitty/constants.py @@ -230,12 +230,14 @@ def read_kitty_resource(name: str, package_name: str = 'kitty') -> bytes: return read_binary(package_name, name) -def website_url(doc_name: str = '') -> str: +def website_url(doc_name: str = '', website: str = 'https://sw.kovidgoyal.net/kitty/') -> str: if doc_name: - doc_name = doc_name.rstrip('/') - if doc_name: - doc_name += '/' - return f'https://sw.kovidgoyal.net/kitty/{doc_name}' + base, _, frag = doc_name.partition('#') + base = base.rstrip('/') + if base: + base += '/' + doc_name = base + (f'#{frag}' if frag else '') + return website + doc_name handled_signals: Set[int] = set() diff --git a/kitty/utils.py b/kitty/utils.py index c28df6d9f..0ebce949e 100644 --- a/kitty/utils.py +++ b/kitty/utils.py @@ -1071,11 +1071,14 @@ def safer_fork() -> int: return pid -def docs_url(which: str = '', local_docs_root: str = '') -> str: +def docs_url(which: str = '', local_docs_root: Optional[str] = '') -> str: from urllib.parse import quote from .constants import local_docs, website_url from .conf.types import resolve_ref - ld = local_docs_root or local_docs() + if local_docs_root is None: + ld = '' + else: + ld = local_docs_root or local_docs() base, frag = which.partition('#')[::2] base = base.strip('/') if frag.startswith('ref='): diff --git a/kitty_tests/check_build.py b/kitty_tests/check_build.py index 35faa3985..abb5d1346 100644 --- a/kitty_tests/check_build.py +++ b/kitty_tests/check_build.py @@ -77,18 +77,23 @@ class TestBuild(BaseTest): def test_docs_url(self): from kitty.utils import docs_url - p = partial(docs_url, local_docs_root='/docs') + from kitty.constants import website_url - def t(x, e): - self.ae(p(x), 'file:///docs/' + e) - t('', 'index.html') - t('conf', 'conf.html') - t('kittens/ssh#frag', 'kittens/ssh.html#frag') - t('#ref=confloc', 'conf.html#confloc') - t('#ref=conf-kitty-fonts', 'conf.html#conf-kitty-fonts') - t('#ref=conf-kitten-ssh-xxx', 'kittens/ssh.html#conf-kitten-ssh-xxx') - t('#ref=at_close_tab', 'remote-control.html#at_close-tab') - t('#ref=action-copy', 'actions.html#copy') + def run_tests(p, base, suffix='.html'): + def t(x, e): + self.ae(p(x), base + e) + t('', 'index.html' if suffix == '.html' else '') + t('conf', f'conf{suffix}') + t('kittens/ssh#frag', f'kittens/ssh{suffix}#frag') + t('#ref=confloc', f'conf{suffix}#confloc') + t('#ref=conf-kitty-fonts', f'conf{suffix}#conf-kitty-fonts') + t('#ref=conf-kitten-ssh-xxx', f'kittens/ssh{suffix}#conf-kitten-ssh-xxx') + t('#ref=at_close_tab', f'remote-control{suffix}#at_close-tab') + t('#ref=action-copy', f'actions{suffix}#copy') + + run_tests(partial(docs_url, local_docs_root='/docs'), 'file:///docs/') + w = website_url() + run_tests(partial(docs_url, local_docs_root=None), w, '/') def main() -> None: