feat(helpers/text): add headings and descriptions

This commit is contained in:
Mohammad Rafiq 2025-07-05 04:47:03 +08:00
parent dd0ff1e6ac
commit 9b54a02eaa
No known key found for this signature in database
3 changed files with 41 additions and 10 deletions

View file

@ -1,5 +1,9 @@
# readme
## generated-files
This flake uses the [files flake-parts module](https://flake.parts/options/files.html) to generate documentation. This flake uses the [files flake-parts module](https://flake.parts/options/files.html) to generate documentation.
The list of generated files are: The list of generated files are:
- [docs/cheatsheet.md](docs/cheatsheet.md) - [docs/cheatsheet.md](docs/cheatsheet.md)
- [README.md](README.md) - [README.md](README.md)
## helpers
### text-helper
The option `text.<name> supports either a string or a submodule with attributes order and parts. The parts attribute can either be a string, which will get concatenated in the order laid out in `text.<name>.order`, or can itself have the attributes order and parts, in which case it will be evaluated recursively. The option `text.<name> supports either a string or a submodule with attributes order and parts. The parts attribute can either be a string, which will get concatenated in the order laid out in `text.<name>.order`, or can itself have the attributes order and parts, in which case it will be evaluated recursively.

View file

@ -1 +1,2 @@
# cheatsheet
`__curPos.file` will give the full evaluated path of the nix file it is called in. See [this issue](https://github.com/NixOS/nix/issues/5897#issuecomment-1012165198) for more information. `__curPos.file` will give the full evaluated path of the nix file it is called in. See [this issue](https://github.com/NixOS/nix/issues/5897#issuecomment-1012165198) for more information.

View file

@ -1,10 +1,13 @@
{ lib, ... }: { lib, ... }:
let let
inherit (lib) inherit (lib)
optional
concatStrings
flatten
mkOption mkOption
mapAttrs mapAttrs
isString isString
pipe replicate
flip flip
getAttr getAttr
concatStringsSep concatStringsSep
@ -20,29 +23,52 @@ let
str str
(submodule { (submodule {
options = { options = {
heading = mkOption {
type = str;
default = "";
};
description = mkOption {
type = str;
default = "";
};
order = mkOption { type = listOf str; }; order = mkOption { type = listOf str; };
parts = mkOption { type = lazyAttrsOf textType; }; parts = mkOption { type = lazyAttrsOf textType; };
}; };
}) })
]; ];
recurseAttrs = mkListFromAttrs =
value: prefix:
{ name, value }:
let
sectionHeading = result: "${concatStrings (replicate prefix "#")} ${result}";
in
if isString value then if isString value then
value [
(sectionHeading name)
value
]
else else
# TODO: handle order being empty # TODO: handle order being empty
# TODO: add headings for each part with possible option to disable flatten [
pipe value.order [ [
(map (flip getAttr value.parts)) (sectionHeading (if value.heading == "" then name else value.heading))
(map recurseAttrs) ]
(concatStringsSep "\n") (optional (value.description != "") value.description)
(map (mkListFromAttrs (prefix + 1)) (
map (x: {
name = x;
value = flip getAttr value.parts x;
}) value.order
))
]; ];
in in
{ {
options.text = mkOption { options.text = mkOption {
default = { }; default = { };
type = lazyAttrsOf textType; type = lazyAttrsOf textType;
apply = mapAttrs (_: recurseAttrs); apply = mapAttrs (
name: value: concatStringsSep "\n" (flatten (mkListFromAttrs 1 { inherit name value; }))
);
}; };
config.text.readme.parts.helpers.parts.text-helper = config.text.readme.parts.helpers.parts.text-helper =
"The option `text.<name> supports either a string or a submodule with attributes order and parts. The parts attribute can either be a string, which will get concatenated in the order laid out in `text.<name>.order`, or can itself have the attributes order and parts, in which case it will be evaluated recursively."; "The option `text.<name> supports either a string or a submodule with attributes order and parts. The parts attribute can either be a string, which will get concatenated in the order laid out in `text.<name>.order`, or can itself have the attributes order and parts, in which case it will be evaluated recursively.";