Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: link shortening for gitlab #1012

Merged
merged 4 commits into from
Oct 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion docs/user_guide/theme-elements.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
31 changes: 16 additions & 15 deletions src/pydata_sphinx_theme/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<organisation>/<repository>#<element number>"
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) > 3:
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

Expand Down
24 changes: 21 additions & 3 deletions tests/sites/base/page1.rst
Original file line number Diff line number Diff line change
@@ -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
8 changes: 4 additions & 4 deletions tests/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
3 changes: 0 additions & 3 deletions tests/test_build/github_link.html

This file was deleted.

16 changes: 16 additions & 0 deletions tests/test_build/github_links.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<div class="github-container docutils container">
<p>
<a class="github reference external" href="https://github.com">
github
</a>
<a class="github reference external" href="https://github.com/pydata">
pydata
</a>
<a class="github reference external" href="https://github.com/pydata/pydata-sphinx-theme">
pydata/pydata-sphinx-theme
</a>
<a class="github reference external" href="https://github.com/pydata/pydata-sphinx-theme/pull/1012">
pydata/pydata-sphinx-theme#1012
</a>
</p>
</div>
3 changes: 0 additions & 3 deletions tests/test_build/gitlab_link.html

This file was deleted.

16 changes: 16 additions & 0 deletions tests/test_build/gitlab_links.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<div class="gitlab-container docutils container">
<p>
<a class="gitlab reference external" href="https://gitlab.com">
gitlab
</a>
<a class="gitlab reference external" href="https://gitlab.com/gitlab-org">
gitlab-org
</a>
<a class="gitlab reference external" href="https://gitlab.com/gitlab-org/gitlab">
gitlab-org/gitlab
</a>
<a class="gitlab reference external" href="https://gitlab.com/gitlab-org/gitlab/-/issues/375583">
gitlab-org/gitlab#375583
</a>
</p>
</div>