Skip to content

Commit

Permalink
remove repo_type and make repo_name intuitive (#260)
Browse files Browse the repository at this point in the history
resolves #259
  • Loading branch information
2bndy5 authored May 7, 2023
1 parent 99befd6 commit 6713712
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
1 change: 0 additions & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@
"site_url": "https://jbms.github.io/sphinx-immaterial/",
"repo_url": "https://github.com/jbms/sphinx-immaterial/",
"repo_name": "Sphinx-Immaterial",
"repo_type": "github",
"edit_uri": "blob/main/docs",
"globaltoc_collapse": True,
"features": [
Expand Down
22 changes: 17 additions & 5 deletions docs/customization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,27 @@ Configuration Options

.. themeconf:: repo_url

Set the repo url for the link to appear.
The link to the repository will be rendered next to the search bar on large screens and as
part of the main navigation drawer on smaller screen sizes. Additionally, for public
repositories hosted on `GitHub <https://github.com>`_ or `GitLab
<https://about.gitlab.com/>`_, the number of stars and forks is automatically requested and
rendered.

GitHub repositories also include the tag of the latest release. Unfortunately, GitHub
only provides `an API endpoint to obtain the latest release
<https://docs.github.com/en/rest/releases/releases#get-the-latest-release>`_ - not the
latest tag. Thus, make sure to create a release (not pre-release) for the latest tag you
want to display next to the number of stars and forks.

.. themeconf:: repo_name

The name of the repo. If must be set if repo_url is set.
The name of the repository. If :themeconf:`repo_url` is set, then the repository's name
will be extracted from the given URL. Optionally, this can be set to customize the
repository name.

.. themeconf:: repo_type

Must be one of github, gitlab or bitbucket.
.. warning::
If the :themeconf:`repo_url` does not use a ``github``, ``gitlab``, or ``bitbucket``
domain, then this option must be set explicitly.

.. themeconf:: icon

Expand Down
16 changes: 16 additions & 0 deletions sphinx_immaterial/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Sphinx-Immaterial theme."""

import os
import re
from urllib.parse import urlparse
from typing import cast, List, Type, Dict, Mapping, Optional

import docutils.nodes
Expand Down Expand Up @@ -245,6 +247,20 @@ def _config_inited(
DEFAULT_THEME_OPTIONS, config["html_theme_options"]
)

theme_options: dict = config["html_theme_options"]
repo_url: Optional[str] = theme_options.get("repo_url", None)
if not theme_options.get("repo_name", None) and repo_url:
# auto-extract repo_name from repo_url
url = urlparse(repo_url)
if re.search("github|gitlab|bitbucket", url.netloc) is None:
raise AttributeError(
"'repo_url' does not use a github, gitlab, or bitbucket domain, so"
" the `repo_name` must be set explicitly."
)
config["html_theme_options"]["repo_name"] = url.path.split("/")[2].rstrip(
".git"
)


def setup(app: Sphinx):
app.connect("config-inited", _config_inited)
Expand Down
2 changes: 0 additions & 2 deletions sphinx_immaterial/theme.conf
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ consent =
repo_url =
# The name of the repo. If must be set if repo_url is set
repo_name =
# Must be one of github, gitlab or bitbucket
repo_type = github
# url segment that is concatenated with repo_url to point readers to the document's
# source file. This is typically in the form of 'blob/<branch name>/<docs source folder>'.
edit_uri =
Expand Down

0 comments on commit 6713712

Please sign in to comment.