제목 없음
• 읽는데 1분
import re from pathlib import Path
POSTS_DIR = Path("posts")
image_pattern = re.compile(r')')
def process_post_dir(post_dir: Path): md_path = post_dir / "index.md" if not md_path.exists(): return
text = md_path.read_text(encoding="utf-8")
matches = list(image_pattern.finditer(text))
if not matches:
return
rename_map = {}
counter = 1
for m in matches:
alt = m.group(1)
path = m.group(2)
filename = Path(path).name
old_file = post_dir / filename
if not old_file.exists():
continue
ext = old_file.suffix
new_name = f"{counter}{ext}"
rename_map[filename] = new_name
counter += 1
# 파일 이름 변경 (충돌 방지 위해 임시 이름 사용)
temp_map = {}
for old, new in rename_map.items():
old_path = post_dir / old
temp_path = post_dir / f"__tmp__{new}"
old_path.rename(temp_path)
temp_map[temp_path] = post_dir / new
for temp, final in temp_map.items():
temp.rename(final)
# md 경로 수정
def replace(match):
alt = match.group(1)
path = match.group(2)
name = Path(path).name
if name in rename_map:
return f""
return match.group(0)
new_text = image_pattern.sub(replace, text)
md_path.write_text(new_text, encoding="utf-8")
print(f"processed: {post_dir.name}")
def main(): for p in POSTS_DIR.iterdir(): if p.is_dir(): process_post_dir(p)
if name == "main": main()