|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Verification script for rich metadata extension. |
| 4 | +
|
| 5 | +This script checks if metadata has been properly injected into built HTML files. |
| 6 | +
|
| 7 | +Usage: |
| 8 | + python verify_metadata.py <path_to_built_html> |
| 9 | +
|
| 10 | +Example: |
| 11 | + python verify_metadata.py ../../_build/html/index.html |
| 12 | + python verify_metadata.py ../../_build/html/get-started/text.html |
| 13 | +""" |
| 14 | + |
| 15 | +import argparse |
| 16 | +import json |
| 17 | +import re |
| 18 | +import sys |
| 19 | +from pathlib import Path |
| 20 | + |
| 21 | + |
| 22 | +def extract_meta_tags(html_content: str) -> dict[str, list[str]]: |
| 23 | + """Extract all meta tags from HTML content.""" |
| 24 | + meta_tags = { |
| 25 | + "standard": [], |
| 26 | + "open_graph": [], |
| 27 | + "twitter": [], |
| 28 | + "custom": [], |
| 29 | + } |
| 30 | + |
| 31 | + # Extract standard meta tags |
| 32 | + for match in re.finditer(r'<meta name="([^"]+)" content="([^"]*)"', html_content): |
| 33 | + name, content = match.groups() |
| 34 | + meta_tags["standard"].append(f"{name}: {content}") |
| 35 | + |
| 36 | + # Extract Open Graph tags |
| 37 | + for match in re.finditer(r'<meta property="og:([^"]+)" content="([^"]*)"', html_content): |
| 38 | + name, content = match.groups() |
| 39 | + meta_tags["open_graph"].append(f"og:{name}: {content}") |
| 40 | + |
| 41 | + # Extract Twitter Card tags |
| 42 | + for match in re.finditer(r'<meta name="twitter:([^"]+)" content="([^"]*)"', html_content): |
| 43 | + name, content = match.groups() |
| 44 | + meta_tags["twitter"].append(f"twitter:{name}: {content}") |
| 45 | + |
| 46 | + return meta_tags |
| 47 | + |
| 48 | + |
| 49 | +def extract_json_ld(html_content: str) -> dict | None: |
| 50 | + """Extract JSON-LD structured data from HTML content.""" |
| 51 | + match = re.search( |
| 52 | + r'<script type="application/ld\+json">\s*(\{.*?\})\s*</script>', |
| 53 | + html_content, |
| 54 | + re.DOTALL, |
| 55 | + ) |
| 56 | + |
| 57 | + if match: |
| 58 | + try: |
| 59 | + return json.loads(match.group(1)) |
| 60 | + except json.JSONDecodeError as e: |
| 61 | + print(f"❌ Error parsing JSON-LD: {e}") |
| 62 | + return None |
| 63 | + |
| 64 | + return None |
| 65 | + |
| 66 | + |
| 67 | +def _display_meta_tags(tags: list[str], tag_type: str) -> bool: |
| 68 | + """Display meta tags of a specific type.""" |
| 69 | + if tags: |
| 70 | + print(f"✅ {tag_type}:") |
| 71 | + for tag in tags: |
| 72 | + print(f" • {tag}") |
| 73 | + print() |
| 74 | + return True |
| 75 | + |
| 76 | + print(f"⚠️ No {tag_type.lower()} found\n") |
| 77 | + return False |
| 78 | + |
| 79 | + |
| 80 | +def _display_json_ld(json_ld: dict | None) -> bool: |
| 81 | + """Display JSON-LD structured data.""" |
| 82 | + if not json_ld: |
| 83 | + print("⚠️ No JSON-LD structured data found\n") |
| 84 | + return False |
| 85 | + |
| 86 | + print("✅ JSON-LD Structured Data:") |
| 87 | + print(f" • @type: {json_ld.get('@type', 'N/A')}") |
| 88 | + print(f" • headline: {json_ld.get('headline', 'N/A')}") |
| 89 | + |
| 90 | + description = json_ld.get("description", "N/A") |
| 91 | + if description != "N/A": |
| 92 | + print(f" • description: {description[:80]}...") |
| 93 | + |
| 94 | + if "keywords" in json_ld and isinstance(json_ld["keywords"], list): |
| 95 | + print(f" • keywords: {', '.join(json_ld['keywords'][:5])}") |
| 96 | + |
| 97 | + if "audience" in json_ld: |
| 98 | + audience_type = json_ld["audience"].get("audienceType", []) |
| 99 | + if isinstance(audience_type, list): |
| 100 | + print(f" • audience: {', '.join(audience_type)}") |
| 101 | + |
| 102 | + if "proficiencyLevel" in json_ld: |
| 103 | + print(f" • proficiency: {json_ld['proficiencyLevel']}") |
| 104 | + |
| 105 | + print() |
| 106 | + return True |
| 107 | + |
| 108 | + |
| 109 | +def _display_no_metadata_help() -> None: |
| 110 | + """Display help message when no metadata is found.""" |
| 111 | + print("❌ No rich metadata found in this file.") |
| 112 | + print(" This could mean:") |
| 113 | + print(" • The page has no frontmatter") |
| 114 | + print(" • The extension is not enabled in conf.py") |
| 115 | + print(" • The template is not rendering {{ metatags }} or {{ rich_metadata }}") |
| 116 | + |
| 117 | + |
| 118 | +def verify_html_file(html_path: Path) -> bool: |
| 119 | + """ |
| 120 | + Verify that a built HTML file contains rich metadata. |
| 121 | +
|
| 122 | + Returns: |
| 123 | + True if metadata is present, False otherwise |
| 124 | + """ |
| 125 | + if not html_path.exists(): |
| 126 | + print(f"❌ File not found: {html_path}") |
| 127 | + return False |
| 128 | + |
| 129 | + print(f"\n{'='*80}") |
| 130 | + print(f"Verifying: {html_path.name}") |
| 131 | + print(f"{'='*80}\n") |
| 132 | + |
| 133 | + html_content = html_path.read_text(encoding="utf-8") |
| 134 | + |
| 135 | + # Extract metadata |
| 136 | + meta_tags = extract_meta_tags(html_content) |
| 137 | + json_ld = extract_json_ld(html_content) |
| 138 | + |
| 139 | + # Display results and track if any metadata was found |
| 140 | + has_metadata = False |
| 141 | + has_metadata |= _display_meta_tags(meta_tags["standard"], "Standard Meta Tags") |
| 142 | + has_metadata |= _display_meta_tags(meta_tags["open_graph"], "Open Graph Tags") |
| 143 | + has_metadata |= _display_meta_tags(meta_tags["twitter"], "Twitter Card Tags") |
| 144 | + has_metadata |= _display_json_ld(json_ld) |
| 145 | + |
| 146 | + # Overall result |
| 147 | + if has_metadata: |
| 148 | + print("✅ Rich metadata extension is working!") |
| 149 | + return True |
| 150 | + |
| 151 | + _display_no_metadata_help() |
| 152 | + return False |
| 153 | + |
| 154 | + |
| 155 | +def main() -> None: |
| 156 | + """Main entry point for the verification script.""" |
| 157 | + parser = argparse.ArgumentParser( |
| 158 | + description="Verify rich metadata injection in built HTML files" |
| 159 | + ) |
| 160 | + parser.add_argument( |
| 161 | + "html_files", |
| 162 | + nargs="+", |
| 163 | + type=Path, |
| 164 | + help="Path(s) to HTML file(s) to verify", |
| 165 | + ) |
| 166 | + parser.add_argument( |
| 167 | + "--verbose", |
| 168 | + "-v", |
| 169 | + action="store_true", |
| 170 | + help="Show detailed output", |
| 171 | + ) |
| 172 | + |
| 173 | + args = parser.parse_args() |
| 174 | + |
| 175 | + all_passed = True |
| 176 | + for html_file in args.html_files: |
| 177 | + if not verify_html_file(html_file): |
| 178 | + all_passed = False |
| 179 | + |
| 180 | + print(f"\n{'='*80}") |
| 181 | + if all_passed: |
| 182 | + print("✅ All files verified successfully!") |
| 183 | + else: |
| 184 | + print("⚠️ Some files are missing metadata") |
| 185 | + print(f"{'='*80}\n") |
| 186 | + |
| 187 | + sys.exit(0 if all_passed else 1) |
| 188 | + |
| 189 | + |
| 190 | +if __name__ == "__main__": |
| 191 | + main() |
| 192 | + |
0 commit comments