for index_elem in index_elements: toc_links = index_elem.findall(".//ul[@class='markdownIt-TOC']//a") if toc_links isnotNone: header_link = index_elem.find(".//header//a") if header_link isnotNone: for toc_link in toc_links: old_href = toc_link.get('href') text = toc_link.text new_href = header_link.get('href') + old_href html_content = re.sub(fr'<a href="{old_href}">', f'<a href="{new_href}">',html_content)
withopen(html_file, 'w', encoding='utf-8') as f: f.write(html_content) if __name__ == '__main__': dir_path = sys.argv[1] iflen(sys.argv)>1else os.path.dirname(os.path.abspath(__file__)) for root, dirs, files in os.walk(dir_path): for subdir_count in dirs: file_path = os.path.join("page", subdir_count, "index.html") if os.path.exists(file_path): update_links(file_path) update_links(os.path.join(dir_path,"index.html"))
for index_elem in index_elements: toc_links = index_elem.findall(".//ul[@class='markdownIt-TOC']//a") if toc_links isnotNone: header_link = index_elem.find(".//header//a") if header_link isnotNone: for toc_link in toc_links: old_href = toc_link.get('href') text = toc_link.text new_href = header_link.get('href') + old_href html_content = re.sub(fr'<a href="{old_href}">', f'<a href="{new_href}">',html_content)
withopen(html_file, 'w', encoding='utf-8') as f: f.write(html_content) if __name__ == '__main__': dir_path = sys.argv[1] iflen(sys.argv)>1else os.path.dirname(os.path.abspath(__file__)) for root, dirs, files in os.walk(dir_path): for subdir_count in dirs: file_path = os.path.join("page", subdir_count, "index.html") if os.path.exists(file_path): update_links(file_path) update_links(os.path.join(dir_path,"index.html"))