|
1 | | -#import "/src/lib.typ" as cetz |
2 | | - |
3 | | -#import "/docs/style.typ": show-type |
| 1 | +#import "/docs/style.typ": show-type, show-module, setup, cetz |
4 | 2 | #import "/docs/typlodocus/extractor.typ" |
5 | 3 |
|
6 | | -#set heading(numbering: (..nums) => { |
7 | | - let nums = nums.pos() |
8 | | - if nums.len() <= 2 { |
9 | | - return nums.map(n => [#n]).join([.]) |
10 | | - } |
11 | | -}, hanging-indent: 0cm) |
| 4 | +#show: setup |
12 | 5 |
|
13 | 6 | #let modules = ( |
14 | 7 | [Canvas], |
|
28 | 21 | "src/draw/util.typ", |
29 | 22 |
|
30 | 23 | [Libraries], 1, |
31 | | - [Angle], |
32 | | - "src/lib/angle.typ", |
33 | | - [Tree], |
34 | | - "src/lib/tree.typ", |
35 | | - [Decorations], |
36 | | - [Path], |
37 | | - "src/lib/decorations/path.typ", |
38 | | - [Brace], |
39 | | - "src/lib/decorations/brace.typ", |
40 | | - [Palette], |
41 | | - "src/lib/palette.typ", |
| 24 | + [Angle], |
| 25 | + "src/lib/angle.typ", |
| 26 | + [Tree], |
| 27 | + "src/lib/tree.typ", |
| 28 | + [Decorations], 2, |
| 29 | + [Path], |
| 30 | + "src/lib/decorations/path.typ", |
| 31 | + [Brace], |
| 32 | + "src/lib/decorations/brace.typ", |
| 33 | + 1, |
| 34 | + [Palette], |
| 35 | + "src/lib/palette.typ", |
42 | 36 | 0, |
43 | 37 |
|
44 | 38 | [Internals], 1, |
|
82 | 76 | // Generate query metadata |
83 | 77 | #metadata(docs) <metadata> |
84 | 78 |
|
85 | | -/// Show a function signature annoted with types from the docstring |
86 | | -#let show-annotated-signature(signature, comment) = block({ |
87 | | - set par(leading: 0.35em) |
88 | | - |
89 | | - let name = signature.name |
90 | | - |
91 | | - let arguments = signature.arguments.map(arg => { |
92 | | - let comment-arg = comment.arguments.find(comment-arg => { |
93 | | - comment-arg.name == arg.name |
94 | | - }) |
95 | | - if comment-arg == none { |
96 | | - comment-arg = (types: (), name: name) |
97 | | - } |
98 | | - |
99 | | - let types = comment-arg.types |
100 | | - if types == none { types = () } |
101 | | - |
102 | | - if arg.has-default { |
103 | | - raw(arg.name + ":") + [ ] + types.map(show-type).join([ ]) |
104 | | - } else { |
105 | | - raw(arg.name) + [ ] + types.map(show-type).join([ ]) |
106 | | - } |
107 | | - }) |
108 | | - |
109 | | - let result = if comment.result != () { |
110 | | - [ #sym.arrow.r ] + comment.result.map(r => show-type(r.type)).join([ or ]) |
111 | | - } else { |
112 | | - [] |
113 | | - } |
114 | | - |
115 | | - text(blue, raw(name)) + raw("(") + [\ ] + arguments.map(v => h(1em) + v).join([,\ ]) + [\ ] + raw(")") + result |
116 | | -}) |
117 | | - |
118 | | -/// Render an example side-by-side with its source code. |
119 | | -#let render-example(code, vertical: false) = { |
120 | | - let columns = if vertical { |
121 | | - (1fr,) |
122 | | - } else { |
123 | | - (1fr, 2fr,) |
124 | | - } |
125 | | - |
126 | | - let align = if vertical { |
127 | | - (x, y) => (center + top, left + top).at(y) |
128 | | - } else { |
129 | | - (x, y) => (center + horizon, left + top).at(x) |
130 | | - } |
131 | | - |
132 | | - let stroke = 1pt + gray |
133 | | - let line = if vertical { table.hline } else { table.vline } |
134 | | - |
135 | | - block(radius: 2pt, stroke: stroke, { |
136 | | - table(columns: columns, align: align, stroke: none, |
137 | | - cetz.canvas({ |
138 | | - let preamble = "import cetz.draw: *\n" |
139 | | - eval(preamble + code, mode: "code", scope: ( |
140 | | - cetz: cetz, |
141 | | - )) |
142 | | - }), |
143 | | - line(stroke: (paint: gray, thickness: 1pt, dash: "dashed")), |
144 | | - raw(code, lang: "typc"), |
145 | | - ) |
146 | | - }) |
147 | | -} |
148 | | - |
149 | | -#show raw.where(lang: "example"): it => render-example(it.text) |
150 | | -#show raw.where(lang: "example-vertical"): it => render-example(it.text, vertical: true) |
151 | | -#show regex("type:([\w-]+)"): it => show-type(it.text.replace("type:", "")) |
152 | | - |
153 | | -#let show-docstring(comment, level) = { |
154 | | - let text = comment.text |
155 | | - let arguments = comment.arguments |
156 | | - let result = comment.result |
157 | | - |
158 | | - set heading(outlined: false, offset: level) |
159 | | - |
160 | | - block([ |
161 | | - #eval(text, mode: "markup", scope: ( |
162 | | - cetz: cetz, |
163 | | - )) |
164 | | - ]) |
165 | | - |
166 | | - if arguments != () { |
167 | | - heading("Parameters") |
168 | | - list(..arguments.map(arg => { |
169 | | - let types = arg.types |
170 | | - if types == none { types = ("any",) } |
171 | | - block( |
172 | | - strong(raw(arg.name)) + [ ] |
173 | | - + types.map(show-type).join([ ]) + [\ ] |
174 | | - + eval(arg.text, mode: "markup") |
175 | | - ) |
176 | | - })) |
177 | | - } |
178 | | - |
179 | | - if result.any(r => r.text != "") { |
180 | | - heading("Result") |
181 | | - |
182 | | - let show-result(r) = if r.text != "" { |
183 | | - let type = r.type |
184 | | - if type == none { type = "any" } |
185 | | - block( |
186 | | - show-type(type) + [\ ] + eval(r.text, mode: "markup") |
187 | | - ) |
188 | | - } |
189 | | - |
190 | | - if result.len() > 1 { |
191 | | - list(..result.map(show-result)) |
192 | | - } else { |
193 | | - show-result(result.first()) |
194 | | - } |
195 | | - } |
196 | | -} |
197 | | - |
198 | | -/// Show a single module/file |
199 | | -#let show-module(name, level: 3) = { |
200 | | - for item in docs.at(name) { |
201 | | - if item.at("signature", default: none) == none { |
202 | | - continue |
203 | | - } |
204 | | - |
205 | | - let function-name = item.signature.name |
206 | | - if function-name.starts-with("_") { |
207 | | - continue |
208 | | - } |
209 | | - |
210 | | - [#heading(function-name, level: level) #label(function-name)] |
211 | | - show-annotated-signature(item.signature, item.comment) |
212 | | - show-docstring(item.comment, level) |
213 | | - } |
214 | | -} |
215 | | - |
216 | | - |
| 79 | +// Outline |
217 | 80 | #columns(2, outline(depth: 4)) |
218 | 81 | #pagebreak() |
219 | 82 |
|
@@ -244,7 +107,10 @@ From this point on only the code inside the |
244 | 107 | canvas block will be shown in examples unless specified otherwise. |
245 | 108 |
|
246 | 109 | ```example |
| 110 | +// Draw a circle |
247 | 111 | circle((0, 0)) |
| 112 | +
|
| 113 | +// Draw a line |
248 | 114 | line((1,-1), (2,1)) |
249 | 115 | ``` |
250 | 116 |
|
@@ -297,13 +163,13 @@ stroke and fill styling unless said otherwise. |
297 | 163 | How to stroke the border or the path of the draw element. #link("https://typst.app/docs/reference/visualize/line/#parameters-stroke")[See Typst's line documentation for more details.] |
298 | 164 |
|
299 | 165 | = API |
| 166 | +#let heading-offset = 1 |
300 | 167 | #for item in modules { |
301 | | - let heading-offset = 1 |
302 | 168 | if type(item) == int { |
303 | 169 | heading-offset = 1 + item |
304 | 170 | } else if type(item) != str { |
305 | 171 | heading(item, offset: heading-offset) |
306 | 172 | } else { |
307 | | - show-module(item, level: 2 + heading-offset) |
| 173 | + show-module(docs, item, level: 2 + heading-offset) |
308 | 174 | } |
309 | 175 | } |
0 commit comments