Meta Tags Generator — Python source
Build the SEO head tags for any page — title, description, canonical, robots, Open Graph with smart fallbacks, Twitter card and theme-color — with live length warnings.
This is the Python implementation — the same logic the interactive tool runs, in a shareable, citable form.
# meta-tags-generator — build the ordered SEO head-tag set: title,
# description, canonical, robots, Open Graph (og_title/og_description/og_url
# falling back to the base fields, og:type website whenever the block is
# present), Twitter card, theme-color. Python port (canonical TS:
# src/lib/meta-tags.ts; Go twin:
# cli/meta-tags-generator/meta-tags-generator.go). Same fields, same
# fallbacks, same emission order, same warnings (title > 60,
# description > 160) and the same hard errors (missing title/description,
# non-absolute og:image, non-hex theme-color suppress emission entirely).
from __future__ import annotations
import re
from dataclasses import dataclass, field
_HEX_RE = re.compile(r"^#[0-9a-fA-F]{6}$")
_HTTP_RE = re.compile(r"^https?://")
@dataclass
class MetaTagsInput:
title: str = ""
description: str = ""
canonical_url: str = ""
robots: str = ""
og_title: str = ""
og_description: str = ""
og_image: str = ""
og_url: str = ""
twitter_card: str = ""
twitter_site: str = ""
theme_color: str = ""
@dataclass
class MetaTagsResult:
ok: bool
errors: list[str] = field(default_factory=list)
warnings: list[str] = field(default_factory=list)
tags: list[str] = field(default_factory=list)
def build_meta_tags(inp: MetaTagsInput) -> MetaTagsResult:
"""Build the ordered head-tag set. Blank optional fields are skipped;
hard errors suppress emission entirely; length warnings never block."""
errors: list[str] = []
warnings: list[str] = []
title = inp.title.strip()
description = inp.description.strip()
canonical = inp.canonical_url.strip()
og_title = inp.og_title.strip() or title
og_description = inp.og_description.strip() or description
og_image = inp.og_image.strip()
og_url = inp.og_url.strip() or canonical
theme_color = inp.theme_color.strip()
if not title:
errors.append("Title is required.")
if not description:
errors.append("Description is required.")
if og_image and not _HTTP_RE.search(og_image):
errors.append("og:image must be an absolute http(s) URL.")
if theme_color and not _HEX_RE.search(theme_color):
errors.append("theme-color must be a #rrggbb hex value.")
if errors:
return MetaTagsResult(ok=False, errors=errors)
if len(title) > 60:
warnings.append(f"Title is {len(title)} characters — search results truncate around 60.")
if len(description) > 160:
warnings.append(f"Description is {len(description)} characters — search results truncate around 160.")
tags = [
f"<title>{title}</title>",
f'<meta name="description" content="{description}" />',
]
if canonical:
tags.append(f'<link rel="canonical" href="{canonical}" />')
if inp.robots.strip():
tags.append(f'<meta name="robots" content="{inp.robots.strip()}" />')
has_og = bool(inp.og_title.strip() or inp.og_description.strip() or og_image or og_url)
if has_og:
tags.append(f'<meta property="og:title" content="{og_title}" />')
tags.append(f'<meta property="og:description" content="{og_description}" />')
if og_image:
tags.append(f'<meta property="og:image" content="{og_image}" />')
if og_url:
tags.append(f'<meta property="og:url" content="{og_url}" />')
tags.append('<meta property="og:type" content="website" />')
if inp.twitter_card:
tags.append(f'<meta name="twitter:card" content="{inp.twitter_card}" />')
if inp.twitter_site.strip():
tags.append(f'<meta name="twitter:site" content="{inp.twitter_site.strip()}" />')
if theme_color:
tags.append(f'<meta name="theme-color" content="{theme_color}" />')
return MetaTagsResult(ok=True, warnings=warnings, tags=tags)
Also available in 13 other languages
Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →