Skip to content

Commit 7c8cb45

Browse files
committed
Merge branch 'haita'
2 parents c6fbd6d + 768740d commit 7c8cb45

7 files changed

Lines changed: 174 additions & 59 deletions

File tree

‎docs/book/dist.typ‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
#import "@preview/haita:0.3.0": *
22

3+
#import "/src/lib.typ" as prequery
4+
#import "tidy.typ": set-info
5+
6+
#set-info(
7+
package-meta: toml("/typst.toml").package,
8+
scope: (prequery: prequery),
9+
)
10+
311
#book(
412
title: "Prequery",
513
canonical-url: "https://https://typst-community.github.io",

‎docs/book/package/api.typ‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
#import "@preview/crudo:0.1.1"
22

3+
#import "/docs/book/tidy.typ": module, tidy.parse-module
4+
35
#title[Prequery API]
46

5-
For now, you can find the prequery package's API in the PDF manual.
6-
The latest released version can be found on the package's #link("https://typst.app/universe/package/prequery")[Universe page],
7-
the development version can be found #link("https://github.com/typst-community/prequery/blob/main/docs/manual.pdf?raw=1")[on Github].
7+
#module(
8+
read("/src/lib.typ"),
9+
name: none,
10+
label-prefix: none,
11+
)

‎docs/book/tidy.typ‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#import "@preview/tidy:0.4.3"
2+
3+
#import "../man-style.typ"
4+
5+
#let _info = state("manual:info")
6+
7+
// set the package metadata and scope
8+
#let set-info(
9+
package-meta: none,
10+
scope: (:),
11+
) = {
12+
_info.update((
13+
meta: package-meta,
14+
scope: scope,
15+
))
16+
}
17+
18+
// retrieve the package metadata (contextual)
19+
#let package-meta() = _info.get().meta
20+
21+
#let module(
22+
code,
23+
name: none,
24+
label-prefix: auto,
25+
scope: (:),
26+
preamble: "",
27+
..args,
28+
) = {
29+
let (name, label-prefix) = (name, label-prefix)
30+
if label-prefix == auto and name != none {
31+
label-prefix = name + "."
32+
} else if type(label-prefix) == str {
33+
label-prefix += "."
34+
} else {
35+
assert(label-prefix == none or name == none)
36+
label-prefix = ""
37+
}
38+
if name != none {
39+
name = raw(name)
40+
}
41+
42+
context {
43+
let scope = _info.get().scope + scope
44+
let module = tidy.parse-module(
45+
code,
46+
name: name,
47+
label-prefix: label-prefix,
48+
scope: scope,
49+
preamble: preamble,
50+
)
51+
tidy.show-module(
52+
module,
53+
show-module-name: name != none,
54+
sort-functions: none,
55+
style: man-style,
56+
show-outline: false,
57+
..args,
58+
)
59+
// [#module]
60+
}
61+
}

‎docs/man-style.typ‎

Lines changed: 98 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
// adapted from https://github.com/Mc-Zen/tidy/blob/v0.3.0/src/styles/minimal.typ
22

3+
#import "@preview/bullseye:0.1.0": match-target, on-target
4+
5+
#let lazy-target(..targets) = match-target(..targets)()
6+
37
// ==== internal utilities
48

59
// https://github.com/jneug/typst-mantys/blob/cb32c63394ef441eb6038d4090634c7d823b9e11/src/api/types.typ
@@ -114,49 +118,71 @@
114118
#let radius = 2pt
115119
#let preview-radius = 0.32em
116120

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 = {
132123
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+
}
136141
}
137142
}
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+
}
139158

140159
#let get-type-color(type) = rgb("#eff0f3")
141160

142161
#let show-types(types, style-args, joiner: h(0.3em)) = {
143162
types.map(style-args.style.show-type.with(style-args: style-args)).join(joiner)
144163
}
145164

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+
)
160186

161187
#let preview-block(body, no-codly: true, in-raw: true, ..args) = {
162188
import "template.typ": codly
@@ -199,8 +225,7 @@
199225
let gen-entry(name, args: none) = {
200226
let entry = mono-fn(name, args: args)
201227
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
204229
entry = link(label(lbl), entry)
205230
}
206231
entry
@@ -227,13 +252,19 @@
227252
}
228253

229254
// 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+
)
237268

238269
#let show-function(
239270
fn, style-args,
@@ -242,13 +273,9 @@
242273
import tidy.utilities: *
243274

244275
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)
250277
})
251-
pad(x: 0em, eval-docstring(fn.description, style-args))
278+
block(eval-docstring(fn.description, style-args))
252279

253280
let args = fn.args.pairs()
254281
if style-args.omit-private-parameters {
@@ -274,13 +301,16 @@
274301
[*#parameters-string:*]
275302
args.join()
276303
}
277-
v(4em, weak: true)
304+
context on-target(paged: {
305+
v(4em, weak: true)
306+
})
278307
}
279308

280309
#let show-parameter-list(fn, style-args) = {
281310
signature-block(breakable: style-args.break-param-descriptions, {
282311
mono-fn(
283312
fn.name,
313+
lbl: label(style-args.label-prefix + "fn-" + fn.name),
284314
args: {
285315
let args = fn.args.pairs()
286316
if style-args.omit-private-parameters {
@@ -324,6 +354,19 @@
324354
)
325355

326356
#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+
327370
let (name, args) = if name.ends-with("()") {
328371
(name.slice(0, -2), ())
329372
} else {
@@ -339,18 +382,17 @@
339382
import tidy.utilities: *
340383

341384
signature-block(breakable: style-args.break-param-descriptions, {
342-
let var-signature = mono-fn(
385+
mono-fn(
343386
var.name,
387+
lbl: label(style-args.label-prefix + "var-" + var.name),
344388
ret: if "type" in var { (style-args.style.show-type)(var.type, style-args: style-args) },
345389
)
346-
let lbl = if style-args.enable-cross-references {
347-
label(style-args.label-prefix + var.name)
348-
}
349-
[#var-signature #lbl]
350390
})
351-
pad(x: 0em, eval-docstring(var.description, style-args))
391+
block(eval-docstring(var.description, style-args))
352392

353-
v(4em, weak: true)
393+
context on-target(paged: {
394+
v(4em, weak: true)
395+
})
354396
}
355397

356398
#let show-example(no-codly: true, in-raw: true, ..args) = {

‎docs/manual.pdf‎

27 KB
Binary file not shown.

‎gallery/test-fallback.pdf‎

443 Bytes
Binary file not shown.

‎gallery/test.pdf‎

-285 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)