# Generate the content for docs/glossary folder based on the format below:## # <folder Name># - [<title>](<../../../file-path>)## <folder-name>: the direct child folder name under docs/glossary.# <title>: the title("# <title>") of markdown.# <file-path>: the file name.## the contents are sorted based on the second line marker# <!-- generate.py marker: 20250123 -->importosimportrefromdatetimeimportdatetimedefgenerate_readme_for_folders(base_path):# Iterate through each direct child folder in the base pathforfolder_nameinos.listdir(base_path):folder_path=os.path.join(base_path,folder_name)# Only process directoriesifos.path.isdir(folder_path):readme_path=os.path.join(folder_path,"README.md")# If README.md doesn't exist, create itifnotos.path.exists(readme_path):print(f"Creating README.md in {folder_path}")# Gather Markdown files and sort by marker date (latest to oldest)md_files=[]forfile_nameinos.listdir(folder_path):file_path=os.path.join(folder_path,file_name)iffile_name.endswith(".md")andfile_name!="README.md":marker_date=extract_marker_date(file_path)md_files.append((marker_date,file_name))# Sort files by marker date (latest to oldest)md_files.sort(key=lambdax:x[0],reverse=True)# Generate content for README.mdcontent=f"# {folder_name}\n\n"content+=f"<!-- generate.py marker: {datetime.now().strftime('%Y%m%d')} -->\n\n"content+=f"contents file is generated by generate.py script.\n\n"formarker_date,file_nameinmd_files:file_path=os.path.join(folder_path,file_name)title=extract_title(file_path)content+=f"- [{title}](../../../{file_name})\n"# Write the README.md filewithopen(readme_path,"w",encoding="utf-8")asreadme_file:readme_file.write(content)defextract_title(file_path):"""Extract the first title (# ...) from a Markdown file."""try:withopen(file_path,"r",encoding="utf-8")asfile:forlineinfile:line=line.strip()ifline.startswith("# "):# Look for the first Markdown titlereturnline[2:].strip()# Remove the '# ' and return the titleexceptExceptionase:print(f"Error reading file {file_path}: {e}")return"Untitled"# Default title if no title is founddefextract_marker_date(file_path):"""Extract the marker date from the file (e.g., <!-- generate.py marker: YYYYMMDD -->)."""try:withopen(file_path,"r",encoding="utf-8")asfile:forlineinfile:line=line.strip()# Match the marker tag using regexmatch=re.search(r"<!-- generate\.py marker: (\d{8}) -->",line)ifmatch:returnint(match.group(1))# Return the date as an integer for sortingexceptExceptionase:print(f"Error reading file {file_path}: {e}")return0# Default to 0 if no marker is found# Replace 'your_base_directory' with the actual path to your base folderbase_directory="docs/glossary"generate_readme_for_folders(base_directory)