passive zrewidował ten Gist 1 year ago. Przejdź do rewizji
1 file changed, 32 insertions
add_spaces.py(stworzono plik)
| @@ -0,0 +1,32 @@ | |||
| 1 | + | import os | |
| 2 | + | import re | |
| 3 | + | import sys | |
| 4 | + | ||
| 5 | + | # Add spaces between Chinese and English | |
| 6 | + | def add_spaces_between_chinese_and_english(directory): | |
| 7 | + | md_files = [f for f in os.listdir(directory) if f.endswith((".md", ".mdx"))] | |
| 8 | + | pattern = re.compile(r'([\u4e00-\u9fff])([a-zA-Z0-9])|([a-zA-Z0-9])([\u4e00-\u9fff])') | |
| 9 | + | ||
| 10 | + | for md_file in md_files: | |
| 11 | + | md_path = os.path.join(directory, md_file) | |
| 12 | + | with open(md_path, "r", encoding="utf-8") as file: | |
| 13 | + | content = file.read() | |
| 14 | + | ||
| 15 | + | new_content = pattern.sub(lambda m: f'{m.group(1) or m.group(3)} {m.group(2) or m.group(4)}', content) | |
| 16 | + | ||
| 17 | + | with open(md_path, "w", encoding="utf-8") as file: | |
| 18 | + | file.write(new_content) | |
| 19 | + | ||
| 20 | + | print(f"Processed: {md_file}") | |
| 21 | + | ||
| 22 | + | if __name__ == "__main__": | |
| 23 | + | if len(sys.argv) != 2: | |
| 24 | + | print("Usage: python script.py <directory>") | |
| 25 | + | sys.exit(1) | |
| 26 | + | ||
| 27 | + | directory = sys.argv[1] | |
| 28 | + | if not os.path.isdir(directory): | |
| 29 | + | print("Error: Directory does not exist.") | |
| 30 | + | sys.exit(1) | |
| 31 | + | ||
| 32 | + | add_spaces_between_chinese_and_english(directory) | |
Nowsze
Starsze