Skip to content

Commit

Permalink
FTBQ 颜色字符检查1.4 显示key
Browse files Browse the repository at this point in the history
  • Loading branch information
Wulian233 committed Jan 19, 2025
1 parent 11957a0 commit d3fe24a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 70 deletions.
3 changes: 1 addition & 2 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -629,8 +629,7 @@ to attach them to the start of each source file to most effectively
state the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.

<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
Copyright (C) 2024-present VM Chinese Translate group, Wulian233

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
Expand Down
25 changes: 8 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,18 @@
输入一个json文件路径或目录,会检查所有json文件内的颜色字符是否合法。

- 支持彩色提示信息。报错为红色,通过为绿色
- 新增支持导出错误报告为txt
- 可选是否在控制台打印详细信息
1. 开启会在控制台显示具体译文内容
2. 关闭会显示行号

开启详细信息:
```
[CNPack\kubejs\assets\ftbquest\lang\zh_cn.json] SyntaxError: Invalid character '。' after '&' at line 2705
"不用末影龙,直接制造&6龙息&。",
```

关闭详细信息:
- 支持导出错误报告为txt
- 支持检查单个或整个目录的json文件
- 支持检查json本身格式问题
- 错误会显示具体译文内容,出错位置和对应的键名

效果预览:
```
[CNPack\kubejs\assets\ftbquest\lang\zh_cn.json] SyntaxError: Invalid character after '&' at line 2705
[kubejs\assets\integrated_mc\lang\zh_cn.json] SyntaxError: Invalid character '里' after '&' at line 1
Value: 从附近的&b药水罐&里吸收&b药水&r以生产魔源。
Key: ftbquests.chapter.ars_nouveau.quest18.description1
```

注:无论选择什么,最终保存报错的文件永远为详细版本
- 支持检查单个或整个目录的json文件
- 支持检查json本身格式问题。

# Paratranz译文同步工具

批量上传多个文件到Paratranz翻译平台,同时支持下载译文到本地。
Expand Down
65 changes: 28 additions & 37 deletions ftbq_color_check/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,34 @@


def check_line_for_errors(
line: str, line_number: int, verbose: bool, relative_file_path: str
line: str, line_number: int, relative_file_path: str, key: str
) -> list[str]:
errors = []
matches = re.finditer(r"&([^a-v0-9\s])", line)
for match in matches:
error_message = f"SyntaxError{Fore.RESET}: {Fore.RED}Invalid character '{match.group(1)}' after '&' at line {line_number}\n {line}"
if verbose:
print(f"[{relative_file_path}] {Fore.RED}{error_message}")
else:
print(
f"[{relative_file_path}] SyntaxError{Fore.RESET}: {Fore.RED}Invalid character after '&' at line {line_number}"
)
error_message = f"SyntaxError{Fore.RESET}: {Fore.RED}Invalid character '{match.group(1)}' after '&' at line {line_number}\n {Fore.RESET}Value: {line.strip()}\n Key: {Fore.YELLOW}{key}"
print(f"[{relative_file_path}] {Fore.RED}{error_message}")
errors.append(f"[{relative_file_path}] {Fore.RED}{error_message}")
return errors


def check_json_file(
file_path: str, relative_file_path: str, verbose: bool
) -> list[str]:
def check_json_file(file_path: str, relative_file_path: str) -> list[str]:
errors = []
try:
with open(file_path, "r", encoding="utf-8-sig") as file:
try:
json.load(file)
json_data = json.load(file)
except json.JSONDecodeError as e:
error_message = f"JSONDecodeError: {e.msg} at line {e.lineno}"
print(f"[{relative_file_path}] {Fore.RED}{error_message}")
errors.append(f"[{relative_file_path}] {error_message}")
return errors

