Debian Codegraph @

docs/hooks.py

raw
1 from __future__ import annotations
2
3 import re
4 from pathlib import Path
5 from typing import TYPE_CHECKING
6
7 if TYPE_CHECKING:
8 from mkdocs.config.defaults import MkDocsConfig
9 from mkdocs.structure.nav import Page
10
11
12 def _get_language_of_translation_file(path: Path) -> str:
13 with path.open(encoding='utf-8') as f:
14 translation_line = f.readline()
15 m = re.search('^# (.+) translations ', translation_line)
16 assert m
17 return m[1]
18
19
20 def on_page_markdown(markdown: str, page: Page, config: MkDocsConfig, **kwargs) -> str | None:
21 if page.file.src_uri == 'user-guide/choosing-your-theme.md':
22 here = Path(config.config_file_path).parent
23
24 def replacement(m: re.Match) -> str:
25 lines = []
26 for d in sorted(here.glob(m[2])):
27 lang = _get_language_of_translation_file(Path(d, 'LC_MESSAGES', 'messages.po'))
28 lines.append(f'{m[1]}`{d.name}`: {lang}')
29 return '\n'.join(lines)
30
31 return re.sub(
32 r'^( *\* )\(see the list of existing directories `(.+)`\)$',
33 replacement,
34 markdown,
35 flags=re.MULTILINE,
36 )