This repository has been archived by the owner on Aug 13, 2022. It is now read-only.
forked from styx-static/styx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibrary-doc.nix
140 lines (106 loc) · 3.6 KB
/
library-doc.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
{ pkgs, lib}: with lib;
let
namespaces = mapAttrs (namespace: _:
let functions = filter (x: x != {}) (mapAttrsToList (name: fn:
optionalAttrs (isDocFunction fn)
({ fullname = "lib.${namespace}.${name}"; inherit namespace name; } // fn)
) lib."${namespace}");
in {
inherit functions;
documentation = optionalString (lib."${namespace}" ? _documentation) (lib."${namespace}"._documentation true);
}) {
conf = {};
data = {};
generation = {};
pages = {};
proplist = {};
template = {};
themes = {};
utils = {};
};
mkFunctionDoc = function:
''
:sectnums!:
[[${function.fullname}]]
=== ${function.name}
${optionalString (function ? description) "==== Description\n\n${function.description}\n"}
${optionalString (function ? arguments) (mkFunctionArgs function.arguments)}
${optionalString (function ? return) "==== Return\n\n${function.return}\n"}
${optionalString (function ? examples) "==== Example\n\n${concatStringsSep "\n---\n" (map mkFunctionExample function.examples)}\n"}
${optionalString (function ? notes) "[NOTE]\n====\n${function.notes}\n====\n"}
:sectnums:
'';
mkFunctionArgs = args:
let
args' = if isAttrs args
then mapAttrsToList (name: data: data // { inherit name; }) args
else args;
type = if isAttrs args
then " (Attribute Set)"
else " (Standard)";
isMultiline = arg:
let arg' = if is "literalExpression" arg
then arg.text
else arg;
in if isString arg'
then (match "^.*(\n).*$" arg') != null
else false;
in "==== Arguments${type}\n\n" + concatStringsSep "\n" (map (arg:
let
description = optionalString (arg ? description) arg.description;
type = optionalString (arg ? type) "Type: `${arg.type}`. ";
default = optionalString (arg ? default)
(if isMultiline arg.default
then "Optional, defaults to:\n\n[source, nix]\n----\n${prettyNix arg.default}\n----\n"
else "Optional, defaults to `${prettyNix arg.default}`.");
example = optionalString (arg ? example)
(if isMultiline arg.example
then "Example:\n\n[source, nix]\n----\n${prettyNix arg.example}\n----\n"
else "Example: `${prettyNix arg.example}`.");
in "\n===== ${arg.name}\n\n" + (concatStringsSep "\n\n" (filter (x: x != "") [ description type default example ]))
) args') + "\n";
mkFunctionExample = example:
optionalString (example ? literalCode)
''
[source, nix]
.Code
----
${example.literalCode}
----
${optionalString (example ? code) ''
[source, nix]
.Result
----
${prettyNix (example.displayCode example.code)}
----
''
}
'';
content = concatStringsSep "\n" (mapAttrsToList (namespace: data: ''
== ${namespace}
${data.documentation}
---
${concatStringsSep "\n\n---\n\n" (map mkFunctionDoc data.functions)}
---
'') namespaces);
doc = pkgs.writeText "lib.adoc" ''
////
File automatically generated, do not edit
////
${content}
'';
in pkgs.stdenv.mkDerivation {
name = "styx-docs";
preferLocalBuild = true;
allowSubstitutes = false;
unpackPhase = ":";
buildInputs = [ pkgs.asciidoctor ];
buildPhase = ''
mkdir build
cp ${doc} build/library-generated.adoc
'';
installPhase = ''
mkdir $out
cp build/* $out
'';
}