]>
Commit | Line | Data |
---|---|---|
aa99b349 DM |
1 | #!/usr/bin/perl |
2 | ||
3 | use strict; | |
4 | use warnings; | |
835dd63b DM |
5 | use Getopt::Long; |
6 | use File::Path; | |
7 | use File::Basename; | |
8 | use IO::File; | |
9 | ||
aa99b349 DM |
10 | use JSON; |
11 | ||
034b35c8 DM |
12 | my $verbose; |
13 | my $keep_artifacts; | |
14 | ||
835dd63b DM |
15 | my $release = '@RELEASE@'; |
16 | ||
17 | my $clicmd = shift or | |
18 | die "no command specified\n"; | |
19 | ||
aa99b349 DM |
20 | my $data_str = ""; |
21 | while (<main::DATA>) { $data_str .= $_; } | |
22 | ||
23 | my $fileinfo = decode_json($data_str); | |
aa99b349 | 24 | |
835dd63b DM |
25 | my $tmpprefix = ".asciidoc-pve-tmp_"; |
26 | ||
27 | my $adoc_source_dir = "/usr/share/pve-doc-generator"; | |
28 | ||
29 | # inside pve-docs source dir? | |
30 | if (-f "attributes.txt" && -f "pve-admin-guide.adoc") { | |
31 | $adoc_source_dir = "." | |
32 | } | |
33 | ||
34 | my $prepared_files = {}; | |
35 | ||
c38115e9 | 36 | my $man_target_html = 0; |
835dd63b DM |
37 | my $env_stack = []; |
38 | my $env_skip = 0; | |
39 | ||
40 | sub push_environment { | |
41 | my ($env, $skip) = @_; | |
42 | ||
43 | $skip = 1 if $env_skip; | |
44 | $skip = 0 if !defined($skip); | |
45 | ||
46 | push @$env_stack, [$env, $skip]; | |
47 | ||
48 | $env_skip = $skip; | |
49 | } | |
50 | ||
51 | sub pop_environment { | |
52 | my ($env) = @_; | |
53 | ||
54 | my $last_stack_entry = pop @$env_stack; | |
55 | die "unable to pop env '$env'" if !defined($last_stack_entry); | |
56 | ||
57 | my ($last_env, $skip) = @$last_stack_entry; | |
58 | die "environment missmatch (${last_env} != $env)\n" if $last_env ne $env; | |
59 | ||
60 | if (!scalar(@$env_stack)) { | |
61 | $env_skip = 0; | |
62 | } else { | |
63 | my (undef, $skip) = @{$env_stack->[-1]}; | |
64 | $env_skip = $skip; | |
65 | } | |
66 | } | |
67 | ||
034b35c8 DM |
68 | my $files_for_cleanup = []; |
69 | ||
17d8be0c DM |
70 | sub cleanup { |
71 | ||
034b35c8 DM |
72 | return if $keep_artifacts; |
73 | ||
74 | foreach my $file (@$files_for_cleanup) { | |
75 | unlink $file; | |
76 | } | |
17d8be0c DM |
77 | } |
78 | ||
835dd63b DM |
79 | sub replace_wiki_xref { |
80 | my ($blockid, $text) = @_; | |
81 | ||
82 | my $link = $fileinfo->{blockid_target}->{wiki}->{$blockid}; | |
45930831 | 83 | my $reftext = $fileinfo->{reftext}->{wiki}->{$blockid}; |
835dd63b DM |
84 | |
85 | die "unable to resolve wiki link (xref:$blockid)\n" | |
86 | if !defined($link); | |
87 | ||
45930831 DM |
88 | $text = $reftext if !length($text); |
89 | ||
c38115e9 | 90 | die "xref: no text for wiki link '$blockid'\n" if !$text; |
508e0012 | 91 | |
835dd63b DM |
92 | return "$link\[$text\]"; |
93 | } | |
94 | ||
c38115e9 DM |
95 | sub replace_default_xref { |
96 | my ($blockid, $text) = @_; | |
97 | ||
98 | my $link = $fileinfo->{blockid_target}->{default}->{$blockid}; | |
99 | my $reftext = $fileinfo->{reftext}->{default}->{$blockid}; | |
100 | ||
101 | die "unable to resolve chapter link (xref:$blockid)\n" | |
102 | if !defined($link); | |
103 | ||
104 | $text = $reftext if !length($text); | |
105 | ||
106 | die "xref: no text for chapter link '$blockid'\n" if !$text; | |
107 | ||
108 | return "$link\[$text\]"; | |
109 | } | |
110 | ||
111 | sub replace_man_xref { | |
112 | my ($blockid, $text) = @_; | |
113 | ||
114 | my $link = $fileinfo->{blockid_target}->{manvolnum}->{$blockid}; | |
115 | my $reftext = $fileinfo->{reftext}->{manvolnum}->{$blockid}; | |
116 | ||
117 | die "unable to resolve man page link (xref:$blockid)\n" | |
118 | if !defined($link); | |
119 | ||
120 | $text = $reftext if !length($text); | |
121 | ||
122 | die "xref: no text for man page link '$blockid'\n" if !$text; | |
123 | ||
124 | my $section = $fileinfo->{mansection}->{manvolnum}->{$link}; | |
125 | die "link target is not a manual page" if !defined($section); | |
126 | ||
127 | ||
128 | if (0 && $man_target_html) { | |
129 | my $target = $link; | |
130 | $target =~ s/\.adoc//; | |
131 | $target .= ".$section"; | |
132 | return "link:${target}.html#${blockid}\[$text\]"; | |
133 | } else { | |
134 | my $command = $link; | |
135 | $command =~ s/\.adoc//; | |
136 | return "\*${text}\* (man \*${command}\*($section))"; | |
137 | } | |
138 | } | |
139 | ||
a22a4a80 DM |
140 | sub replace_xref { |
141 | my ($env, $blockid, $text) = @_; | |
835dd63b | 142 | |
a22a4a80 DM |
143 | if ($env eq 'wiki') { |
144 | return replace_wiki_xref($blockid, $text); | |
c38115e9 DM |
145 | } elsif ($env eq 'manvolnum') { |
146 | return replace_man_xref($blockid, $text); | |
147 | } elsif ($env eq 'default') { | |
148 | return replace_default_xref($blockid, $text); | |
a22a4a80 | 149 | } else { |
c38115e9 | 150 | die "internal error"; |
a22a4a80 DM |
151 | } |
152 | } | |
153 | ||
154 | sub prepare_adoc_file { | |
155 | my ($target_env, $filename, $attributes) = @_; | |
508e0012 | 156 | |
c38115e9 | 157 | return $prepared_files->{$filename} if defined($prepared_files->{$filename}); |
835dd63b DM |
158 | |
159 | print "PREPARE $filename\n"; | |
160 | ||
835dd63b DM |
161 | my $dirname = dirname($filename); |
162 | my $basename = basename($filename); | |
163 | ||
164 | my $outfilename = "$dirname/${tmpprefix}$basename"; | |
165 | ||
c38115e9 DM |
166 | $prepared_files->{$filename} = $outfilename; |
167 | ||
835dd63b DM |
168 | my $fh = IO::File->new("$filename", "r") or |
169 | die "unable to open file '$filename' - $!\n"; | |
170 | ||
171 | my $outfh = IO::File->new("$outfilename", "w") or | |
172 | die "unable to open temporary file '$outfilename'\n"; | |
173 | ||
034b35c8 DM |
174 | push @$files_for_cleanup, $outfilename; |
175 | ||
835dd63b | 176 | while (defined (my $line = <$fh>)) { |
c38115e9 | 177 | chomp $line; |
835dd63b DM |
178 | if ($line =~ m/^if(n?)def::(\S+)\[(.*)\]\s*$/) { |
179 | my ($not, $env, $text) = ($1, $2, $3); | |
180 | die "unsuported ifdef usage - implement me" if $text; | |
181 | ||
182 | my $skip = !exists($attributes->{$env}) ? 1 : 0; | |
183 | $skip = ($skip ? 0 : 1 ) if $not; | |
184 | ||
185 | push_environment($env, $skip); | |
186 | next; | |
187 | } elsif ($line =~ m/^endif::(\S+)\[(.*)\]\s*$/) { | |
188 | my ($env, $text) = ($1, $2); | |
189 | die "unsuported ifdef usage - implement me" if $text; | |
190 | pop_environment($env); | |
191 | next; | |
192 | } | |
193 | ||
194 | next if $env_skip; | |
195 | ||
196 | if ($line =~ m/^include::(\S+)(\[.*\]\s*)$/) { | |
197 | my ($fn, $rest) = ($1, $2); | |
198 | print "INCLUDE: $fn\n"; | |
a22a4a80 | 199 | my $new_fn = prepare_adoc_file($target_env, $fn, $attributes); |
c38115e9 DM |
200 | |
201 | print $outfh "include::${new_fn}$rest\n"; | |
202 | next; | |
835dd63b DM |
203 | } |
204 | ||
205 | # fix xrefs | |
a22a4a80 | 206 | $line =~ s/xref:([^\s\[\]]+)\[([^\]]*)\]/replace_xref(${target_env},$1,$2)/ge; |
835dd63b | 207 | |
a22a4a80 | 208 | $line =~ s/<<([^\s,\[\]]+)(?:,(.*?))?>>/replace_xref(${target_env},$1,$2)/ge; |
835dd63b | 209 | |
c38115e9 | 210 | print $outfh $line . "\n"; |
835dd63b DM |
211 | } |
212 | ||
213 | return $outfilename; | |
214 | } | |
215 | ||
a22a4a80 DM |
216 | sub compile_asciidoc { |
217 | my ($env) = @_; | |
835dd63b | 218 | |
835dd63b | 219 | my $outfile; |
835dd63b DM |
220 | |
221 | GetOptions ("outfile=s" => \$outfile, | |
034b35c8 | 222 | "keep-artifacts" => \$keep_artifacts, |
835dd63b DM |
223 | "verbose" => \$verbose) or |
224 | die("Error in command line arguments\n"); | |
225 | ||
226 | my $infile = shift(@ARGV) or | |
227 | die "no input file specified\n"; | |
228 | ||
229 | scalar(@ARGV) == 0 or | |
230 | die "too many arguments...\n"; | |
231 | ||
c38115e9 | 232 | defined($fileinfo->{titles}->{$env}) || |
a22a4a80 | 233 | die "unknown environment '$env'"; |
a22a4a80 | 234 | |
835dd63b | 235 | my $title = $fileinfo->{titles}->{$env}->{$infile} or |
c38115e9 | 236 | die "unable to get title for '$infile'$env\n"; |
835dd63b | 237 | |
835dd63b DM |
238 | print "compile: $title\n"; |
239 | ||
240 | my $leveloffset = 0; | |
241 | ||
c38115e9 DM |
242 | my $doctype = $fileinfo->{doctype}->{$env}->{$infile}; |
243 | ||
244 | die "unable to get document type for '$infile'\n" | |
245 | if !defined($doctype); | |
246 | ||
b489b02c | 247 | $leveloffset = - $doctype; |
835dd63b DM |
248 | |
249 | my $date = `date`; | |
250 | chomp $date; | |
251 | ||
252 | my $attributes = { | |
253 | $env => undef, | |
835dd63b | 254 | leveloffset => $leveloffset, |
835dd63b DM |
255 | }; |
256 | ||
c38115e9 DM |
257 | my $mansection = $fileinfo->{mansection}->{$env}->{$infile}; |
258 | ||
259 | if ($env eq 'wiki') { | |
260 | } elsif ($env eq 'manvolnum') { | |
261 | die "undefined man section" if !defined($mansection); | |
262 | $attributes->{manvolnum} = $mansection; | |
263 | } elsif ($env eq 'default') { | |
264 | die "$infile: wrong doctype\n" if $doctype != 0; | |
a22a4a80 DM |
265 | $attributes->{toc} = undef; |
266 | } | |
267 | ||
c38115e9 DM |
268 | if (!defined($outfile)) { |
269 | $outfile = $infile; | |
270 | $outfile =~ s/\.adoc$//; | |
271 | if ($env eq 'manvolnum') { | |
272 | if ($man_target_html) { | |
273 | $outfile .= ".$mansection.html"; | |
274 | } else { | |
275 | $outfile .= ".$mansection"; | |
276 | } | |
835dd63b | 277 | } else { |
c38115e9 | 278 | $outfile .= ".html"; |
835dd63b DM |
279 | } |
280 | } | |
281 | ||
c38115e9 | 282 | if (($env eq 'manvolnum') && !$man_target_html) { |
835dd63b | 283 | |
c38115e9 DM |
284 | # asciidoc /etc/asciidoc/docbook-xsl/manpage.xsl skip REFERENCES |
285 | # section like footnotes, so we cannot use a2x. | |
286 | # We use xmlto instead. | |
835dd63b | 287 | |
c38115e9 | 288 | my $cmd = ['asciidoc', '-dmanpage', '-bdocbook', '-a', 'docinfo1']; |
835dd63b | 289 | |
c38115e9 DM |
290 | foreach my $key (keys %$attributes) { |
291 | my $value = $attributes->{$key}; | |
292 | if (defined($value)) { | |
293 | push @$cmd, '-a', "$key=$value"; | |
294 | } else { | |
295 | push @$cmd, '-a', $key; | |
296 | } | |
297 | } | |
835dd63b | 298 | |
c38115e9 | 299 | push @$cmd, '--verbose' if $verbose; |
835dd63b | 300 | |
c38115e9 | 301 | my $tmpxmlfile = "${outfile}.xml.tmp"; |
835dd63b | 302 | |
c38115e9 DM |
303 | push @$cmd, '--out-file', $tmpxmlfile; |
304 | ||
034b35c8 DM |
305 | push @$files_for_cleanup, $tmpxmlfile; |
306 | ||
c38115e9 DM |
307 | my $new_infile = prepare_adoc_file($env, $infile, $attributes); |
308 | ||
309 | push @$cmd, $new_infile; | |
310 | ||
311 | print "RUN " . join(' ', @$cmd) . "\n"; | |
312 | ||
313 | system(@$cmd) == 0 or | |
314 | die "aciidoc error"; | |
315 | ||
316 | $cmd = ['xmlto', 'man', $tmpxmlfile]; | |
317 | ||
318 | push @$cmd, '-v' if $verbose; | |
319 | ||
320 | print "RUN " . join(' ', @$cmd) . "\n"; | |
321 | ||
322 | system(@$cmd) == 0 or | |
323 | die "xmlto error"; | |
324 | ||
325 | } else { | |
835dd63b | 326 | |
c38115e9 DM |
327 | $attributes->{icons} = undef; |
328 | $attributes->{'data-uri'} = undef; | |
329 | $attributes->{revnumber} = $release; | |
330 | ||
331 | my $cmd = ['asciidoc']; | |
332 | ||
333 | push @$cmd, '-s' if $env eq 'wiki'; | |
334 | ||
335 | foreach my $key (keys %$attributes) { | |
336 | my $value = $attributes->{$key}; | |
337 | if (defined($value)) { | |
338 | push @$cmd, '-a', "$key=$value"; | |
339 | } else { | |
340 | push @$cmd, '-a', $key; | |
341 | } | |
342 | } | |
343 | ||
344 | push @$cmd, '--verbose' if $verbose; | |
345 | ||
346 | push @$cmd, '--out-file', $outfile; | |
347 | ||
348 | my $new_infile = prepare_adoc_file($env, $infile, $attributes); | |
349 | ||
350 | push @$cmd, $new_infile; | |
351 | ||
352 | print "RUN " . join(' ', @$cmd) . "\n"; | |
353 | ||
354 | system(@$cmd) == 0 or | |
355 | die "aciidoc error"; | |
356 | } | |
357 | } | |
a22a4a80 | 358 | |
b489b02c | 359 | if ($clicmd eq 'compile-wiki') { |
835dd63b | 360 | |
a22a4a80 | 361 | eval { compile_asciidoc('wiki'); }; |
835dd63b DM |
362 | my $err = $@; |
363 | ||
17d8be0c | 364 | cleanup(); |
835dd63b DM |
365 | |
366 | die $err if $err; | |
367 | ||
c38115e9 DM |
368 | } elsif ($clicmd eq 'compile-chapter') { |
369 | ||
370 | eval { compile_asciidoc('default'); }; | |
371 | my $err = $@; | |
372 | ||
373 | cleanup(); | |
374 | ||
375 | die $err if $err; | |
376 | ||
377 | } elsif ($clicmd eq 'compile-man-html') { | |
378 | ||
379 | $man_target_html = 1; | |
380 | ||
381 | eval { compile_asciidoc('manvolnum'); }; | |
382 | my $err = $@; | |
383 | ||
384 | cleanup(); | |
385 | ||
386 | die $err if $err; | |
387 | ||
388 | } elsif ($clicmd eq 'compile-man') { | |
389 | ||
390 | eval { compile_asciidoc('manvolnum'); }; | |
391 | my $err = $@; | |
392 | ||
393 | cleanup(); | |
394 | ||
395 | die $err if $err; | |
396 | ||
835dd63b DM |
397 | } else { |
398 | ||
399 | die "unknown command '$clicmd'\n"; | |
400 | ||
401 | } | |
402 | ||
403 | ||
404 | ||
405 | ||
406 | ||
407 | ||
408 | exit 0; | |
aa99b349 DM |
409 | |
410 | __END__ |