import os import re import sys # Add spaces between Chinese and English def add_spaces_between_chinese_and_english(directory): md_files = [f for f in os.listdir(directory) if f.endswith((".md", ".mdx"))] pattern = re.compile(r'([\u4e00-\u9fff])([a-zA-Z0-9])|([a-zA-Z0-9])([\u4e00-\u9fff])') for md_file in md_files: md_path = os.path.join(directory, md_file) with open(md_path, "r", encoding="utf-8") as file: content = file.read() new_content = pattern.sub(lambda m: f'{m.group(1) or m.group(3)} {m.group(2) or m.group(4)}', content) with open(md_path, "w", encoding="utf-8") as file: file.write(new_content) print(f"Processed: {md_file}") if __name__ == "__main__": if len(sys.argv) != 2: print("Usage: python script.py ") sys.exit(1) directory = sys.argv[1] if not os.path.isdir(directory): print("Error: Directory does not exist.") sys.exit(1) add_spaces_between_chinese_and_english(directory)