Files
proxy-suite-flake/nix/options-doc/render-options-markdown.nix
T
2026-09-06 09:57:16 +05:00

197 lines
4.5 KiB
Nix

{
pkgs,
optionDocs,
defaultConfigText,
exampleConfigText,
}:
pkgs.runCommand "proxy-suite-options.md" { } ''
preamble="$TMPDIR/preamble.md"
sections="$TMPDIR/sections.md"
sections_with_anchors="$TMPDIR/sections-with-anchors.md"
section_index="$TMPDIR/sections-index.tsv"
toc="$TMPDIR/toc.md"
cat >"$preamble" <<'EOF'
# proxy-suite options
This file is generated from the `services.proxy-suite` option descriptions in [`modules/proxy-suite/options/default.nix`](/modules/proxy-suite/options/default.nix).
Update module option docs there instead of editing this file by hand.
## Table of contents
EOF
cat >"$sections" <<'EOF'
## Complete default config
```nix
services.proxy-suite = ${defaultConfigText};
```
## Complete example config
This is generated by filling in all the options with example values (or defaults if no example is provided). This is not meant to be a recommended config, just a comprehensive example of how to set all the options.
```nix
services.proxy-suite = ${exampleConfigText};
```
EOF
cat ${optionDocs.optionsCommonMark} >>"$sections"
: >"$section_index"
: >"$sections_with_anchors"
awk -v section_index="$section_index" -v out="$sections_with_anchors" '
function slugify(text, slug) {
slug = text
gsub(/\\/, "", slug)
slug = tolower(slug)
gsub(/[^a-z0-9]+/, "-", slug)
sub(/^-+/, "", slug)
sub(/-+$/, "", slug)
return slug
}
/^## / {
title = substr($0, 4)
base = slugify(title)
count = seen[base]
seen[base]++
slug = count == 0 ? base : base "-" count
print title "\t" slug >> section_index
print "<a id=\"" slug "\"></a>" >> out
}
{
print >> out
}
' "$sections"
: >"$toc"
awk -F '\t' -v toc="$toc" '
function addChild(parent, child, key) {
key = parent SUBSEP child
if (!(key in seenChild)) {
seenChild[key] = 1
childCount[parent]++
childOrder[parent, childCount[parent]] = child
}
}
function indent(depth, i, prefix) {
prefix = ""
for (i = 0; i < depth; i++) {
prefix = prefix " "
}
return prefix
}
function displayLabel(label) {
if (label == "*") {
return "item"
}
if (label ~ /^<.*>$/) {
return "`" label "`"
}
return label
}
function printNode(path, label, depth, i, child, childPath, text) {
text = displayLabel(label)
if (path in slugByPath) {
text = "[" text "](#" slugByPath[path] ")"
}
print indent(depth) "- " text >> toc
for (i = 1; i <= childCount[path]; i++) {
child = childOrder[path, i]
childPath = path "." child
printNode(childPath, child, depth + 1)
}
}
{
title = $1
slug = $2
normalized = title
gsub(/\\/, "", normalized)
if (normalized ~ /^Complete /) {
sampleCount++
sampleTitle[sampleCount] = title
sampleSlug[sampleCount] = slug
next
}
if (index(normalized, "services.proxy-suite") == 1) {
optionRootSeen = 1
slugByPath[normalized] = slug
count = split(normalized, parts, /\./)
if (count < 3) {
next
}
parent = "services.proxy-suite"
for (i = 3; i <= count; i++) {
child = parts[i]
addChild(parent, child)
parent = parent "." child
}
next
}
otherCount++
otherTitle[otherCount] = title
otherSlug[otherCount] = slug
}
END {
if (sampleCount > 0) {
print "- Config samples" >> toc
for (i = 1; i <= sampleCount; i++) {
print " - [" sampleTitle[i] "](#" sampleSlug[i] ")" >> toc
}
}
if (optionRootSeen) {
print "- services.proxy-suite" >> toc
for (i = 1; i <= childCount["services.proxy-suite"]; i++) {
child = childOrder["services.proxy-suite", i]
printNode("services.proxy-suite." child, child, 1)
}
}
for (i = 1; i <= otherCount; i++) {
print "- [" otherTitle[i] "](#" otherSlug[i] ")" >> toc
}
}
' "$section_index"
raw="$TMPDIR/options.md.raw"
cat "$preamble" >"$raw"
cat "$toc" >>"$raw"
printf '\n' >>"$raw"
cat "$sections_with_anchors" >>"$raw"
awk '
NF {
for (i = 0; i < blanks; i++) {
print ""
}
blanks = 0
print
next
}
{
blanks++
}
' "$raw" >"$out"
''