1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| import os import re
def fix_spacing_rules(line): line = re.sub(r'([\u4e00-\u9fff])([a-zA-Z])', r'\1 \2', line) line = re.sub(r'([a-zA-Z])([\u4e00-\u9fff])', r'\1 \2', line)
line = re.sub(r'([\u4e00-\u9fff])([0-9])', r'\1 \2', line) line = re.sub(r'([0-9])([\u4e00-\u9fff])', r'\1 \2', line)
units = ['%', '°C', 'km', 'm', 'kg', 'g', 'cm', 'mm', 'L', 'ml', 'h', 'min', 's'] for unit in units: line = re.sub(r'([0-9])\s+({})'.format(unit), r'\1\2', line)
line = re.sub(r'([\u3000-\u303F\uFF00-\uFFEF])\s+', r'\1', line) line = re.sub(r'\s+([\u3000-\u303F\uFF00-\uFFEF])', r'\1', line)
return line
def process_md_files(directory): for filename in os.listdir(directory): if filename.endswith(".md"): file_path = os.path.join(directory, filename) with open(file_path, 'r', encoding='utf-8') as file: lines = file.readlines()
updated_lines = [] inside_yaml_header = False
for line in lines: if line.strip() == "---": inside_yaml_header = not inside_yaml_header updated_lines.append(line) elif inside_yaml_header: updated_lines.append(line) else: updated_lines.append(fix_spacing_rules(line.rstrip()) + line[len(line.rstrip()):])
if lines != updated_lines: with open(file_path, 'w', encoding='utf-8') as file: file.writelines(updated_lines) print(f"Processed: {filename}")
directory_path = "your_directory_path_here" process_md_files(directory_path)
|