Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-pkg-server

Source of release 0.6.0

0.6.0 / src / pages.lucb

## HTML for the public package site. Markup and class names follow luce.luciaos.com so
## the shared stylesheets in /assets apply unchanged. Pages are complete without
## JavaScript; assets/packages.js only adds filtering to the index.
import c
from luce_std import files
import interop
from luce_std import paths
import strings
from luce_pkg import pkg
from luce_prism import prism

pub let max_page: usize = 2097152
pub let max_readme: usize = 262144
pub let max_notes: usize = 16384
## Larger or binary files are listed in the source browser but not rendered.
pub let max_source: usize = 524288

pub struct Page:
    var bytes: u8[]
    pub var used: usize

    pub func put(text: str) -> !:
        if self.used + text.length > self.bytes.length: error(prism.invalid, "release: generated page is too large")
        for i in 0..<text.length: self.bytes[self.used + i] = text.bytes[i]
        self.used += text.length

    ## Text and attribute values from packages are always escaped.
    pub func escaped(text: str) -> !:
        var start: usize
        for i in 0..<text.length:
            let byte = text.bytes[i]
            let entity = "&amp;" if byte == '&' else ("&lt;" if byte == '<' else ("&gt;" if byte == '>' else ("&quot;" if byte == '"' else ("&#x27;" if byte == '\'' else ""))))
            if entity.length == 0: continue
            try self.put((str)text.bytes[start..<i])
            try self.put(entity)
            start = i + 1
        try self.put((str)text.bytes[start..])

pub func page(buffer: u8[]) -> Page:
    return Page(bytes = buffer)

## `active` is "applications", "tools", "packages" or empty, and marks that tab.
func head(out: Page*, title: str, description: str, root: str, active: str = "") -> !:
    try out.put("<!doctype html>\n<html lang=\"en\">\n<head>\n<meta charset=\"utf-8\">\n<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n<title>")
    try out.escaped(title)
    try out.put("</title>\n<meta name=\"description\" content=\"")
    try out.escaped(description)
    try out.put("\">\n<link rel=\"stylesheet\" href=\"")
    try out.put(root)
    try out.put("assets/core.css\">\n<link rel=\"stylesheet\" href=\"")
    try out.put(root)
    try out.put("assets/style.css\">\n<link rel=\"stylesheet\" href=\"")
    try out.put(root)
    try out.put("assets/packages.css\">\n<link rel=\"icon\" href=\"")
    try out.put(root)
    try out.put("assets/mark.svg\" type=\"image/svg+xml\">\n<script src=\"")
    try out.put(root)
    try out.put("assets/theme.js\"></script>\n<script src=\"")
    try out.put(root)
    try out.put("assets/site.js\" defer></script>\n<script src=\"")
    try out.put(root)
    try out.put("assets/packages.js\" defer></script>\n</head>\n<body>\n<a class=\"skip\" href=\"#content\">Skip to content</a>\n<header class=\"top\">\n<a class=\"mark\" href=\"")
    try out.put(root)
    try out.put("\">Luce packages</a>\n<nav class=\"tabs\">\n<a ")
    if active == "applications": try out.put("class=\"here\" ")
    try out.put("href=\"/applications/\">Applications</a>\n<a ")
    if active == "tools": try out.put("class=\"here\" ")
    try out.put("href=\"/tools/\">Tools</a>\n<a ")
    if active == "packages": try out.put("class=\"here\" ")
    try out.put("href=\"/packages/\">Packages</a>\n")
    try out.put("</nav>\n<div class=\"tools\">\n<a class=\"cross\" href=\"https://luce.luciaos.com\">Luce</a><a class=\"cross\" href=\"https://luce-base.luciaos.com\">Luce Base</a><a class=\"cross\" href=\"https://luciaos.com\">LuciaOS</a>\n<button id=\"theme\" type=\"button\" aria-label=\"Switch between light and dark\">&#9681;</button>\n</div>\n</header>\n<div class=\"shell\">\n")

func foot(out: Page*) -> !:
    try out.put("</article>\n</main>\n</div>\n</body>\n</html>\n")

func field(line: str, separator: u8, index: usize) -> str:
    var start: usize
    var seen: usize
    for i in 0..<line.length + 1:
        if i != line.length and line.bytes[i] != separator: continue
        if seen == index: return (str)line.bytes[start..<i]
        seen += 1
        start = i + 1
    return ""

