import yaml
import os, shutil
from pathlib import Path

html = ""

def doSomethingWithFile(p):
  html = ""
  with open(p, 'r') as file:
    html += "\n<li>"
    config = yaml.safe_load(file)

    if "name" in config:
      if config["name"]:
        html += "<strong><span>" + config["name"] + "</span></strong>"

    title = config["title"]
    if "subtitle" in config:
      if config["subtitle"]:
        title += " – " + config["subtitle"] 
    html += "<a href='{}'>{}</a></li>".format(p.replace('./',"").replace('config.yml', "index.php"),title)
    return html


for root, dirs, files in os.walk("."):
  for filename in files:
    if(filename == "config.yml"):
      html += doSomethingWithFile(os.path.join(root, filename))



# with os.scandir('.') as entries:
#   for entry in entries:
#     if(entry.is_dir()):
#       dirstodelete=["_inc","JoliTypo","Kaoken","PageTypeToPrint","Spyc", "theme","docs"]
#       for d in dirstodelete:
#         dirpath = Path(os.path.join(entry.name,d))        
#         if dirpath.exists() and dirpath.is_dir():
#           print(dirpath)
#           shutil.rmtree(dirpath)
      
#       dirstocopy=["_inc", "theme"]
#       for d in dirstocopy:
#         dest = Path(os.path.join(entry.name,d))
#         source = Path(os.path.join("/home/julienbidoret/Code/www/PageTypeToPrint/",d))
#         shutil.copytree(source, dest, dirs_exist_ok=True)
      

print(html)