-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support JSON for config files, string files, and data files
Version 0.5.0
- Loading branch information
Showing
3 changed files
with
13 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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 | ||
|
||
|
@@ -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 '' | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|