## The front page, after pypi.org: what you do here, search first, the packages, then
## a plain definition and how to install and publish. `listing` is the site's `index` file.
## The three kinds of thing the index holds, in the order the site shows them.
let kinds: str[3] = ["application", "tool", "package"]
let kind_titles: str[3] = ["Applications", "Tools", "Packages"]
let kind_pages: str[3] = ["applications", "tools", "packages"]
let kind_notes: str[3] = ["Desktop programs you install and open: <code>luc install owner/name</code>",
                          "Command-line programs you install and run in a terminal: <code>luc install owner/name</code>",
                          "Libraries you build on in your own project: <code>luc add owner/name</code>"]

## The kind recorded on an index line; a line from before kinds existed is a package.
func kind_of(line: str) -> usize:
    let value = field(line, '\t', 3)
    if value == "application": return 0
    if value == "tool": return 1
    return 2

func plural(out: Page*, count: usize, index: usize) -> !:
    var number: u8[32]
    try out.put(try strings.format(number, f"{count} "))
    let singular: str[3] = ["application", "tool", "package"]
    try out.put(singular[index])
    if count != 1: try out.put("s")

## The front page, after pypi.org: what you do here, search first, then everything by
## kind, then a plain definition and how to install and publish. `listing` is the site's
## `index` file: owner/name TAB latest TAB description TAB kind. `view` is empty for the
## front page, or "applications" / "tools" / "packages" for the page of one kind.
pub func index_page(out: Page*, listing: const u8[], view: str = "") -> !:
    var only: usize = 3
    for i in 0..<(usize)3:
        if view == kind_pages[i]: only = i
    let heading = kind_titles[only] if only < 3 else "Find and install Luce packages"
    try head(out, heading if only < 3 else "Luce Package Index", "Find and install applications, tools and packages for Luce and Luce Base.", "/", view)
    var counts: usize[3]
    var start: usize
    for end in 0..<listing.length:
        if listing[end] != '\n': continue
        let line = (str)listing[start..<end]
        start = end + 1
        if field(line, '\t', 0).length > 0: counts[kind_of(line)] += 1
    try out.put("<div class=\"side\">\n<details class=\"nav\" open>\n<summary>Pages</summary>\n<ul>\n<li><a ")
    if only == 3: try out.put("class=\"here\" ")
    try out.put("href=\"/\">Everything</a></li>\n")
    for i in 0..<(usize)3:
        try out.put("<li><a ")
        if only == i: try out.put("class=\"here\" ")
        try out.put("href=\"/")
        try out.put(kind_pages[i])
        try out.put("/\">")
        try out.put(kind_titles[i])
        try out.put("</a></li>\n")
    try out.put("</ul>\n</details>\n")
    if only == 3:
        try out.put("<details class=\"nav\" open>\n<summary>On this page</summary>\n<ul>\n")
        for i in 0..<(usize)3:
            if counts[i] == 0: continue
            try out.put("<li class=\"h2\"><a href=\"#")
            try out.put(kind_pages[i])
            try out.put("\">")
            try out.put(kind_titles[i])
            try out.put("</a></li>")
        try out.put("<li class=\"h2\"><a href=\"#installing\">Installing</a></li>\n</ul>\n</details>\n")
    try out.put("</div>\n<main id=\"content\">\n<article>\n<h1>")
    try out.put(heading)
    try out.put("</h1>\n<div class=\"find\"><input id=\"filter\" type=\"search\" placeholder=\"Search ")
    try out.put(kind_pages[only] if only < 3 else "applications, tools and packages")
    try out.put("\" aria-label=\"Search\" autocomplete=\"off\"></div>\n<p class=\"count\">")
    var written = false
    for i in 0..<(usize)3:
        if only < 3 and only != i: continue
        if written: try out.put(" &middot; ")
        try plural(out, counts[i], i)
        written = true
    try out.put("</p>\n<p id=\"no-results\" hidden>Nothing matches that search.</p>\n")
    for i in 0..<(usize)3:
        if counts[i] == 0 or (only < 3 and only != i): continue
        try out.put("<section class=\"kind\" id=\"")
        try out.put(kind_pages[i])
        try out.put("\">\n<h2>")
        try out.put(kind_titles[i])
        try out.put("<a class=\"anchor\" href=\"#")
        try out.put(kind_pages[i])
        try out.put("\" aria-label=\"link to this section\">#</a></h2>\n<p class=\"kind-note\">")
        try out.put(kind_notes[i])
        try out.put("</p>\n<div class=\"cards\">\n")
        start = 0
        for end in 0..<listing.length:
            if listing[end] != '\n': continue
            let line = (str)listing[start..<end]
            start = end + 1
            let coordinate = field(line, '\t', 0)
            if coordinate.length == 0 or kind_of(line) != i: continue
            try out.put("<a class=\"card\" href=\"/")
            try out.escaped(coordinate)
            try out.put("/\"><strong>")
            try out.escaped(coordinate)
            try out.put("</strong><span>")
            let summary = field(line, '\t', 2)
            if summary.length > 0:
                try out.escaped(summary)
                try out.put(" &middot; ")
            try out.escaped(field(line, '\t', 1))
            try out.put("</span></a>\n")
        try out.put("</div>\n</section>\n")
    if only < 3:
        try foot(out)
        return
    try out.put("<p>The Luce Package Index is a repository of applications, tools and packages for the <a href=\"https://luce.luciaos.com\">Luce</a> and <a href=\"https://luce-base.luciaos.com\">Luce Base</a> programming languages.</p>\n<h2 id=\"installing\">Installing<a class=\"anchor\" href=\"#installing\" aria-label=\"link to this section\">#</a></h2>\n<p>Everything here is fetched and built by <code>luc</code>, which comes with Luce. Get it once, on macOS or Linux:</p>\n<div class=\"code\"><button class=\"copy\" type=\"button\">copy</button><pre><code class=\"language-sh\">curl -fsSL https://luce.luciaos.com/install.sh | sh &amp;&amp; . \"$HOME/.local/luce/env\"</code></pre></div>\n<p>On Windows, in PowerShell: <code>irm https://luce.luciaos.com/install.ps1 | iex</code>. Already have it? <code>luc update</code> brings luc and both compilers to the latest release, and <code>luc update owner/name</code> brings an installed application to its newest version.</p>\n<p>Then install an application or a tool. It is built from source on your machine:</p>\n<div class=\"code\"><button class=\"copy\" type=\"button\">copy</button><pre><code class=\"language-sh\">luc install owner/name</code></pre></div>\n<p>Add a package to your project. <code>luc</code> resolves its dependencies and records exact versions and checksums in <code>luc.lock</code>:</p>\n<div class=\"code\"><button class=\"copy\" type=\"button\">copy</button><pre><code class=\"language-sh\">luc add owner/name</code></pre></div>\n<p>Every release keeps its source, which you can browse here or clone with Git.</p>\n")
    try foot(out)

