Skip to content

Commit

Permalink
fix: cleanup the precommit and codes
Browse files Browse the repository at this point in the history
  • Loading branch information
lwasser committed Aug 20, 2024
2 parents b388acd + 4b020e0 commit 8b2cb17
Show file tree
Hide file tree
Showing 10 changed files with 172 additions and 79 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,32 @@ jobs:
uses: actions/upload-pages-artifact@v3
with:
path: ./_build/html

build-artifact:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: "3.9"
cache: "pip"
cache-dependency-path: pyproject.toml

- name: Upgrade pip
run: |
python3 -m pip install --upgrade pip
- name: Install dependencies
run: python3 -m pip install nox

- name: Build project
run: nox -s build-project

- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: pyos-sphinx-theme-artifact
path: dist/
retention-days: 7
8 changes: 8 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
repos:

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.3.3
hooks:
- id: ruff
args: [--fix]
- id: ruff-format

- repo: https://github.com/pre-commit/mirrors-prettier
rev: 'v4.0.0-alpha.8'
hooks:
Expand Down
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
# pyos-sphinx-theme
# ``pyos-sphinx-theme``
[![Documentation Status](https://readthedocs.org/projects/pyos-sphinx-theme/badge/?version=latest)](https://pyos-sphinx-theme.readthedocs.io/en/latest/?badge=latest)
[![PyPI version](https://badge.fury.io/py/pyos-sphinx-theme.svg)](https://badge.fury.io/py/pyos-sphinx-theme)
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
[![All Contributors](https://img.shields.io/badge/all_contributors-2-orange.svg?style=flat-square)](#contributors-)
<!-- ALL-CONTRIBUTORS-BADGE:END -->
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
[![All Contributors](https://img.shields.io/badge/all_contributors-1-orange.svg?style=flat-square)](#contributors-)
<!-- ALL-CONTRIBUTORS-BADGE:END -->

A modified version of the [pydata sphinx theme](https://
github.com/pydata/sphinx-theme) for use with the [pyOpenSci](https://pyopensci.org) project.
A modified version of the [PyData Sphinx Theme](github.com/pydata/sphinx-theme) for use with the [pyOpenSci](https://pyopensci.org) project.

## Contributors

Expand Down
5 changes: 3 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""pyOpenSci Sphinx Theme configuration."""

from pyos_sphinx_theme import __version__

project = "Sphinx pyOpenSci Theme"
project = "pyOpenSci Sphinx Theme"
copyright = "2024"
author = "pyOpenSci"
master_doc = "index"
Expand All @@ -26,7 +27,7 @@
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]

html_theme = "pyos_sphinx_theme"
html_title = "pyOpenSci Sphinx Theme"
html_title = project
html_copy_source = True
html_sourcelink_suffix = ""

Expand Down
51 changes: 39 additions & 12 deletions noxfile.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""
Build and preview this theme documentation locally.
"""Build and preview this theme documentation locally.
To build with a live-server:
Expand All @@ -9,8 +8,9 @@
nox -s docs -- -r
"""
import nox

import pathlib

import nox

nox.options.reuse_existing_virtualenvs = True
Expand All @@ -32,7 +32,7 @@
BUILD_PARAMETERS = ["-b", "html"]

# Sphinx parameters used to test the build of the guide
TEST_PARAMETERS = ['-W', '--keep-going', '-E', '-a']
TEST_PARAMETERS = ["-W", "--keep-going", "-E", "-a"]

AUTOBUILD_IGNORE = [
"_build",
Expand All @@ -41,6 +41,7 @@
"tmp",
]


@nox.session
def docs(session):
"""Build the packaging guide."""
Expand All @@ -50,18 +51,19 @@ def docs(session):

@nox.session(name="docs-live")
def docs_live(session):
"""
Build and launch a local copy of the guide.
"""Build and launch a local copy of the guide.
This session will use sphinx-autobuild to build the guide and launch a local server so you can
browse it locally. It will automatically rebuild the guide when changes are detected in the source.
browse it locally. It will automatically rebuild the guide when changes are
detected in the source.
It can be used with the language parameter to build a specific translation, for example:
nox -s docs-live -- -D language=es
Note: The docs-live-lang session below is provided as a convenience session for beginner contributors
so they don't need to remember the specific sphinx-build parameters to build a different language.
Note: The docs-live-lang session below is provided as a convenience session for beginner
contributors. so they don't need to remember the specific sphinx-build parameters to
build a different language.
"""
session.install("-e", ".")
cmd = [SPHINX_AUTO_BUILD, *BUILD_PARAMETERS, SOURCE_DIR, OUTPUT_DIR, *session.posargs]
Expand All @@ -72,12 +74,37 @@ def docs_live(session):
# cmd.extend(["--watch", folder])
session.run(*cmd)


@nox.session(name="docs-test")
def docs_test(session):
"""
Build the packaging guide with more restricted parameters.
"""Build the packaging guide with more restricted parameters.
Note: this is the session used in CI/CD to release the guide.
"""
session.install("-e", ".")
session.run(SPHINX_BUILD, *BUILD_PARAMETERS, *TEST_PARAMETERS, SOURCE_DIR, OUTPUT_DIR, *session.posargs)
session.run(
SPHINX_BUILD, *BUILD_PARAMETERS, *TEST_PARAMETERS, SOURCE_DIR, OUTPUT_DIR, *session.posargs
)


@nox.session(name="linkcheck")
def linkcheck(session):
"""Check the links in the documentation."""
session.install("-e", ".")
session.run(
SPHINX_BUILD,
*BUILD_PARAMETERS,
"-b",
"linkcheck",
SOURCE_DIR,
OUTPUT_DIR,
*session.posargs,
)


@nox.session(name="build-project")
def build_project(session):
"""Build the project and create a wheel distribution for pyproject.toml file."""
session.install("build", "twine")
session.run("python", "-m", "build")
session.run("twine", "check", "dist/*")
19 changes: 16 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ dependencies = [
]

license = { file = "LICENSE"}
authors = [
{name = "pyos", email = "[email protected]"},
]
classifiers = [
"Framework :: Sphinx",
"Framework :: Sphinx :: Theme",
Expand Down Expand Up @@ -59,3 +56,19 @@ docs = [

[project.entry-points."sphinx.html_themes"]
"pyos_sphinx_theme" = "pyos_sphinx_theme"

[tool.ruff]
line-length = 100
select = [
"E", # pycodestyle, see https://beta.ruff.rs/docs/rules/#pycodestyle-e-w
"D", # pydocstyle, see https://beta.ruff.rs/docs/rules/#pydocstyle-d
"F", # pyflakes, see https://beta.ruff.rs/docs/rules/#pyflakes-f
"I", # isort, see https://beta.ruff.rs/docs/rules/#isort-i
"N", # pep8-naming, see https://beta.ruff.rs/docs/rules/#pep8-naming-n
]
[tool.ruff.format]
quote-style = "double"

[tool.ruff.lint.isort]
combine-as-imports = true
force-sort-within-sections = true
94 changes: 50 additions & 44 deletions src/pyos_sphinx_theme/__init__.py
Original file line number Diff line number Diff line change
@@ -1,68 +1,72 @@
"""A lightweight theme for pyOpenSci."""

from pathlib import Path
# from sphinx_book_theme import hash_assets_for_files
from sphinx.util import logging

from pydata_sphinx_theme.utils import config_provided_by_user

# disable the video directive and html2dirhtml redirection because they are not implemented yet with this theme
from .video import Video # activate this line to enable the video directive
from .html2dirhtml import redirect_from_html_to_dirhtml # nactivate this line to enable the html2dirhtml redirection
# from sphinx_book_theme import hash_assets_for_files
from sphinx.util import logging

__version__ = "0.0.1dev0"
LOGGER = logging.getLogger(__name__)

THIS_PATH = Path(__file__).parent.resolve()
THEME_PATH = THIS_PATH / "theme" / "pyos_sphinx_theme"
LOGO_LIGHT = str((THEME_PATH / "static" / "images" / "logo-light-mode.png").absolute()).replace("\\", "/")
LOGO_DARK = str((THEME_PATH / "static" / "images" / "logo-dark-mode.png").absolute()).replace("\\", "/")
LOGO_LIGHT = str((THEME_PATH / "static" / "images" / "logo-light-mode.png").absolute()).replace(
"\\", "/"
)
LOGO_DARK = str((THEME_PATH / "static" / "images" / "logo-dark-mode.png").absolute()).replace(
"\\", "/"
)

PYOPENSCI_LOGO_PACKAGE_GUIDE = str(
(THEME_PATH / "static" / "images" / "pyopensci-logo-package-guide.png").absolute()
).replace("\\", "/")
PYOPENSCI_PYTHON_PACKAGE_GUIDE = str(
(THEME_PATH / "static" / "images" / "pyopensci-python-package-guide.png").absolute()
).replace("\\", "/")

PYOPENSCI_LOGO_PACKAGE_GUIDE = str((THEME_PATH / "static" / "images" / "pyopensci-logo-package-guide.png").absolute()).replace("\\", "/")
PYOPENSCI_PYTHON_PACKAGE_GUIDE = str((THEME_PATH / "static" / "images" /"pyopensci-python-package-guide.png").absolute()).replace("\\", "/")

def update_config(app):
"""Update the Sphinx config with theme-specific options."""
# These are the theme options that will be used in the build
theme_options = app.config.html_theme_options

# add icons to the navbar
if "icon_links" not in theme_options:

theme_options["icon_links"] =[
{
"name": "Mastodon",
"url": "https://fosstodon.org/@pyOpenSci",
"icon": "fa-brands fa-mastodon",
},
]
theme_options["icon_links"] = [
{
"name": "Mastodon",
"url": "https://fosstodon.org/@pyOpenSci",
"icon": "fa-brands fa-mastodon",
},
]

if "external_links" not in theme_options:
theme_options["external_links"] = [
{
"url": "https://www.pyopensci.org",
"name": "pyOpenSci Website",
},
{
"url": "https://www.pyopensci.org/software-peer-review",
"name": "Peer Review Guide",
},
{
"url": "https://pyopensci.org/handbook",
"name": "Handbook",
},
]

if not "logo" in theme_options:
theme_options["logo"] = {
"image_dark": LOGO_DARK,
"image_light": LOGO_LIGHT
}

if not "header_links_before_dropdown" in theme_options:
{
"url": "https://www.pyopensci.org",
"name": "pyOpenSci Website",
},
{
"url": "https://www.pyopensci.org/software-peer-review",
"name": "Peer Review Guide",
},
{
"url": "https://pyopensci.org/handbook",
"name": "Handbook",
},
]

if "logo" not in theme_options:
theme_options["logo"] = {"image_dark": LOGO_DARK, "image_light": LOGO_LIGHT}

if "header_links_before_dropdown" not in theme_options:
theme_options["header_links_before_dropdown"] = 4

# Social previews config
social_cards = app.config.__dict__.get("ogp_social_cards", {})

app.config.ogp_site_name = "pyOpenSci Python Package Guide"
app.config.ogp_social_cards = {
"line_color": "#6D597A",
Expand All @@ -84,9 +88,8 @@ def activate_extensions(app, extensions):
This also manually triggers the `config-inited` build step to account for
added extensions that hook into this event.
"""

# Remove all of the `config-inited` event listeners because they've already triggered
# We'll then re-trigger this event after adding extensions so that *only* their event hooks trigger
# Remove all of the `config-inited` event listeners because they've
# already triggered
old_listeners = app.events.listeners["config-inited"]
app.events.listeners["config-inited"] = []

Expand All @@ -105,14 +108,17 @@ def activate_extensions(app, extensions):


def setup(app):
"""Initialize Sphinx extension."""
app.add_html_theme("pyos_sphinx_theme", THEME_PATH)
app.config.html_favicon = "https://www.pyopensci.org/images/favicon.ico"
app.connect("builder-inited", update_config)
# app.connect("html-page-context", redirect_from_html_to_dirhtml)
# app.add_css_file("static/styles/pyos-sphinx-theme.css")
# app.add_js_file("static/scripts/matomo.js")
app.add_css_file("https://fonts.googleapis.com/css2?family=Itim&family=Poppins:wght@400;700&family=Work+Sans:wght@400;700")
# activate_extensions(app, add_extensions)
app.add_css_file(
"https://fonts.googleapis.com/css2?family=Itim&family=Poppins:wght@400;700&family=Work+Sans:wght@400;700"
)
# activate_extensions(app, add_extensions)

# Video directive
# app.add_directive("video", Video)
Expand Down
5 changes: 2 additions & 3 deletions src/pyos_sphinx_theme/assets/styles/pyos-sphinx-theme.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

@import "pydata_sphinx_theme.css";

// Load theme-specific SCSS
Expand Down Expand Up @@ -277,6 +276,6 @@ ugly scrollbar to show all the time
.bd-header .navbar-nav li a.nav-link:hover {
text-decoration: none;
text-decoration-thickness: none;
border-bottom: max(3px, .1875rem, .12em) solid var(--pst-color-link-hover);
border-bottom: max(3px, 0.1875rem, 0.12em) solid var(--pst-color-link-hover);
text-underline-offset: none;
}
}
10 changes: 7 additions & 3 deletions src/pyos_sphinx_theme/html2dirhtml.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from sphinx.util import logging
"""Module to redirect html to dirhtml pages in Sphinx."""

from pathlib import Path

from sphinx.util import logging

LOGGER = logging.getLogger(__name__)

REDIRECT_TEMPLATE = """
Expand All @@ -18,10 +21,11 @@
<a href="{rel_url}">If not, click here to continue.</a>
</body>
</html>
"""
""" # noqa: E501


def redirect_from_html_to_dirhtml(app, pagename, templatename, context, doctree):
"""If dirhtml builder is used, redirect pagename.html to the directory `pagename`"""
"""If dirhtml builder is used, redirect pagename.html to the directory `pagename`."""
if not hasattr(app, "builder"):
return
if app.builder.name == "dirhtml":
Expand Down
Loading

0 comments on commit 8b2cb17

Please sign in to comment.