]>
git.proxmox.com Git - pve-common.git/blob - src/PVE/PodParser.pm
1 package PVE
::PodParser
;
6 use base
qw(Pod::Parser);
8 my $currentYear = (localtime(time))[5] + 1900;
11 pve_copyright
=> <<EODATA,
12 \=head1 COPYRIGHT AND DISCLAIMER
14 Copyright (C) 2007-$currentYear Proxmox Server Solutions GmbH
16 This program is free software: you can redistribute it and\/or modify
17 it under the terms of the GNU Affero General Public License as
18 published by the Free Software Foundation, either version 3 of the
19 License, or (at your option) any later version.
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU Affero General Public License for more details.
26 You should have received a copy of the GNU Affero General Public License
27 along with this program. If not, see L<http://www.gnu.org/licenses/>.
32 my ($self, $cmd, $text, $line_num, $pod_para) = @_;
34 if (($cmd eq 'include' && $text =~ m/^\s*(\S+)\s/)) {
36 my $data = $stdinclude->{$incl} ?
$stdinclude->{$incl} :
37 $self->{include
}->{$incl};
39 $self->textblock("$data\n\n", $line_num, $pod_para);
41 $self->textblock($pod_para->raw_text(), $line_num, $pod_para);
45 # helpers used to generate our manual pages
47 sub generate_typetext
{
50 my (@optional, @required);
51 foreach my $key (sort keys %$schema) {
52 my $entry = $schema->{$key};
53 next if !$entry->{format_description
} &&
54 !$entry->{typetext
} &&
56 $entry->{type
} ne 'boolean';
57 if ($schema->{$key}->{optional
}) {
63 my ($pre, $post) = ('', '');
67 my $entry = $schema->{$key};
68 if (my $alias = $entry->{alias
}) {
70 $entry = $schema->{$key};
72 if (!defined($entry->{typetext
})) {
73 $typetext .= $entry->{default_key
} ?
"[$key=]" : "$key=";
75 if (my $desc = $entry->{format_description
}) {
76 $typetext .= "<$desc>";
77 } elsif (my $text = $entry->{typetext
}) {
79 } elsif (my $enum = $entry->{enum
}) {
80 $typetext .= '<' . join('|', @$enum) . '>';
81 } elsif ($entry->{type
} eq 'boolean') {
84 die "internal error: neither format_description nor typetext found";
88 foreach my $key (@required) {
92 $pre = $pre ?
' [,' : '[';
94 foreach my $key (@optional) {
101 sub schema_get_type_text
{
104 if ($phash->{typetext
}) {
105 return $phash->{typetext
};
106 } elsif ($phash->{enum
}) {
107 return "(" . join(' | ', sort @{$phash->{enum
}}) . ")";
108 } elsif ($phash->{pattern
}) {
109 return $phash->{pattern
};
110 } elsif ($phash->{type
} eq 'integer' || $phash->{type
} eq 'number') {
111 if (defined($phash->{minimum
}) && defined($phash->{maximum
})) {
112 return "$phash->{type} ($phash->{minimum} - $phash->{maximum})";
113 } elsif (defined($phash->{minimum
})) {
114 return "$phash->{type} ($phash->{minimum} - N)";
115 } elsif (defined($phash->{maximum
})) {
116 return "$phash->{type} (-N - $phash->{maximum})";
118 } elsif ($phash->{type
} eq 'string') {
119 if (my $format = $phash->{format
}) {
120 $format = PVE
::JSONSchema
::get_format
($format) if ref($format) ne 'HASH';
121 if (ref($format) eq 'HASH') {
122 return generate_typetext
($format);
127 my $type = $phash->{type
} || 'string';
132 sub generate_property_text
{
135 foreach my $key (sort keys %$schema) {
136 my $d = $schema->{$key};
138 my $desc = $d->{description
};
139 my $typetext = schema_get_type_text
($d);
140 $desc = 'No description available' if !$desc;
141 $data .= "=item $key: $typetext\n\n$desc\n\n";
146 # generate pod from JSON schema properties
147 sub dump_properties
{
148 my ($properties) = @_;
150 my $data = "=over 1\n\n";
152 my $idx_param = {}; # -vlan\d+ -scsi\d+
154 foreach my $key (sort keys %$properties) {
155 my $d = $properties->{$key};
157 if ($key =~ m/^([a-z]+)(\d+)$/) {
159 next if $idx_param->{$name};
160 $idx_param->{$name} = 1;
161 $base = "${name}[n]";
164 my $descr = $d->{description
} || 'No description avalable.';
167 if (defined(my $dv = $d->{default})) {
168 my $multi = $descr =~ m/\n\n/; # multi paragraph ?
169 $descr .= $multi ?
"\n\n" : " ";
170 $descr .= "Default value is '$dv'.";
173 my $typetext = schema_get_type_text
($d);
174 $data .= "=item $base: $typetext\n\n";
175 $data .= "$descr\n\n";
177 if ($d->{type
} eq 'string') {
178 my $format = $d->{format
};
179 if ($format && ref($format) eq 'HASH') {
180 $data .= "=over 1.1\n\n";
181 $data .= generate_property_text
($format);
182 $data .= "=back\n\n";