-
Notifications
You must be signed in to change notification settings - Fork 14
/
generate-docs.nix
235 lines (210 loc) · 6.79 KB
/
generate-docs.nix
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
{
inputs,
lib,
...
}:
with lib; let
showOption = concatStringsSep ".";
match = name: cases: cases.${name} or cases._;
indent = entries: "${pipe entries [
toList
(concatStringsSep "\n")
(splitString "\n")
(map (s: " ${s}"))
(concatStringsSep "\n")
]}";
delimit-pretty = start: content: end: concatStringsSep "\n" [start content end];
delimit-min = start: content: end: 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 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' (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)}>";
};
describe = path: opt: {
${showOption path} =
opt
// {
defaultText =
opt.defaultText
or (
if opt ? default
then display-value {omit-empty-composites = true;} opt.default
else null
);
};
};
traverse = path: v: (
if (v ? _type && v._type == "option")
then let v' = v // {loc = v.override-loc or id v.loc;}; in (optionalAttrs (v.visible or true != false) (describe path v')) // (optionalAttrs (v.visible or true == true) (traverse path (v.type.getSubOptions v'.loc)))
else concatMapAttrs (name: traverse (path ++ [name])) (filterAttrs (name: const (name != "_module")) 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:
>
${pipe values [
(map (display-value {pretty = false;}))
(map (s: "> - `${s}`"))
(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:
mkOption {
type = 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: "[`v${version}`](https://github.com/YaLTeR/niri/releases/tag/v${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: strings.match pat str != null;
anchor = flip pipe [
(replaceStrings (upperChars ++ [" "]) (lowerChars ++ ["-"]))
(splitString "")
(filter (test "[a-z0-9-]"))
concatStrings
];
anchor' = loc: anchor "`${loc}`";
link = title: "[${title}](#${anchor title})";
link' = loc: "[`${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 length (splitString "\n" text) == 1
then "- default: `${text}`"
else ''
- default:
${indent (delimit-pretty "```nix" text "```")}
'';
nested-newtype = type:
if type == null
then null
else if type.name == "newtype"
then type
else
nested-newtype (
type.nestedTypes.elemType or null
);
describe-type = type:
match type.name {
newtype = let
display' = describe-type type.nestedTypes.display;
inner' = describe-type type.nestedTypes.inner;
in
display' + optionalString (inner' != null) ", which is a ${inner'}";
shorthand = link' "<${type.description}>";
_ = match type.description {
submodule = null;
_ = let
type' = nested-newtype type;
desc = "`${type.description}`";
in
if type' != null && type'.nestedTypes.display.name == "shorthand"
then replaceStrings ["``"] [""] (replaceStrings [type'.nestedTypes.display.description] ["`${describe-type type'.nestedTypes.display}`"] desc)
else desc;
};
};
make-docs = flip pipe [
types.submodule
(m: m.getSubOptions [])
(traverse [])
(mapAttrsToList (
path: opt:
"<!-- sorting key: ${path} -->\n"
+ (
if opt.type.name == "docs-override"
then "${opt.description}"
else if elem opt.type.name ["record" "submodule"] && opt.description or null == null
then "<!-- ${showOption opt.loc} -->"
else
(concatStringsSep "\n" (
remove null [
"## ${opt.override-header or "`${showOption opt.loc}`"}"
(optionalString (opt.type.description != "submodule")
"- type: ${describe-type opt.type}")
(maybe make-default opt.defaultText)
""
(maybe id opt.description or null)
]
))
)
))
(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;
};
}