quartz/clean-md.py
Ahmad Thakur a588a8fa37 Add and update Islamic Studies and Pakistan Affairs notes with new content and dates
- Added new sections on belief in angels, revealed books, concept of Islam, doctrine of Risalat, finality of prophethood, Islamic culture, and contemporary challenges in Islamic Studies.
- Updated Pakistan Affairs notes including topics on Article 370, BRICS, CPEC, climate change, political instability, and foreign policy.
- Revised existing notes for clarity and added relevant dates.
- Updated GitHub link in layout file and added Vercel configuration for clean URLs.
2025-07-19 17:53:08 +05:00

58 lines
1.8 KiB
Python

import os
import re
from datetime import datetime
def convert_to_frontmatter(filepath):
with open(filepath, 'r', encoding='utf-8') as f:
lines = f.readlines()
original_lines = lines[:]
# Extract title from line 0
title = lines[0].strip().lstrip("# ").strip() if lines and lines[0].startswith("#") else None
# Remove blank lines to find the actual date line
lines = lines[1:]
while lines and lines[0].strip() == "":
lines = lines[1:]
# Extract date from next line (line 1 after heading)
date_line = lines[0].strip() if lines else None
if re.fullmatch(r'\d{2}-\d{2}-\d{4}', date_line):
# Convert to YYYY-MM-DD
date = datetime.strptime(date_line, "%d-%m-%Y").strftime("%Y-%m-%d")
lines = lines[1:] # Remove the date line
else:
date = None # Optional: fallback or error handling
# Remove leading blank lines after date
while lines and lines[0].strip() == "":
lines = lines[1:]
# Build frontmatter
frontmatter = ["---\n"]
if title:
frontmatter.append(f'title: "{title}"\n')
if date:
frontmatter.append(f"date: {date}\n")
frontmatter.append("---\n\n")
# Combine and write
final_content = frontmatter + lines
with open(filepath, 'w', encoding='utf-8') as f:
f.writelines(final_content)
print(f"[{filepath}] ✔ Frontmatter added.")
def process_markdown_files(root_folder):
for dirpath, _, filenames in os.walk(root_folder):
for filename in filenames:
if filename.endswith(".md"):
filepath = os.path.join(dirpath, filename)
convert_to_frontmatter(filepath)
# 👇 Replace this with your folder path
root_folder = "coontent/Notes"
process_markdown_files(root_folder)