|
1 | 1 | // adapted from https://github.com/Mc-Zen/tidy/blob/v0.3.0/src/styles/minimal.typ |
2 | 2 |
|
| 3 | +#import "@preview/bullseye:0.1.0": match-target, on-target |
| 4 | + |
| 5 | +#let lazy-target(..targets) = match-target(..targets)() |
| 6 | + |
3 | 7 | // ==== internal utilities |
4 | 8 |
|
5 | 9 | // https://github.com/jneug/typst-mantys/blob/cb32c63394ef441eb6038d4090634c7d823b9e11/src/api/types.typ |
|
114 | 118 | #let radius = 2pt |
115 | 119 | #let preview-radius = 0.32em |
116 | 120 |
|
117 | | -#let mono-fn(name, args: none, ret: none) = mono({ |
118 | | - text(name-fill, name) |
119 | | - if args != none { |
120 | | - let args = args.map(box) |
121 | | - if args.len() <= 3 { |
122 | | - "(" |
123 | | - args.join(", ") |
124 | | - ")" |
125 | | - } else { |
126 | | - "(\n" |
127 | | - args.map(arg => " " + arg + ",").join("\n") |
128 | | - "\n)" |
129 | | - } |
130 | | - } |
131 | | - if ret != none { |
| 121 | +#let mono-fn(name, args: none, ret: none, lbl: none) = { |
| 122 | + let signature = { |
132 | 123 | if args != none { |
133 | | - box[~-> #ret] |
134 | | - } else { |
135 | | - box[: #ret] |
| 124 | + let args = args.map(box) |
| 125 | + if args.len() <= 3 { |
| 126 | + "(" |
| 127 | + args.join(", ") |
| 128 | + ")" |
| 129 | + } else { |
| 130 | + "(\n" |
| 131 | + args.map(arg => " " + arg + ",").join("\n") |
| 132 | + "\n)" |
| 133 | + } |
| 134 | + } |
| 135 | + if ret != none { |
| 136 | + if args != none { |
| 137 | + box[~-> #ret] |
| 138 | + } else { |
| 139 | + box[: #ret] |
| 140 | + } |
136 | 141 | } |
137 | 142 | } |
138 | | -}) |
| 143 | + context lazy-target( |
| 144 | + paged: () => mono({ |
| 145 | + text(name-fill, name) |
| 146 | + [#signature#lbl] |
| 147 | + }), |
| 148 | + html: () => { |
| 149 | + if lbl != none { |
| 150 | + let chapter-heading-state = state("prequery/package/api chapter state", ()) |
| 151 | + chapter-heading-state.update(arr => arr + (lbl,)) |
| 152 | + } |
| 153 | + [#html.span(class: "text-[#1f2a63]", name)#lbl] |
| 154 | + signature |
| 155 | + } |
| 156 | + ) |
| 157 | +} |
139 | 158 |
|
140 | 159 | #let get-type-color(type) = rgb("#eff0f3") |
141 | 160 |
|
142 | 161 | #let show-types(types, style-args, joiner: h(0.3em)) = { |
143 | 162 | types.map(style-args.style.show-type.with(style-args: style-args)).join(joiner) |
144 | 163 | } |
145 | 164 |
|
146 | | -#let signature-block(..args) = { |
147 | | - let bar-width = 1mm |
148 | | - set par(justify: false) |
149 | | - block( |
150 | | - width: 100%, |
151 | | - radius: radius, |
152 | | - fill: signature-fill, |
153 | | - stroke: (left: bar-width + name-fill), |
154 | | - outset: (left: -bar-width / 2), |
155 | | - inset: (x: 0.7em, y: 0.7em), |
156 | | - sticky: true, |
157 | | - ..args |
158 | | - ) |
159 | | -} |
| 165 | +#let signature-block(..args) = context lazy-target( |
| 166 | + paged: () => { |
| 167 | + let bar-width = 1mm |
| 168 | + set par(justify: false) |
| 169 | + block( |
| 170 | + width: 100%, |
| 171 | + radius: radius, |
| 172 | + fill: signature-fill, |
| 173 | + stroke: (left: bar-width + name-fill), |
| 174 | + outset: (left: -bar-width / 2), |
| 175 | + inset: (x: 0.7em, y: 0.7em), |
| 176 | + sticky: true, |
| 177 | + ..args |
| 178 | + ) |
| 179 | + }, |
| 180 | + html: () => { |
| 181 | + let (body,) = args.pos() |
| 182 | + show: html.div.with(class: "mt-8 mb-2 p-2 border-l-4 border-l-[#1f2a63] rounded-md bg-[#d8dbed] text-sm font-mono not-prose") |
| 183 | + body |
| 184 | + }, |
| 185 | +) |
160 | 186 |
|
161 | 187 | #let preview-block(body, no-codly: true, in-raw: true, ..args) = { |
162 | 188 | import "template.typ": codly |
|
199 | 225 | let gen-entry(name, args: none) = { |
200 | 226 | let entry = mono-fn(name, args: args) |
201 | 227 | if style-args.enable-cross-references { |
202 | | - let lbl = prefix + name |
203 | | - if args != none { lbl += "()" } |
| 228 | + let lbl = prefix + if args != none {"fn-"} else {"var-"} + name |
204 | 229 | entry = link(label(lbl), entry) |
205 | 230 | } |
206 | 231 | entry |
|
227 | 252 | } |
228 | 253 |
|
229 | 254 | // Create beautiful, colored type box |
230 | | -#let show-type(type, style-args: (:)) = { |
231 | | - h(2pt) |
232 | | - type-link(type, { |
233 | | - box(outset: 2pt, fill: get-type-color(type), radius: 2pt, raw(type, lang: none)) |
234 | | - }) |
235 | | - h(2pt) |
236 | | -} |
| 255 | +#let show-type(type, style-args: (:)) = context lazy-target( |
| 256 | + paged: () => { |
| 257 | + h(2pt) |
| 258 | + type-link(type, { |
| 259 | + box(outset: 2pt, fill: get-type-color(type), radius: 2pt, raw(type, lang: none)) |
| 260 | + }) |
| 261 | + h(2pt) |
| 262 | + }, |
| 263 | + html: () => { |
| 264 | + show: html.span.with(class: "m-0.5 px-[2px] py-[1px] rounded-sm bg-gray-100 not-prose") |
| 265 | + type-link(type, type) |
| 266 | + }, |
| 267 | +) |
237 | 268 |
|
238 | 269 | #let show-function( |
239 | 270 | fn, style-args, |
|
242 | 273 | import tidy.utilities: * |
243 | 274 |
|
244 | 275 | block(breakable: style-args.break-param-descriptions, { |
245 | | - let parameter-list = (style-args.style.show-parameter-list)(fn, style-args) |
246 | | - let lbl = if style-args.enable-cross-references { |
247 | | - label(style-args.label-prefix + fn.name + "()") |
248 | | - } |
249 | | - [#parameter-list#lbl] |
| 276 | + (style-args.style.show-parameter-list)(fn, style-args) |
250 | 277 | }) |
251 | | - pad(x: 0em, eval-docstring(fn.description, style-args)) |
| 278 | + block(eval-docstring(fn.description, style-args)) |
252 | 279 |
|
253 | 280 | let args = fn.args.pairs() |
254 | 281 | if style-args.omit-private-parameters { |
|
274 | 301 | [*#parameters-string:*] |
275 | 302 | args.join() |
276 | 303 | } |
277 | | - v(4em, weak: true) |
| 304 | + context on-target(paged: { |
| 305 | + v(4em, weak: true) |
| 306 | + }) |
278 | 307 | } |
279 | 308 |
|
280 | 309 | #let show-parameter-list(fn, style-args) = { |
281 | 310 | signature-block(breakable: style-args.break-param-descriptions, { |
282 | 311 | mono-fn( |
283 | 312 | fn.name, |
| 313 | + lbl: label(style-args.label-prefix + "fn-" + fn.name), |
284 | 314 | args: { |
285 | 315 | let args = fn.args.pairs() |
286 | 316 | if style-args.omit-private-parameters { |
|
324 | 354 | ) |
325 | 355 |
|
326 | 356 | #let show-reference(label, name, style-args: none) = { |
| 357 | + // references in Tidy docs look like `@@variable` or `@@function()` |
| 358 | + // transform them into `prefix.var-variable` and `prefix.fn-function` |
| 359 | + let txt = str(label) |
| 360 | + let index = txt.rev().position(".") |
| 361 | + // index _after_ the period, if any |
| 362 | + let index = if index == none { 0 } else { txt.len() - index } |
| 363 | + if str(label).ends-with("()") { |
| 364 | + label = txt.slice(0, index) + "fn-" + txt.slice(index, -2) |
| 365 | + } else { |
| 366 | + label = txt.slice(0, index) + "var-" + txt.slice(index) |
| 367 | + } |
| 368 | + label = std.label(label) |
| 369 | + |
327 | 370 | let (name, args) = if name.ends-with("()") { |
328 | 371 | (name.slice(0, -2), ()) |
329 | 372 | } else { |
|
339 | 382 | import tidy.utilities: * |
340 | 383 |
|
341 | 384 | signature-block(breakable: style-args.break-param-descriptions, { |
342 | | - let var-signature = mono-fn( |
| 385 | + mono-fn( |
343 | 386 | var.name, |
| 387 | + lbl: label(style-args.label-prefix + "var-" + var.name), |
344 | 388 | ret: if "type" in var { (style-args.style.show-type)(var.type, style-args: style-args) }, |
345 | 389 | ) |
346 | | - let lbl = if style-args.enable-cross-references { |
347 | | - label(style-args.label-prefix + var.name) |
348 | | - } |
349 | | - [#var-signature #lbl] |
350 | 390 | }) |
351 | | - pad(x: 0em, eval-docstring(var.description, style-args)) |
| 391 | + block(eval-docstring(var.description, style-args)) |
352 | 392 |
|
353 | | - v(4em, weak: true) |
| 393 | + context on-target(paged: { |
| 394 | + v(4em, weak: true) |
| 395 | + }) |
354 | 396 | } |
355 | 397 |
|
356 | 398 | #let show-example(no-codly: true, in-raw: true, ..args) = { |
|
0 commit comments