diff --git a/jweb/context.py b/jweb/context.py index 6ef44dc..7e115a6 100644 --- a/jweb/context.py +++ b/jweb/context.py @@ -20,13 +20,12 @@ class Context: self.__root_directory = root_directory self.__output_directory = root_directory / "build" self.__content_directory = root_directory / "content" - self.__environment = Environment( - loader=FileSystemLoader(searchpath=root_directory / "src"), - ) + self.__environment = Environment(loader=FileSystemLoader(searchpath=root_directory / "src")) self.__translations: dict[str, GNUTranslations | NullTranslations] = {} self.__current_language: str | None = None - self.add_filters(load=self.__load, write=self.__write) + self.add_filters(load=self.__load, write=self.__write, glob=self.__glob) + self.add_globals(load=self.__load, write=self.__write, glob=self.__glob) @property def current_language(self) -> str | None: @@ -35,6 +34,9 @@ class Context: def add_filters(self, **filters: Any) -> None: self.__environment.filters.update(**filters) + def add_globals(self, **globals: Any) -> None: + self.__environment.globals.update(**globals) + def load_extensions(self, *extensions: str) -> None: for extension in extensions: module = import_module(extension) @@ -80,6 +82,26 @@ class Context: with localized_path.open("r", encoding="utf-8") as content_file: return Content(localized_path, content_file.read(), current_language) + @pass_context + def __glob( + self, context: JinjaContext, pattern: str, include_base_language: bool = False + ) -> Iterator[Content]: + roots: list[Path] = [] + current_language = self.current_language + if current_language is None: + roots.append(self.__content_directory) + else: + if include_base_language: + roots.append(self.__content_directory / _DEFAULT_LANGUAGE) + roots.append(self.__content_directory / current_language) + + paths: set[Path] = set() + for root in roots: + paths = paths | set(it.relative_to(root) for it in root.glob(pattern)) + + for path in paths: + yield self.__load(context, path) + def __write(self, content: Content) -> Path: relative_path = content.path.relative_to(self.__content_directory) output_path = self.__output_directory / relative_path diff --git a/tests/test_context.py b/tests/test_context.py index c2c2620..4208402 100644 --- a/tests/test_context.py +++ b/tests/test_context.py @@ -51,3 +51,32 @@ def test_write(datadir: Path) -> None: with open(datadir / "build" / "output.html", encoding="utf-8") as ouput_file: assert ouput_file.read() == "/content.txt" + + +def test_glob(datadir: Path) -> None: + context = Context(datadir) + context.render("test-glob.html", "output.html") + + with open(datadir / "build" / "output.html", encoding="utf-8") as ouput_file: + assert ouput_file.read() == "Otters\nCaiman\n" + + +def test_glob_localized(datadir: Path) -> None: + context = Context(datadir) + context.load_translations("tests", datadir / "locale", "fr") + context.render("test-glob.html", "output.html") + + with open(datadir / "build" / "en" / "output.html", encoding="utf-8") as ouput_file: + assert ouput_file.read() == "Otters\nCaimans\n" + + with open(datadir / "build" / "fr" / "output.html", encoding="utf-8") as ouput_file: + assert ouput_file.read() == "Loutres\n" + + +def test_glob_localized_include_base_language(datadir: Path) -> None: + context = Context(datadir) + context.load_translations("tests", datadir / "locale", "fr") + context.render("test-glob-include-base-language.html", "output.html") + + with open(datadir / "build" / "fr" / "output.html", encoding="utf-8") as ouput_file: + assert ouput_file.read() == "Caimans\nLoutres\n" diff --git a/tests/test_context/content/other-content.txt b/tests/test_context/content/other-content.txt new file mode 100644 index 0000000..5b954d3 --- /dev/null +++ b/tests/test_context/content/other-content.txt @@ -0,0 +1 @@ +Caiman diff --git a/tests/test_context/locale/fr/LC_MESSAGES/tests.po b/tests/test_context/locale/fr/LC_MESSAGES/tests.po index 1c3f1db..b5e464d 100644 --- a/tests/test_context/locale/fr/LC_MESSAGES/tests.po +++ b/tests/test_context/locale/fr/LC_MESSAGES/tests.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PROJECT VERSION\n" "Report-Msgid-Bugs-To: EMAIL@ADDRESS\n" -"POT-Creation-Date: 2024-05-21 22:35+0200\n" +"POT-Creation-Date: 2024-05-21 23:12+0200\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language: fr\n" diff --git a/tests/test_context/src/test-glob-include-base-language.html b/tests/test_context/src/test-glob-include-base-language.html new file mode 100644 index 0000000..febb020 --- /dev/null +++ b/tests/test_context/src/test-glob-include-base-language.html @@ -0,0 +1,3 @@ +{% for content in glob("*.txt", include_base_language=True) | sort(attribute='path') -%} + {{ content -}} +{% endfor %} diff --git a/tests/test_context/src/test-glob.html b/tests/test_context/src/test-glob.html new file mode 100644 index 0000000..9c411d3 --- /dev/null +++ b/tests/test_context/src/test-glob.html @@ -0,0 +1,3 @@ +{% for content in glob("*.txt") | sort(attribute='path') -%} + {{ content -}} +{% endfor %}