migrate-contents-automation/redirect-site-upload-map/generate-nginx-map.py

58 lines
1.8 KiB
Python

import os
import yaml
VALID_TAGS = {"Tech", "Tech tutorials"}
CONTENT_DIR = "/var/www/html/arulbalaji.xyz/content/journal"
OUTPUT_DIR = os.path.join(os.path.dirname(__file__), "redirect-slugs")
OUTPUT_FILE = os.path.join(OUTPUT_DIR, "tech-blogs.map")
def extract_tags_and_slug(filepath):
print(f"Processing file: {filepath}")
with open(filepath, "r", encoding="utf-8") as f:
lines = f.readlines()
if not lines or lines[0].strip() != "---":
print(" - No front matter found.")
return None
front_matter = []
for line in lines[1:]:
if line.strip() == "---":
break
front_matter.append(line)
try:
data = yaml.safe_load(''.join(front_matter))
tags = set(map(str.strip, data.get("tags", [])))
print(f" - Tags found: {tags}")
if tags & VALID_TAGS:
slug = data.get("slug", os.path.splitext(os.path.basename(filepath))[0])
print(f" - Matched tags! Returning slug: /journal/{slug}")
return f"/journal/{slug}"
else:
print(" - No matching tags.")
except Exception as e:
print(f"Error parsing {filepath}: {e}")
return None
def generate_map():
os.makedirs(OUTPUT_DIR, exist_ok=True)
entries = []
for filename in os.listdir(CONTENT_DIR):
if filename.endswith(".md"):
path = os.path.join(CONTENT_DIR, filename)
result = extract_tags_and_slug(path)
if result:
entries.append(f"{result} 1;")
with open(OUTPUT_FILE, "w", encoding="utf-8") as f:
f.write("# Auto-generated by Python\n")
for entry in sorted(entries):
f.write(entry + "\n")
print(f"✅ Generated NGINX map at {OUTPUT_FILE} with {len(entries)} entries.")
if __name__ == "__main__":
generate_map()