Skip to content

Commit

Permalink
generate-manpage.nix: Make more readable
Browse files Browse the repository at this point in the history
  • Loading branch information
edolstra committed Mar 21, 2023
1 parent 3ac3e31 commit 6f62cb3
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion doc/manual/generate-manpage.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ let

showCommand = { command, details, filename, toplevel }:
let

result = ''
> **Warning** \
> This program is **experimental** and its interface is subject to change.
Expand All @@ -25,40 +26,51 @@ let
${maybeOptions}
'';

showSynopsis = command: args:
let
showArgument = arg: "*${arg.label}*" + (if arg ? arity then "" else "...");
arguments = concatStringsSep " " (map showArgument args);
in ''
`${command}` [*option*...] ${arguments}
'';

maybeSubcommands = if details ? commands && details.commands != {}
then ''
where *subcommand* is one of the following:
${subcommands}
''
else "";

subcommands = if length categories > 1
then listCategories
else listSubcommands details.commands;

categories = sort (x: y: x.id < y.id) (unique (map (cmd: cmd.category) (attrValues details.commands)));

listCategories = concatStrings (map showCategory categories);

showCategory = cat: ''
**${toString cat.description}:**
${listSubcommands (filterAttrs (n: v: v.category == cat) details.commands)}
'';

listSubcommands = cmds: concatStrings (attrValues (mapAttrs showSubcommand cmds));

showSubcommand = name: subcmd: ''
* [`${command} ${name}`](./${appendName filename name}.md) - ${subcmd.description}
'';

maybeDocumentation = if details ? doc then details.doc else "";

maybeOptions = if details.flags == {} then "" else ''
# Options
${showOptions details.flags toplevel.flags}
'';

showOptions = options: commonOptions:
let
allOptions = options // commonOptions;
Expand Down Expand Up @@ -99,7 +111,7 @@ let
in [ cmd ] ++ concatMap subcommand (attrNames details.commands or {});

parsedToplevel = builtins.fromJSON toplevel;

manpages = processCommand {
command = "nix";
details = parsedToplevel;
Expand Down

0 comments on commit 6f62cb3

Please sign in to comment.