## An image name the release's raw files serve as-is.
pub func image_name(name: str) -> bool:
    for suffix in [".png", ".jpg", ".jpeg", ".gif", ".svg", ".webp"]:
        if strings.ends_with(name, suffix): return true
    return false

## Links in a README: absolute web links and in-page anchors pass through; a relative
## path points into this release's own source (an image as a raw file, anything else as
## its source page); everything else, including javascript:, becomes "#".
func safe_links(out: Page*, fragment: str, coordinate: str, version: str) -> !:
    var at: usize
    while at < fragment.length:
        var attribute = ""
        if strings.starts_with((str)fragment.bytes[at..], " href=\""): attribute = " href=\""
        elif strings.starts_with((str)fragment.bytes[at..], " src=\""): attribute = " src=\""
        if attribute.length == 0:
            try out.put((str)fragment.bytes[at..<at + 1])
            at += 1
            continue
        var end = at + attribute.length
        while end < fragment.length and fragment.bytes[end] != '"': end += 1
        var value = (str)fragment.bytes[at + attribute.length..<end]
        try out.put(attribute)
        if strings.starts_with(value, "https://") or strings.starts_with(value, "http://") or (attribute == " href=\"" and strings.starts_with(value, "#")):
            try out.put(value)
        elif relative_path(value):
            if strings.starts_with(value, "./"): value = (str)value.bytes[2..]
            try out.put("/")
            try out.escaped(coordinate)
            try out.put("/")
            try out.escaped(version)
            let raw = image_name(value) or attribute == " src=\""
            try out.put("/raw/" if raw else "/blob/")
            try out.escaped(value)
            if not raw: try out.put(".html")
        else:
            try out.put("#")
        at = end

