refactor(build_epub): replace fragile dataclass class-attr access with module constant

Language metadata (subdir, filename, title, subtitle) was stored as
EPUBConfig instance fields and then accessed as class-level attributes
in lang_map — a fragile pattern that silently breaks if any field loses
its default. Move this data to a module-level _LANG_METADATA dict and
remove the now-redundant vi_/en_/zh_ title/subtitle fields from EPUBConfig.
This commit is contained in:
Luong NGUYEN
2026-04-07 09:08:54 +02:00
parent 3885943071
commit 40a697c516
+26 -31
View File
@@ -92,6 +92,29 @@ class CoverGenerationError(EPUBBuildError):
# Configuration and State
# =============================================================================
# Language metadata: maps lang code → (subdir, output filename, title, subtitle).
# "" subdir means use the repo root (English content lives at the top level).
_LANG_METADATA: dict[str, tuple[str, str, str, str]] = {
"en": (
"",
"claude-howto-guide.epub",
"Claude Code How-To Guide",
"Master Claude Code in a Weekend",
),
"vi": (
"vi",
"claude-howto-guide-vi.epub",
"Hướng Dẫn Claude Code",
"Làm chủ Claude Code trong một cuối tuần",
),
"zh": (
"zh",
"claude-howto-guide-zh.epub",
"Claude Code 使用指南",
"一个周末掌握 Claude Code",
),
}
@dataclass
class EPUBConfig:
@@ -109,14 +132,6 @@ class EPUBConfig:
language: str = "en"
author: str = "Claude Code Community"
# Language-specific metadata
vi_title: str = "Hướng Dẫn Claude Code"
vi_subtitle: str = "Làm chủ Claude Code trong một cuối tuần"
en_title: str = "Claude Code How-To Guide"
en_subtitle: str = "Master Claude Code in a Weekend"
zh_title: str = "Claude Code 使用指南"
zh_subtitle: str = "一个周末掌握 Claude Code"
# Cover Settings
cover_width: int = 600
cover_height: int = 900
@@ -1081,29 +1096,9 @@ def main() -> int:
repo_root = args.root if args.root else Path(__file__).parent.parent
repo_root = repo_root.resolve()
# Set language-specific paths and metadata.
# Each entry: (source root, default output filename, title, subtitle)
lang_map: dict[str, tuple[Path, str, str, str]] = {
"en": (
repo_root,
"claude-howto-guide.epub",
EPUBConfig.en_title,
EPUBConfig.en_subtitle,
),
"vi": (
repo_root / "vi",
"claude-howto-guide-vi.epub",
EPUBConfig.vi_title,
EPUBConfig.vi_subtitle,
),
"zh": (
repo_root / "zh",
"claude-howto-guide-zh.epub",
EPUBConfig.zh_title,
EPUBConfig.zh_subtitle,
),
}
root, default_output_name, title, subtitle = lang_map[args.lang]
# Resolve language-specific paths and metadata from the module-level constant.
subdir, default_output_name, title, subtitle = _LANG_METADATA[args.lang]
root = repo_root / subdir if subdir else repo_root
output = args.output or (repo_root / default_output_name)
language = args.lang