]>
Commit | Line | Data |
---|---|---|
e143e9d8 DM |
1 | package PVE::PodParser; |
2 | ||
3 | use strict; | |
c36f332e | 4 | use warnings; |
e143e9d8 DM |
5 | use Pod::Parser; |
6 | use base qw(Pod::Parser); | |
7 | ||
d415354b DM |
8 | my $currentYear = (localtime(time))[5] + 1900; |
9 | ||
e143e9d8 DM |
10 | my $stdinclude = { |
11 | pve_copyright => <<EODATA, | |
12 | \=head1 COPYRIGHT AND DISCLAIMER | |
13 | ||
d415354b | 14 | Copyright (C) 2007-$currentYear Proxmox Server Solutions GmbH |
e143e9d8 DM |
15 | |
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. | |
20 | ||
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. | |
25 | ||
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/>. | |
28 | EODATA | |
29 | }; | |
30 | ||
31 | sub 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 |
47 | sub 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}; | |
65 | if (my $desc = $entry->{format_description}) { | |
66 | $typetext .= $entry->{default_key} ? "[$key=]" : "$key="; | |
67 | $typetext .= "<$desc>"; | |
68 | } elsif (my $text = $entry->{typetext}) { | |
69 | $typetext .= $text; | |
70 | } else { | |
71 | die "internal error: neither format_description nor typetext found"; | |
72 | } | |
73 | $typetext .= $post; | |
74 | }; | |
75 | foreach my $key (@required) { | |
76 | &$add($key); | |
77 | $pre = ', '; | |
78 | } | |
79 | $pre = $pre ? ' [,' : '['; | |
80 | $post = ']'; | |
81 | foreach my $key (@optional) { | |
82 | &$add($key); | |
83 | $pre = ' [,'; | |
84 | } | |
85 | return $typetext; | |
86 | } | |
87 | ||
e143e9d8 DM |
88 | sub schema_get_type_text { |
89 | my ($phash) = @_; | |
90 | ||
91 | if ($phash->{typetext}) { | |
92 | return $phash->{typetext}; | |
93 | } elsif ($phash->{enum}) { | |
94 | return "(" . join(' | ', sort @{$phash->{enum}}) . ")"; | |
95 | } elsif ($phash->{pattern}) { | |
96 | return $phash->{pattern}; | |
97 | } elsif ($phash->{type} eq 'integer' || $phash->{type} eq 'number') { | |
98 | if (defined($phash->{minimum}) && defined($phash->{maximum})) { | |
99 | return "$phash->{type} ($phash->{minimum} - $phash->{maximum})"; | |
100 | } elsif (defined($phash->{minimum})) { | |
101 | return "$phash->{type} ($phash->{minimum} - N)"; | |
102 | } elsif (defined($phash->{maximum})) { | |
103 | return "$phash->{type} (-N - $phash->{maximum})"; | |
104 | } | |
990b6a1a | 105 | } elsif ($phash->{type} eq 'string') { |
2421fba1 WB |
106 | if (my $format = $phash->{format}) { |
107 | $format = PVE::JSONSchema::get_format($format) if ref($format) ne 'HASH'; | |
108 | if (ref($format) eq 'HASH') { | |
109 | return generate_typetext($format); | |
110 | } | |
990b6a1a | 111 | } |
e143e9d8 DM |
112 | } |
113 | ||
114 | my $type = $phash->{type} || 'string'; | |
115 | ||
116 | return $type; | |
117 | } | |
118 | ||
119 | # generta epop from JSON schema properties | |
120 | sub dump_properties { | |
121 | my ($properties) = @_; | |
122 | ||
123 | my $data = "=over 1\n\n"; | |
124 | ||
125 | my $idx_param = {}; # -vlan\d+ -scsi\d+ | |
126 | ||
127 | foreach my $key (sort keys %$properties) { | |
128 | my $d = $properties->{$key}; | |
129 | my $base = $key; | |
130 | if ($key =~ m/^([a-z]+)(\d+)$/) { | |
131 | my $name = $1; | |
132 | next if $idx_param->{$name}; | |
133 | $idx_param->{$name} = 1; | |
134 | $base = "${name}[n]"; | |
135 | } | |
136 | ||
137 | my $descr = $d->{description} || 'No description avalable.'; | |
138 | chomp $descr; | |
139 | ||
140 | if (defined(my $dv = $d->{default})) { | |
141 | my $multi = $descr =~ m/\n\n/; # multi paragraph ? | |
142 | $descr .= $multi ? "\n\n" : " "; | |
143 | $descr .= "Default value is '$dv'."; | |
144 | } | |
145 | ||
146 | my $typetext = schema_get_type_text($d); | |
147 | $data .= "=item $base: $typetext\n\n"; | |
148 | $data .= "$descr\n\n"; | |
149 | } | |
150 | ||
151 | $data .= "=back"; | |
152 | ||
153 | return $data; | |
154 | } | |
155 | ||
156 | 1; |