from url_mappings import *
import os
from pathlib import Path

def traverse(path):
	for item1 in os.listdir(path):
		subpath = (path + "/" if len(path) > 0 else "") + item1
		if os.path.isdir(subpath):
			# Subdirectory, traverse it as well
			for item2 in traverse(subpath):
				yield item2
		else:
			# File, simply return this file
			yield subpath

for input_path in traverse("source"):
	# Skip the following exceptional files, they are handled by other preprocessing scripts
	if input_path.split("/")[-1] not in ["general-information.md", "cli.rst"]: 
		output_path = "/".join(["preprocessed", ] + input_path.split("/")[1:])
		Path("/".join(output_path.split("/")[:-1])).mkdir(parents=True, exist_ok=True)
		with open(input_path, "r") as f_in:
			with open(output_path, "w") as f_out:
				f_out.write(apply_url_mappings(f_in.read()))
