RacesOfValheim/scripts/nexus-readme.py
kanym 02c640e1d9 chore(release): v1.5.7 - Bone Ships and the Russian translation
README: the stack of "New in" blocks at the top is down to the latest
release, a languages line with flags sits under the intro, Bone Ships gets
its own section, and the Translations section credits Rudra and Axibed for
the Russian. nexus-readme.py keeps an image inside a line of text inline,
so the flags do not come out as 350 px pictures on Nexus.
2026-10-06 14:20:22 +02:00

108 lines
3.9 KiB
Python

"""Turns the mod's README.md into the BBCode Nexus wants.
The Nexus page had been rewritten by hand each release and had fallen thirteen
versions behind the README, so it is generated now. Run it after the changelog
entry is in and the version is bumped:
python scripts/nexus-readme.py
It reads manifest.json for the version and writes
dist/NEXUS_README_<version>.bbcode.txt.
The conversion follows what the 1.4.11 page did by hand, so the two are
comparable: headings become sized bold, a horizontal rule goes above every
top-level section, images are gathered onto one line per group, backticks
become quotes because Nexus has no code style, and the <details> wrappers
around the progressions are dropped while their summaries survive as headings.
"""
import json
import pathlib
import re
ROOT = pathlib.Path(__file__).resolve().parent.parent
RULE = "[s]---------------------------------------------------[/s]"
def inline(text):
"""Markdown's inline marks, in an order where they cannot eat each other."""
text = re.sub(r"\*\*(.+?)\*\*", r"[b]\1[/b]", text)
text = re.sub(r"`([^`]+)`", r'"\1"', text)
# An image inside a line of text (the language flags) stays inline at its own size; a line that
# is only an image never gets here, convert() gathers those first.
text = re.sub(r"!\[[^\]]*\]\(([^)]+)\)", r"[img]\1[/img]", text)
# An in-page anchor means nothing on a Nexus description, so the link text stays and the
# link goes: "Full details under Sverm" reads the same, and a dead #10-sverm does not.
text = re.sub(r"\[([^\]]+)\]\(#[^)]*\)", r"\1", text)
text = re.sub(r"\[([^\]]+)\]\(([^)]+)\)", r"[url=\2]\1[/url]", text)
return text
def image(url):
# The march gif is a strip rather than a portrait, and was shown smaller by hand.
return f"[img height={200 if 'small' in url else 350}]{url}[/img]"
def table_row(line):
cells = [inline(c.strip()) for c in line.strip().strip("|").split("|")]
if not cells:
return None
return "- [b]" + cells[0] + "[/b]" + ("" if len(cells) == 1 else " — " + " — ".join(cells[1:]))
def convert(markdown):
out = []
pending_images = []
def flush_images():
if pending_images:
out.append("".join(pending_images))
pending_images.clear()
for line in markdown.splitlines():
stripped = line.strip()
# Images stack up so a group of them lands on one line, the way Nexus shows them.
match = re.fullmatch(r"!\[[^\]]*\]\(([^)]+)\)", stripped)
if match:
pending_images.append(image(match.group(1)))
continue
flush_images()
if not stripped or stripped in ("<details>", "</details>", "---"):
continue
match = re.fullmatch(r"<summary><strong>(.+?)</strong></summary>", stripped)
if match:
out.append(f"[size=4][b]{inline(match.group(1))}[/b][/size]")
continue
match = re.fullmatch(r"(#{1,3})\s+(.*)", stripped)
if match:
level, title = len(match.group(1)), inline(match.group(2))
if level == 2:
out.append(RULE)
out.append(f"[size={7 - level}][b]{title}[/b][/size]")
continue
if stripped.startswith("|"):
if re.fullmatch(r"\|[\s\-:|]+\|", stripped): # the --- separator row
continue
row = table_row(stripped)
if row:
out.append(row)
continue
out.append(inline(stripped))
flush_images()
return "\n".join(out) + "\n"
def main():
version = json.loads((ROOT / "manifest.json").read_text(encoding="utf-8-sig"))["version_number"]
target = ROOT / "dist" / f"NEXUS_README_{version}.bbcode.txt"
target.write_text(convert((ROOT / "README.md").read_text(encoding="utf-8-sig")), encoding="utf-8")
print(f"{target.relative_to(ROOT)} ({len(target.read_text(encoding='utf-8').splitlines())} lines)")
main()