nuclear_explosions/tools/fix-astro-img.py

27 lines
634 B
Python
Raw Normal View History

2024-07-03 18:00:37 +00:00
#!/usr/bin/env python3
# Replaces all img tags with markdown ![image-tags](./aiming/at/output/dir)
import re
import os
import sys
if not os.getenv("QUARTO_PROJECT_RENDER_ALL"):
sys.exit(0)
q_output_dir = os.getenv("QUARTO_PROJECT_OUTPUT_DIR")
q_output_files = os.getenv("QUARTO_PROJECT_OUTPUT_FILES")
if not q_output_files:
sys.exit(1)
for fname in q_output_files.splitlines():
if not fname.endswith(".md"):
continue
with open(fname, "r") as f:
content = f.read()
modified = re.sub(r'<img src="(.+?)".*/>', r"![fig](./\1)", content)
with open(fname, "w") as f:
f.write(modified)