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

Serve static files with the version in the name #23

Merged
merged 2 commits into from
Mar 11, 2024
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
8 changes: 3 additions & 5 deletions docs/source/_static/colorsets.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const DEFAULT = "cyan";
const DEFAULT = "default";

export class ColorSetHandler {
constructor() {
Expand Down Expand Up @@ -29,11 +29,9 @@ export class ColorSetHandler {
applyColor(color) {
// This method replaces the current stylesheet by the new one,
// based on the given color.
let stylesheet = color === DEFAULT
? `sphinx-nefertiti`
: `sphinx-nefertiti-${color}`;
let stylesheet = `sphinx-nefertiti-${color}`;

const re = new RegExp("\/(?<name>sphinx\-nefertiti[\-]?[a-z]*)\.");
const re = new RegExp("\/(?<name>sphinx\-nefertiti[\-]{1}[a-z]+)");
for (const sheet of document.getElementsByTagName("link")) {
if (sheet.href) {
const url = new URL(sheet.href);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sphinx-nefertiti",
"version": "0.2.3",
"version": "0.3.1",
"private": true,
"description": "Nefertiti is a theme for the Sphinx Documentation Generator.",
"engines": {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
@import "functions";

// 2. Include any default variable overrides here.
@import "variables";
@import "variables-cyan";

// 3. Include remainder of required Bootstrap stylesheets.
@import "../node_modules/bootstrap/scss/variables";
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

setup(
name="sphinx-nefertiti",
version="0.3.0",
version="0.3.1",
packages=find_packages(),
include_package_data=True,
license="MIT",
Expand Down
2 changes: 1 addition & 1 deletion site/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="/css/bootstrap-icons.css">
<link rel="stylesheet" href="/css/sphinx-nefertiti.css">
<link rel="stylesheet" href="/css/sphinx-nefertiti-cyan.css">
<link rel="stylesheet" type="text/css" href="/css/pygments.css">
<link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="/css/pygments_dark.css">
<link rel="stylesheet" type="text/css" href="/css/pygments_dark.css">
Expand Down
629 changes: 0 additions & 629 deletions site/index_wo_toc.html

This file was deleted.

49 changes: 38 additions & 11 deletions sphinx_nefertiti/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
"""sphinx-nefertiti theme"""

import json
from pathlib import Path
import pkg_resources
import sys
from pathlib import Path

from sphinx_nefertiti import colorsets, fonts, pygments, versions
from sphinx_nefertiti import colorsets, fonts, pygments, docsver, utils


__version__ = pkg_resources.require("sphinx_nefertiti")[0].version
__version_full__ = __version__
__version__ = utils.get_version()


def get_html_theme_path():
Expand All @@ -30,15 +28,17 @@ def initialize_theme(app):
pygments_provider = pygments.PygmentsProvider(app)
app.pygments_assets = [asset for asset in pygments_provider]

version_provider = versions.VersionProvider(app)
app.all_versions = [version for version in version_provider]
docsver_provider = docsver.DocsVersionProvider(app)
app.all_docs_versions = [version for version in docsver_provider]

except (fonts.FontNotSupportedException, Exception) as exc:
print(exc)
sys.exit(1)


def copy_nefertiti_files(app, exc):
static_path = Path(app.builder.outdir) / "_static"

if app.builder.format != "html" or exc:
return

Expand All @@ -52,13 +52,40 @@ def copy_nefertiti_files(app, exc):
for asset in app.pygments_assets:
asset.create_pygments_style_file(app.builder.outdir)

versions_json = Path(app.builder.outdir) / "_static" / "doc_versions.js"
versions_json = static_path / "doc_versions.js"
with versions_json.open("w") as f:
f.write("const doc_versions = " + json.dumps(app.all_versions))
f.write("const doc_versions = " + json.dumps(app.all_docs_versions))

# Rename files sphinx-nefertiti.min.js and bootstrap.bundle.min.js to
# add the version number.
rename_files = [ # Tuples: (orig_name, rename_to)
(
"sphinx-nefertiti.min.js",
f"sphinx-nefertiti-{utils.get_version()}.min.js",
),
(
"sphinx-nefertiti.min.js.map",
f"sphinx-nefertiti-{utils.get_version()}.min.js.map",
),
(
"bootstrap.bundle.min.js",
f"bootstrap.bundle-{utils.get_version()}.min.js",
),
]
for orig_name, rename_to in rename_files:
orig_path = static_path / orig_name
dest_path = static_path / rename_to
orig_path.rename(static_path / dest_path)


def update_context(app, pagename, templatename, context, doctree):
context["nefertiti_version"] = __version__
nftt_bundle = f"_static/sphinx-nefertiti-{utils.get_version()}.min.js"
bs_bundle = f"_static/bootstrap.bundle-{utils.get_version()}.min.js"

context["nefertiti_bundle"] = nftt_bundle
context["bootstrap_bundle"] = bs_bundle

context["nefertiti_version"] = utils.get_version()
context["show_colorset_choices"] = app.show_colorset_choices
context["all_colorsets"] = colorsets.all_colorsets

Expand All @@ -82,7 +109,7 @@ def setup(app):
app.add_html_theme("sphinx_nefertiti", theme_path)

return {
"version": __version__,
"version": utils.get_version(),
"parallel_read_safe": True,
"parallel_write_safe": True,
}
4 changes: 2 additions & 2 deletions sphinx_nefertiti/colorsets-dropdown.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ <h6 class="dropdown-header">{{ _('Change color set') }}</h6>
</li>
{% for item in all_colorsets %}
<li>
<a class="dropdown-item d-flex align-items-center" data-snftt-color="{% if item=='default' %}cyan{% else %}{{ item }}{% endif %}" href="#" aria-pressed="false">
<a class="dropdown-item d-flex align-items-center" data-snftt-color="{{ item }}" href="#" aria-pressed="false">
<span class="{% if item=='default' %}cyan{% else %}{{ item }}{% endif %} small">
<i class="bi bi-circle-fill"></i>
</span>
<span class="small ms-3">{{ _(item|capitalize) }}</span>
<span class="small ms-3">{% if item == "default" %}Cyan{% else %}{{ _(item|capitalize) }}{% endif %}</span>
<i class="bi bi-check ms-auto"></i>
</a>
</li>
Expand Down
21 changes: 11 additions & 10 deletions sphinx_nefertiti/colorsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import shutil


from sphinx_nefertiti import utils


colorsets_rel_path = Path("colorsets")
colorsets_abs_path = Path(__file__).parent / "colorsets"
static_abs_path = Path(__file__).parent / "static"
Expand All @@ -18,7 +21,7 @@
"yellow",
"green",
"teal",
"default",
"default", # cyan.
]


Expand All @@ -35,23 +38,21 @@ def __init__(self, name):
)
self._name = _name

@property
def name(self):
if self._name == "default":
return f"sphinx-nefertiti.min.css"
else:
return f"sphinx-nefertiti-{self._name}.min.css"
def get_name(self, with_version=False):
version = f"-{utils.get_version()}" if with_version else ""
return f"sphinx-nefertiti-{self._name}{version}.min.css"

@property
def link_stylesheet(self):
return str(colorsets_rel_path / self.name)
return str(colorsets_rel_path / self.get_name(with_version=True))

def copy_to_static(self, outdir: str, is_map_file=False):
ext = ".map" if is_map_file else ""
filename = f"{self.name}{ext}"
filename = f"{self.get_name()}{ext}"
filename_with_version = f"{self.get_name(with_version=True)}{ext}"
src_path = colorsets_abs_path / filename
dest_dir = Path(outdir) / "_static" / "colorsets"
dest_path = dest_dir / filename
dest_path = dest_dir / filename_with_version
if not dest_dir.exists():
os.mkdir(dest_dir)
shutil.copyfile(src_path, dest_path)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class VersionProvider:
class DocsVersionProvider:
def __init__(self, app):
theme_user_prefs = app.config.html_theme_options

Expand Down
6 changes: 4 additions & 2 deletions sphinx_nefertiti/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@
{%- for css in css_files %}
{%- if css not in ["_static/basic.css",] %}
{%- if css|attr("filename") %}
<!-- add (1) pathto "{{ css }}" -->
{{ css_tag(css) }}
{%- else %}
<!-- add (2) pathto "{{ css }}" -->
<link rel="stylesheet" href="{{ pathto(css, 1)|e }}" type="text/css" />
{%- endif %}
{%- endif %}
Expand Down Expand Up @@ -238,8 +240,8 @@
{%- endblock %}
{%- endif %}

<script type="text/javascript" src="{{ pathto('_static/bootstrap.bundle.min.js', 1) }}"></script>
<script type="text/javascript" src="{{ pathto('_static/sphinx-nefertiti.min.js', 1) }}"></script>
<script type="text/javascript" src="{{ pathto(nefertiti_bundle, 1) }}"></script>
<script type="text/javascript" src="{{ pathto(bootstrap_bundle, 1) }}"></script>
{% if show_colorset_choices %}<script type="module" src="{{ pathto('_static/colorsets.js', 1) }}"></script>{% endif %}
<script type="text/javascript" src="{{ pathto('_static/doc_versions.js', 1) }}"></script>
</body>
Expand Down
9 changes: 9 additions & 0 deletions sphinx_nefertiti/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
_CURRENT_VERSION = (0, 3, 1, "f", 0) # following PEP 440


def get_version():
major, minor, patch, letter, subv = _CURRENT_VERSION
ver = "%s.%s.%s" % (major, minor, patch)
if letter != "f":
ver = "%s%s%s" % (ver, letter, subv)
return ver
Loading