## A plain relative path: no scheme, no `..`, no leading slash, no query or fragment.
func relative_path(value: str) -> bool:
    if value.length == 0 or value.length > 512 or value.bytes[0] == '/' or value.bytes[0] == '#': return false
    var start: usize
    for end in 0..<value.length + 1:
        if end != value.length:
            let byte = value.bytes[end]
            if byte == ':' or byte == '?' or byte == '\\' or byte < 0x20 or byte == '"' or byte == '\'': return false
            if byte != '/': continue
        let part = (str)value.bytes[start..<end]
        if part.length == 0 or part == "..": return false
        start = end + 1
    return true

## One release's notes, `<version>.notes` in the package's site directory.
func release_notes(directory: str, version: str) -> interop.Owned[const u8[]]!:
    var name: u8[96]
    let path = try paths.join(directory, try strings.format(name, f"{version}.notes"))
    defer path.release()
    return try files.read((c.str)path.value, limit = max_notes)

## One package: description, install command, versions, dependencies and README.
## `versions` is the package's `versions` file; `readme` is already-rendered HTML.
## `directory` is the package's site directory, where each release's notes live.
pub func package_page(out: Page*, definition: pkg.Definition*, versions: const u8[], readme: str, directory: str) -> !:
    var title: u8[160]
    let coordinate = try strings.format(title, f"{definition.owner}/{definition.name}")
    try head(out, coordinate, definition.description, "/")
    try out.put("<div class=\"side\">\n<details class=\"nav\" open>\n<summary>Pages</summary>\n<ul>\n<li><a href=\"/\">Packages</a></li>\n</ul>\n</details>\n<details class=\"nav\" open>\n<summary>On this page</summary>\n<ul>\n<li class=\"h2\"><a href=\"#install\">Install</a></li>")
    if readme.length > 0: try out.put("<li class=\"h2\"><a href=\"#readme\">Documentation</a></li>")
    if definition.dependency_count > 0: try out.put("<li class=\"h2\"><a href=\"#dependencies\">Dependencies</a></li>")
    try out.put("<li class=\"h2\"><a href=\"#versions\">Versions</a></li>")
    try out.put("\n</ul>\n</details>\n</div>\n<main id=\"content\">\n<article>\n<h1>")
    try out.escaped(coordinate)
    try out.put("</h1>\n<p class=\"lede\">")
    try out.escaped(definition.description)
    try out.put("</p>\n<p>")
    try out.put("An application" if definition.is_application() else "A library")
    try out.put(" written in <code>")
    try out.escaped(definition.language)
    try out.put("</code>. Latest release <code>")
    try out.escaped(definition.version)
    try out.put("</code>.")
    if definition.license.length > 0:
        try out.put(" License <code>")
        try out.escaped(definition.license)
        try out.put("</code>.")
    try out.put("</p>\n<h2 id=\"install\">Install<a class=\"anchor\" href=\"#install\" aria-label=\"link to this section\">#</a></h2>\n")
    if coordinate == "dymokomi/luc":
        try out.put("<p><code>luc</code> is the tool that installs everything else, so it comes with Luce itself. On macOS or Linux:</p>\n<div class=\"code\"><button class=\"copy\" type=\"button\">copy</button><pre><code class=\"language-sh\">curl -fsSL https://luce.luciaos.com/install.sh | sh &amp;&amp; . \"$HOME/.local/luce/env\"</code></pre></div>\n<p>On Windows, in PowerShell: <code>irm https://luce.luciaos.com/install.ps1 | iex</code>. Already installed? <code>luc update</code>.</p>\n")
    else:
        try out.put("<div class=\"code\"><button class=\"copy\" type=\"button\">copy</button><pre><code class=\"language-sh\">luc ")
        try out.put("install " if definition.is_application() else "add ")
        try out.escaped(coordinate)
        try out.put("</code></pre></div>\n")
    if readme.length > 0:
        try out.put("<h2 id=\"readme\">Documentation<a class=\"anchor\" href=\"#readme\" aria-label=\"link to this section\">#</a></h2>\n")
        try safe_links(out, readme, coordinate, definition.version)
    if definition.dependency_count > 0:
        try out.put("<h2 id=\"dependencies\">Dependencies<a class=\"anchor\" href=\"#dependencies\" aria-label=\"link to this section\">#</a></h2>\n<div class=\"table\"><table><thead><tr><th>Package</th><th>Version</th></tr></thead><tbody>\n")
        for i in 0..<definition.dependency_count:
            let item = try definition.dependency_at(i)
            try out.put("<tr><td><a href=\"/")
            try out.escaped(item.owner)
            try out.put("/")
            try out.escaped(item.name)
            try out.put("/\">")
            try out.escaped(item.owner)
            try out.put("/")
            try out.escaped(item.name)
            # no version is the newest release
            if item.version.length == 0:
                try out.put("</a></td><td>newest</td></tr>\n")
            else:
                try out.put("</a></td><td><code>")
                try out.escaped(item.version)
                try out.put("</code></td></tr>\n")
        try out.put("</tbody></table></div>\n")
    try out.put("<h2 id=\"versions\">Versions<a class=\"anchor\" href=\"#versions\" aria-label=\"link to this section\">#</a></h2>\n")
    # Newest last in the file; show newest first. Each release carries its notes.
    var end = versions.length
    while end > 0:
        var start = end - 1
        while start > 0 and versions[start - 1] != '\n': start -= 1
        let line = (str)versions[start..<end - 1] if versions[end - 1] == '\n' else (str)versions[start..<end]
        end = start
        let version = field(line, ' ', 0)
        if version.length == 0: continue
        let commit = field(line, ' ', 2)
        try out.put("<h3 id=\"v")
        try out.escaped(version)
        try out.put("\">")
        try out.escaped(version)
        try out.put("</h3>\n<p class=\"release\"><a href=\"/")
        try out.escaped(coordinate)
        try out.put("/")
        try out.escaped(version)
        try out.put("/tree/\">Browse source</a> &middot; <a href=\"/")
        try out.escaped(coordinate)
        try out.put("/")
        try out.escaped(version)
        try out.put(".pack\">pack</a> &middot; commit <code>")
        try out.escaped((str)commit.bytes[..<12] if commit.length > 12 else commit)
        try out.put("</code></p>\n<p class=\"release\">SHA-256 <code>")
        try out.escaped(field(line, ' ', 1))
        try out.put("</code></p>\n")
        let notes = release_notes(directory, version) catch: continue
        defer notes.release()
        try out.put("<pre class=\"notes\">")
        try out.escaped((str)notes.value)
        try out.put("</pre>\n")
    try foot(out)

