bump version to 4.0-25
[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
47sub schema_get_type_text {
48 my ($phash) = @_;
49
50 if ($phash->{typetext}) {
51 return $phash->{typetext};
52 } elsif ($phash->{enum}) {
53 return "(" . join(' | ', sort @{$phash->{enum}}) . ")";
54 } elsif ($phash->{pattern}) {
55 return $phash->{pattern};
56 } elsif ($phash->{type} eq 'integer' || $phash->{type} eq 'number') {
57 if (defined($phash->{minimum}) && defined($phash->{maximum})) {
58 return "$phash->{type} ($phash->{minimum} - $phash->{maximum})";
59 } elsif (defined($phash->{minimum})) {
60 return "$phash->{type} ($phash->{minimum} - N)";
61 } elsif (defined($phash->{maximum})) {
62 return "$phash->{type} (-N - $phash->{maximum})";
63 }
990b6a1a
WB
64 } elsif ($phash->{type} eq 'string') {
65 my $format = $phash->{format};
66 if ($format && ref($format) eq 'HASH') {
67 return PVE::JSONSchema::generate_typetext($format);
68 }
e143e9d8
DM
69 }
70
71 my $type = $phash->{type} || 'string';
72
73 return $type;
74}
75
76# generta epop from JSON schema properties
77sub dump_properties {
78 my ($properties) = @_;
79
80 my $data = "=over 1\n\n";
81
82 my $idx_param = {}; # -vlan\d+ -scsi\d+
83
84 foreach my $key (sort keys %$properties) {
85 my $d = $properties->{$key};
86 my $base = $key;
87 if ($key =~ m/^([a-z]+)(\d+)$/) {
88 my $name = $1;
89 next if $idx_param->{$name};
90 $idx_param->{$name} = 1;
91 $base = "${name}[n]";
92 }
93
94 my $descr = $d->{description} || 'No description avalable.';
95 chomp $descr;
96
97 if (defined(my $dv = $d->{default})) {
98 my $multi = $descr =~ m/\n\n/; # multi paragraph ?
99 $descr .= $multi ? "\n\n" : " ";
100 $descr .= "Default value is '$dv'.";
101 }
102
103 my $typetext = schema_get_type_text($d);
104 $data .= "=item $base: $typetext\n\n";
105 $data .= "$descr\n\n";
106 }
107
108 $data .= "=back";
109
110 return $data;
111}
112
1131;