diff --git a/jweb/content.py b/jweb/content.py index 5ce4402..77aebf0 100644 --- a/jweb/content.py +++ b/jweb/content.py @@ -23,6 +23,8 @@ class Content(Generic[TData]): return self.__data def __str__(self) -> str: + if isinstance(self.__data, bytes): + return self.__data.decode("utf-8") return str(self.__data) diff --git a/jweb/context.py b/jweb/context.py index 9a46374..9bf329d 100644 --- a/jweb/context.py +++ b/jweb/context.py @@ -70,7 +70,7 @@ class Context: self.__render(source, self.__output_directory / output, **context) @pass_context - def __load(self, context: JinjaContext, path: str | Path | ContentField) -> Content[str]: + def __load(self, context: JinjaContext, path: str | Path | ContentField) -> Content[bytes]: if isinstance(path, ContentField): path = path.as_path() @@ -82,13 +82,13 @@ class Context: else: localized_path = self.__content_directory / path - with localized_path.open("r", encoding="utf-8") as content_file: + with localized_path.open("rb") 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[str]]: + ) -> Iterator[Content[bytes]]: roots: list[Path] = [] current_language = self.current_language if current_language is None: @@ -105,11 +105,11 @@ class Context: for path in paths: yield self.__load(context, path) - def __write(self, content: Content[str]) -> Path: + def __write(self, content: Content[bytes]) -> Path: relative_path = content.path.relative_to(self.__content_directory) output_path = self.__output_directory / relative_path output_path.parent.mkdir(parents=True, exist_ok=True) - with output_path.open("w") as output_file: + with output_path.open("wb") as output_file: output_file.write(content.data) return Path(f"/{relative_path}")