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

Allow gitlab URL link shortening from non-gitlab/github.com domains #2068

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions docs/user_guide/source-buttons.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Source Buttons

Source buttons are links to the source of your page's content (either on your site, or on hosting sites like GitHub).

.. _add-edit-button:

Add an edit button
==================

Expand Down
22 changes: 21 additions & 1 deletion docs/user_guide/theme-elements.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ All will end up as numbers in the rendered HTML, but in the source they look lik

## Link shortening for git repository services

Many projects have links back to their issues / PRs hosted on platforms like **GitHub** or **GitLab**.
Many projects have links back to their issues / PRs hosted on platforms like **GitHub**, **GitLab**, or **Bitbucket**.
Instead of displaying these as raw links, this theme does some lightweight formatting for these platforms specifically.

In **reStructuredText**, URLs are automatically converted to links, so this works automatically.
Expand Down Expand Up @@ -252,5 +252,25 @@ There are a variety of link targets supported, here's a table for reference:
- `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/-/issues/375583`: https://gitlab.com/gitlab-org/gitlab/-/issues/375583
- `https://gitlab.com/gitlab-org/gitlab/-/merge_requests/174667`: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/174667

**Bitbucket**

- `https://bitbucket.org`: https://bitbucket.org
- `https://bitbucket.org/atlassian/workspace/overview`: https://bitbucket.org/atlassian/workspace/overview
- `https://bitbucket.org/atlassian/aui`: https://bitbucket.org/atlassian/aui
- `https://bitbucket.org/atlassian/aui/pull-requests/4758`: https://bitbucket.org/atlassian/aui/pull-requests/4758

Links provided with a text body won't be changed.

If you have links to GitHub, GitLab, or Bitbucket repository URLs that are on non-standard domains
(i.e., not on `github.com`, `gitlab.com`, or `bitbucket.org`, respectively), then these will be
shortened if the base URL is given in the `html_context` section of your `conf.py` file (see
{ref}`Add an edit button <add-edit-button>`), e.g.,

```python
html_context = {
"gitlab_url": "https://gitlab.mydomain.com", # your self-hosted GitLab
...
}
```
22 changes: 21 additions & 1 deletion src/pydata_sphinx_theme/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,27 @@ def setup(app: Sphinx) -> Dict[str, str]:

app.add_html_theme("pydata_sphinx_theme", str(theme_path))

app.add_post_transform(short_link.ShortenLinkTransform)
if hasattr(app.config, "html_context"):
github_url = app.config.html_context.get("github_url", None)
gitlab_url = app.config.html_context.get("gitlab_url", None)
bitbucket_url = app.config.html_context.get("bitbucket_url", None)

url_update = {}
for url, platform in zip(
[github_url, gitlab_url, bitbucket_url], ["github", "gitlab", "bitbucket"]
):
if url:
# remove "http[s]://" and leading/trailing "/"s
url = urlparse(url)._replace(scheme="").geturl().lstrip("/").rstrip("/")
url_update[url] = platform

class ShortenLinkTransformCustom(short_link.ShortenLinkTransform):
supported_platform = short_link.ShortenLinkTransform.supported_platform
supported_platform.update(url_update)

app.add_post_transform(ShortenLinkTransformCustom)
else:
app.add_post_transform(short_link.ShortenLinkTransform)

app.connect("builder-inited", translator.setup_translators)
app.connect("builder-inited", update_config)
Expand Down
7 changes: 6 additions & 1 deletion src/pydata_sphinx_theme/assets/styles/base/_base.scss
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ a {

// set up a icon next to the shorten links from github and gitlab
&.github,
&.gitlab {
&.gitlab,
&.bitbucket {
&::before {
color: var(--pst-color-text-muted);
font: var(--fa-font-brands);
Expand All @@ -63,6 +64,10 @@ a {
&.gitlab::before {
content: var(--pst-icon-gitlab);
}

&.bitbucket::before {
content: var(--pst-icon-bitbucket);
}
}

%heading-style {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ html {
--pst-icon-search-minus: "\f010"; // fa-solid fa-magnifying-glass-minus
--pst-icon-github: "\f09b"; // fa-brands fa-github
--pst-icon-gitlab: "\f296"; // fa-brands fa-gitlab
--pst-icon-bitbucket: "\f171"; // fa-brands fa-bitbucket
--pst-icon-share: "\f064"; // fa-solid fa-share
--pst-icon-bell: "\f0f3"; // fa-solid fa-bell
--pst-icon-pencil: "\f303"; // fa-solid fa-pencil
Expand Down
21 changes: 19 additions & 2 deletions src/pydata_sphinx_theme/short_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

class ShortenLinkTransform(SphinxPostTransform):
"""
Shorten link when they are coming from github or gitlab and add an extra class to
the tag for further styling.
Shorten link when they are coming from github, gitlab, or bitbucket and add
an extra class to the tag for further styling.

Before:
.. code-block:: html
Expand All @@ -37,6 +37,7 @@ class ShortenLinkTransform(SphinxPostTransform):
supported_platform: ClassVar[dict[str, str]] = {
"github.com": "github",
"gitlab.com": "gitlab",
"bitbucket.org": "bitbucket",
}
platform = None

Expand Down Expand Up @@ -96,6 +97,22 @@ def parse_url(self, uri: ParseResult) -> str:
if parts[2] in ["issues", "pull", "discussions"]:
text += f"#{parts[-1]}" # element number

elif self.platform == "bitbucket":
# split the url content
parts = path.split("/")

if len(parts) > 0:
text = parts[0] # organisation
if len(parts) > 1 and not (
parts[-2] == "workspace" and parts[-1] == "overview"
):
if len(parts) > 1:
text += f"/{parts[1]}" # repository
if len(parts) > 2:
if parts[2] in ["issues", "pull-requests"]:
itemnumber = parts[3]
text += f"#{itemnumber}" # element number

elif self.platform == "gitlab":
# cp. https://docs.gitlab.com/ee/user/markdown.html#gitlab-specific-references
if "/-/" in path and any(
Expand Down