mirror of
https://github.com/morluto/rea.git
synced 2026-12-05 14:31:22 +00:00
The default sharing card shows the pixel wordmark beside disassembly, a decompiled function, a hex dump and the agent workflow. The 1280 × 640 design is scaled to 1200 wide and extended with its own edge colours to 1200 × 630, so nothing is cropped. Chat apps and social sites cache link previews by image URL, so Discord still showed an older white card. The default card is now published as assets/rea-social-card.png, which those sites have not cached.
206 lines
8.5 KiB
Python
206 lines
8.5 KiB
Python
"""Prepare downloads, the sitemap and the sharing image for the static website."""
|
|
|
|
from io import BytesIO
|
|
from pathlib import Path
|
|
import json
|
|
import sys
|
|
from urllib.parse import urljoin
|
|
import xml.etree.ElementTree as ET
|
|
from zipfile import ZipFile
|
|
|
|
from website import SOCIAL_FIGURES, SITE_ORIGIN, indexable_pages, page_url, write_download_member
|
|
from website_agents import prepare_discovery, prepare_markdown
|
|
|
|
|
|
EXAMPLE_FILES = (
|
|
"package.json",
|
|
"main.js",
|
|
"preload.js",
|
|
"renderer.js",
|
|
"csv.js",
|
|
"index.html",
|
|
)
|
|
|
|
|
|
def prepare_sitemap(site):
|
|
"""Discover HTML routes automatically, excluding pages marked noindex."""
|
|
namespace = "http://www.sitemaps.org/schemas/sitemap/0.9"
|
|
ET.register_namespace("", namespace)
|
|
sitemap = ET.Element(f"{{{namespace}}}urlset")
|
|
for path, _, _ in indexable_pages(site):
|
|
entry = ET.SubElement(sitemap, f"{{{namespace}}}url")
|
|
ET.SubElement(
|
|
entry, f"{{{namespace}}}loc"
|
|
).text = page_url(site, path)
|
|
ET.indent(sitemap, space=" ")
|
|
ET.ElementTree(sitemap).write(
|
|
site / "sitemap.xml", encoding="utf-8", xml_declaration=True
|
|
)
|
|
print(f"Prepared sitemap.xml ({len(sitemap)} indexable pages).")
|
|
|
|
|
|
def prepare_images(site):
|
|
"""Render social previews and the conventional browser icon from their sources."""
|
|
try:
|
|
import cairosvg
|
|
except (ImportError, OSError) as error:
|
|
sys.exit(
|
|
f"Cannot prepare sharing image: {error}. Install website/requirements.txt and native Cairo; see website/README.md."
|
|
)
|
|
from PIL import Image, ImageOps
|
|
root = Path(__file__).resolve().parent.parent
|
|
artwork = root / "website/artwork"
|
|
|
|
def render(source, width):
|
|
png = cairosvg.svg2png(url=str(source), output_width=width)
|
|
with Image.open(BytesIO(png)) as image:
|
|
return image.convert("RGBA")
|
|
|
|
favicon = render(site / "assets/favicon.svg", 192)
|
|
favicon.save(site / "favicon.ico", sizes=[(n, n) for n in (16, 32, 48, 64, 192)])
|
|
|
|
with Image.open(artwork / "rea-social-preview.png") as original:
|
|
default = ImageOps.fit(original.convert("RGB"), (1200, 630), method=Image.Resampling.LANCZOS)
|
|
default.save(site / "assets/rea-social-card.png", optimize=True)
|
|
|
|
directory = site / "assets/share"
|
|
directory.mkdir(exist_ok=True)
|
|
for source in sorted(set(SOCIAL_FIGURES.values())):
|
|
figure = ImageOps.contain(render(root / source, 1200), (1200, 630))
|
|
card = Image.new("RGB", (1200, 630), "white")
|
|
card.paste(figure, ((1200 - figure.width) // 2, (630 - figure.height) // 2), figure)
|
|
card.save(directory / (Path(source).stem + ".png"), optimize=True)
|
|
|
|
|
|
def prepare_notes_example(site):
|
|
"""Create a ZIP with public source only and reproducible metadata."""
|
|
source = site / "examples/notes-electron"
|
|
contents = BytesIO()
|
|
with ZipFile(contents, "w") as archive:
|
|
for name in EXAMPLE_FILES:
|
|
write_download_member(archive, f"notes-example/{name}", (source / name).read_bytes(), compresslevel=9)
|
|
destination = site / "examples/notes-example.zip"
|
|
destination.write_bytes(contents.getvalue())
|
|
print(f"Prepared examples/notes-example.zip ({len(EXAMPLE_FILES)} files).")
|
|
|
|
|
|
def site_identity(description):
|
|
"""Name the site and the product for search engines on the English homepage only."""
|
|
root = f"{SITE_ORIGIN}/"
|
|
return [
|
|
{
|
|
"@type": "WebSite", "url": root,
|
|
"name": "REA", "alternateName": ["Reverse Engineer Anything", "rea.tools"],
|
|
},
|
|
{
|
|
"@type": "SoftwareApplication", "url": root,
|
|
"name": "REA", "alternateName": "Reverse Engineer Anything",
|
|
"description": description,
|
|
"applicationCategory": "DeveloperApplication",
|
|
"operatingSystem": "macOS, Linux, Windows",
|
|
"license": "https://opensource.org/license/mit",
|
|
"offers": {"@type": "Offer", "price": "0", "priceCurrency": "USD"},
|
|
"sameAs": [
|
|
"https://github.com/morluto/rea",
|
|
"https://www.npmjs.com/package/rea-agents",
|
|
"https://x.com/reatools",
|
|
],
|
|
},
|
|
]
|
|
|
|
|
|
def breadcrumb_list(source, url):
|
|
"""Describe the page's visible breadcrumb trail, in its own language, for search results."""
|
|
from bs4 import BeautifulSoup, SoupStrainer
|
|
trail = BeautifulSoup(source, "html.parser", parse_only=SoupStrainer("p", class_=lambda value: value is not None and "breadcrumb" in str(value).split())).p
|
|
links = trail.find_all("a", href=True) if trail else []
|
|
if not links:
|
|
return None
|
|
following = "".join(
|
|
node.get_text() if hasattr(node, "get_text") else str(node) for node in links[-1].next_siblings
|
|
)
|
|
current = " ".join(following.split()).lstrip("/ ").strip()
|
|
if not current:
|
|
return None
|
|
items = [{"name": " ".join(link.get_text().split()), "item": urljoin(url, link["href"])} for link in links]
|
|
items.append({"name": current})
|
|
return {
|
|
"@type": "BreadcrumbList",
|
|
"itemListElement": [{"@type": "ListItem", "position": n, **item} for n, item in enumerate(items, 1)],
|
|
}
|
|
|
|
|
|
def prepare_search_images(site):
|
|
from bs4 import BeautifulSoup
|
|
for path in site.rglob("*.html"):
|
|
source = path.read_text(encoding="utf-8")
|
|
head = BeautifulSoup(source.partition("</head>")[0], "html.parser")
|
|
fields = {tag["property"]: tag["content"] for tag in head.select("meta[property][content]")}
|
|
if "og:image" not in fields:
|
|
continue
|
|
url = fields["og:url"]
|
|
data = {
|
|
"@type": "WebPage", "url": url,
|
|
"name": fields["og:title"], "inLanguage": head.html["lang"],
|
|
"primaryImageOfPage": fields["og:image"],
|
|
}
|
|
breadcrumb = breadcrumb_list(source, url)
|
|
if breadcrumb:
|
|
data["breadcrumb"] = breadcrumb
|
|
if fields["og:type"] == "article":
|
|
data["mainEntity"] = {
|
|
"@type": "BlogPosting", "headline": fields["og:title"],
|
|
"description": fields["og:description"], "image": fields["og:image"],
|
|
"mainEntityOfPage": url,
|
|
}
|
|
if url == f"{SITE_ORIGIN}/":
|
|
data = {"@graph": [data, *site_identity(fields["og:description"])]}
|
|
data = {"@context": "https://schema.org", **data}
|
|
payload = json.dumps(data, ensure_ascii=False).replace("<", "\\u003c")
|
|
path.write_text(source.replace("</head>", f'<script type="application/ld+json">{payload}</script>\n </head>', 1), encoding="utf-8")
|
|
|
|
|
|
def prepare_redirects(site):
|
|
"""Publish permanent Cloudflare redirects and equivalent static Pages links."""
|
|
routes = {
|
|
"showcases": ("showcase", "REA showcases"),
|
|
"environments": ("guides/rl-environments", "Build RL environments"),
|
|
}
|
|
from bs4 import BeautifulSoup
|
|
rules = []
|
|
for alias, (target, title) in routes.items():
|
|
rules.extend(f"/{alias}{suffix} /{target}/ 301" for suffix in ("", "/"))
|
|
directory = site / alias
|
|
directory.mkdir()
|
|
destination = BeautifulSoup((site / target / "index.html").read_text(encoding="utf-8").partition("</head>")[0], "html.parser")
|
|
sharing = "\n".join(str(tag) for tag in destination.select('meta[property^="og:"], meta[name^="twitter:"]'))
|
|
directory.joinpath("index.html").write_text(
|
|
f'<!doctype html><html lang="en"><head><meta charset="utf-8">\n'
|
|
f'<title>{title}</title><meta name="robots" content="noindex">\n'
|
|
f'<link rel="canonical" href="{SITE_ORIGIN}/{target}/">\n'
|
|
f'{sharing}\n<meta http-equiv="refresh" content="0; url=../{target}/"></head>\n'
|
|
f'<body><h1><a href="../{target}/">{title}</a></h1></body></html>\n',
|
|
encoding="utf-8",
|
|
)
|
|
(site / "_redirects").write_text("\n".join(rules) + "\n", encoding="utf-8")
|
|
|
|
|
|
def prepare_assets(site):
|
|
"""Finish the shared publish artifact before it replaces the current preview."""
|
|
from rcssmin import cssmin
|
|
stylesheet = site / "assets/styles.css"
|
|
stylesheet.write_text(cssmin(stylesheet.read_text(encoding="utf-8")), encoding="utf-8")
|
|
prepare_search_images(site)
|
|
prepare_redirects(site)
|
|
prepare_images(site)
|
|
prepare_sitemap(site)
|
|
prepare_notes_example(site)
|
|
prepare_discovery(Path(__file__).resolve().parent.parent, site)
|
|
prepare_markdown(site)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
root = Path(__file__).resolve().parent.parent
|
|
from website_i18n import build_translations
|
|
build_translations(root, root / "website/_site", finalize=prepare_assets)
|