Branchwell

PulseCanvas / website · pulsecanvas.ai / scripts / create-og-review.py

Raw 6.4 KB on 9a61f4f728665274f519e7ae9ba8b9b38ca7d963
1# Create a link-preview review page from actual built metadata.
2from html.parser import HTMLParser
3from html import escape
4from pathlib import Path
5from urllib.parse import urlsplit
6import json
7import sys
8root = Path(sys.argv[1] if len(sys.argv) > 1 else '.vercel/output/static')
9class Head(HTMLParser):
10 def __init__(self):
11 super().__init__(); self.meta = {}; self.title = ''; self.in_title = False; self.images = []
12 def handle_starttag(self, tag, attrs):
13 attrs = dict(attrs)
14 if tag == 'title': self.in_title = True
15 if tag == 'meta': self.meta[attrs.get('property', attrs.get('name', ''))] = attrs.get('content', '')
16 if tag == 'img' and attrs.get('src'): self.images.append(attrs['src'])
17 def handle_endtag(self, tag):
18 if tag == 'title': self.in_title = False
19 def handle_data(self, data):
20 if self.in_title: self.title += data
21rows = []
22home = None
23for file in sorted(root.rglob('index.html')):
24 head = Head(); head.feed(file.read_text())
25 route = urlsplit(head.meta.get('og:url', '')).path
26 if route == '/': home = head
27 rows.append(f'<article class="page"><a class="route" href="{escape(route)}">{escape(route)}</a><h3>{escape(head.title)}</h3><p>{escape(head.meta["description"])}</p><dl><dt>Social title</dt><dd>{escape(head.meta["og:title"])}</dd><dt>Address</dt><dd>{escape(head.meta.get("og:url", ""))}</dd></dl></article>')
28image_url = urlsplit(home.meta['og:image'])
29image_path = image_url.path + ('?' + image_url.query if image_url.query else '')
30html = '''<!doctype html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1"><meta name="robots" content="noindex, nofollow"><title>OG & SEO review — PulseCanvas Studio</title><link rel="icon" href="/images/favicon.png"><style>
31:root{color-scheme:dark;font-family:Arial,sans-serif;color:#f7f1ed;background:#17121b}*{box-sizing:border-box}body{margin:0}main{width:min(1060px,100% - 40px);margin:56px auto 80px}a{color:#f2b394}a:focus-visible{outline:3px solid #f2b394;outline-offset:5px}.eyebrow{color:#f2b394;text-transform:uppercase;letter-spacing:.14em;font-size:12px;font-weight:700}h1{font-size:clamp(32px,5vw,54px);line-height:1.08;margin:18px 0 16px;letter-spacing:-.04em}h2{font-size:26px;margin:40px 0 18px}p{line-height:1.7;color:#c8bdcd}.intro{max-width:710px;margin:0 0 28px}.hero{width:100%;height:auto;display:block;border-radius:16px;border:1px solid #3b303f}.meta-line{display:flex;flex-wrap:wrap;gap:14px;justify-content:space-between;font-size:13px;color:#b8adbf;margin:14px 0 30px}.split{display:grid;grid-template-columns:1fr 1fr;gap:22px}.card{background:#201925;border:1px solid #3b303f;border-radius:14px;overflow:hidden}.card img{display:block;width:100%;height:auto}.card-copy{padding:20px}.domain{font-size:12px;color:#c5b3df}.card h3{font-size:20px;line-height:1.3;margin:12px 0}.card p{font-size:14px;margin:0}.search-card{padding:24px;background:#fff;color:#202124;border-radius:14px;align-self:start}.search-card .search-domain{font-size:13px;color:#3c4043}.search-card h3{color:#1a0dab;font-size:22px;line-height:1.3;font-weight:400;margin:16px 0 10px}.search-card p{color:#4d5156;font-size:14px;margin:0}.caption{font-size:12px;color:#b8adbf;margin-top:10px}.checks{padding:22px 26px;border:1px solid #3b303f;border-radius:14px;background:#201925;line-height:1.8}.checks li{color:#d4c8db}.checks li+li{margin-top:8px}.status{color:#bde1c5}.page{border-top:1px solid #3b303f;padding:26px 0}.route{font-size:13px;overflow-wrap:anywhere}.page h3{font-size:22px;margin:12px 0}.page p{margin:0;font-size:15px;max-width:800px}dl{display:grid;grid-template-columns:95px 1fr;gap:8px;font-size:13px;margin-bottom:0}dt{color:#b8adbf}dd{margin:0;overflow-wrap:anywhere}.note{margin:22px 0;color:#c5b3df;font-size:14px}footer{padding-top:30px;border-top:1px solid #3b303f;margin-top:24px;font-size:14px}@media(max-width:680px){main{margin-top:32px;width:calc(100% - 32px)}.split{grid-template-columns:1fr}.checks{padding:20px}dl{grid-template-columns:1fr;gap:4px}dd{margin-bottom:8px}}
32</style></head><body><main><p class="eyebrow">PulseCanvas Studio · Production review</p><h1>Your next first impression.</h1><p class="intro">Review the new social image and metadata for all 11 pages. The social image and metadata are live.</p>'''
33html += f'<a href="{image_path}"><img class="hero" src="{image_path}" width="1200" height="630" alt="{escape(home.meta["og:image:alt"])}"></a><div class="meta-line"><span>1200 × 630 · PNG · Open Graph + X</span><a href="{image_path}">Open full image</a></div><h2>How the homepage could appear</h2><div class="split"><div><article class="card"><img src="{image_path}" width="1200" height="630" alt=""><div class="card-copy"><div class="domain">PULSECANVAS.AI</div><h3>{escape(home.meta["og:title"])}</h3><p>{escape(home.meta["og:description"])}</p></div></article><p class="caption">Illustrative social preview. Platforms may crop images and shorten text.</p></div><div><article class="search-card"><div class="search-domain">PulseCanvas Studio<br>https://pulsecanvas.ai</div><h3>{escape(home.title)}</h3><p>{escape(home.meta["description"])}</p></article><p class="caption">Illustrative search snippet. Search engines choose the final title and description.</p></div></div>'
34html += '''<h2>Metadata for every page</h2>'''
35html += ''.join(rows)
36html += '''<footer><a href="/">Open the live website</a> · <a href="https://developers.google.com/search/docs/essentials/technical">Google technical requirements</a> · <a href="https://developers.google.com/search/docs/appearance/structured-data/software-app">Software-app rich results</a></footer></main></body></html>'''
37review = Head()
38review.feed(html)
39for source in review.images:
40 asset = root / urlsplit(source).path.lstrip('/')
41 if not asset.is_file():
42 raise FileNotFoundError(f'Review image missing from deployment package: {source}')
43(root / 'og-review.html').write_text(html)
44# Vercel package: mark the page noindex in its routes (Cloudflare does this in _headers).
45config_path = Path('.vercel/output/config.json')
46if config_path.exists():
47 config = json.loads(config_path.read_text())
48 if not any(route.get('src') == '^/og-review\\.html$' for route in config['routes']):
49 config['routes'].insert(0, {'src': '^/og-review\\.html$', 'headers': {'X-Robots-Tag': 'noindex, nofollow'}, 'continue': True})
50 config_path.write_text(json.dumps(config, indent=2)+'\n')
51print('Link-preview review page created at /og-review.html.')