X-Git-Url: https://git.proxmox.com/?p=pve-common.git;a=blobdiff_plain;f=src%2FPVE%2FPodParser.pm;fp=src%2FPVE%2FPodParser.pm;h=7e31e1957e71c42a542c9ae1eb7ab76eb91b7af1;hp=0000000000000000000000000000000000000000;hb=b51b16e6f58de4cb385bd461d97866b3d94c93ec;hpb=47e4eb112911b530d0646c1808fd06cbeb921572 diff --git a/src/PVE/PodParser.pm b/src/PVE/PodParser.pm new file mode 100644 index 0000000..7e31e19 --- /dev/null +++ b/src/PVE/PodParser.pm @@ -0,0 +1,108 @@ +package PVE::PodParser; + +use strict; +use warnings; +use Pod::Parser; +use base qw(Pod::Parser); + +my $currentYear = (localtime(time))[5] + 1900; + +my $stdinclude = { + pve_copyright => <. +EODATA +}; + +sub command { + my ($self, $cmd, $text, $line_num, $pod_para) = @_; + + if (($cmd eq 'include' && $text =~ m/^\s*(\S+)\s/)) { + my $incl = $1; + my $data = $stdinclude->{$incl} ? $stdinclude->{$incl} : + $self->{include}->{$incl}; + chomp $data; + $self->textblock("$data\n\n", $line_num, $pod_para); + } else { + $self->textblock($pod_para->raw_text(), $line_num, $pod_para); + } +} + +# helpers used to generate our manual pages + +sub schema_get_type_text { + my ($phash) = @_; + + if ($phash->{typetext}) { + return $phash->{typetext}; + } elsif ($phash->{enum}) { + return "(" . join(' | ', sort @{$phash->{enum}}) . ")"; + } elsif ($phash->{pattern}) { + return $phash->{pattern}; + } elsif ($phash->{type} eq 'integer' || $phash->{type} eq 'number') { + if (defined($phash->{minimum}) && defined($phash->{maximum})) { + return "$phash->{type} ($phash->{minimum} - $phash->{maximum})"; + } elsif (defined($phash->{minimum})) { + return "$phash->{type} ($phash->{minimum} - N)"; + } elsif (defined($phash->{maximum})) { + return "$phash->{type} (-N - $phash->{maximum})"; + } + } + + my $type = $phash->{type} || 'string'; + + return $type; +} + +# generta epop from JSON schema properties +sub dump_properties { + my ($properties) = @_; + + my $data = "=over 1\n\n"; + + my $idx_param = {}; # -vlan\d+ -scsi\d+ + + foreach my $key (sort keys %$properties) { + my $d = $properties->{$key}; + my $base = $key; + if ($key =~ m/^([a-z]+)(\d+)$/) { + my $name = $1; + next if $idx_param->{$name}; + $idx_param->{$name} = 1; + $base = "${name}[n]"; + } + + my $descr = $d->{description} || 'No description avalable.'; + chomp $descr; + + if (defined(my $dv = $d->{default})) { + my $multi = $descr =~ m/\n\n/; # multi paragraph ? + $descr .= $multi ? "\n\n" : " "; + $descr .= "Default value is '$dv'."; + } + + my $typetext = schema_get_type_text($d); + $data .= "=item $base: $typetext\n\n"; + $data .= "$descr\n\n"; + } + + $data .= "=back"; + + return $data; +} + +1;