-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate_html.py
More file actions
70 lines (57 loc) · 2.33 KB
/
validate_html.py
File metadata and controls
70 lines (57 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import os
import sys
import logging
from pathlib import Path
from html_validator import validate_html_file, validate_directory, print_validation_report
# 配置日志
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler("validate_html.log"),
logging.StreamHandler(sys.stdout)
]
)
logger = logging.getLogger(__name__)
def main():
"""主函数:验证HTML文件或目录"""
try:
import argparse
parser = argparse.ArgumentParser(description='验证HTML文件或目录')
parser.add_argument('path', help='要验证的HTML文件或目录路径')
parser.add_argument('-r', '--recursive', action='store_true', help='递归验证目录中的所有HTML文件')
parser.add_argument('-v', '--verbose', action='store_true', help='显示详细报告')
args = parser.parse_args()
path = args.path
path_obj = Path(path)
logger.info(f"开始验证: {path}")
if path_obj.is_file():
logger.info(f"验证单个文件: {path}")
result = validate_html_file(path)
print_validation_report(result, args.verbose)
if result['valid']:
logger.info(f"文件验证通过: {path}")
return 0
else:
logger.error(f"文件验证失败: {path}")
return 1
elif path_obj.is_dir():
logger.info(f"验证目录: {path} (递归: {args.recursive})")
result = validate_directory(path, args.recursive)
print_validation_report(result, args.verbose)
if result['invalid_files'] == 0:
logger.info(f"所有文件验证通过: {path}")
return 0
else:
logger.warning(f"有 {result['invalid_files']} 个文件验证失败: {path}")
return 1
else:
logger.error(f"路径不存在: {path}")
print(f"错误: 路径不存在 - {path}")
return 1
except Exception as e:
logger.error(f"验证过程中发生错误: {e}")
print(f"错误: {e}")
return 1
if __name__ == '__main__':
sys.exit(main())