From 475759daae00f1619a19445717618966ec665da2 Mon Sep 17 00:00:00 2001 From: 12rambau Date: Tue, 11 Oct 2022 11:12:45 +0200 Subject: [PATCH 1/4] FIX: link shortening for gitlab --- src/pydata_sphinx_theme/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pydata_sphinx_theme/__init__.py b/src/pydata_sphinx_theme/__init__.py index bb895fa07..230bdac4d 100644 --- a/src/pydata_sphinx_theme/__init__.py +++ b/src/pydata_sphinx_theme/__init__.py @@ -948,7 +948,7 @@ def parse_url(self, path): text = s[1] if len(s) > 2: text += f"/{s[2]}" - if len(s) > 3: + if len(s) > 4: if s[4] in ["issues", "merge_requests"]: text += f"#{s[-1]}" From 7b6bcb592ba627aeb32aa19859b8608fc4cc8ab7 Mon Sep 17 00:00:00 2001 From: 12rambau Date: Tue, 11 Oct 2022 13:16:48 +0200 Subject: [PATCH 2/4] rename variables --- src/pydata_sphinx_theme/__init__.py | 31 +++++++++++++++-------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/src/pydata_sphinx_theme/__init__.py b/src/pydata_sphinx_theme/__init__.py index 230bdac4d..f27885246 100644 --- a/src/pydata_sphinx_theme/__init__.py +++ b/src/pydata_sphinx_theme/__init__.py @@ -929,28 +929,29 @@ def parse_url(self, path): # split the url content # be careful the first one is a "/" - s = path.split("/") + parts = path.split("/") # check the platform name and read the information accordingly + # as "/#" if self.platform == "github": text = "github" - if len(s) > 1: - text = s[1] - if len(s) > 2: - text += f"/{s[2]}" - if len(s) > 3: - if s[3] in ["issues", "pull", "discussions"]: - text += f"#{s[-1]}" + if len(parts) > 1: + text = parts[1] # organisation + if len(parts) > 2: + text += f"/{parts[2]}" # repository + if len(parts) > 3: + if parts[3] in ["issues", "pull", "discussions"]: + text += f"#{parts[-1]}" # element number elif self.platform == "gitlab": text = "gitlab" - if len(s) > 1: - text = s[1] - if len(s) > 2: - text += f"/{s[2]}" - if len(s) > 4: - if s[4] in ["issues", "merge_requests"]: - text += f"#{s[-1]}" + if len(parts) > 1: + text = parts[1] # organisation + if len(parts) > 2: + text += f"/{parts[2]}" # repository + if len(parts) > 4: + if parts[4] in ["issues", "merge_requests"]: + text += f"#{parts[-1]}" # element number return text From cedb43ea7934f7c7762c7145272665299130c258 Mon Sep 17 00:00:00 2001 From: 12rambau Date: Wed, 12 Oct 2022 14:12:30 +0200 Subject: [PATCH 3/4] update the tests --- tests/sites/base/page1.rst | 24 +++++++++++++++++++++--- tests/test_build.py | 8 ++++---- tests/test_build/github_link.html | 3 --- tests/test_build/github_links.html | 16 ++++++++++++++++ tests/test_build/gitlab_link.html | 3 --- tests/test_build/gitlab_links.html | 16 ++++++++++++++++ 6 files changed, 57 insertions(+), 13 deletions(-) delete mode 100644 tests/test_build/github_link.html create mode 100644 tests/test_build/github_links.html delete mode 100644 tests/test_build/gitlab_link.html create mode 100644 tests/test_build/gitlab_links.html diff --git a/tests/sites/base/page1.rst b/tests/sites/base/page1.rst index 9318bceef..b05b07d6f 100644 --- a/tests/sites/base/page1.rst +++ b/tests/sites/base/page1.rst @@ -1,6 +1,24 @@ Page 1 ====== -- here's a normal link: https://pydata-sphinx-theme.readthedocs.io/en/latest/ -- Here's a github link: https://github.com/2i2c-org/infrastructure/issues/1329 -- Here's a gitlab link: https://gitlab.com/gitlab-org/gitlab/-/issues/375583 +**normal link** + +- https://pydata-sphinx-theme.readthedocs.io/en/latest/ + +**GitHub** + +.. container:: github-container + + https://github.com + https://github.com/pydata + https://github.com/pydata/pydata-sphinx-theme + https://github.com/pydata/pydata-sphinx-theme/pull/1012 + +**GitLab** + +.. container:: gitlab-container + + https://gitlab.com + https://gitlab.com/gitlab-org + https://gitlab.com/gitlab-org/gitlab + https://gitlab.com/gitlab-org/gitlab/-/issues/375583 diff --git a/tests/test_build.py b/tests/test_build.py index e42dac895..ec2e34377 100644 --- a/tests/test_build.py +++ b/tests/test_build.py @@ -656,11 +656,11 @@ def test_shorten_link(sphinx_build_factory, file_regression): sphinx_build = sphinx_build_factory("base").build() - github = sphinx_build.html_tree("page1.html").select(".github")[0] - file_regression.check(github.prettify(), basename="github_link", extension=".html") + github = sphinx_build.html_tree("page1.html").select(".github-container")[0] + file_regression.check(github.prettify(), basename="github_links", extension=".html") - gitlab = sphinx_build.html_tree("page1.html").select(".gitlab")[0] - file_regression.check(gitlab.prettify(), basename="gitlab_link", extension=".html") + gitlab = sphinx_build.html_tree("page1.html").select(".gitlab-container")[0] + file_regression.check(gitlab.prettify(), basename="gitlab_links", extension=".html") def test_math_header_item(sphinx_build_factory, file_regression): diff --git a/tests/test_build/github_link.html b/tests/test_build/github_link.html deleted file mode 100644 index adfedcf19..000000000 --- a/tests/test_build/github_link.html +++ /dev/null @@ -1,3 +0,0 @@ - - 2i2c-org/infrastructure#1329 - diff --git a/tests/test_build/github_links.html b/tests/test_build/github_links.html new file mode 100644 index 000000000..43ae05662 --- /dev/null +++ b/tests/test_build/github_links.html @@ -0,0 +1,16 @@ + diff --git a/tests/test_build/gitlab_link.html b/tests/test_build/gitlab_link.html deleted file mode 100644 index 5242c3127..000000000 --- a/tests/test_build/gitlab_link.html +++ /dev/null @@ -1,3 +0,0 @@ - - gitlab-org/gitlab#375583 - diff --git a/tests/test_build/gitlab_links.html b/tests/test_build/gitlab_links.html new file mode 100644 index 000000000..86f023af7 --- /dev/null +++ b/tests/test_build/gitlab_links.html @@ -0,0 +1,16 @@ + From e152e95583dfeedc3d93e1e736d13e139345908e Mon Sep 17 00:00:00 2001 From: 12rambau Date: Wed, 12 Oct 2022 14:17:53 +0200 Subject: [PATCH 4/4] add some example --- docs/user_guide/theme-elements.md | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/docs/user_guide/theme-elements.md b/docs/user_guide/theme-elements.md index a7ae4f0d1..9f29cd397 100644 --- a/docs/user_guide/theme-elements.md +++ b/docs/user_guide/theme-elements.md @@ -146,6 +146,20 @@ Some sidebar content. ## Link shortening for git repository services -Many projects have links back to their issues / PRs hosted on platforms like **GitHub** or **GitLab**. Instead of displaying these as raw links, this theme does some lightweight formatting for these platforms specifically. Here is an example from the issue requesting this feature: [https://github.com/pydata/pydata-sphinx-theme/issues/841](https://github.com/pydata/pydata-sphinx-theme/issues/841). +Many projects have links back to their issues / PRs hosted on platforms like **GitHub** or **GitLab**. Instead of displaying these as raw links, this theme does some lightweight formatting for these platforms specifically. Here is some examples from github and gitlab: + +**GitHub** + +- "https://github.com" -> [https://github.com](https://github.com) +- "https://github.com/pydata" -> [https://github.com/pydata](https://github.com/pydata) +- "https://github.com/pydata/pydata-sphinx-theme" -> [https://github.com/pydata/pydata-sphinx-theme](https://github.com/pydata/pydata-sphinx-theme) +- "https://github.com/pydata/pydata-sphinx-theme/pull/1012" -> [https://github.com/pydata/pydata-sphinx-theme/pull/1012](https://github.com/pydata/pydata-sphinx-theme/pull/1012) + +**GitLab** + +- "https://gitlab.com" -> [https://gitlab.com](https://gitlab.com) +- "https://gitlab.com/gitlab-org" -> [https://gitlab.com/gitlab-org](https://gitlab.com/gitlab-org) +- "https://gitlab.com/gitlab-org/gitlab" -> [https://gitlab.com/gitlab-org/gitlab](https://gitlab.com/gitlab-org/gitlab) +- "https://gitlab.com/gitlab-org/gitlab/-/issues/375583" -> [https://gitlab.com/gitlab-org/gitlab/-/issues/375583](https://gitlab.com/gitlab-org/gitlab/-/issues/375583) Links provided with a text body won't be changed.