Skip to content

Commit

Permalink
feat: Generate root file to navigate i18n contents
Browse files Browse the repository at this point in the history
  • Loading branch information
attakei committed Mar 23, 2024
1 parent d7ebdaa commit d40c351
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ dev-dependencies = [
"sphinx-tabs~=3.4.5",
"sphinx-autobuild~=2021.3.14",
"furo~=2024.1.29",
"beautifulsoup4~=4.12.3",
]

[tool.rye.scripts]
Expand Down
15 changes: 15 additions & 0 deletions src/atsphinx/mini18n/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"""Sphinx builder for i18n site on single deployment."""

from pathlib import Path
from subprocess import PIPE, run

from jinja2 import Template
from sphinx.application import Sphinx
from sphinx.builders.dummy import DummyBuilder

Expand All @@ -16,6 +18,19 @@ class Mini18nBuilderBase(DummyBuilder):
def init(self): # noqa: D102
self.app.connect("build-finished", self.build_i18_contents)

def finish(self): # noqa: D102
self.finish_tasks.add_task(self.build_heading_content)

def build_heading_content(self):
"""Generate root index file."""
ctx = {
"config": self.app.config,
}
out_index = self.app.outdir + "/index.html"
out_template = Path(__file__).parent / "templates" / "index.html"
template = Template(out_template.read_text())
Path(out_index).write_text(template.render(ctx))

def build_i18_contents(self, app, err):
"""Run build for coufigured multi-languages."""
cmd_base = [
Expand Down
24 changes: 24 additions & 0 deletions src/atsphinx/mini18n/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html>

<head>
<title>{{ config.html_title }}</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>

<body>
<script>
const url = new URL(window.location.href);
if (["/", "/index.html"].includes(url.pathname)) {
// TODO: Set default lauguage
url.pathname = "/en/";
location.href = url.href;
}
</script>

<noscript>
</noscript>
</body>

</html>
5 changes: 5 additions & 0 deletions tests/test_it.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from pathlib import Path

import pytest
from bs4 import BeautifulSoup
from sphinx.testing.util import SphinxTestApp


Expand All @@ -17,3 +18,7 @@ def test__custom_builder(app: SphinxTestApp):
"""Test to pass."""
app.build()
assert (Path(app.outdir) / "en").exists()
index_html = Path(app.outdir) / "index.html"
assert index_html.exists()
soup = BeautifulSoup(index_html.read_text(), "html.parser")
assert soup.title.get_text() == app.config.html_title

0 comments on commit d40c351

Please sign in to comment.