Skip to content

Commit

Permalink
Support JSON for config files, string files, and data files
Browse files Browse the repository at this point in the history
Version 0.5.0
  • Loading branch information
PhuNH committed Sep 9, 2023
1 parent 720a3b6 commit 8b0e802
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
6 changes: 4 additions & 2 deletions hugo_gettext/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,17 @@ 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
return ''


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
Expand Down
8 changes: 8 additions & 0 deletions hugo_gettext/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SPDX-FileCopyrightText: 2023 Phu Hung Nguyen <[email protected]>
# SPDX-License-Identifier: LGPL-2.1-or-later

import json
import os
import re
from enum import Enum
Expand Down Expand Up @@ -51,13 +52,16 @@ class TextFormat(Enum):
ELSE = ''
YAML = '.yaml'
TOML = '.toml'
JSON = '.json'

@classmethod
def decide_by_path(cls, path: str) -> 'TextFormat':
if (ext := os.path.splitext(path)[1]) == cls.YAML.value or ext == '.yml':
return cls.YAML
elif ext == cls.TOML.value:
return cls.TOML
elif ext == cls.JSON.value:
return cls.JSON
else:
return cls.ELSE

Expand All @@ -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 {}

Expand All @@ -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 ''

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>"]
license = "LGPL-2.1-or-later"
Expand Down

0 comments on commit 8b0e802

Please sign in to comment.