Document Generator for Lean 4 — external-linking fork.
Heads-up — this fork differs from upstream. This fork does NOT generate documentation HTML for external libraries (third-party deps + Lean core). All such references link to the Mathlib documentation site via the same
find/?pattern=…#docredirect that the Lean Zulip uses fordocs#Foo. Build wall-clock drops ~10×; output size drops ~100×. If you need to document Lean core or your deps locally, use upstreamleanprover/doc-gen4. See External library linking for full detail.A worked example (this fork documenting itself) is live at https://gametheoryinlean.github.io/docgen/.
- Usage
- Requirements to run
doc-gen4 - External library linking
- Source locations
- Disabling equations
- Troubleshooting
- Upgrading from upstream
doc-gen4 - How does
docs#Nat.addfrom the Lean Zulip work? - Development of doc-gen4
- Design notes
doc-gen4 is easiest to use via its custom Lake facet. The currently
recommended setup is a nested docbuild project inside your existing Lake
project.
-
Create a subdirectory within your existing Lake project called
docbuild. -
Create a
lakefile.tomlwithindocbuildwith the following content:name = "docbuild" reservoir = false version = "0.1.0" packagesDir = "../.lake/packages" [[require]] scope = "leanprover" name = "doc-gen4" # If you are developing against a release candidate or a stable version `v4.x`, # replace `main` below by `v4.x`. If you do not use `main` keep in mind to # update this field as you update your Lean version. rev = "main" [[require]] name = "Your Library Name" path = "../"
-
Run
lake update doc-gen4withindocbuildto pindoc-gen4and its dependencies to the chosen versions.IMPORTANT: If you depend on mathlib4 run
MATHLIB_NO_CACHE_ON_UPDATE=1 lake update doc-gen4instead to mitigate a small issue in mathlib's caching mechanism for now. -
If your parent project has dependencies, run
lake update YourLibraryNamewithindocbuildwhenever you update the dependencies of your parent project.
After this setup step you can generate documentation for an entire library using:
lake build YourLibraryName:docsFor multiple libraries:
lake build Test:docs YourLibraryName:docsdoc-gen4 only generates documentation for modules belonging to your
project's root package. External dependencies (Mathlib, Batteries, your own
deps, etc.) and Lean core (Init, Std, Lake, Lean) are NOT analyzed
and produce no HTML in your build. See External library
linking for how references to those declarations
resolve.
The root of the built docs will be docbuild/.lake/build/doc/index.html.
Due to the "Same Origin Policy", the generated website will be partially
broken if you open the generated HTML files directly in your browser. Serve
them from an HTTP server instead, e.g.:
cd docbuild/.lake/build/doc && python3 -m http.serverTo compile, doc-gen4 requires:
- A Lean 4 or
elaninstallation. - A C compiler on Linux / macOS (Windows uses Lean's bundled clang).
For lake build YourLibraryName:docs to work, your target library must
build (lake build YourLibraryName exits clean). sorry-containing code is
fine; uncompilable code is not. If the target library doesn't compile,
documentation generation will fail and you'll be left with partial build
artefacts in docbuild/.lake/build/doc.
If you're working on a project that only partially compiles, you can
temporarily remove imports of the failing files (and of files that
transitively reference them) from your top-level library file. You'll get
incomplete but working documentation of the rest of the project. We don't
recommend this as a long-term workflow — fix the underlying compile issues,
or sorry-out blockers, and keep your top-level library compiling.
To keep documentation builds fast and outputs small, this fork does not generate HTML pages for external libraries. Instead, any reference to a declaration or module that doc-gen4 did not analyze locally is rewritten to point at the Mathlib documentation site:
- Decl reference:
https://leanprover-community.github.io/mathlib4_docs/find/?pattern=<fullName>#doc— the same mechanism the Lean Zulip uses fordocs#Foo. The external site'sdeclaration-data.bmp+find.jsresolve the owning module client-side, so doc-gen4 doesn't need to track external module ownership. - Module reference:
https://leanprover-community.github.io/mathlib4_docs/<Module/Path>.html.
The Mathlib documentation site hosts the entire Lean core (Init, Std,
Lake, Lean) plus Mathlib and most commonly-used dependencies, so this
covers the typical Lean 4 ecosystem.
- Build wall-clock is roughly an order of magnitude faster than analyzing every dependency, and disk usage drops by a similar factor. Measured on doc-gen4's own sources: 334 s → 25 s wall-clock, 225 M → 1.7 M output.
- The local search index only finds declarations in your own modules. To search the Lean / Mathlib ecosystem, users go to the Mathlib site's search.
- The find redirect adds one extra HTTP hop when a user clicks an external link.
- For projects whose audience does NOT use the Mathlib ecosystem (a pure Std research codebase, an internal-only project), external links will 404. See Pointing at a different external site.
See docs/usage/pointing-elsewhere.md.
Short version: edit the externalDocBase constant in
DocGen4/Output/External.lean and rebuild.
Source locations default to guessing the GitHub repo for the library, but
different schemes can be used by setting the DOCGEN_SRC environment
variable. For example, to open the local source file in VSCode:
DOCGEN_SRC="vscode" lake build YourLibraryName:docsDOCGEN_SRC |
Effect |
|---|---|
github (default if unset) |
Infers the GitHub project for each library and links to the GitHub source view. |
file |
Creates file:// references to local source files. |
vscode |
Creates VSCode URLs to local source files. |
Generation of equations for definitions is enabled by default; disable it
by setting DISABLE_EQUATIONS=1.
| Symptom | Likely cause | Fix |
|---|---|---|
lake build :docs fails before producing any HTML |
Target library doesn't compile | Run lake build YourLibraryName first; fix the underlying compile error. |
| Build succeeds but external links 404 in browser | The Mathlib site doesn't host your dep | Edit externalDocBase (see pointing-elsewhere). |
Empty HTML pages for things like MD4Lean.html, SQLite.html |
Stale doc-gen4 binary (pre-fix) cached by Lake | lake clean inside docbuild, then rebuild. |
| Cross-module local links 404 | Stale index.html / navbar.html from a previous build with a different module set |
Delete .lake/build/doc/ (or run lake clean) and rebuild. |
| Generated site renders but JS / search broken | Opening index.html directly via file:// |
Serve via python3 -m http.server (see Usage). |
| Want to verify it actually works | — | Browse the dogfooded build of this fork: https://gametheoryinlean.github.io/docgen/. |
More detail in docs/usage/troubleshooting.md.
If you previously used upstream doc-gen4 and want to switch to this fork:
-
Update your
docbuild/lakefile.toml[[require]]entry to point at this fork instead ofleanprover/doc-gen4. If pinning via Reservoir / scope isn't set up for this fork, point at the GitHub URL directly:[[require]] name = "doc-gen4" git = "https://github.com/gametheoryinlean/docgen" rev = "main"
-
Run
lake update doc-gen4withindocbuild. -
Run
lake cleanwithindocbuild(force a full rebuild — without this, stale per-module marker files prevent the redesign from taking effect). -
Run
lake build YourLibraryName:docs.
Expected differences after upgrade:
- Build is significantly faster and produces a much smaller
doc/directory. - No more
Init/,Std/,Lake/,Lean/subdirectories or HTML for them. - References that previously linked to local Lean-core HTML now link to
https://leanprover-community.github.io/mathlib4_docs/find/?pattern=…#doc. - Your search box only finds declarations in your own modules.
If anything regresses, revert to upstream.
If someone sends a message containing docs#Nat.add on the Lean Zulip,
this auto-links to Nat.add in the Mathlib4 documentation. The mechanism
is the /find redirect:
https://example.com/path/to/docs/find/?pattern=Nat.add#doc
For Mathlib this ends up resolving to
https://leanprover-community.github.io/mathlib4_docs/find/?pattern=Nat.add#doc
which the page's declaration-data.bmp + find.js then redirect to the
real declaration page.
This fork uses the same redirect form to link external decls — see External library linking.
To build docs using a locally-modified doc-gen4, replace the doc-gen4
require in your docbuild/lakefile.toml:
[[require]]
name = "doc-gen4"
path = "../../path/to/your/doc-gen4"Note: if you modify the
.jsor.cssfiles indoc-gen4, they won't necessarily be copied over when you rebuild the documentation. Either manually copy the changes todocbuild/.lake/build/doc, or do a full recompilation (lake cleanandlake buildinside thedoc-gen4directory).
For the design rationale behind the external-linking redesign, hook points,
trade-offs, and edge cases, see
docs/dev/design/external-linking.md.
The per-issue implementation specs are under
docs/dev/issues/.