# mark: Source browser =========================================================================

## One entry of a directory listing in the source browser.
pub struct Listed:
    pub var name: str
    pub var directory: bool
    pub var size: usize
    ## False for names the site cannot safely address; they are listed without a link.
    pub var linked: bool

## A name is addressable when it is plain enough to be one URL and file-system segment.
pub func addressable(name: str) -> bool:
    if name.length == 0 or name.length > 128 or name == "." or name == ".." or name == "index.html": return false
    for byte in name.bytes:
        let plain = (byte >= 'a' and byte <= 'z') or (byte >= 'A' and byte <= 'Z') or (byte >= '0' and byte <= '9')
        if not plain and byte != '.' and byte != '_' and byte != '-': return false
    return true

func source_head(out: Page*, coordinate: str, version: str, location: str) -> !:
    var title: u8[512]
    try head(out, try strings.format(title, f"{coordinate} {version}"), "Source code", "/")
    try out.put("<div class=\"side\">\n<details class=\"nav\" open>\n<summary>Pages</summary>\n<ul>\n<li><a href=\"/\">Packages</a></li>\n<li><a href=\"/")
    try out.escaped(coordinate)
    try out.put("/\">")
    try out.escaped(coordinate)
    try out.put("</a></li>\n</ul>\n</details>\n</div>\n<main id=\"content\">\n<article>\n<h1>")
    try out.escaped(coordinate)
    try out.put("</h1>\n<p class=\"lede\">Source of release ")
    try out.escaped(version)
    try out.put("</p>\n<p class=\"crumbs\"><a href=\"/")
    try out.escaped(coordinate)
    try out.put("/")
    try out.escaped(version)
    try out.put("/tree/\">")
    try out.escaped(version)
    try out.put("</a>")
    # Breadcrumbs: every directory above `location` links to its listing.
    var start: usize
    for end in 0..<location.length + 1:
        if end != location.length and location.bytes[end] != '/': continue
        if end > start:
            try out.put(" / ")
            if end == location.length:
                try out.escaped((str)location.bytes[start..<end])
            else:
                try out.put("<a href=\"/")
                try out.escaped(coordinate)
                try out.put("/")
                try out.escaped(version)
                try out.put("/tree/")
                try out.escaped((str)location.bytes[..<end])
                try out.put("/\">")
                try out.escaped((str)location.bytes[start..<end])
                try out.put("</a>")
        start = end + 1
    try out.put("</p>\n")

