|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import subprocess |
| 4 | +from bs4 import BeautifulSoup |
| 5 | + |
| 6 | +def build_website(): |
| 7 | + """Build the website using the specified command.""" |
| 8 | + try: |
| 9 | + subprocess.check_call(['./hack/docker/test.sh']) |
| 10 | + except subprocess.CalledProcessError as e: |
| 11 | + print(f"Website build failed: {e}") |
| 12 | + sys.exit(1) |
| 13 | + |
| 14 | +def check_img_tags(base_dir): |
| 15 | + """Recursively check HTML files for <img> tags without the lazy loading attribute.""" |
| 16 | + issues_found = False |
| 17 | + |
| 18 | + for root, _, files in os.walk(base_dir): |
| 19 | + for file in files: |
| 20 | + if file.endswith('.html'): |
| 21 | + file_path = os.path.join(root, file) |
| 22 | + with open(file_path, 'r', encoding='utf-8') as f: |
| 23 | + content = f.read() |
| 24 | + soup = BeautifulSoup(content, 'html.parser') |
| 25 | + for img in soup.find_all('img'): |
| 26 | + if 'loading' not in img.attrs or img.attrs['loading'] != 'lazy': |
| 27 | + issues_found = True |
| 28 | + line_number = find_line_number(content, img) |
| 29 | + print(f"File: {file_path}, Line: {line_number}, Missing lazy loading: {img}") |
| 30 | + |
| 31 | + return issues_found |
| 32 | + |
| 33 | +def find_line_number(content, tag): |
| 34 | + """Find the line number of the given tag in the content.""" |
| 35 | + content_lines = content.splitlines() |
| 36 | + |
| 37 | + |
| 38 | + tag_attrs = {k: ' '.join(v) if isinstance(v, list) else v for k, v in tag.attrs.items()} |
| 39 | + |
| 40 | + for i, line in enumerate(content_lines): |
| 41 | + |
| 42 | + if all(str(attr) in line for attr in tag_attrs.values()): |
| 43 | + return i + 1 |
| 44 | + |
| 45 | + return 'unknown' |
| 46 | + |
| 47 | +if __name__ == "__main__": |
| 48 | + |
| 49 | + build_website() |
| 50 | + |
| 51 | + |
| 52 | + base_dir = 'site' |
| 53 | + issues_found = check_img_tags(base_dir) |
| 54 | + |
| 55 | + |
| 56 | + if issues_found: |
| 57 | + sys.exit(1) |
| 58 | + else: |
| 59 | + print("All img tags have lazy loading attribute.") |
| 60 | + sys.exit(0) |
| 61 | + |
0 commit comments