Skip to content

Commit

Permalink
refactor: Delegete build-func to task instead of core event
Browse files Browse the repository at this point in the history
  • Loading branch information
attakei committed Mar 23, 2024
1 parent d40c351 commit 5345b18
Showing 1 changed file with 30 additions and 16 deletions.
46 changes: 30 additions & 16 deletions src/atsphinx/mini18n/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Sphinx builder for i18n site on single deployment."""

from dataclasses import dataclass
from pathlib import Path
from subprocess import PIPE, run

Expand All @@ -10,16 +11,27 @@
__version__ = "0.0.0"


@dataclass
class BuildArgs:
"""Parameter class to build other langage."""

app: Sphinx
builder: str
lang: str


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 finish(self): # noqa: D102
self.finish_tasks.add_task(self.build_heading_content)
for lang in [self.app.config.language]:
self.finish_tasks.add_task(
build_i18_contents,
BuildArgs(self.app, self.name[len(self.name_prefix) :], lang),
)

def build_heading_content(self):
"""Generate root index file."""
Expand All @@ -31,19 +43,21 @@ def build_heading_content(self):
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 = [
"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 build_i18_contents(args: BuildArgs):
"""Run build for coufigured language."""
cmd = [
"sphinx-build",
"-b",
args.builder,
"-d",
args.app.doctreedir,
args.app.srcdir,
"/".join([args.app.outdir, args.lang]),
"-D",
f"laungage={args.lang}",
]
run(cmd, stdout=PIPE, stderr=PIPE)


def register_i18n_builders(app: Sphinx):
Expand Down

0 comments on commit 5345b18

Please sign in to comment.