149 lines
3.5 KiB
Python
149 lines
3.5 KiB
Python
import re
|
|||
|
|
import base64
|
||
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
# ==========================================================
|
||
|
|
# CONFIG
|
||
|
|
# ==========================================================
|
||
|
|
|
||
|
|
WORKING_DIR = Path(r"C:\Users\edsoe\Desktop\md_conversion_images")
|
||
|
|
|
||
|
|
ASSETS_DIR = WORKING_DIR / "assets" / "images"
|
||
|
|
ASSETS_DIR.mkdir(parents=True, exist_ok=True)
|
||
|
|
|
||
|
|
# ==========================================================
|
||
|
|
# MIME TYPE -> FILE EXTENSION
|
||
|
|
# ==========================================================
|
||
|
|
|
||
|
|
MIME_MAP = {
|
||
|
|
"image/png": "png",
|
||
|
|
"image/jpeg": "jpg",
|
||
|
|
"image/jpg": "jpg",
|
||
|
|
"image/gif": "gif",
|
||
|
|
"image/webp": "webp",
|
||
|
|
"image/svg+xml": "svg",
|
||
|
|
}
|
||
|
|
|
||
|
|
# ==========================================================
|
||
|
|
# REGEX
|
||
|
|
# ==========================================================
|
||
|
|
|
||
|
|
IMAGE_PATTERN = re.compile(
|
||
|
|
r'\[(.*?)\]:\s*<data:(image\/.*?);base64,(.*?)>',
|
||
|
|
re.DOTALL
|
||
|
|
)
|
||
|
|
|
||
|
|
# ==========================================================
|
||
|
|
# SLUGIFY
|
||
|
|
# ==========================================================
|
||
|
|
|
||
|
|
def slugify(text):
|
||
|
|
|
||
|
|
text = text.lower()
|
||
|
|
|
||
|
|
text = re.sub(r'[^a-z0-9]+', '-', text)
|
||
|
|
|
||
|
|
text = text.strip('-')
|
||
|
|
|
||
|
|
return text
|
||
|
|
|
||
|
|
# ==========================================================
|
||
|
|
# PROCESS FILE
|
||
|
|
# ==========================================================
|
||
|
|
|
||
|
|
def process_markdown_file(md_path: Path):
|
||
|
|
|
||
|
|
print(f"\nProcessing: {md_path.name}")
|
||
|
|
|
||
|
|
text = md_path.read_text(encoding="utf-8")
|
||
|
|
|
||
|
|
matches = list(IMAGE_PATTERN.finditer(text))
|
||
|
|
|
||
|
|
if not matches:
|
||
|
|
print(" No embedded base64 images found.")
|
||
|
|
return
|
||
|
|
|
||
|
|
article_slug = slugify(md_path.stem)
|
||
|
|
|
||
|
|
replacements = []
|
||
|
|
|
||
|
|
for match in matches:
|
||
|
|
|
||
|
|
image_key = match.group(1).strip()
|
||
|
|
mime_type = match.group(2).strip()
|
||
|
|
base64_data = match.group(3).strip()
|
||
|
|
|
||
|
|
extension = MIME_MAP.get(mime_type)
|
||
|
|
|
||
|
|
if not extension:
|
||
|
|
print(f" Unsupported MIME type: {mime_type}")
|
||
|
|
continue
|
||
|
|
|
||
|
|
image_filename = f"{article_slug}-{image_key}.{extension}"
|
||
|
|
|
||
|
|
image_path = ASSETS_DIR / image_filename
|
||
|
|
|
||
|
|
try:
|
||
|
|
|
||
|
|
image_binary = base64.b64decode(base64_data)
|
||
|
|
|
||
|
|
with open(image_path, "wb") as f:
|
||
|
|
f.write(image_binary)
|
||
|
|
|
||
|
|
print(f" Extracted: {image_filename}")
|
||
|
|
|
||
|
|
old_reference = match.group(0)
|
||
|
|
|
||
|
|
new_reference = (
|
||
|
|
f"[{image_key}]: "
|
||
|
|
f"/assets/images/{image_filename}"
|
||
|
|
)
|
||
|
|
|
||
|
|
replacements.append((old_reference, new_reference))
|
||
|
|
|
||
|
|
except Exception as e:
|
||
|
|
|
||
|
|
print(f" Failed to decode {image_key}: {e}")
|
||
|
|
|
||
|
|
# ======================================================
|
||
|
|
# REWRITE MARKDOWN
|
||
|
|
# ======================================================
|
||
|
|
|
||
|
|
cleaned_text = text
|
||
|
|
|
||
|
|
for old, new in replacements:
|
||
|
|
cleaned_text = cleaned_text.replace(old, new)
|
||
|
|
|
||
|
|
output_md = md_path.with_name(
|
||
|
|
md_path.stem + "_cleaned.md"
|
||
|
|
)
|
||
|
|
|
||
|
|
output_md.write_text(
|
||
|
|
cleaned_text,
|
||
|
|
encoding="utf-8"
|
||
|
|
)
|
||
|
|
|
||
|
|
print(f" Clean markdown saved:")
|
||
|
|
print(f" {output_md.name}")
|
||
|
|
|
||
|
|
# ==========================================================
|
||
|
|
# MAIN
|
||
|
|
# ==========================================================
|
||
|
|
|
||
|
|
def main():
|
||
|
|
|
||
|
|
md_files = list(WORKING_DIR.glob("*.md"))
|
||
|
|
|
||
|
|
if not md_files:
|
||
|
|
print("No markdown files found.")
|
||
|
|
return
|
||
|
|
|
||
|
|
print(f"Found {len(md_files)} markdown file(s).")
|
||
|
|
|
||
|
|
for md_file in md_files:
|
||
|
|
process_markdown_file(md_file)
|
||
|
|
|
||
|
|
print("\nDone.")
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
main()
|