docs/packages/nixpkgs-manual/default.nix
Pyrox 08305f7d57
Some checks failed
/ build (push) Failing after 5m45s
fix admonitions
2024-07-20 21:58:12 -04:00

142 lines
4 KiB
Nix

{
lib,
pkgs,
nixos-render-docs,
python3,
perl,
stdenvNoCC,
inputs,
...
}:
let
optionsDoc = pkgs.nixosOptionsDoc {
inherit
(pkgs.lib.evalModules {
modules = [ "${inputs.nixpkgs.outPath}/pkgs/top-level/config.nix" ];
class = "nixpkgsConfig";
})
options
;
documentType = "none";
transformOptions =
opt:
opt
// {
declarations = map (
decl:
if lib.hasPrefix (toString ../..) (toString decl) then
let
subpath = lib.removePrefix "/" (lib.removePrefix (toString ../.) (toString decl));
in
{
url = "https://github.com/NixOS/nixpkgs/blob/master/${subpath}";
name = subpath;
}
else
decl
) opt.declarations;
};
};
optionsMD = optionsDoc.optionsCommonMark;
p = "find . -type f -exec perl -pi -e";
s = "find . -type f -exec sed -i";
in
stdenvNoCC.mkDerivation {
name = "nixpkgs-manual-md";
nativeBuildInputs = [
nixos-render-docs
python3
perl
];
src = inputs.nixpkgs;
postPatch = ''
cd doc
'';
pythonInterpreterTable = pkgs.callPackage ./python-interp-table.nix { };
passAsFile = [ "pythonInterpreterTable" ];
buildPhase = ''
substituteInPlace ./languages-frameworks/python.section.md --subst-var-by python-interpreter-table "$(<"$pythonInterpreterTablePath")"
mv ./functions/library.md.in ./functions/library.md
substitute ./manual.md.in ./manual.md \
--replace-fail '@MANUAL_VERSION@' '${lib.version}'
mkdir out
cp ${optionsMD} out/options.md
mv preface.chapter.md out/index.md
mv build-helpers out/Build-Helpers
mv build-helpers.md out/Build-Helpers/index.md
mv development out/Development
mv development.md out/Development/index.md
mv functions out/Functions
mv functions.md out/Functions/index.md
mv hooks out/Hooks
mv languages-frameworks out/Languages-And-Frameworks
mkdir out/Library-Reference
cp ${pkgs.amg.nixpkgs-lib-docs}/*.md out/Library-Reference/
mv module-system out/Module-System
mv packages out/Packages
mv stdenv out/Standard-Environment
mv stdenv.md out/Standard-Environment/index.md
mv using out/Using-Nixpkgs
mv using-nixpkgs.md out/Using-Nixpkgs/index.md
pushd out
# Fix-up options page
sed -i 's!${inputs.nixpkgs.outPath}!https://github.com/NixOS/nixpkgs/blob/master!g' options.md
sed -i 's!file://!!g' options.md
sed -i 's/\\//g' options.md
# Clean up all index pages
${s} '/```{=include=}/,$d' {} \;
# Remove all ::: sequences
${s} 's/\s*::://g' {} \;
# Indent all nix codeblocks
# ${p} 's/\n```nix/\ \ \ \ ```nix/g' -0 {} \;
# Replace nix-style note admonitions
${p} 's/\s*(\w?){.note}/!!! note/' {} \;
${s} '/^\!\!\![[:space:]]note/,/^$/s/^/\ \ \ \ /' {} \;
${s} 's/^\ \ \ \ !!! note/!!! note/' {} \;
# Replace nix-style warning admonitions
${p} 's/\s*(\w?){.warning}/!!! warning/' {} \;
${s} '/^\!\!\![[:space:]]warning/,/^$/s/^/\ \ \ \ /' {} \;
${s} 's/^\ \ \ \ !!! warning/!!! warning/' {} \;
# Replace nix-style tip admonitions
${p} 's/\s*(\w?){.tip}/!!! tip/' {} \;
${s} '/^\!\!\![[:space:]]tip/,/^$/s/^/\ \ \ \ /' {} \;
${s} 's/^\ \ \ \ !!! tip/!!! tip/' {} \;
# Replace nix-style example admonitions
${p} 's/\s*(\w?){.example.*}/!!! example/' {} \;
${p} 's/\n\n# /\ \"/g' -0 {} \;
${s} '/^!!! example/,/^$/s/^/\ \ \ \ /' {} \;
${s} 's/^\ \ \ \ !!! example/!!! example/' {} \;
# Have to special-case some example admonitions
${p} 's/\n\n\n/placeholderstring123/g' -0 {} \;
${s} '/^!!! example/,/placeholderstring123/s/^/\ \ \ \ /' {} \;
${p} 's/placeholderstring123/\n\n\n/g' -0 {} \;
${s} 's/^\ \ \ \ !!! example/!!! example/' {} \;
${p} 's/example\n[ ]{8}#### /example "/g' -0 {} \;
${s} '/^!!! example \"/s/$/\"/' {} \;
popd
'';
installPhase = ''
mkdir $out
mv out/* $out
'';
}