import os import re # 新的菜单HTML内容 MENU_HTML = '''
''' def update_menu_in_file(file_path, current_page): with open(file_path, 'r', encoding='utf-8') as f: content = f.read() # 更新当前页面的菜单项样式 menu_html = MENU_HTML.replace( f'href="{current_page}" class="flex items-center px-4 py-2 text-gray-300 hover:bg-gray-700 rounded"', f'href="{current_page}" class="flex items-center px-4 py-2 text-blue-300 bg-blue-600 rounded"' ) # 使用正则表达式替换菜单部分 pattern = r'
.*?
\s*\s*' new_content = re.sub(pattern, menu_html, content, flags=re.DOTALL) with open(file_path, 'w', encoding='utf-8') as f: f.write(new_content) def main(): # 获取所有HTML文件 html_files = [f for f in os.listdir('.') if f.endswith('.html')] # 更新每个文件的菜单 for html_file in html_files: print(f'Updating menu in {html_file}...') update_menu_in_file(html_file, html_file) if __name__ == '__main__': main()