file.seek(0)
for line_number, line in enumerate(file, start=1):
if ":" in line:
_, value = line.split(":", 1)
for key, value in json_data.items():
for line_number, line in enumerate(value.splitlines(), start=1):
errors.extend(
check_line_for_errors(
value.strip(), line_number, verbose, relative_file_path
line.strip(), line_number, relative_file_path, key
)
)
except Exception as e:
Expand All @@ -54,7 +45,7 @@ def check_json_file(
return errors


def check_directory(directory_path: str, verbose: bool) -> list[str]:
def check_directory(directory_path: str) -> list[str]:
errors = []
found_json = False
for root, _, files in os.walk(directory_path):
Expand All @@ -63,15 +54,16 @@ def check_directory(directory_path: str, verbose: bool) -> list[str]:
for file_name in json_files:
file_path = os.path.join(root, file_name)
relative_file_path = os.path.relpath(file_path, start=directory_path)
errors.extend(check_json_file(file_path, relative_file_path, verbose))
errors.extend(check_json_file(file_path, relative_file_path))
if not found_json:
print(Fore.YELLOW + f"目录 '{directory_path}' 中没有找到任何 JSON 文件")
return errors


def remove_color_codes(text: str) -> str:
# 去除所有 ANSI 转义字符
return re.sub(r'\x1b\[[0-9;]*m', '', text)
"""去除所有 ANSI 转义字符"""
return re.sub(r"\x1b\[[0-9;]*m", "", text)


def save_errors_to_file(errors: list[str]) -> None:
with open("error_report.txt", "w", encoding="utf-8") as f:
Expand All @@ -81,27 +73,13 @@ def save_errors_to_file(errors: list[str]) -> None:


def main() -> None:
print(
Fore.LIGHTGREEN_EX
+ "FTB任务颜色字符合法检查 [版本 1.3 (2024)]\n作者:Wulian233(捂脸)\n\n"
+ Fore.RESET
+ """VM之禅:
一,即使翻译难易各异,译者应持己见自立。
二,即使遇到词句争议,组员务必同心共力。
三,即使译途坎坷跌宕,仍应坚守质量保障。
四,即使成果乏人褒奖,仍不计事后短长。
五,即使面临质疑声浪,仍要对正道守望。
六,即使译句纷乱无章,仍应看向前方、重塑文章。
"""
)
path = input("请输入 JSON 文件或目录路径:").strip()
verbose = input("是否显示详细错误信息?(y/n): ").strip().lower() == "y"

if os.path.isdir(path):
errors = check_directory(path, verbose)
errors = check_directory(path)
elif os.path.isfile(path) and path.endswith(".json"):
errors = check_json_file(
path, os.path.relpath(path, start=os.path.dirname(path)), verbose
path, os.path.relpath(path, start=os.path.dirname(path))
)
else:
print(f"{Fore.RED}输入的路径无效,请输入有效的 JSON 文件路径或目录。")
Expand All @@ -116,4 +94,17 @@ def main() -> None:


if __name__ == "__main__":
print(
Fore.LIGHTGREEN_EX
+ "FTB任务颜色字符合法检查 [版本 1.4 (2025)]\n作者:Wulian233(捂脸)\n\n"
+ Fore.RESET
+ """VM之禅:
一,即使翻译难易各异,译者应持己见自立。
二,即使遇到词句争议,组员务必同心共力。
三,即使译途坎坷跌宕,仍应坚守质量保障。
四,即使成果乏人褒奖,仍不计事后短长。
五,即使面临质疑声浪,仍要对正道守望。
六,即使译句纷乱无章,仍应看向前方、重塑文章。
"""
)
main()
27 changes: 13 additions & 14 deletions snbt_json_converter/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,6 @@ def convert_directory(directory_path: Path, to_json: bool) -> None:


def main() -> None:
print(
Fore.LIGHTGREEN_EX
+ "snbt json 互转工具 [版本 1.1 (2024)]\n作者:Wulian233(捂脸)\n\n"
+ Fore.RESET
+ """VM之禅:
一,即使翻译难易各异,译者应持己见自立。
二,即使遇到词句争议,组员勿必同心共力。
三,即使译途坏垣跌荡,仍应坚守质量保障。
四,即使成果乏人褒奖,仍不计事后短长。
五,即使面临质疑声涌,仍要对正道守望。
六,即使译句翻乱无章,仍应看向前方、重塑文章。
"""
)

path = input("请输入文件或目录路径:").strip()
input_path = Path(path)

Expand All @@ -93,4 +79,17 @@ def main() -> None:


if __name__ == "__main__":
print(
Fore.LIGHTGREEN_EX
+ "snbt json 互转工具 [版本 1.2 (2025)]\n作者:Wulian233(捂脸)\n\n"
+ Fore.RESET
+ """VM之禅:
一,即使翻译难易各异,译者应持己见自立。
二,即使遇到词句争议,组员勿必同心共力。
三,即使译途坏垣跌荡,仍应坚守质量保障。
四,即使成果乏人褒奖,仍不计事后短长。
五,即使面临质疑声涌,仍要对正道守望。
六,即使译句翻乱无章,仍应看向前方、重塑文章。
"""
)
main()

0 comments on commit d3fe24a

Please sign in to comment.