From ccec8835842d2035fd52e09f62b8bec265b8b39e Mon Sep 17 00:00:00 2001 From: Alter-xyz <88554920+alterxyz@users.noreply.github.com> Date: Fri, 16 May 2025 21:41:57 +0800 Subject: [PATCH] chore: update tools --- .gitignore | 5 ++ tools/contributing_in_page.py | 148 ++++++++++++++++++++++------------ tools/rename_zh.py | 2 +- tools/requirements.txt | 1 + 4 files changed, 103 insertions(+), 53 deletions(-) create mode 100644 tools/requirements.txt diff --git a/.gitignore b/.gitignore index ad64a3f4..ac98462b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,7 @@ ._DS_Store .DS_Store +.venv +plugin_dev_zh_* +plugin_dev_ja_* +plugin_dev_en_* +/tool \ No newline at end of file diff --git a/tools/contributing_in_page.py b/tools/contributing_in_page.py index 7067bcb3..432e7d8d 100644 --- a/tools/contributing_in_page.py +++ b/tools/contributing_in_page.py @@ -4,7 +4,9 @@ import re BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) -def generate_contributing_section(repo_owner, repo_name, target_dir_relative, filename, language): +def generate_contributing_section( + repo_owner, repo_name, target_dir_relative, filename, language +): repo_url = f"https://github.com/{repo_owner}/{repo_name}" if language == "zh": contributing_section = f""" @@ -56,7 +58,7 @@ It will be automatically generated by the script. """ - elif language == "jp": + elif language == "ja": contributing_section = f""" {{/* Contributing Section @@ -83,13 +85,19 @@ It will be automatically generated by the script. """ else: raise ValueError( - "Unsupported language. Supported languages are 'zh', 'en', and 'jp'.") + "Unsupported language. Supported languages are 'zh', 'en', and 'ja'." + ) return contributing_section def append_content_to_files( - target_dir_relative, repo_owner, repo_name, language, base_dir=BASE_DIR, file_extension=".mdx" + target_dir_relative, + repo_owner, + repo_name, + language, + base_dir=BASE_DIR, + file_extension=".mdx", ): target_path = os.path.join(base_dir, target_dir_relative) @@ -106,8 +114,15 @@ def append_content_to_files( current_dir_relative = os.path.relpath(root, base_dir) try: with open(filepath, "a", encoding="utf-8") as f: - f.write(generate_contributing_section( - repo_owner, repo_name, current_dir_relative, filename, language)) + f.write( + generate_contributing_section( + repo_owner, + repo_name, + current_dir_relative, + filename, + language, + ) + ) appended_count += 1 except (IOError, OSError) as e: print(f"Error processing file {filepath}: {e}") @@ -118,7 +133,9 @@ def append_content_to_files( ) -def remove_contributing_section(target_dir_relative, base_dir=BASE_DIR, file_extension=".mdx"): +def remove_contributing_section( + target_dir_relative, base_dir=BASE_DIR, file_extension=".mdx" +): target_path = os.path.join(base_dir, target_dir_relative) if not os.path.isdir(target_path): print(f"Error: Directory '{target_path}' not found.") @@ -133,7 +150,9 @@ def remove_contributing_section(target_dir_relative, base_dir=BASE_DIR, file_ext with open(filepath, "r", encoding="utf-8") as f: content = f.read() pattern = re.compile( - r"\{\/\*\s*Contributing Section[\s\S]*?\*\/\}[\s\S]*?[\s\S]*?", re.DOTALL) + r"\{\/\*\s*Contributing Section[\s\S]*?\*\/\}[\s\S]*?[\s\S]*?", + re.DOTALL, + ) new_content = re.sub(pattern, "", content) if new_content != content: with open(filepath, "w", encoding="utf-8") as f: @@ -145,7 +164,8 @@ def remove_contributing_section(target_dir_relative, base_dir=BASE_DIR, file_ext fix_md_endings(target_dir_relative, base_dir, file_extension) print( f"Finished processing directory tree starting at '{target_path}'. " - f"Removed from {removed_count} files. Encountered {error_count} errors.") + f"Removed from {removed_count} files. Encountered {error_count} errors." + ) def fix_md_endings(target_dir_relative, base_dir=BASE_DIR, file_extension=".mdx"): @@ -174,49 +194,73 @@ def fix_md_endings(target_dir_relative, base_dir=BASE_DIR, file_extension=".mdx" error_count += 1 print( f"Finished processing directory tree starting at '{target_path}'. " - f"Fixed line endings and trailing lines in {fixed_count} files. Encountered {error_count} errors.") + f"Fixed line endings and trailing lines in {fixed_count} files. Encountered {error_count} errors." + ) + + +def refresh( + target_dir_relative, + repo_owner, + repo_name, + language, + base_dir=BASE_DIR, + file_extension=".mdx", +): + remove_contributing_section(target_dir_relative, base_dir, file_extension) + append_content_to_files( + target_dir_relative, repo_owner, repo_name, language, base_dir, file_extension + ) + + +def loop(dict): + for config_name, config_data in dict.items(): + refresh( + target_dir_relative=config_data["target_dir_relative"], + repo_owner=config_data["repo_owner"], + repo_name=config_data["repo_name"], + language=config_data["language"], + ) if __name__ == "__main__": - remove_contributing_section(target_dir_relative="plugin_dev_en") - remove_contributing_section(target_dir_relative="plugin_dev_zh") - remove_contributing_section(target_dir_relative="en") - remove_contributing_section(target_dir_relative="ja-jp") - remove_contributing_section(target_dir_relative="zh-hans") - remove_contributing_section(target_dir_relative="api_access") - append_content_to_files( - target_dir_relative="plugin_dev_en", - repo_owner="langgenius", - repo_name="dify-docs-mintlify", - language="en" - ) - append_content_to_files( - target_dir_relative="plugin_dev_zh", - repo_owner="langgenius", - repo_name="dify-docs-mintlify", - language="zh" - ) - append_content_to_files( - target_dir_relative="en", - repo_owner="langgenius", - repo_name="dify-docs-mintlify", - language="en" - ) - append_content_to_files( - target_dir_relative="ja-jp", - repo_owner="langgenius", - repo_name="dify-docs-mintlify", - language="jp" - ) - append_content_to_files( - target_dir_relative="zh-hans", - repo_owner="langgenius", - repo_name="dify-docs-mintlify", - language="zh" - ) - append_content_to_files( - target_dir_relative="api_access", - repo_owner="langgenius", - repo_name="dify-docs-mintlify", - language="en" - ) + process = { + # Help Documentation + "zh_help": { + "target_dir_relative": "zh-hans", + "repo_owner": "langgenius", + "repo_name": "dify-docs-mintlify", + "language": "zh", + }, + "en_help": { + "target_dir_relative": "en", + "repo_owner": "langgenius", + "repo_name": "dify-docs-mintlify", + "language": "en", + }, + "ja_help": { + "target_dir_relative": "ja-jp", + "repo_owner": "langgenius", + "repo_name": "dify-docs-mintlify", + "language": "ja", + }, + # Plugin Development + "zh_plugin_dev": { + "target_dir_relative": "plugin_dev_zh", + "repo_owner": "langgenius", + "repo_name": "dify-docs-mintlify", + "language": "zh", + }, + "en_plugin_dev": { + "target_dir_relative": "plugin_dev_en", + "repo_owner": "langgenius", + "repo_name": "dify-docs-mintlify", + "language": "en", + }, + "ja_plugin_dev": { + "target_dir_relative": "plugin_dev_ja-jp", + "repo_owner": "langgenius", + "repo_name": "dify-docs-mintlify", + "language": "ja" + }, + } + loop(process) diff --git a/tools/rename_zh.py b/tools/rename_zh.py index 1a88e972..61480a64 100644 --- a/tools/rename_zh.py +++ b/tools/rename_zh.py @@ -1,5 +1,5 @@ import os -import yaml +import yaml # pip install pyyaml import re import datetime diff --git a/tools/requirements.txt b/tools/requirements.txt new file mode 100644 index 00000000..8392d541 --- /dev/null +++ b/tools/requirements.txt @@ -0,0 +1 @@ +PyYAML==6.0.2