From 8b0e802d59d0b4fef979fcb1806fc05e3456b02c Mon Sep 17 00:00:00 2001 From: PhuNH <1079742+PhuNH@users.noreply.github.com> Date: Sat, 9 Sep 2023 09:16:40 +0200 Subject: [PATCH] Support JSON for config files, string files, and data files Version 0.5.0 --- hugo_gettext/config.py | 6 ++++-- hugo_gettext/utils.py | 8 ++++++++ pyproject.toml | 2 +- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/hugo_gettext/config.py b/hugo_gettext/config.py index c1ea412..b7cf334 100644 --- a/hugo_gettext/config.py +++ b/hugo_gettext/config.py @@ -68,7 +68,8 @@ def _find_string_file(default_lang: str) -> str: if not os.path.isdir('i18n'): return '' possible_paths = [f'i18n/{default_lang}.toml', - f'i18n/{default_lang}.yaml'] + f'i18n/{default_lang}.yaml', + f'i18n/{default_lang}.json'] for path in possible_paths: if os.path.isfile(path): return path @@ -76,7 +77,8 @@ def _find_string_file(default_lang: str) -> str: def _find_config_file() -> str: - possible_paths = ['hugo.toml', 'hugo.yaml', 'config.toml', 'config.yaml'] + possible_paths = ['hugo.toml', 'hugo.yaml', 'hugo.json', + 'config.toml', 'config.yaml', 'config.json'] for path in possible_paths: if os.path.isfile(path): return path diff --git a/hugo_gettext/utils.py b/hugo_gettext/utils.py index cb233cc..4566d4d 100644 --- a/hugo_gettext/utils.py +++ b/hugo_gettext/utils.py @@ -1,6 +1,7 @@ # SPDX-FileCopyrightText: 2023 Phu Hung Nguyen # SPDX-License-Identifier: LGPL-2.1-or-later +import json import os import re from enum import Enum @@ -51,6 +52,7 @@ class TextFormat(Enum): ELSE = '' YAML = '.yaml' TOML = '.toml' + JSON = '.json' @classmethod def decide_by_path(cls, path: str) -> 'TextFormat': @@ -58,6 +60,8 @@ def decide_by_path(cls, path: str) -> 'TextFormat': return cls.YAML elif ext == cls.TOML.value: return cls.TOML + elif ext == cls.JSON.value: + return cls.JSON else: return cls.ELSE @@ -66,6 +70,8 @@ def load_content(self, content: str): return yaml.safe_load(content) elif self == TextFormat.TOML: return tomlkit.loads(content) + elif self == TextFormat.JSON: + return json.loads(content) else: return {} @@ -74,6 +80,8 @@ def dump_obj(self, obj): return yaml.dump(obj, default_flow_style=False, allow_unicode=True) elif self == TextFormat.TOML: return tomlkit.dumps(obj) + elif self == TextFormat.JSON: + return json.dumps(obj, ensure_ascii=False, indent=4) else: return '' diff --git a/pyproject.toml b/pyproject.toml index 349004d..a0b2949 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -3,7 +3,7 @@ [tool.poetry] name = "hugo-gettext" -version = "0.4.1" +version = "0.5.0" description = "I18n with gettext for Hugo" authors = ["Phu Hung Nguyen "] license = "LGPL-2.1-or-later"