#!/usr/bin/env python3
"""
批量更新所有 HTML 文件的導航列結構
從 9 個平鋪項目改為 6 個項目(含下拉菜單)
"""
import os
import re
from pathlib import Path
# 新的導航列HTML(6個項目,含下拉菜單)
NEW_NAVBAR_ITEMS = '''
'''
# 頁面對應的 active 標記
PAGE_ACTIVE_MAP = {
'dashboard.html': {'active_dashboard': ' active'},
'edm_dashboard.html': {'active_edm': '{edm_active}'}, # 保留原有的 Jinja2 邏輯
'sales_analysis.html': {'active_analysis': ' active'},
'daily_sales.html': {'active_analysis': ' active'},
'vendor_stockout_index.html': {'active_vendor': ' active'},
'vendor_stockout_list.html': {'active_vendor': ' active'},
'vendor_stockout_import.html': {'active_vendor': ' active'},
'vendor_stockout_send_email.html': {'active_vendor': ' active'},
'vendor_stockout_history.html': {'active_vendor': ' active'},
'auto_import_index.html': {'active_import': ' active'},
'settings.html': {'active_system': ' active'},
'system_settings.html': {'active_system': ' active'},
'logs.html': {'active_system': ' active'},
}
def update_navbar_in_file(file_path):
"""更新單個 HTML 文件的導航列"""
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# 檢查是否有舊的導航列結構
if '' not in content:
print(f"⏭️ 跳過 {file_path.name}(沒有找到導航列)")
return False
# 確定這個頁面的 active 標記
filename = file_path.name
active_map = PAGE_ACTIVE_MAP.get(filename, {})
# 準備新的導航列內容
navbar_items = NEW_NAVBAR_ITEMS
navbar_items = navbar_items.replace('{active_dashboard}', active_map.get('active_dashboard', ''))
navbar_items = navbar_items.replace('{active_edm}', active_map.get('active_edm', ''))
navbar_items = navbar_items.replace('{active_analysis}', active_map.get('active_analysis', ''))
navbar_items = navbar_items.replace('{active_vendor}', active_map.get('active_vendor', ''))
navbar_items = navbar_items.replace('{active_import}', active_map.get('active_import', ''))
navbar_items = navbar_items.replace('{active_system}', active_map.get('active_system', ''))
# 特殊處理 edm_dashboard.html 的 Jinja2 邏輯
if filename == 'edm_dashboard.html':
navbar_items = navbar_items.replace('{edm_active}', " {% if current_promo_page in ['edm', 'festival'] %}active{% endif %}")
else:
navbar_items = navbar_items.replace('{edm_active}', '')
# 替換導航列項目(使用正則匹配從 )
pattern = r''
new_content = re.sub(pattern, navbar_items, content, flags=re.DOTALL)
if new_content == content:
print(f"⏭️ 跳過 {file_path.name}(沒有變更)")
return False
# 同時確保使用 navbar-expand-xl
new_content = re.sub(r'