2024-01-12 23:22:03 +01:00
|
|
|
{
|
|
|
|
pkgs,
|
|
|
|
lib,
|
2024-08-02 01:45:45 +01:00
|
|
|
evaledModules,
|
2024-02-03 19:04:09 +01:00
|
|
|
nixosOptionsDoc,
|
|
|
|
transformOptions,
|
2024-03-30 14:52:32 +01:00
|
|
|
hmOptions,
|
2024-07-19 20:14:20 +02:00
|
|
|
search,
|
2024-01-12 23:22:03 +01:00
|
|
|
}:
|
2024-05-05 19:39:35 +02:00
|
|
|
let
|
2024-08-02 01:45:45 +01:00
|
|
|
inherit (evaledModules.config.meta) nixvimInfo;
|
2024-02-20 21:20:59 +01:00
|
|
|
|
2024-05-05 19:39:35 +02:00
|
|
|
mkMDDoc =
|
|
|
|
options:
|
2024-02-03 19:04:09 +01:00
|
|
|
(nixosOptionsDoc {
|
|
|
|
inherit options transformOptions;
|
|
|
|
warningsAreErrors = false;
|
2024-05-05 19:39:35 +02:00
|
|
|
}).optionsCommonMark;
|
2024-01-12 23:22:03 +01:00
|
|
|
|
2024-05-05 19:39:35 +02:00
|
|
|
removeUnwanted =
|
|
|
|
attrs:
|
2024-01-12 23:22:03 +01:00
|
|
|
builtins.removeAttrs attrs [
|
|
|
|
"_module"
|
|
|
|
"_freeformOptions"
|
|
|
|
"warnings"
|
|
|
|
"assertions"
|
|
|
|
"content"
|
|
|
|
];
|
|
|
|
|
2024-05-05 19:39:35 +02:00
|
|
|
removeWhitespace = builtins.replaceStrings [ " " ] [ "" ];
|
2024-01-12 23:22:03 +01:00
|
|
|
|
2024-06-25 17:47:31 +01:00
|
|
|
getSubOptions =
|
2024-08-30 14:37:32 -05:00
|
|
|
opts: path: lib.optionalAttrs (isVisible opts) (removeUnwanted (opts.type.getSubOptions path));
|
2024-01-12 23:22:03 +01:00
|
|
|
|
2024-05-05 19:39:35 +02:00
|
|
|
isVisible =
|
|
|
|
opts:
|
2024-08-30 14:37:32 -05:00
|
|
|
if lib.isOption opts then
|
2024-06-25 17:41:42 +01:00
|
|
|
opts.visible or true
|
2024-05-05 19:39:35 +02:00
|
|
|
else if opts.isOption then
|
2024-06-25 17:41:42 +01:00
|
|
|
opts.index.options.visible or true
|
2024-05-05 19:39:35 +02:00
|
|
|
else
|
|
|
|
let
|
2024-08-30 14:37:32 -05:00
|
|
|
filterFunc = lib.filterAttrs (_: v: if lib.isAttrs v then isVisible v else true);
|
2024-01-12 23:22:03 +01:00
|
|
|
|
2024-05-05 19:39:35 +02:00
|
|
|
hasEmptyIndex = (filterFunc opts.index.options) == { };
|
|
|
|
hasEmptyComponents = (filterFunc opts.components) == { };
|
|
|
|
in
|
2024-01-12 23:22:03 +01:00
|
|
|
!hasEmptyIndex || !hasEmptyComponents;
|
|
|
|
|
|
|
|
wrapModule = path: opts: isOpt: rec {
|
|
|
|
index = {
|
|
|
|
options =
|
2024-05-05 19:39:35 +02:00
|
|
|
if isOpt then
|
|
|
|
opts
|
|
|
|
else
|
2024-08-30 14:37:32 -05:00
|
|
|
lib.filterAttrs (_: component: component.isOption && (isVisible component)) opts;
|
|
|
|
path = removeWhitespace (lib.concatStringsSep "/" path);
|
2024-02-20 21:20:59 +01:00
|
|
|
moduleDoc =
|
2024-06-07 21:30:00 +01:00
|
|
|
let
|
2024-08-30 14:37:32 -05:00
|
|
|
info = lib.attrByPath path { } nixvimInfo;
|
2024-08-02 01:45:45 +01:00
|
|
|
maintainers = lib.unique (evaledModules.config.meta.maintainers.${info.file} or [ ]);
|
2024-06-13 05:31:31 +01:00
|
|
|
maintainersNames = builtins.map maintToMD maintainers;
|
|
|
|
maintToMD = m: if m ? github then "[${m.name}](https://github.com/${m.github})" else m.name;
|
2024-06-07 21:30:00 +01:00
|
|
|
in
|
|
|
|
# Make sure this path has a valid info attrset
|
|
|
|
if info ? file && info ? description && info ? url then
|
2024-02-20 21:20:59 +01:00
|
|
|
"# ${lib.last path}\n\n"
|
2024-06-13 05:46:16 +01:00
|
|
|
+ (lib.optionalString (info.url != null) "**URL:** [${info.url}](${info.url})\n\n")
|
2024-05-05 19:39:35 +02:00
|
|
|
+ (lib.optionalString (
|
2024-06-07 21:30:00 +01:00
|
|
|
maintainers != [ ]
|
2024-05-05 19:39:35 +02:00
|
|
|
) "**Maintainers:** ${lib.concatStringsSep ", " maintainersNames}\n\n")
|
2024-08-22 13:47:57 -05:00
|
|
|
+ lib.optionalString (info.description != null) ''
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
${info.description}
|
|
|
|
|
|
|
|
''
|
2024-05-05 19:39:35 +02:00
|
|
|
else
|
|
|
|
null;
|
2024-01-12 23:22:03 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
components =
|
2024-05-05 19:39:35 +02:00
|
|
|
if isOpt then
|
|
|
|
{ }
|
|
|
|
else
|
2024-08-30 14:37:32 -05:00
|
|
|
lib.filterAttrs (_: component: !component.isOption && (isVisible component)) opts;
|
2024-01-12 23:22:03 +01:00
|
|
|
|
2024-05-05 19:39:35 +02:00
|
|
|
hasComponents = components != { };
|
2024-01-12 23:22:03 +01:00
|
|
|
|
|
|
|
isOption = isOpt;
|
|
|
|
};
|
|
|
|
|
2024-05-05 19:39:35 +02:00
|
|
|
processModulesRec =
|
|
|
|
modules:
|
|
|
|
let
|
|
|
|
recurse =
|
|
|
|
path: mods:
|
|
|
|
let
|
|
|
|
g =
|
|
|
|
name: opts:
|
2024-08-30 14:37:32 -05:00
|
|
|
if !lib.isOption opts then
|
2024-05-05 19:39:35 +02:00
|
|
|
wrapModule (path ++ [ name ]) (recurse (path ++ [ name ]) opts) false
|
|
|
|
else
|
|
|
|
let
|
|
|
|
subOpts = getSubOptions opts (path ++ [ name ]);
|
|
|
|
in
|
|
|
|
if subOpts != { } then
|
|
|
|
wrapModule (path ++ [ name ]) (
|
|
|
|
(recurse (path ++ [ name ]) subOpts)
|
|
|
|
// {
|
|
|
|
# This is necessary to include the submodule option's definition in the docs (description, type, etc.)
|
|
|
|
# For instance, this helps submodules like "autoCmd" to include their base definitions and examples in the docs
|
|
|
|
# Though there might be a better, less "hacky" solution than this.
|
2024-08-30 14:37:32 -05:00
|
|
|
${name} = lib.recursiveUpdate opts {
|
2024-05-05 19:39:35 +02:00
|
|
|
isOption = true;
|
|
|
|
type.getSubOptions = _: _: { }; # Used to exclude suboptions from the submodule definition itself
|
|
|
|
};
|
|
|
|
}
|
|
|
|
) false
|
|
|
|
else
|
|
|
|
wrapModule (path ++ [ name ]) opts true;
|
2024-01-12 23:22:03 +01:00
|
|
|
in
|
2024-08-30 14:37:32 -05:00
|
|
|
lib.mapAttrs g mods;
|
2024-01-12 23:22:03 +01:00
|
|
|
in
|
2024-08-30 14:37:32 -05:00
|
|
|
lib.foldlAttrs (
|
2024-05-05 19:39:35 +02:00
|
|
|
acc: name: opts:
|
|
|
|
let
|
|
|
|
group = if !opts.hasComponents then "Neovim Options" else "none";
|
2024-01-12 23:22:03 +01:00
|
|
|
|
|
|
|
last =
|
2024-05-05 19:39:35 +02:00
|
|
|
acc.${group} or {
|
2024-01-12 23:22:03 +01:00
|
|
|
index = {
|
2024-05-05 19:39:35 +02:00
|
|
|
options = { };
|
2024-01-12 23:22:03 +01:00
|
|
|
path = removeWhitespace "${group}";
|
2024-02-20 21:20:59 +01:00
|
|
|
moduleDoc = null;
|
2024-01-12 23:22:03 +01:00
|
|
|
};
|
2024-05-05 19:39:35 +02:00
|
|
|
components = { };
|
2024-01-12 23:22:03 +01:00
|
|
|
isGroup = true;
|
|
|
|
hasComponents = false;
|
|
|
|
};
|
|
|
|
|
2024-08-30 14:37:32 -05:00
|
|
|
isOpt = !opts.hasComponents && (lib.isOption opts.index.options);
|
2024-01-12 23:22:03 +01:00
|
|
|
in
|
2024-05-05 19:39:35 +02:00
|
|
|
acc
|
|
|
|
// {
|
2024-08-30 14:37:32 -05:00
|
|
|
${group} = lib.recursiveUpdate last {
|
|
|
|
index.options = lib.optionalAttrs isOpt { ${name} = opts.index.options; };
|
2024-01-12 23:22:03 +01:00
|
|
|
|
2024-08-30 14:37:32 -05:00
|
|
|
components = lib.optionalAttrs (!isOpt) {
|
|
|
|
${name} = lib.recursiveUpdate opts {
|
2024-05-05 19:39:35 +02:00
|
|
|
index.path = removeWhitespace (
|
2024-08-30 14:37:32 -05:00
|
|
|
lib.concatStringsSep "/" ((lib.optional (group != "none") group) ++ [ opts.index.path ])
|
2024-05-05 19:39:35 +02:00
|
|
|
);
|
|
|
|
hasComponents = true;
|
2024-01-12 23:22:03 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-05-05 19:39:35 +02:00
|
|
|
hasComponents = last.components != { };
|
|
|
|
};
|
|
|
|
}
|
|
|
|
) { } (recurse [ ] modules);
|
|
|
|
|
|
|
|
mapModulesToString =
|
|
|
|
f: modules:
|
|
|
|
let
|
|
|
|
recurse =
|
|
|
|
mods:
|
|
|
|
let
|
|
|
|
g =
|
|
|
|
name: opts:
|
|
|
|
if (opts ? "isGroup") then
|
|
|
|
if name != "none" then (f name opts) + ("\n" + recurse opts.components) else recurse opts.components
|
|
|
|
else if opts.hasComponents then
|
|
|
|
(f name opts) + ("\n" + recurse opts.components)
|
|
|
|
else
|
|
|
|
f name opts;
|
|
|
|
in
|
2024-08-30 14:37:32 -05:00
|
|
|
lib.concatStringsSep "\n" (lib.mapAttrsToList g mods);
|
2024-01-12 23:22:03 +01:00
|
|
|
in
|
|
|
|
recurse modules;
|
|
|
|
|
|
|
|
docs = rec {
|
2024-08-02 01:45:45 +01:00
|
|
|
modules = processModulesRec (removeUnwanted evaledModules.options);
|
2024-05-05 19:39:35 +02:00
|
|
|
commands = mapModulesToString (
|
|
|
|
name: opts:
|
|
|
|
let
|
2024-08-30 14:37:32 -05:00
|
|
|
isBranch = if (lib.hasSuffix "index" opts.index.path) then true else opts.hasComponents;
|
2024-05-05 19:39:35 +02:00
|
|
|
path = if isBranch then "${opts.index.path}/index.md" else "${opts.index.path}.md";
|
|
|
|
in
|
2024-08-30 14:37:32 -05:00
|
|
|
(lib.optionalString isBranch "mkdir -p ${opts.index.path}\n")
|
2024-05-05 19:39:35 +02:00
|
|
|
+ (
|
|
|
|
if opts.index.moduleDoc == null then
|
|
|
|
"cp ${mkMDDoc opts.index.options} ${path}"
|
|
|
|
else
|
|
|
|
# Including moduleDoc's text directly will result in bash interpreting special chars,
|
|
|
|
# write it to the nix store and `cat` the file instead.
|
|
|
|
''
|
|
|
|
{
|
|
|
|
cat ${pkgs.writeText "module-doc" opts.index.moduleDoc}
|
|
|
|
cat ${mkMDDoc opts.index.options}
|
|
|
|
} > ${path}
|
|
|
|
''
|
2024-01-12 23:22:03 +01:00
|
|
|
)
|
2024-05-05 19:39:35 +02:00
|
|
|
) modules;
|
2024-01-12 23:22:03 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
mdbook = {
|
2024-05-05 19:39:35 +02:00
|
|
|
nixvimOptions = mapModulesToString (
|
|
|
|
name: opts:
|
|
|
|
let
|
|
|
|
isBranch = if name == "index" then true else opts.hasComponents && opts.index.options != { };
|
2024-01-12 23:22:03 +01:00
|
|
|
|
2024-05-05 19:39:35 +02:00
|
|
|
path =
|
|
|
|
if isBranch then
|
|
|
|
"${opts.index.path}/index.md"
|
|
|
|
else if !opts.hasComponents then
|
|
|
|
"${opts.index.path}.md"
|
|
|
|
else
|
|
|
|
"";
|
2024-01-12 23:22:03 +01:00
|
|
|
|
2024-05-05 19:39:35 +02:00
|
|
|
indentLevel = with builtins; length (filter isString (split "/" opts.index.path)) - 1;
|
2024-01-12 23:22:03 +01:00
|
|
|
|
2024-08-30 14:37:32 -05:00
|
|
|
padding = lib.concatStrings (builtins.genList (_: "\t") indentLevel);
|
2024-05-05 19:39:35 +02:00
|
|
|
in
|
|
|
|
"${padding}- [${name}](${path})"
|
|
|
|
) docs.modules;
|
2024-01-12 23:22:03 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
prepareMD = ''
|
|
|
|
# Copy inputs into the build directory
|
|
|
|
cp -r --no-preserve=all $inputs/* ./
|
|
|
|
cp ${../../CONTRIBUTING.md} ./CONTRIBUTING.md
|
2024-02-18 15:56:18 +01:00
|
|
|
cp -r ${../user-guide} ./user-guide
|
2024-03-30 14:52:32 +01:00
|
|
|
cp -r ${../modules} ./modules
|
2024-01-12 23:22:03 +01:00
|
|
|
|
|
|
|
# Copy the generated MD docs into the build directory
|
|
|
|
# Using pkgs.writeShellScript helps to avoid the "bash: argument list too long" error
|
|
|
|
bash -e ${pkgs.writeShellScript "copy_docs" docs.commands}
|
|
|
|
|
|
|
|
# Prepare SUMMARY.md for mdBook
|
|
|
|
# Using pkgs.writeText helps to avoid the same error as above
|
|
|
|
substituteInPlace ./SUMMARY.md \
|
2024-03-30 14:52:32 +01:00
|
|
|
--replace-fail "@NIXVIM_OPTIONS@" "$(cat ${pkgs.writeText "nixvim-options-summary.md" mdbook.nixvimOptions})"
|
|
|
|
|
|
|
|
substituteInPlace ./modules/hm.md \
|
|
|
|
--replace-fail "@HM_OPTIONS@" "$(cat ${mkMDDoc hmOptions})"
|
2024-01-12 23:22:03 +01:00
|
|
|
'';
|
|
|
|
in
|
2024-05-05 19:39:35 +02:00
|
|
|
pkgs.stdenv.mkDerivation {
|
|
|
|
name = "nixvim-docs";
|
2024-01-12 23:22:03 +01:00
|
|
|
|
2024-05-05 19:39:35 +02:00
|
|
|
phases = [ "buildPhase" ];
|
2024-01-12 23:22:03 +01:00
|
|
|
|
2024-09-09 22:27:59 +02:00
|
|
|
buildInputs = [
|
|
|
|
pkgs.mdbook
|
|
|
|
pkgs.mdbook-alerts
|
|
|
|
];
|
2024-08-30 14:37:32 -05:00
|
|
|
inputs = lib.sourceFilesBySuffices ./. [
|
2024-05-05 19:39:35 +02:00
|
|
|
".md"
|
|
|
|
".toml"
|
|
|
|
".js"
|
|
|
|
];
|
2024-01-12 23:22:03 +01:00
|
|
|
|
2024-05-05 19:39:35 +02:00
|
|
|
buildPhase = ''
|
2024-07-25 19:21:46 +02:00
|
|
|
dest=$out/share/doc/nixvim
|
2024-05-05 19:39:35 +02:00
|
|
|
mkdir -p $dest
|
|
|
|
${prepareMD}
|
|
|
|
mdbook build
|
|
|
|
cp -r ./book/* $dest
|
2024-07-19 20:14:20 +02:00
|
|
|
mkdir -p $dest/search
|
|
|
|
cp -r ${search}/* $dest/search
|
2024-05-05 19:39:35 +02:00
|
|
|
'';
|
|
|
|
}
|