#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
一键修复所有音频问题
将 <div data-audio-src="..."> 转换为 <a href="sound://...">
"""

import os
import re
from pathlib import Path

def fix_audio_button(html_content):
    """
    将音频按钮从 div 转换为 a 标签，使用 sound:// 协议
    
    转换前:
    <div class="sound audio_play_button pron-uk icon-audio" 
         data-audio-src="audio/gb/zither__gb_1.mp3" 
         data-sound="sound://audio/gb/zither__gb_1.mp3" 
         title="..."></div>
    
    转换后:
    <a class="sound audio_play_button pron-uk icon-audio" 
       href="sound://audio/gb/zither__gb_1.mp3" 
       title="..."></a>
    
    注意：使用正斜杠 sound://audio/gb/... 有最好的兼容性
    """
    
    # 匹配音频按钮的正则表达式
    # 匹配 <div class="...audio_play_button..." data-audio-src="..." ...></div>
    pattern = r'<div\s+class="([^"]*audio_play_button[^"]*)"\s+data-audio-src="([^"]+)"\s+data-sound="[^"]*"\s+title="([^"]*)"[^>]*>\s*</div>'
    
    def replace_func(match):
        class_attr = match.group(1)  # class 属性值
        audio_path = match.group(2)  # audio/gb/file.mp3
        title_attr = match.group(3)  # title 属性值
        
        # 保持正斜杠格式，有最好的兼容性
        # audio/gb/file.mp3 -> sound://audio/gb/file.mp3
        
        # 构建新的 a 标签
        new_tag = f'<a class="{class_attr}" href="sound://{audio_path}" title="{title_attr}"></a>'
        
        return new_tag
    
    # 执行替换
    fixed_content = re.sub(pattern, replace_func, html_content)
    
    return fixed_content

def process_html_file(file_path):
    """处理单个HTML文件"""
    try:
        # 读取文件
        with open(file_path, 'r', encoding='utf-8') as f:
            content = f.read()
        
        # 检查是否包含音频按钮
        if 'audio_play_button' not in content:
            return False
        
        # 修复音频按钮
        fixed_content = fix_audio_button(content)
        
        # 检查是否有变化
        if fixed_content == content:
            return False
        
        # 写回文件
        with open(file_path, 'w', encoding='utf-8') as f:
            f.write(fixed_content)
        
        return True
        
    except Exception as e:
        print(f"处理文件 {file_path} 时出错: {e}")
        return False

def main():
    """主函数"""
    # 获取当前目录下所有HTML文件
    current_dir = Path('.')
    html_files = list(current_dir.glob('*.html'))
    
    if not html_files:
        print("当前目录没有找到HTML文件")
        return
    
    print(f"找到 {len(html_files)} 个HTML文件")
    print("开始修复...\n")
    
    processed_count = 0
    for html_file in html_files:
        if process_html_file(html_file):
            processed_count += 1
            print(f"✓ 已修复: {html_file.name}")
    
    print(f"\n完成! 共修复了 {processed_count} 个文件")
    print("\n修改说明:")
    print("1. <div> 标签改为 <a> 标签")
    print("2. data-audio-src 改为 href='sound://...'")
    print("3. 路径格式: 保持正斜杠 audio/gb/file.mp3 (最佳兼容性)")
    print("4. 移除了 data-sound 属性")
    print("\n✅ 现在可以在 GoldenDict 和欧路词典中正常播放音频了！")

if __name__ == '__main__':
    main()
