|
| 1 | +# lua-std-doc |
| 2 | + |
| 3 | +**The Lua 5.5 standard library, documented by rustdoc.** |
| 4 | + |
| 5 | +This repository abuses `rustdoc` as a documentation generator for a |
| 6 | +language it has never heard of. A small Rust crate models Lua's world |
| 7 | +and a client-side script rewrites rustdoc's vocabulary on the fly. The result |
| 8 | +is a browsable, searchable, cross-linked reference for Lua 5.5, with |
| 9 | +rustdoc's search index, sidebar, and theming intact. |
| 10 | + |
| 11 | +📖 **Live**: <https://tkr-sh.github.io/lua-std-doc/> |
| 12 | + |
| 13 | +## The extended library |
| 14 | + |
| 15 | +Beyond the standard reference, the `extended` cargo feature documents |
| 16 | +**`lua/extended.lua`**: 34 functions other languages ship in their |
| 17 | +standard libraries (inspired by Rust), implemented, tested, and usable |
| 18 | +today: |
| 19 | + |
| 20 | +```lua |
| 21 | +require("extended") |
| 22 | + |
| 23 | +("a,b,c"):split(",") --> { "a", "b", "c" } |
| 24 | +(" hi "):trim() --> "hi" |
| 25 | +table.map({1, 2, 3}, f) |
| 26 | +table.fold(t, f, init) |
| 27 | +math.clamp(x, 1, 10) |
| 28 | +print(inspect({ nested = { ok = true } })) |
| 29 | +``` |
| 30 | + |
| 31 | +`lua/extended.min.lua` is the same library on a single line (Lua's |
| 32 | +grammar is newline-free once `--` comments are gone), and |
| 33 | +`lua/test_extended.lua` is the assertion suite covering both: |
| 34 | + |
| 35 | +```console |
| 36 | +$ cd lua && lua5.4 test_extended.lua |
| 37 | +$ cd lua && EXT=extended.min.lua lua5.4 test_extended.lua |
| 38 | +``` |
| 39 | + |
| 40 | + |
| 41 | +## How the trick works |
| 42 | + |
| 43 | +| Rust concept | Rendered as | |
| 44 | +| --------------------- | ------------------------------------------------ | |
| 45 | +| `pub struct string;` | `data type string` | |
| 46 | +| `pub trait tostring` | `metatable tostring` (metamethods as functions) | |
| 47 | +| `impl` blocks | *Method form* vs *Library functions* sections | |
| 48 | +| `Repeatable<T>` param | `...values: T` (Lua varargs) | |
| 49 | +| `option<T>` | `T \| nil`, with `nil` linked to its type page | |
| 50 | +| `<T: tostring>` | a clickable metatable bound | |
| 51 | +| modules | libraries (`math`, `io`, `os`, `coroutine`, ...) | |
| 52 | + |
| 53 | +The machinery lives in three files: |
| 54 | + |
| 55 | +- **`custom.html`**: injected into every page via `--html-in-header`. |
| 56 | + Swaps rustdoc's vocabulary (`pub fn` → `function`, `Trait` → |
| 57 | + `Metatable`, ...), rewrites the marker generics above at the HTML |
| 58 | + level so type links survive, and ships a small Lua syntax highlighter |
| 59 | + that emits rustdoc's own token classes (`kw`, `string`, `number`, |
| 60 | + `comment`), so both themes color Lua code through their existing |
| 61 | + variables. It cooperates with rustdoc's *line numbers* setting in |
| 62 | + both orderings. |
| 63 | +- **`lua.css`**: the theme, and the extension stylesheet |
| 64 | + (`--extend-css`): hides Rust-only sections (auto traits, blanket |
| 65 | + impls), styles the logo and the function-call highlight class. |
| 66 | +- **`src/*.rs`**: the reference itself, one file per library, in an |
| 67 | + MDN-ish per-function format: summary, Parameters, Return value, |
| 68 | + Description, Examples (every function has one, with outputs matching |
| 69 | + Lua 5.5's round-trip float printing), See also. |
| 70 | + |
| 71 | +## Building |
| 72 | + |
| 73 | +Docs require **nightly** (pinned by `rust-toolchain.toml`) for |
| 74 | +`doc_auto_cfg`, which renders the "Available on crate feature |
| 75 | +`extended` only" badges; everything else is stable Rust. |
| 76 | + |
| 77 | +```console |
| 78 | +$ just docs # or the cargo rustdoc line from the justfile |
| 79 | +$ open target/doc/lua_std/index.html |
| 80 | +``` |
| 81 | + |
| 82 | +## Deploying |
| 83 | + |
| 84 | +`.github/workflows/deploy.yml` tests the Lua library, builds the site |
| 85 | +on nightly, and publishes `target/doc/` to GitHub Pages on every push |
| 86 | +to `main`. One-time setup: repository **Settings → Pages → Source → |
| 87 | +GitHub Actions**. |
| 88 | + |
| 89 | +## Credits |
| 90 | + |
| 91 | +- Lua and the Lua logo are from [Lua.org](https://www.lua.org/); logo |
| 92 | + © 1998 Lua.org, graphic design by Alexandre Nakonechnyj. |
| 93 | +- Reference content follows the |
| 94 | + [Lua 5.5 manual](https://www.lua.org/manual/5.5/), rephrased and |
| 95 | + with original examples. |
| 96 | +- Per-function page structure inspired by |
| 97 | + [luadocs.com](https://www.luadocs.com/). |
0 commit comments