scribble-tools
| (require scribble-tools) | package: scribble-tools |
This library provides Scribble forms for typesetting CSS, HTML, JavaScript, shell scripts (Bash/Zsh/PowerShell), WebAssembly (WAT), and Scribble snippets with syntax coloring.
The inline forms (css-code, html-code, js-code, shell-code, wasm-code, and scribble-code) produce content.
The block forms (cssblock, htmlblock, jsblock, shellblock, wasmblock, and scribbleblock) produce code blocks with optional line numbers, file labels, and escapes.
1 Guide
This section gives a practical introduction to the forms and the most useful options.
1.1 Inline Forms
Use inline forms when you want code inside running text:
Language |
| Scribble Form |
CSS |
| @css-code{.card { color: #c33; }} |
HTML |
| @html-code{<button class="primary">Save</button>} |
JavaScript |
| @js-code{const total = items.reduce((a, b) => a + b, 0);} |
Shell |
| @shell-code[#:shell 'bash]{if [ -f ~/.zshrc ]; then echo ok; fi} |
WebAssembly |
| @wasm-code{(module (func (result i32) (i32.const 42)))} |
Scribble |
| @scribble-code{"@bold{Hello} world."} |
Language |
| Result |
CSS |
| .card { color: #c33; } |
HTML |
| |
JavaScript |
| |
Shell |
| |
WebAssembly |
| |
Scribble |
| @bold{Hello} world. |
If you want scribble-code to link identifiers to their documentation, you need to provide a context. Either add "#:context #'here" when calling scribble-code, or set the context using a parameter:
@current-scribble-context[#'here] @scribble-code["@bold{Hello} world."]
@bold{Hello} world.
1.2 Block Forms
Use block forms for larger snippets:
|
|
| |||||||||||||
|
|
| |||||||||||||
|
|
| |||||||||||||
|
|
| |||||||||||||
|
|
| |||||||||||||
|
|
|
1.3 Block Form Decorations
Use these options to add decorations to block output:
Scribble source |
| Rendered result | |||||||||
|
|
| |||||||||
|
|
| |||||||||
|
|
|
1.4 Preview Visualizations
css-code and cssblock can show visual helpers:
.badge { color: #0a7; background: linear-gradient(90deg, #0a7, #5cf); font-family: "Fira Code", monospace;Aa margin: 16px; border-radius: 4px; border-radius: 8px; }
1.5 Escapes
All forms support escapes to splice Scribble content:
.notice { color: tomato; }
1.6 Documentation Links
By default, code output includes documentation links for common identifiers:
CSS properties (for example display, grid, border-radius).
Common JavaScript classes, methods, and language keywords (for example Array, querySelector, map, const).
Common shell keywords and builtins (for example if, setopt, Get-ChildItem), linked to GNU Bash, Zsh, or PowerShell documentation.
Common WebAssembly instructions and declarations (for example module, func, local.get, i32.add), linked to the WebAssembly Core Spec site by default.
2 Reference
This section documents each form and procedure in detail.
2.1 Inline Forms
syntax
(css-code maybe-option ... str-expr ...+)
maybe-option =
| #:color-swatch? color-swatch?-expr | #:font-preview? font-preview?-expr | #:dimension-preview? dimension-preview?-expr | #:mdn-links? mdn-links?-expr | #:preview-mode preview-mode-expr | #:preview-tooltips? preview-tooltips?-expr | #:preview-css-url preview-css-url-expr | #:escape escape-id
#:color-swatch? controls whether detected CSS color literals are followed by a small color swatch (default: #t). Gradient literals (for example linear-gradient (...)) are shown as a small bar swatch.
#:font-preview? controls whether font-family declarations are followed by a small Aa preview in the selected font (default: #t).
#:dimension-preview? controls whether spacing and radius declarations such as margin, padding, gap, and border-radius get tiny inline visualizers (default: #t).
#:mdn-links? controls whether common CSS tokens are wrapped as hyperlinks to MDN documentation (default: #t).
#:preview-mode controls when previews are shown: 'always, 'hover, or 'none (default: 'always).
#:preview-tooltips? controls whether preview decorations expose tooltips (hover/focus) and related runtime tooltip behavior (default: #t).
#:preview-css-url optionally points to an external stylesheet for preview UI classes. When provided, the runtime loads that stylesheet instead of injecting inline preview CSS.
An optional #:escape identifier configures escapes of the form (escape-id expr) to splice expr-produced elements into the typeset output.
Example: h1 { color: #c33; }
syntax
(html-code maybe-escape str-expr ...+)
maybe-escape =
| #:mdn-links? mdn-links?-expr | #:escape escape-id
An optional #:escape identifier configures escapes of the form (escape-id expr) to splice expr-produced elements into the typeset output.
#:mdn-links? controls whether common HTML tokens are wrapped as hyperlinks to MDN documentation, including CSS and JavaScript tokens that appear inside <style> and <script> sections (default: #t).
Example: <em class="note">Hi</em>
syntax
(js-code maybe-escape str-expr ...+)
maybe-escape =
| #:jsx? jsx?-expr | #:mdn-links? mdn-links?-expr | #:escape escape-id
#:jsx? enables JSX-aware tokenization for snippets that contain embedded tags (default: #f).
#:mdn-links? controls whether common JavaScript tokens are wrapped as hyperlinks to MDN documentation (default: #t).
An optional #:escape identifier configures escapes of the form (escape-id expr) to splice expr-produced elements into the typeset output.
Example: const n = 42;
syntax
(shell-code maybe-options str-expr ...+)
maybe-options =
| #:shell shell-expr | #:docs-source docs-source-expr | #:escape escape-id
#:shell selects shell flavor: 'bash, 'zsh, 'powershell, or 'pwsh. Default: (current-scribble-shell).
#:docs-source selects where shell documentation links point: 'auto, 'bash, 'zsh, 'powershell, 'posix, or 'none. Default: (current-shell-docs-source). When the effective value is 'auto, links follow the effective shell: 'bash when #:shell (or current-scribble-shell) is 'bash, 'zsh when it is 'zsh, and 'powershell when it is 'powershell or 'pwsh.
An optional #:escape identifier configures escapes of the form (escape-id expr) to splice expr-produced elements into the typeset output.
Example: if [ -f ~/.zshrc ]; then echo ok; fi
syntax
(wasm-code maybe-escape str-expr ...+)
maybe-escape =
| #:docs-source docs-source-expr | #:escape escape-id
#:docs-source selects where WebAssembly documentation links point: 'wasm-spec-3.0, 'mdn, or 'none. The default comes from current-wasm-docs-source, which defaults to 'wasm-spec-3.0.
An optional #:escape identifier configures escapes of the form (escape-id expr) to splice expr-produced elements into the typeset output.
Example: (module (func (result i32) (i32.const 42)))
syntax
(scribble-code maybe-options str-expr ...+)
maybe-options =
| #:context context-expr | #:escape escape-id
#:context supplies syntax context for identifier link resolution (default: (current-scribble-context)). Recommended: use #'here when you want identifiers in a snippet to resolve against the current manual’s for-label imports.
An optional #:escape identifier configures escapes of the form (escape-id expr) to splice expr-produced elements into the typeset output.
Example: @bold{Hi} there.
2.2 Block Forms
syntax
(cssblock option ... str-expr ...+)
option = #:indent indent-expr | #:line-numbers line-number-expr | #:line-number-sep line-number-sep-expr | #:copy-button? copy-button?-expr | #:color-swatch? color-swatch?-expr | #:font-preview? font-preview?-expr | #:dimension-preview? dimension-preview?-expr | #:mdn-links? mdn-links?-expr | #:preview-mode preview-mode-expr | #:preview-tooltips? preview-tooltips?-expr | #:preview-css-url preview-css-url-expr | #:file filename-expr | #:escape escape-id
indent-expr : exact-nonnegative-integer?
line-number-expr : (or/c #f exact-nonnegative-integer?)
line-number-sep-expr : exact-nonnegative-integer?
#:indent controls left indentation in spaces (default: 0).
#:line-numbers enables line numbers when not #f, using the given start number (default: #f).
#:line-number-sep controls the spacing between the line number and code (default: 1).
#:copy-button? controls whether a copy icon appears on hover/focus to copy the block text to the clipboard (default: #t).
#:color-swatch? controls whether detected CSS color literals are followed by a small swatch; gradient literals are shown as a small bar (default: #t).
#:font-preview? controls whether font-family declarations are followed by a small Aa preview (default: #t).
#:dimension-preview? controls whether spacing and radius declarations (for example margin, padding, gap, letter-spacing, text-indent, filter: blur (...), and border-radius) are followed by small visualizer decorations (default: #t).
#:mdn-links? controls whether common CSS tokens are wrapped as hyperlinks to MDN documentation (default: #t).
#:preview-mode controls when previews are shown: 'always, 'hover, or 'none (default: 'always).
#:preview-tooltips? controls whether preview decorations include tooltip text and interactive hover/focus tooltip UI (default: #t).
#:preview-css-url optionally points to an external stylesheet URL/path for preview classes; when set, runtime links that stylesheet instead of injecting inline preview CSS (default: #f).
#:file wraps the result in filebox with filename-expr as label (default: #f, i.e. no file label).
#:escape changes the escape identifier; subforms of the shape (escape-id expr) splice expr as content (default escape id: unsyntax).
Example:
1 .card { 2 color: #c33; 3 }
syntax
(cssblock0 option ... str-expr ...+)
Example:
.compact { color: #444; }
syntax
(htmlblock option ... str-expr ...+)
option = #:indent indent-expr | #:line-numbers line-number-expr | #:line-number-sep line-number-sep-expr | #:copy-button? copy-button?-expr | #:mdn-links? mdn-links?-expr | #:file filename-expr | #:escape escape-id
indent-expr : exact-nonnegative-integer?
line-number-expr : (or/c #f exact-nonnegative-integer?)
line-number-sep-expr : exact-nonnegative-integer?
#:indent controls left indentation in spaces (default: 0).
#:line-numbers enables line numbers when not #f, using the given start number (default: #f).
#:line-number-sep controls the spacing between the line number and code (default: 1).
#:copy-button? controls whether a copy icon appears on hover/focus to copy the block text to the clipboard (default: #t).
#:mdn-links? controls whether common HTML tokens are wrapped as hyperlinks to MDN documentation, including CSS and JavaScript tokens that appear inside <style> and <script> sections (default: #t).
#:file wraps the result in filebox with filename-expr as label (default: #f, i.e. no file label).
#:escape changes the escape identifier; subforms of the shape (escape-id expr) splice expr as content (default escape id: unsyntax).
Example:
syntax
(htmlblock0 option ... str-expr ...+)
Example:
syntax
(jsblock option ... str-expr ...+)
option = #:indent indent-expr | #:line-numbers line-number-expr | #:line-number-sep line-number-sep-expr | #:copy-button? copy-button?-expr | #:jsx? jsx?-expr | #:mdn-links? mdn-links?-expr | #:file filename-expr | #:escape escape-id
indent-expr : exact-nonnegative-integer?
line-number-expr : (or/c #f exact-nonnegative-integer?)
line-number-sep-expr : exact-nonnegative-integer?
#:indent controls left indentation in spaces (default: 0).
#:line-numbers enables line numbers when not #f, using the given start number (default: #f).
#:line-number-sep controls the spacing between the line number and code (default: 1).
#:copy-button? controls whether a copy icon appears on hover/focus to copy the block text to the clipboard (default: #t).
#:jsx? enables JSX-aware tokenization for snippets containing embedded tags (default: #f).
#:mdn-links? controls whether common JavaScript tokens are wrapped as hyperlinks to MDN documentation (default: #t).
#:file wraps the result in filebox with filename-expr as label (default: #f, i.e. no file label).
#:escape changes the escape identifier; subforms of the shape (escape-id expr) splice expr as content (default escape id: unsyntax).
Example:
syntax
(jsblock0 option ... str-expr ...+)
Example:
syntax
(shellblock option ... str-expr ...+)
option = #:shell shell-expr | #:docs-source docs-source-expr | #:indent indent-expr | #:line-numbers line-number-expr | #:line-number-sep line-number-sep-expr | #:copy-button? copy-button?-expr | #:file filename-expr | #:escape escape-id
indent-expr : exact-nonnegative-integer?
line-number-expr : (or/c #f exact-nonnegative-integer?)
line-number-sep-expr : exact-nonnegative-integer?
#:shell selects shell flavor: 'bash, 'zsh, 'powershell, or 'pwsh. Default: (current-scribble-shell).
#:docs-source selects link targets: 'auto, 'bash, 'zsh, 'powershell, 'posix, or 'none. Default: (current-shell-docs-source). With 'auto, links follow the effective shell selected by #:shell (or current-scribble-shell).
#:indent controls left indentation in spaces (default: 0).
#:line-numbers enables line numbers when not #f, using the given start number (default: #f).
#:line-number-sep controls the spacing between the line number and code (default: 1).
#:copy-button? controls whether a copy icon appears on hover/focus to copy the block text to the clipboard (default: #t).
#:file wraps the result in filebox with filename-expr as label (default: #f, i.e. no file label).
#:escape changes the escape identifier; subforms of the shape (escape-id expr) splice expr as content (default escape id: unsyntax).
Example:
syntax
(shellblock0 option ... str-expr ...+)
Example:
syntax
(wasmblock option ... str-expr ...+)
option = #:indent indent-expr | #:line-numbers line-number-expr | #:line-number-sep line-number-sep-expr | #:copy-button? copy-button?-expr | #:docs-source docs-source-expr | #:file filename-expr | #:escape escape-id
indent-expr : exact-nonnegative-integer?
line-number-expr : (or/c #f exact-nonnegative-integer?)
line-number-sep-expr : exact-nonnegative-integer?
#:indent controls left indentation in spaces (default: 0).
#:line-numbers enables line numbers when not #f, using the given start number (default: #f).
#:line-number-sep controls the spacing between the line number and code (default: 1).
#:copy-button? controls whether a copy icon appears on hover/focus to copy the block text to the clipboard (default: #t).
#:docs-source selects WebAssembly link targets: 'wasm-spec-3.0, 'mdn, or 'none. Default: (current-wasm-docs-source).
#:file wraps the result in filebox with filename-expr as label (default: #f, i.e. no file label).
#:escape changes the escape identifier; subforms of the shape (escape-id expr) splice expr as content (default escape id: unsyntax).
Example:
syntax
(wasmblock0 option ... str-expr ...+)
Example:
parameter
(current-wasm-docs-source) → (or/c 'wasm-spec-3.0 'mdn 'none)
(current-wasm-docs-source src) → void? src : (or/c 'wasm-spec-3.0 'mdn 'none)
parameter
(current-scribble-shell) → (or/c 'bash 'zsh 'powershell 'pwsh)
(current-scribble-shell sh) → void? sh : (or/c 'bash 'zsh 'powershell 'pwsh)
parameter
→ (or/c 'auto 'bash 'zsh 'powershell 'pwsh 'posix 'none) (current-shell-docs-source src) → void? src : (or/c 'auto 'bash 'zsh 'powershell 'pwsh 'posix 'none)
parameter
(current-scribble-context) → (or/c #f syntax?)
(current-scribble-context ctx) → void? ctx : (or/c #f syntax?)
syntax
(scribbleblock option ... str-expr ...+)
option = #:indent indent-expr | #:line-numbers line-number-expr | #:line-number-sep line-number-sep-expr | #:lang lang-expr | #:context context-expr | #:copy-button? copy-button?-expr | #:file filename-expr | #:escape escape-id
indent-expr : exact-nonnegative-integer?
line-number-expr : (or/c #f exact-nonnegative-integer?)
line-number-sep-expr : exact-nonnegative-integer?
lang-expr : string?
context-expr : (or/c #f syntax?)
The most important option is #:context. If provided identifiers will be linked to their documentation entries. If you are using the same context several times, it can be convenient to set the parameter current-scribble-context instead of using #:context repeatedly.
Options:
#:indent controls left indentation in spaces (default: 0).
#:line-numbers enables line numbers when not #f, using the given start number (default: #f).
#:line-number-sep controls the spacing between the line number and code (default: 1).
#:lang chooses the language line used for parsing/linking when the snippet itself does not start with #lang (default: "scribble/manual").
#:context supplies syntax context for identifier link resolution (default: (current-scribble-context)). Recommended: use #'here when you want identifiers in a snippet to resolve against the current manual’s for-label imports.
#:copy-button? controls whether a copy icon appears on hover/focus to copy the block text to the clipboard (default: #t).
#:file wraps the result in filebox with filename-expr as label (default: #f, i.e. no file label).
#:escape changes the escape identifier; subforms of the shape (escape-id expr) splice expr as content (default escape id: unsyntax).
Example:
syntax
(scribbleblock0 option ... str-expr ...+)
Example:
2.3 Preview Legend
Rendered legend example:
.legend { color: #c33; background: linear-gradient(90deg, red, blue); margin: 4px; margin: 12px; margin: 28px; filter: blur(2px); filter: blur(8px); filter: blur(18px); border-radius: 2px; border-radius: 6px; border-radius: 9px; font-family: "Fira Code", monospace;Aa font-family: "Georgia", serif;Aa font-family: "Helvetica Neue", Arial, sans-serif;Aa }
Color square: a detected color literal such as #c33 or red.
Gradient bar: a detected gradient literal such as linear-gradient (...).
Spacing bar: detected spacing-sized values (for example margin, gap, letter-spacing, or filter: blur (...)) scaled to a compact width.
Radius chip: detected border-radius values, where the chip corner radius mirrors the declaration.
Font Aa: preview of font-family, including fallback resolution tooltip and missing-font warning.
2.4 MDN Maps
MDN maps control which CSS/HTML/JavaScript/WebAssembly identifiers become links to the MDN documentation site. The procedures below let you inspect the active map, install overrides, reset to defaults, and export the bundled entries. Most users will not need these tools, but they are useful when you want to add links that are not covered by the default maps.
procedure
(mdn-map-path) → path?
procedure
(mdn-entry? v) → boolean?
v : any/c
procedure
(mdn-install-map! entries-or-path) → path?
entries-or-path :
(or/c path-string? (listof (list/c symbol? symbol? string? string?)))
procedure
procedure
(mdn-export-default-map! dest) → path-string?
dest : path-string?
Command-line helper:
racket -l scribble-tools/mdn-map-tool -- --path |
racket -l scribble-tools/mdn-map-tool -- --export-default mdn-map.rktd |
racket -l scribble-tools/mdn-map-tool -- --build-default mdn-map-built.rktd |
racket -l scribble-tools/mdn-map-tool -- --install mdn-map.rktd |
racket -l scribble-tools/mdn-map-tool -- --update-from mdn-map-custom.rktd |
racket -l scribble-tools/mdn-map-tool -- --reset |
Map build pipeline (dedupe + optional merge):
racket -l scribble-tools/mdn-map-build -- --stats |
racket -l scribble-tools/mdn-map-build -- --out mdn-map-built.rktd |
racket -l scribble-tools/mdn-map-build -- --merge mdn-map-custom.rktd --out mdn-map-merged.rktd |
racket -l scribble-tools/mdn-map-build -- --merge mdn-map-custom.rktd --install |
3 Extended Examples
This chapter provides longer rendered examples for each supported language. Each block uses line numbers and a file label to make lexer behavior and documentation links easier to inspect.
3.1 CSS
"extended/styles.css"
1 :root { 2 --brand: #0b62a3; 3 --accent: oklch(66% 0.18 28); 4 } 5 6 .layout { 7 display: grid; 8 grid-template-columns: 240px 1fr; 9 gap: clamp(0.75rem, 2vw, 1.5rem); 10 margin: 16px; 11 border-radius: 9px; 12 background: linear-gradient(90deg, #f6f8fb, #eef3ff); 13 } 14 15 .button { 16 color: white; 17 background: color-mix(in srgb, var--brand(--brand) 80%, black); 18 border: 1px solid #0a4f83; 19 padding: 0.5rem 0.8rem; 20 font-family: "Fira Code", "JetBrains Mono", monospace;Aa 21 }
3.2 HTML
"extended/index.html"
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="utf-8"> 5 <title>Extended Example</title> 6 <style> 7 .hero { color: #c33; margin: 12px; } 8 .hero em { font-family: "Georgia", serif;Aa } 9 </style> 10 </head> 11 <body> 12 <main id="app"> 13 <h1 class="hero">Hello <em>world</em></h1> 14 <button type="button" data-role="save">Save</button> 15 </main> 16 <script> 17 const root = document.querySelector("#app"); 18 if (root) root.setAttribute("data-ready", "yes"); 19 </script> 20 </body> 21 </html>
3.3 JavaScript
"extended/app.js"
1 function quickSort(xs, cmp = (a, b) => a - b) { 2 if (xs.length <= 1) return xs.slice(); 3 const [pivot, ...rest] = xs; 4 const left = []; 5 const right = []; 6 for (const x of rest) { 7 if (cmp(x, pivot) < 0) left.push(x); else right.push(x); 8 } 9 return [...quickSort(left, cmp), pivot, ...quickSort(right, cmp)]; 10 } 11 12 function renderNumbers(listEl, numbers) { 13 listEl.textContent = ""; 14 for (const n of numbers) { 15 const li = document.createElement("li"); 16 li.textContent = String(n); 17 listEl.append(li); 18 } 19 } 20 21 function parseInput(inputEl) { 22 return inputEl.value 23 .split(/[\\s,]+/) 24 .map((s) => s.trim()) 25 .filter(Boolean) 26 .map(Number) 27 .filter((n) => Number.isFinite(n)); 28 } 29 30 function boot() { 31 const inputEl = document.querySelector("#numbers"); 32 const buttonEl = document.querySelector("#sort"); 33 const listEl = document.querySelector("#result"); 34 if (!inputEl || !buttonEl || !listEl) return; 35 36 buttonEl.addEventListener("click", () => { 37 const data = parseInput(inputEl); 38 const sorted = quickSort(data); 39 renderNumbers(listEl, sorted); 40 }); 41 } 42 43 boot();
3.4 Shell
This utility copies one directory tree to another and validates arguments before running the copy operation.
"extended/copy-tree.sh"
1 #!/usr/bin/env bash 2 set -euo pipefail 3 4 usage() { 5 echo "usage: $0 <source-dir> [dest-dir]" 6 } 7 8 copy_tree() { 9 local src="$1" 10 local dst="$2" 11 mkdir -p "$dst" 12 cp -R "$src"/. "$dst"/ 13 } 14 15 main() { 16 if [ "$#" -lt 1 ] || [ "$#" -gt 2 ]; then 17 usage 18 return 2 19 fi 20 local src="$1" 21 local dst="${2:-./out}" 22 if [ ! -d "$src" ]; then 23 echo "error: source directory not found: $src" >&2 24 return 1 25 fi 26 copy_tree "$src" "$dst" 27 echo "copied $src -> $dst" 28 } 29 30 main "$@"
3.5 WebAssembly
"extended/module.wat"
1 (module 2 (memory (export "mem") 1) 3 (func $sum_to_acc (param $i i32) (param $n i32) (param $acc i32) (result i32) 4 (if (result i32) (i32.le_s (local.get $i) (local.get $n)) 5 (then 6 (call $sum_to_acc 7 (i32.add (local.get $i) (i32.const 1)) 8 (local.get $n) 9 (i32.add (local.get $acc) (local.get $i)))) 10 (else 11 (local.get $acc)))) 12 (func (export "sum_to") (param $n i32) (result i32) 13 (call $sum_to_acc (i32.const 0) (local.get $n) (i32.const 0))))
3.6 Scribble
"extended/guide.scrbl"
2 @title{Extended Scribble Example} 3 @section{Overview} 4 This paragraph includes @bold{inline formatting}, 5 @italic{emphasis}, and @racket[code] references. 6 @itemlist[ 7 @item{First point} 8 @item{Second point} 9 @item{Third point} 10 ] 11 @subsection{Details} 12 See @secref["reference-inline-forms"] for inline forms. 13