bump version to 4.0-49
[pve-common.git] / src / PVE / PodParser.pm
CommitLineData
e143e9d8
DM
1package PVE::PodParser;
2
3use strict;
c36f332e 4use warnings;
e143e9d8
DM
5use Pod::Parser;
6use base qw(Pod::Parser);
7
d415354b
DM
8my $currentYear = (localtime(time))[5] + 1900;
9
e143e9d8
DM
10my $stdinclude = {
11 pve_copyright => <<EODATA,
12\=head1 COPYRIGHT AND DISCLAIMER
13
d415354b 14Copyright (C) 2007-$currentYear Proxmox Server Solutions GmbH
e143e9d8
DM
15
16This program is free software: you can redistribute it and\/or modify
17it under the terms of the GNU Affero General Public License as
18published by the Free Software Foundation, either version 3 of the
19License, or (at your option) any later version.
20
21This program is distributed in the hope that it will be useful,
22but WITHOUT ANY WARRANTY; without even the implied warranty of
23MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24GNU Affero General Public License for more details.
25
26You should have received a copy of the GNU Affero General Public License
27along with this program. If not, see L<http://www.gnu.org/licenses/>.
28EODATA
29};
30
31sub command {
32 my ($self, $cmd, $text, $line_num, $pod_para) = @_;
33
34 if (($cmd eq 'include' && $text =~ m/^\s*(\S+)\s/)) {
35 my $incl = $1;
36 my $data = $stdinclude->{$incl} ? $stdinclude->{$incl} :
37 $self->{include}->{$incl};
38 chomp $data;
39 $self->textblock("$data\n\n", $line_num, $pod_para);
40 } else {
41 $self->textblock($pod_para->raw_text(), $line_num, $pod_para);
42 }
43}
44
45# helpers used to generate our manual pages
46
2421fba1
WB
47sub generate_typetext {
48 my ($schema) = @_;
49 my $typetext = '';
50 my (@optional, @required);
51 foreach my $key (sort keys %$schema) {
52 next if !$schema->{$key}->{format_description} &&
53 !$schema->{$key}->{typetext};
54 if ($schema->{$key}->{optional}) {
55 push @optional, $key;
56 } else {
57 push @required, $key;
58 }
59 }
60 my ($pre, $post) = ('', '');
61 my $add = sub {
62 my ($key) = @_;
63 $typetext .= $pre;
64 my $entry = $schema->{$key};
303a9b34
WB
65 if (my $alias = $entry->{alias}) {
66 $key = $alias;
67 $entry = $schema->{$key};
68 }
2421fba1
WB
69 if (my $desc = $entry->{format_description}) {
70 $typetext .= $entry->{default_key} ? "[$key=]" : "$key=";
71 $typetext .= "<$desc>";
72 } elsif (my $text = $entry->{typetext}) {
73 $typetext .= $text;
74 } else {
75 die "internal error: neither format_description nor typetext found";
76 }
77 $typetext .= $post;
78 };
79 foreach my $key (@required) {
80 &$add($key);
81 $pre = ', ';
82 }
83 $pre = $pre ? ' [,' : '[';
84 $post = ']';
85 foreach my $key (@optional) {
86 &$add($key);
87 $pre = ' [,';
88 }
89 return $typetext;
90}
91
e143e9d8
DM
92sub schema_get_type_text {
93 my ($phash) = @_;
94
95 if ($phash->{typetext}) {
96 return $phash->{typetext};
97 } elsif ($phash->{enum}) {
98 return "(" . join(' | ', sort @{$phash->{enum}}) . ")";
99 } elsif ($phash->{pattern}) {
100 return $phash->{pattern};
101 } elsif ($phash->{type} eq 'integer' || $phash->{type} eq 'number') {
102 if (defined($phash->{minimum}) && defined($phash->{maximum})) {
103 return "$phash->{type} ($phash->{minimum} - $phash->{maximum})";
104 } elsif (defined($phash->{minimum})) {
105 return "$phash->{type} ($phash->{minimum} - N)";
106 } elsif (defined($phash->{maximum})) {
107 return "$phash->{type} (-N - $phash->{maximum})";
108 }
990b6a1a 109 } elsif ($phash->{type} eq 'string') {
2421fba1
WB
110 if (my $format = $phash->{format}) {
111 $format = PVE::JSONSchema::get_format($format) if ref($format) ne 'HASH';
112 if (ref($format) eq 'HASH') {
113 return generate_typetext($format);
114 }
990b6a1a 115 }
e143e9d8
DM
116 }
117
118 my $type = $phash->{type} || 'string';
119
120 return $type;
121}
122
123# generta epop from JSON schema properties
124sub dump_properties {
125 my ($properties) = @_;
126
127 my $data = "=over 1\n\n";
128
129 my $idx_param = {}; # -vlan\d+ -scsi\d+
130
131 foreach my $key (sort keys %$properties) {
132 my $d = $properties->{$key};
133 my $base = $key;
134 if ($key =~ m/^([a-z]+)(\d+)$/) {
135 my $name = $1;
136 next if $idx_param->{$name};
137 $idx_param->{$name} = 1;
138 $base = "${name}[n]";
139 }
140
141 my $descr = $d->{description} || 'No description avalable.';
142 chomp $descr;
143
144 if (defined(my $dv = $d->{default})) {
145 my $multi = $descr =~ m/\n\n/; # multi paragraph ?
146 $descr .= $multi ? "\n\n" : " ";
147 $descr .= "Default value is '$dv'.";
148 }
149
150 my $typetext = schema_get_type_text($d);
151 $data .= "=item $base: $typetext\n\n";
152 $data .= "$descr\n\n";
153 }
154
155 $data .= "=back";
156
157 return $data;
158}
159
1601;