Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save faithandbrave/8b4197ec353acfd5cc5ab78d5dc26fc5 to your computer and use it in GitHub Desktop.

Select an option

Save faithandbrave/8b4197ec353acfd5cc5ab78d5dc26fc5 to your computer and use it in GitHub Desktop.
import os
import re
# 対象ディレクトリ(スクリプトのあるディレクトリ)
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
# .mdファイルを再帰的に探索
def find_md_files(base_dir):
for root, dirs, files in os.walk(base_dir):
for file in files:
if file.endswith('.md'):
yield os.path.join(root, file)
def replace_number_separator(text):
# (1)、(2) のようなパターンを (1), (2) に置換
# ただし、(数字) の直後に全角カンマが続き、次も (数字) である場合のみ
pattern = r'\((\d+)\)、(?=\(\d+\))'
return re.sub(pattern, r'(\1), ', text)
def process_file(filepath):
with open(filepath, 'r', encoding='utf-8') as f:
content = f.read()
new_content = replace_number_separator(content)
if new_content != content:
with open(filepath, 'w', encoding='utf-8') as f:
f.write(new_content)
print(f"[Replaced] {filepath}")
if __name__ == '__main__':
for md_file in find_md_files(BASE_DIR):
process_file(md_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment