forked from sodiboo/niri-flake
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-docs.nix
More file actions
397 lines (355 loc) · 10.3 KB
/
Copy pathgenerate-docs.nix
File metadata and controls
397 lines (355 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
{
inputs,
lib,
...
}:
let
showOption = lib.concatStringsSep ".";
match = name: cases: cases.${name} or cases._;
indent =
entries:
"${lib.pipe entries [
lib.toList
(lib.concatStringsSep "\n")
(lib.splitString "\n")
(map (s: " ${s}"))
(lib.concatStringsSep "\n")
]}";
delimit-pretty =
start: content: end:
lib.concatStringsSep "\n" [
start
content
end
];
delimit-min =
start: content: end:
lib.concatStrings [
start
content
end
];
display-value =
{
pretty ? true,
omit-empty-composites ? false,
}:
let
display-value' = display-value { inherit pretty; };
indent' = if pretty then indent else lib.id;
delimit' = if pretty then delimit-pretty else delimit-min;
in
v:
match (builtins.typeOf v) {
string = lib.strings.escapeNixString v;
int = toString v;
float = toString v;
bool = if v then "true" else "false";
set =
if v == { } then
if omit-empty-composites then null else "{}"
else
delimit' "{" (indent' (lib.mapAttrsToList (name: val: "${name} = ${display-value' val};") v)) "}";
null = "null";
list =
if v == [ ] then
if omit-empty-composites then null else "[]"
else
delimit' "[" (indent' (map display-value' v)) "]";
_ = "<${(builtins.typeOf v)}>";
};
maybe = f: v: if v != null then f v else null;
unstable-note = ''
> [!important]
> This option is not yet available in stable niri.
>
> If you wish to modify this option, you should make sure ${link' "programs.niri.package"} is set to ${pkg-link "niri-unstable"}.
>
> Otherwise, your system might fail to build.
'';
unstable-enum = values: ''
> [!important]
> The following values for this option are not yet available in stable niri:
>
${lib.pipe values [
(map (display-value {
pretty = false;
}))
(map (s: "> - `${s}`"))
(lib.concatStringsSep "\n")
]}
>
> If you wish to use one of the mentioned values, you should make sure ${link' "programs.niri.package"} is set to ${pkg-link "niri-unstable"}.
>
> Otherwise, your system might fail to build.
'';
section =
contents:
lib.mkOption {
type = lib.mkOptionType { name = "docs-override"; };
description = contents;
};
header = title: section "# ${title}";
fake-option =
loc: contents:
section ''
## `${loc}`
${contents}
'';
link-niri-commit =
{
rev,
shortRev,
}:
"[`${shortRev}`](https://github.com/YaLTeR/niri/tree/${rev})";
link-niri-release =
version: "[`${version}`](https://github.com/YaLTeR/niri/releases/tag/${version})";
link-stylix-opt = opt: "[`${opt}`](https://danth.github.io/stylix/options/hm.html#${anchor opt})";
link-this-github =
path: "https://github.com/sodiboo/niri-flake/blob/${inputs.self.rev or "main"}/${path}";
test = pat: str: lib.strings.match pat str != null;
anchor = lib.flip lib.pipe [
(lib.replaceStrings (lib.upperChars ++ [ " " ]) (lib.lowerChars ++ [ "-" ]))
(lib.splitString "")
(lib.filter (test "[a-z0-9-]"))
lib.concatStrings
];
anchor' = loc: anchor "`${loc}`";
link = title: "[${title}](#${anchor title})";
link' = loc: "[`${lib.removePrefix "programs.niri.settings." loc}`](#${anchor "`${loc}`"})";
module-doc =
name: desc: opts:
{
_ = section ''
# `${name}`
${desc}
'';
}
// opts;
pkg-header = name: "packages.<system>.${name}";
pkg-link = name: "[`pkgs.${name}`](#${anchor' (pkg-header name)})";
nixpkgs-link =
name: "[`pkgs.${name}`](https://search.nixos.org/packages?channel=unstable&show=${name})";
libinput-link =
page: header: "https://wayland.freedesktop.org/libinput/doc/latest/${page}.html#${anchor header}";
libinput-doc = page: header: "[${header}](${libinput-link page header})";
make-default =
text:
if lib.length (lib.splitString "\n" text) == 1 then
"- default: `${text}`"
else
''
- default:
${indent (delimit-pretty "```nix" text "```")}
'';
describe-type =
type:
let
span = content: "`${content}`";
in
if type.name == "rename" then
(span type.description) + ", which is a ${describe-type type.nestedTypes.real}"
else if type.name == "shorthand" then
link' "${type.description}"
else if type.name == "nullOr" && type.nestedTypes.elemType.name == "rename" then
span type.description
+ " (where ${span type.nestedTypes.elemType.description} is a ${describe-type type.nestedTypes.elemType.nestedTypes.real})"
else if type.name == "nullOr" && type.nestedTypes.elemType.name == "shorthand" then
span "null or" + link' "${type.nestedTypes.elemType.description}"
else
span type.description;
describe-type' = type: lib.replaceStrings [ "``" ] [ "" ] (describe-type type);
render-option =
opt:
assert opt._type or null == "option";
lib.optional (opt.visible != false) (
if opt.type.name == "docs-override" then
"${opt.description}"
else if opt.type.name == "submodule" && opt.description or null == null then
"<!-- ${showOption opt.loc} -->"
else
lib.concatStringsSep "\n" (
lib.remove null [
"## ${opt.override-header or "`${showOption opt.loc}`"}"
(
let
described = describe-type' opt.type;
in
lib.optionalString (described != "`submodule`") "- type: ${described}"
)
(maybe make-default opt.defaultText)
""
(maybe lib.id opt.description or null)
]
)
)
++ lib.optionals (opt.visible == true) (render-suboptions opt.loc (opt.type.getSubOptions opt.loc));
render-suboptions =
loc: options:
assert !(options ? _type);
let
to-list = lib.mapAttrsToList (name: opt: { inherit name opt; });
options' =
if options ? _module.niri-flake-ordered-record then
let
ord-record = options._module.niri-flake-ordered-record;
ordering = ord-record.ordering.value;
extra-docs-options = ord-record.extra-docs-options;
ordering' = builtins.listToAttrs (
lib.imap0 (i: v: {
name = v;
value = i;
}) ordering
);
max-ordering = builtins.length ordering;
in
builtins.sort (
a: b: (ordering'.${a.name} or max-ordering) < (ordering'.${b.name} or max-ordering)
) (to-list (builtins.removeAttrs (options // extra-docs-options) [ "_module" ]))
else
to-list (builtins.removeAttrs options [ "_module" ]);
in
builtins.concatMap (
{ name, opt }:
if opt ? _type then
render-option (
opt
// {
defaultText =
opt.defaultText
or (if opt ? default then display-value { omit-empty-composites = true; } opt.default else null);
visible = if opt.niri-flake-document-internal or false then true else opt.visible or true;
loc = opt.override-loc or lib.id opt.loc;
}
)
else
render-suboptions (loc ++ [ name ]) opt
) options';
make-docs = lib.flip lib.pipe [
lib.types.submodule
(m: m.getSubOptions [ ])
(render-suboptions [ ])
(lib.concatStringsSep "\n\n")
];
in
{
inherit make-docs;
lib = {
inherit
unstable-note
unstable-enum
section
header
fake-option
test
anchor
anchor'
link
link'
module-doc
pkg-header
pkg-link
nixpkgs-link
libinput-link
libinput-doc
link-niri-commit
link-niri-release
link-stylix-opt
link-this-github
display-value
;
};
settings-fmt =
let
body = lib.strings.trimWith { end = true; };
indent-except-first-line =
text:
let
lines = lib.splitString "\n" text;
lines' = [ (builtins.head lines) ] ++ (map (line: " ${line}") (builtins.tail lines));
in
if lines == [ ] then "" else builtins.concatStringsSep "\n" lines';
in
rec {
link-to-setting = loc: "#${anchor' "`${showOption loc}`"}";
bare-link = url: url;
masked-link =
{
href,
content,
}:
"[${content}](${href})";
block-quote =
content:
lib.pipe content [
body
(lib.splitString "\n")
(map (s: "> ${s}\n"))
lib.concatStrings
];
code = code: "`${code}`";
admonition = lib.genAttrs [ "note" "tip" "important" "warning" "caution" ] (
kind: content:
block-quote ''
[!${kind}]
${content}
''
);
list = items: ''
${lib.concatStringsSep "\n" (map (item: "- ${indent-except-first-line (body item)}") items)}
'';
ordered-list = items: ''
${lib.concatStringsSep "\n" (map (item: "1. ${indent-except-first-line (body item)}") items)}
'';
nix-code-block = code: ''
```nix
${body code}
```
'';
em = text: "*${text}*";
strong = text: "**${text}**";
table =
{
headers,
align,
rows,
}:
assert (builtins.length headers == builtins.length align);
''
| ${builtins.concatStringsSep " | " headers} |
| ${
builtins.concatStringsSep " | " (
map (
align:
if align == null then
"---"
else
{
left = ":---";
center = ":---:";
right = "---:";
}
.${align}
) align
)
} |
${lib.concatStringsSep "\n" (
map (
row:
assert builtins.length headers == builtins.length row;
"| ${builtins.concatStringsSep " | " row} |"
) rows
)}
'';
kbd = code;
img =
{
src,
title,
alt,
}:
''

'';
};
}