## A directory listing. `location` is the directory's path, empty for the root.
pub func tree_page(out: Page*, coordinate: str, version: str, location: str, entries: const Listed[]) -> !:
    try source_head(out, coordinate, version, location)
    try out.put("<div class=\"table\"><table class=\"tree\"><thead><tr><th>Name</th><th>Size</th></tr></thead><tbody>\n")
    for pass in 0..<(usize)2:
        for entry in entries:
            if entry.directory != (pass == 0): continue
            try out.put("<tr><td>")
            if entry.linked:
                try out.put("<a href=\"/")
                try out.escaped(coordinate)
                try out.put("/")
                try out.escaped(version)
                try out.put("/tree/" if entry.directory else "/blob/")
                if location.length > 0:
                    try out.escaped(location)
                    try out.put("/")
                try out.escaped(entry.name)
                try out.put("/\">" if entry.directory else ".html\">")
            try out.escaped(entry.name)
            if entry.directory: try out.put("/")
            if entry.linked: try out.put("</a>")
            try out.put("</td><td>")
            if not entry.directory:
                var size: u8[32]
                try out.put(try strings.format(size, f"{entry.size}"))
            try out.put("</td></tr>\n")
    try out.put("</tbody></table></div>\n")
    try foot(out)

func renderable(content: const u8[]) -> bool:
    if content.length > max_source: return false
    for byte in content:
        if byte == 0: return false
    return true

# mark: Syntax highlighting ====================================================================
# A small line lexer that emits the token classes the shared stylesheet already styles:
# c comment, s string, n number, k keyword, t type, d declaration name, b builtin.

let luce_keywords: str[46] = ["and", "as", "break", "catch", "class", "const", "continue", "defer", "elif", "else", "enum", "errdefer", "extern", "for", "from", "func", "if", "import", "in", "init", "interface", "is", "let", "match", "mutating", "new", "not", "or", "pub", "recover", "return", "self", "spawn", "static", "struct", "test", "try", "type", "var", "wait", "while", "with", "alloc", "free", "discard", "trap"]
let luce_types: str[27] = ["int", "float", "bool", "str", "bytes", "unit", "never", "list", "map", "set", "Error", "ErrorCode", "Weak", "task", "char", "i8", "i16", "i32", "i64", "u8", "u16", "u32", "u64", "usize", "isize", "f32", "f64"]
let luce_constants: str[3] = ["true", "false", "none"]
let c_keywords: str[36] = ["auto", "break", "case", "const", "continue", "default", "do", "else", "enum", "extern", "for", "goto", "if", "inline", "register", "restrict", "return", "sizeof", "static", "struct", "switch", "typedef", "union", "volatile", "while", "def", "class", "import", "from", "pass", "lambda", "yield", "fn", "use", "mod", "impl"]
let c_types: str[16] = ["void", "char", "short", "int", "long", "float", "double", "signed", "unsigned", "bool", "size_t", "uint8_t", "uint16_t", "uint32_t", "uint64_t", "int64_t"]

## How a file is highlighted, from its name.
enum Syntax as u8:
    plain = 0
    luce = 1
    hash_comments = 2
    slash_comments = 3
    prisma = 4

pub func syntax_for(name: str) -> Syntax:
    var dot = name.length
    for i in 0..<name.length:
        if name.bytes[i] == '.': dot = i
    if dot == name.length: return Syntax.hash_comments if name == "Makefile" else Syntax.plain
    let ext = (str)name.bytes[dot + 1..]
    if ext == "luc" or ext == "lucb": return Syntax.luce
    if ext == "prisma" or ext == "prism": return Syntax.prisma
    if ext == "py" or ext == "sh" or ext == "toml" or ext == "yml" or ext == "yaml" or ext == "rb" or ext == "nft" or ext == "service" or ext == "timer": return Syntax.hash_comments
    if ext == "c" or ext == "h" or ext == "cpp" or ext == "hpp" or ext == "cc" or ext == "js" or ext == "ts" or ext == "rs" or ext == "go" or ext == "zig" or ext == "java" or ext == "swift" or ext == "css" or ext == "m": return Syntax.slash_comments
    return Syntax.plain

