49 lines
1.6 KiB
Python
49 lines
1.6 KiB
Python
|
|
#!/usr/bin/env python3
|
||
|
|
"""
|
||
|
|
修复CSS文件中缺失的图片引用
|
||
|
|
"""
|
||
|
|
import os
|
||
|
|
import re
|
||
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
def check_and_fix_css_files():
|
||
|
|
"""检查并修复CSS文件中的图片引用"""
|
||
|
|
src_dir = Path('src')
|
||
|
|
fixed_count = 0
|
||
|
|
|
||
|
|
# 遍历所有CSS文件
|
||
|
|
for css_file in src_dir.rglob('*.css'):
|
||
|
|
try:
|
||
|
|
content = css_file.read_text(encoding='utf-8')
|
||
|
|
original_content = content
|
||
|
|
|
||
|
|
# 查找所有图片引用
|
||
|
|
pattern = r'background-image:\s*url\("@/assets/images/([^"]+)"\);'
|
||
|
|
matches = re.finditer(pattern, content)
|
||
|
|
|
||
|
|
for match in matches:
|
||
|
|
image_path = match.group(1)
|
||
|
|
full_path = src_dir / 'assets' / 'images' / image_path
|
||
|
|
|
||
|
|
# 如果图片不存在,注释掉这行
|
||
|
|
if not full_path.exists():
|
||
|
|
old_line = match.group(0)
|
||
|
|
new_line = f'/* {old_line} */'
|
||
|
|
content = content.replace(old_line, new_line)
|
||
|
|
print(f"已注释: {css_file} -> {image_path}")
|
||
|
|
fixed_count += 1
|
||
|
|
|
||
|
|
# 如果内容有变化,写回文件
|
||
|
|
if content != original_content:
|
||
|
|
css_file.write_text(content, encoding='utf-8')
|
||
|
|
|
||
|
|
except Exception as e:
|
||
|
|
print(f"处理 {css_file} 时出错: {e}")
|
||
|
|
|
||
|
|
return fixed_count
|
||
|
|
|
||
|
|
if __name__ == '__main__':
|
||
|
|
print("开始检查并修复CSS文件中的图片引用...")
|
||
|
|
count = check_and_fix_css_files()
|
||
|
|
print(f"\n完成!共修复 {count} 处缺失的图片引用")
|