X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;ds=sidebyside;f=src%2FPVE%2FPodParser.pm;h=4f2868a0f6f3e427cae85bf0a93696298deb4c4b;hb=457c3fcb1e30d9e465cbaff9ea857d52a4a95e3c;hp=3cb3c56f0e6dfff9fc295469e929612238375c8c;hpb=990b6a1a5bdb97a1c2203375aae8111f2c993168;p=pve-common.git diff --git a/src/PVE/PodParser.pm b/src/PVE/PodParser.pm index 3cb3c56..4f2868a 100644 --- a/src/PVE/PodParser.pm +++ b/src/PVE/PodParser.pm @@ -44,11 +44,68 @@ sub command { # helpers used to generate our manual pages +sub generate_typetext { + my ($schema) = @_; + my $typetext = ''; + my (@optional, @required); + foreach my $key (sort keys %$schema) { + my $entry = $schema->{$key}; + next if $entry->{alias}; + next if !$entry->{format_description} && + !$entry->{typetext} && + !$entry->{enum} && + $entry->{type} ne 'boolean'; + if ($schema->{$key}->{optional}) { + push @optional, $key; + } else { + push @required, $key; + } + } + my ($pre, $post) = ('', ''); + my $add = sub { + my ($key) = @_; + $typetext .= $pre; + my $entry = $schema->{$key}; + if (my $alias = $entry->{alias}) { + $key = $alias; + $entry = $schema->{$key}; + } + if (!defined($entry->{typetext})) { + $typetext .= $entry->{default_key} ? "[$key=]" : "$key="; + } + if (my $desc = $entry->{format_description}) { + $typetext .= "<$desc>"; + } elsif (my $text = $entry->{typetext}) { + $typetext .= $text; + } elsif (my $enum = $entry->{enum}) { + $typetext .= '<' . join('|', @$enum) . '>'; + } elsif ($entry->{type} eq 'boolean') { + $typetext .= '<1|0>'; + } else { + die "internal error: neither format_description nor typetext found"; + } + $typetext .= $post; + }; + foreach my $key (@required) { + &$add($key); + $pre = ', '; + } + $pre = $pre ? ' [,' : '['; + $post = ']'; + foreach my $key (@optional) { + &$add($key); + $pre = ' [,'; + } + return $typetext; +} + sub schema_get_type_text { my ($phash) = @_; if ($phash->{typetext}) { return $phash->{typetext}; + } elsif ($phash->{format_description}) { + return "<$phash->{format_description}>"; } elsif ($phash->{enum}) { return "(" . join(' | ', sort @{$phash->{enum}}) . ")"; } elsif ($phash->{pattern}) { @@ -62,9 +119,11 @@ sub schema_get_type_text { return "$phash->{type} (-N - $phash->{maximum})"; } } elsif ($phash->{type} eq 'string') { - my $format = $phash->{format}; - if ($format && ref($format) eq 'HASH') { - return PVE::JSONSchema::generate_typetext($format); + if (my $format = $phash->{format}) { + $format = PVE::JSONSchema::get_format($format) if ref($format) ne 'HASH'; + if (ref($format) eq 'HASH') { + return generate_typetext($format); + } } } @@ -73,7 +132,21 @@ sub schema_get_type_text { return $type; } -# generta epop from JSON schema properties +sub generate_property_text { + my ($schema) = @_; + my $data = ''; + foreach my $key (sort keys %$schema) { + my $d = $schema->{$key}; + next if $d->{alias}; + my $desc = $d->{description}; + my $typetext = schema_get_type_text($d); + $desc = 'No description available' if !$desc; + $data .= "=item $key: $typetext\n\n$desc\n\n"; + } + return $data; +} + +# generate pod from JSON schema properties sub dump_properties { my ($properties) = @_; @@ -103,6 +176,15 @@ sub dump_properties { my $typetext = schema_get_type_text($d); $data .= "=item $base: $typetext\n\n"; $data .= "$descr\n\n"; + + if ($d->{type} eq 'string') { + my $format = $d->{format}; + if ($format && ref($format) eq 'HASH') { + $data .= "=over 1.1\n\n"; + $data .= generate_property_text($format); + $data .= "=back\n\n"; + } + } } $data .= "=back";