mirror of
https://github.com/langgenius/dify-docs.git
synced 2026-03-27 13:28:32 +07:00
276 lines
10 KiB
Python
276 lines
10 KiB
Python
import os
|
||
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
|
||
):
|
||
repo_url = f"https://github.com/{repo_owner}/{repo_name}"
|
||
if language == "zh":
|
||
contributing_section = f"""
|
||
{{/*
|
||
Contributing Section
|
||
DO NOT edit this section!
|
||
It will be automatically generated by the script.
|
||
*/}}
|
||
|
||
<CardGroup cols="2">
|
||
<Card
|
||
title="编辑此页面"
|
||
icon="pen-to-square"
|
||
href="{repo_url}/edit/main/{target_dir_relative}/{filename}"
|
||
>
|
||
通过直接提交修改来帮助改进文档内容
|
||
</Card>
|
||
<Card
|
||
title="提交问题"
|
||
icon="github"
|
||
href="{repo_url}/issues/new?title=文档问题%3A%20{filename[5:-7]}&body=%23%23%20问题描述%0A%3C%21--%20请简要描述您发现的问题%20--%3E%0A%0A%23%23%20页面链接%0Ahttps%3A%2F%2Fgithub.com%2F{repo_owner}%2F{repo_name}%2Fblob%2Fmain%2F{target_dir_relative}%2F{filename}%0A%0A%23%23%20建议修改%0A%3C%21--%20如果有具体的修改建议,请在此说明%20--%3E%0A%0A%3C%21--%20感谢您对文档质量的关注!%20--%3E"
|
||
>
|
||
发现错误或有改进建议?请提交问题反馈
|
||
</Card>
|
||
</CardGroup>
|
||
"""
|
||
elif language == "en":
|
||
contributing_section = f"""
|
||
{{/*
|
||
Contributing Section
|
||
DO NOT edit this section!
|
||
It will be automatically generated by the script.
|
||
*/}}
|
||
|
||
<CardGroup cols="2">
|
||
<Card
|
||
title="Edit this page"
|
||
icon="pen-to-square"
|
||
href="{repo_url}/edit/main/{target_dir_relative}/{filename}"
|
||
>
|
||
Help improve our documentation by contributing directly
|
||
</Card>
|
||
<Card
|
||
title="Report an issue"
|
||
icon="github"
|
||
href="{repo_url}/issues/new?title=Documentation%20Issue%3A%20{filename[5:-7]}&body=%23%23%20Issue%20Description%0A%3C%21--%20Please%20briefly%20describe%20the%20issue%20you%20found%20--%3E%0A%0A%23%23%20Page%20Link%0Ahttps%3A%2F%2Fgithub.com%2F{repo_owner}%2F{repo_name}%2Fblob%2Fmain%2F{target_dir_relative}%2F{filename}%0A%0A%23%23%20Suggested%20Changes%0A%3C%21--%20If%20you%20have%20specific%20suggestions%20for%20changes%2C%20please%20describe%20them%20here%20--%3E%0A%0A%3C%21--%20Thank%20you%20for%20helping%20improve%20our%20documentation%21%20--%3E"
|
||
>
|
||
Found an error or have suggestions? Let us know
|
||
</Card>
|
||
</CardGroup>
|
||
"""
|
||
elif language == "ja":
|
||
contributing_section = f"""
|
||
{{/*
|
||
Contributing Section
|
||
DO NOT edit this section!
|
||
It will be automatically generated by the script.
|
||
*/}}
|
||
|
||
<CardGroup cols="2">
|
||
<Card
|
||
title="このページを編集する"
|
||
icon="pen-to-square"
|
||
href="{repo_url}/edit/main/{target_dir_relative}/{filename}"
|
||
>
|
||
直接貢献することでドキュメントの改善にご協力ください
|
||
</Card>
|
||
<Card
|
||
title="問題を報告する"
|
||
icon="github"
|
||
href="{repo_url}/issues/new?title=ドキュメントの問題%3A%20{filename[5:-7]}&body=%23%23%20問題の説明%0A%3C%21--%20発見した問題について簡単に説明してください%20--%3E%0A%0A%23%23%20ページリンク%0Ahttps%3A%2F%2Fgithub.com%2F{repo_owner}%2F{repo_name}%2Fblob%2Fmain%2F{target_dir_relative}%2F{filename}%0A%0A%23%23%20提案される変更%0A%3C%21--%20特定の変更案がある場合は、ここで説明してください%20--%3E%0A%0A%3C%21--%20ドキュメントの品質向上にご協力いただきありがとうございます!%20--%3E"
|
||
>
|
||
エラーを見つけたり提案がありますか?お知らせください
|
||
</Card>
|
||
</CardGroup>
|
||
"""
|
||
else:
|
||
raise ValueError(
|
||
"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_path = os.path.join(base_dir, target_dir_relative)
|
||
|
||
if not os.path.isdir(target_path):
|
||
print(f"Error: Directory '{target_path}' not found.")
|
||
return
|
||
fix_md_endings(target_dir_relative, base_dir, file_extension)
|
||
appended_count = 0
|
||
error_count = 0
|
||
for root, dirs, files in os.walk(target_path):
|
||
for filename in files:
|
||
if filename.endswith(file_extension):
|
||
filepath = os.path.join(root, filename)
|
||
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,
|
||
)
|
||
)
|
||
appended_count += 1
|
||
except (IOError, OSError) as e:
|
||
print(f"Error processing file {filepath}: {e}")
|
||
error_count += 1
|
||
print(
|
||
f"Finished processing directory tree starting at '{target_path}'. "
|
||
f"Appended to {appended_count} files. Encountered {error_count} errors."
|
||
)
|
||
|
||
|
||
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.")
|
||
return
|
||
removed_count = 0
|
||
error_count = 0
|
||
for root, dirs, files in os.walk(target_path):
|
||
for filename in files:
|
||
if filename.endswith(file_extension):
|
||
filepath = os.path.join(root, filename)
|
||
try:
|
||
with open(filepath, "r", encoding="utf-8") as f:
|
||
content = f.read()
|
||
pattern = re.compile(
|
||
r"\{\/\*\s*Contributing Section[\s\S]*?\*\/\}[\s\S]*?<CardGroup cols=\"2\">[\s\S]*?</CardGroup>",
|
||
re.DOTALL,
|
||
)
|
||
new_content = re.sub(pattern, "", content)
|
||
if new_content != content:
|
||
with open(filepath, "w", encoding="utf-8") as f:
|
||
f.write(new_content)
|
||
removed_count += 1
|
||
except (IOError, OSError) as e:
|
||
print(f"Error processing file {filepath}: {e}")
|
||
error_count += 1
|
||
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."
|
||
)
|
||
|
||
|
||
def fix_md_endings(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.")
|
||
return
|
||
fixed_count = 0
|
||
error_count = 0
|
||
for root, dirs, files in os.walk(target_path):
|
||
for filename in files:
|
||
if filename.endswith(file_extension):
|
||
filepath = os.path.join(root, filename)
|
||
try:
|
||
with open(filepath, "r", encoding="utf-8") as f:
|
||
content = f.read()
|
||
processed_content = content.replace("\r\n", "\n")
|
||
processed_content = processed_content.rstrip()
|
||
processed_content += "\n"
|
||
if processed_content != content:
|
||
with open(filepath, "w", encoding="utf-8") as f:
|
||
f.write(processed_content)
|
||
fixed_count += 1
|
||
except (IOError, OSError) as e:
|
||
print(f"Error processing file {filepath}: {e}")
|
||
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."
|
||
)
|
||
|
||
|
||
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"],
|
||
)
|
||
|
||
|
||
def main_contributing_in_page():
|
||
process = {
|
||
# Help Documentation
|
||
"zh_help": {
|
||
"target_dir_relative": "zh-hans",
|
||
"repo_owner": "langgenius",
|
||
"repo_name": "dify-docs",
|
||
"language": "zh",
|
||
},
|
||
"en_help": {
|
||
"target_dir_relative": "en",
|
||
"repo_owner": "langgenius",
|
||
"repo_name": "dify-docs",
|
||
"language": "en",
|
||
},
|
||
"ja_help": {
|
||
"target_dir_relative": "ja-jp",
|
||
"repo_owner": "langgenius",
|
||
"repo_name": "dify-docs",
|
||
"language": "ja",
|
||
},
|
||
# Plugin Development
|
||
"zh_plugin_dev": {
|
||
"target_dir_relative": "plugin-dev-zh",
|
||
"repo_owner": "langgenius",
|
||
"repo_name": "dify-docs",
|
||
"language": "zh",
|
||
},
|
||
"en_plugin_dev": {
|
||
"target_dir_relative": "plugin-dev-en",
|
||
"repo_owner": "langgenius",
|
||
"repo_name": "dify-docs",
|
||
"language": "en",
|
||
},
|
||
"ja_plugin_dev": {
|
||
"target_dir_relative": "plugin-dev-ja",
|
||
"repo_owner": "langgenius",
|
||
"repo_name": "dify-docs",
|
||
"language": "ja"
|
||
},
|
||
}
|
||
try:
|
||
loop(process)
|
||
return "success"
|
||
except Exception as e:
|
||
return (f"{str(e)}")
|
||
|
||
if __name__ == "__main__":
|
||
result_message = main_contributing_in_page()
|
||
print("\n--- Script Execution Result ---")
|
||
print(result_message)
|