func word_byte(byte: u8) -> bool:
    return (byte >= 'a' and byte <= 'z') or (byte >= 'A' and byte <= 'Z') or (byte >= '0' and byte <= '9') or byte == '_'

func listed(word: str, words: const str[]) -> bool:
    for candidate in words:
        if candidate == word: return true
    return false

func span(out: Page*, token: str, text: str) -> !:
    try out.put("<span class=\"")
    try out.put(token)
    try out.put("\">")
    try out.escaped(text)
    try out.put("</span>")

## Highlight one line. Strings and comments do not span lines here, which is what the
## languages in question mostly do; a rare multi-line string is shown plainly after its
## first line.
pub func highlight(out: Page*, line: str, syntax: Syntax) -> !:
    if syntax == Syntax.plain:
        try out.escaped(line)
        return
    var at: usize
    var plain_start: usize
    var previous = ""
    while at < line.length:
        let byte = line.bytes[at]
        # comments
        let hashed = byte == '#' and syntax != Syntax.slash_comments
        let slashes = byte == '/' and at + 1 < line.length and line.bytes[at + 1] == '/' and syntax == Syntax.slash_comments
        if hashed or slashes:
            try out.escaped((str)line.bytes[plain_start..<at])
            try span(out, "c", (str)line.bytes[at..])
            return
        # strings
        if byte == '"' or byte == '\'':
            var end = at + 1
            while end < line.length and line.bytes[end] != byte:
                if line.bytes[end] == '\\' and end + 1 < line.length: end += 1
                end += 1
            if end < line.length: end += 1
            try out.escaped((str)line.bytes[plain_start..<at])
            try span(out, "s", (str)line.bytes[at..<end])
            at = end
            plain_start = at
            continue
        # numbers and words
        if word_byte(byte) and (at == 0 or not word_byte(line.bytes[at - 1])):
            var end = at
            while end < line.length and (word_byte(line.bytes[end]) or (line.bytes[end] == '.' and byte >= '0' and byte <= '9')): end += 1
            let word = (str)line.bytes[at..<end]
            var token = ""
            if byte >= '0' and byte <= '9': token = "n"
            elif syntax == Syntax.luce:
                if listed(word, luce_keywords): token = "k"
                elif listed(word, luce_types) or listed(word, luce_constants): token = "t"
                elif previous == "func" or previous == "struct" or previous == "class" or previous == "enum" or previous == "interface" or previous == "type": token = "d"
                elif byte >= 'A' and byte <= 'Z': token = "t"
            elif syntax == Syntax.prisma:
                if word == "def" or word == "over" or word == "connect" or word == "reference" or word == "delete" or word == "unset": token = "k"
                elif previous == "def" or previous == "over": token = "t"
                elif word == "str" or word == "bool" or word == "int64" or word == "uint64" or word == "float64" or word == "float32" or word == "int32" or word == "true" or word == "false": token = "t"
            else:
                if listed(word, c_keywords): token = "k"
                elif listed(word, c_types) or word == "true" or word == "false" or word == "None" or word == "null": token = "t"
            if token.length > 0:
                try out.escaped((str)line.bytes[plain_start..<at])
                try span(out, token, word)
                plain_start = end
            previous = word
            at = end
            continue
        at += 1
    try out.escaped((str)line.bytes[plain_start..])

## One file, with numbered and addressable lines (#L12).
pub func blob_page(out: Page*, coordinate: str, version: str, location: str, content: const u8[]) -> !:
    try source_head(out, coordinate, version, location)
    if not renderable(content):
        var size: u8[64]
        try out.put("<p>")
        try out.put(try strings.format(size, f"{content.length}"))
        try out.put(" bytes; binary or too large to show. It is in the release pack.</p>\n")
        try foot(out)
        return
    let syntax = syntax_for(paths.base(location))
    try out.put("<pre class=\"source code\"><code>")
    var number: usize = 1
    var start: usize
    for end in 0..<content.length + 1:
        if end != content.length and content[end] != '\n': continue
        if end == content.length and start == end: break
        var label: u8[48]
        try out.put(try strings.format(label, f"<span class=\"l\" id=\"L{number}\">"))
        try highlight(out, (str)content[start..<end], syntax)
        try out.put("</span>\n")
        number += 1
        start = end + 1
    try out.put("</code></pre>\n")
    try foot(out)