Skip to content

Commit

Permalink
feat: Implement mini18n-xxx builder (en only)
Browse files Browse the repository at this point in the history
  • Loading branch information
attakei committed Mar 23, 2024
1 parent b629817 commit 4bad37c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
1 change: 1 addition & 0 deletions doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

# -- General configuration
extensions = [
"atsphinx.mini18n",
"sphinx.ext.todo",
"sphinx_tabs.tabs",
]
Expand Down
44 changes: 44 additions & 0 deletions src/atsphinx/mini18n.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,55 @@
"""Sphinx builder for i18n site on single deployment."""

from subprocess import PIPE, run

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

__version__ = "0.0.0"


class Mini18nBuilderBase(DummyBuilder):
"""Abstract bridge builder to build multi-language settings."""

name_prefix = "mini18n-"

def init(self): # noqa: D102
self.app.connect("build-finished", self.build_i18_contents)

def build_i18_contents(self, app, err):
"""Run build for coufigured multi-languages."""
cmd_base = [
"sphinx-build",
"-b",
self.name[len(self.name_prefix) :],
"-d",
app.doctreedir,
app.srcdir,
]
for lang in [app.config.language]:
cmd = cmd_base + ["/".join([self.outdir, lang]), "-D", f"laungage={lang}"]
run(cmd, stdout=PIPE, stderr=PIPE)


def register_i18n_builders(app: Sphinx):
"""Create and register dynamic builder for html output."""
new_builders = []
for name, Builder in app.registry.builders.items():
if Builder.format != "html":
continue

class Mini18nBuilder(Mini18nBuilderBase):
pass

Mini18nBuilder.name = f"mini18n-{name}"

new_builders.append(Mini18nBuilder)
for builder_class in new_builders:
app.add_builder(builder_class)


def setup(app: Sphinx): # noqa: D103
register_i18n_builders(app)
return {
"version": __version__,
"env_version": 1,
Expand Down
11 changes: 9 additions & 2 deletions tests/test_it.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
"""Standard tests."""

from io import StringIO
from pathlib import Path

import pytest
from sphinx.testing.util import SphinxTestApp


@pytest.mark.sphinx("html")
def test__it(app: SphinxTestApp, status: StringIO, warning: StringIO):
def test__it(app: SphinxTestApp):
"""Test to pass."""
app.build()


@pytest.mark.sphinx("mini18n-html")
def test__custom_builder(app: SphinxTestApp):
"""Test to pass."""
app.build()
assert (Path(app.outdir) / "en").exists()

0 comments on commit 4bad37c

Please sign in to comment.