]> git.proxmox.com Git - pve-docs.git/blob - asciidoc-pve.in
add vIOMMU documentation
[pve-docs.git] / asciidoc-pve.in
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use Getopt::Long;
6 use File::Path;
7 use File::Basename;
8 use IO::File;
9 use Cwd;
10
11 use JSON;
12
13 my $verbose;
14 my $keep_artifacts;
15
16 my $release = '@RELEASE@';
17
18 my $clicmd = shift or
19 die "no command specified\n";
20
21 my $data_str = "";
22 while (<main::DATA>) { $data_str .= $_; }
23
24 my $fileinfo = decode_json($data_str);
25
26 my $tmpprefix = '.asciidoc-pve-tmp'.$$.'_';
27
28 my $adoc_source_dir = "/usr/share/pve-doc-generator";
29
30 # inside pve-docs source dir?
31 if (-f "asciidoc-pve.in" && -f "pve-admin-guide.adoc") {
32 $adoc_source_dir = getcwd();
33 }
34
35 my $prepared_files = {};
36
37 my $man_target = 'man';
38 my $env_stack = [];
39 my $env_skip = 0;
40
41 my $online_help_links = {
42 'pve_service_daemons' => {
43 link => '/pve-docs/index.html#_service_daemons',
44 title => 'Service Daemons',
45 },
46 'pve_documentation_index' => {
47 link => '/pve-docs/index.html',
48 title => 'Proxmox VE Documentation Index',
49 },
50 'pve_admin_guide' => {
51 link => '/pve-docs/pve-admin-guide.html',
52 title => 'Proxmox VE Administration Guide',
53 },
54 };
55
56 sub debug {
57 my $msg = shift;
58
59 return if !$verbose;
60
61 print STDERR "asciidoc-pve: $msg\n";
62 }
63
64 sub push_environment {
65 my ($env, $skip) = @_;
66
67 $skip = 1 if $env_skip;
68 $skip = 0 if !defined($skip);
69
70 push @$env_stack, [$env, $skip];
71
72 $env_skip = $skip;
73 }
74
75 sub pop_environment {
76 my ($env) = @_;
77
78 my $last_stack_entry = pop @$env_stack;
79 die "unable to pop env '$env'" if !defined($last_stack_entry);
80
81 my ($last_env, $skip) = @$last_stack_entry;
82 die "environment missmatch (${last_env} != $env)\n" if $last_env ne $env;
83
84 if (!scalar(@$env_stack)) {
85 $env_skip = 0;
86 } else {
87 my (undef, $skip) = @{$env_stack->[-1]};
88 $env_skip = $skip;
89 }
90 }
91
92 my $files_for_cleanup = [];
93
94 sub cleanup {
95
96 return if $keep_artifacts;
97
98 foreach my $file (@$files_for_cleanup) {
99 unlink $file;
100 }
101 }
102
103 sub replace_wiki_xref {
104 my ($blockid, $text) = @_;
105
106 my $link = $fileinfo->{blockid_target}->{wiki}->{$blockid};
107 my $reftext = $fileinfo->{reftext}->{wiki}->{$blockid};
108
109 die "unable to resolve wiki link (xref:$blockid)\n"
110 if !defined($link);
111
112 $text = $reftext if !length($text);
113
114 die "xref: no text for wiki link '$blockid'\n" if !$text;
115
116 return "$link\[$text\]";
117 }
118
119 sub replace_default_xref {
120 my ($blockid, $text) = @_;
121
122 my $link = $fileinfo->{blockid_target}->{default}->{$blockid};
123 my $reftext = $fileinfo->{reftext}->{default}->{$blockid};
124
125 die "unable to resolve chapter link (xref:$blockid)\n"
126 if !defined($link);
127
128 $text = $reftext if !length($text);
129
130 die "xref: no text for chapter link '$blockid'\n" if !$text;
131
132 return "$link\[$text\]";
133 }
134
135 sub replace_man_xref {
136 my ($blockid, $text) = @_;
137
138 my $link = $fileinfo->{blockid_target}->{manvolnum}->{$blockid};
139 my $reftext = $fileinfo->{reftext}->{manvolnum}->{$blockid};
140
141 die "unable to resolve man page link (xref:$blockid)\n"
142 if !defined($link);
143
144 $text = $reftext if !length($text);
145
146 die "xref: no text for man page link '$blockid'\n" if !$text;
147
148 my $section = $fileinfo->{mansection}->{manvolnum}->{$link};
149 if (!defined($section)) {
150 warn "link '$blockid' target '$link' is not a manual page, ignoring\n";
151 return "$text";
152 }
153
154
155 if ($man_target eq 'html') {
156 my $target = $link;
157 $target =~ s/\.adoc//;
158 $target .= ".$section";
159 return "link:${target}.html#${blockid}\[$text\]";
160 } elsif ($man_target eq 'man') {
161 my $command = $link;
162 $command =~ s/\.adoc//;
163 return "\*${text}\* (man \*${command}\*($section))";
164 } else {
165 die "internal error"
166 }
167 }
168
169 sub replace_xref {
170 my ($env, $blockid, $text) = @_;
171
172 if ($env eq 'wiki') {
173 return replace_wiki_xref($blockid, $text);
174 } elsif ($env eq 'manvolnum') {
175 if (($man_target eq 'man') || ($man_target eq 'html')) {
176 return replace_man_xref($blockid, $text);
177 } elsif ($man_target eq 'wiki') {
178 return replace_wiki_xref($blockid, $text);
179 } else {
180 die "internal error"
181 }
182 } elsif ($env eq 'default') {
183 return replace_default_xref($blockid, $text);
184 } else {
185 die "internal error";
186 }
187 }
188
189 sub prepare_adoc_file {
190 my ($target_env, $filename, $attributes) = @_;
191
192 return $prepared_files->{$filename} if defined($prepared_files->{$filename});
193
194 debug("prepare $filename");
195
196 my $dirname = dirname($filename);
197 my $basename = basename($filename);
198
199 my $outfilename = "$dirname/${tmpprefix}$basename";
200
201 $prepared_files->{$filename} = $outfilename;
202
203 my $fh = IO::File->new("$filename", "r") or
204 die "unable to open file '$filename' - $!\n";
205
206 my $outfh = IO::File->new("$outfilename", "w") or
207 die "unable to open temporary file '$outfilename'\n";
208
209 push @$files_for_cleanup, $outfilename;
210
211 while (defined (my $line = <$fh>)) {
212 chomp $line;
213 if ($line =~ m/^if(n?)def::(\S+)\[(.*)\]\s*$/) {
214 my ($not, $env, $text) = ($1, $2, $3);
215 die "unsuported ifdef usage - implement me" if $text;
216
217 my $skip = !exists($attributes->{$env}) ? 1 : 0;
218 $skip = ($skip ? 0 : 1 ) if $not;
219
220 push_environment($env, $skip);
221 next;
222 } elsif ($line =~ m/^endif::(\S+)\[(.*)\]\s*$/) {
223 my ($env, $text) = ($1, $2);
224 die "unsuported ifdef usage - implement me" if $text;
225 pop_environment($env);
226 next;
227 }
228
229 next if $env_skip;
230
231 if ($line =~ m/^include::(\S+)(\[.*\]\s*)$/) {
232 my ($fn, $rest) = ($1, $2);
233 debug("include $fn");
234 my $new_fn = prepare_adoc_file($target_env, $fn, $attributes);
235
236 print $outfh "include::${new_fn}$rest\n";
237 next;
238 }
239
240 if ($line =~ m/xref:\S+?\[[^\]]*$/) {
241 die "possible xref spanning multiple lines in '$filename':\n(line $.): $line\n";
242 }
243 if ($line =~ m/<<((?!\>\>).)*$/) {
244 die "possible xref spanning multiple lines in '$filename':\n(line $.): $line\n";
245 }
246 # fix xrefs
247 $line =~ s/xref:([^\s\[\]]+)\[([^\]]*)\]/replace_xref(${target_env},$1,$2)/ge;
248
249 $line =~ s/<<([^\s,\[\]]+)(?:,(.*?))?>>/replace_xref(${target_env},$1,$2)/ge;
250
251 print $outfh $line . "\n";
252 }
253
254 return $outfilename;
255 }
256
257 sub compile_asciidoc {
258 my ($env) = @_;
259
260 my $outfile;
261
262 GetOptions ("outfile=s" => \$outfile,
263 "keep-artifacts" => \$keep_artifacts,
264 "verbose" => \$verbose) or
265 die("Error in command line arguments\n");
266
267 my $infile = shift(@ARGV) or
268 die "no input file specified\n";
269
270 scalar(@ARGV) == 0 or
271 die "too many arguments...\n";
272
273 my $outfilemap = $fileinfo->{outfile}->{$env}->{$infile} ||
274 die "no output file mapping for '$infile' ($env)";
275
276 if ($man_target eq 'html') {
277 $outfilemap .= '.html';
278 } elsif ($man_target eq 'wiki') {
279 $outfilemap .= '-plain.html';
280 }
281
282 if (defined($outfile)) {
283 die "wrong output file name '$outfile != $outfilemap' ($env)"
284 if $outfile ne $outfilemap;
285 } else {
286 $outfile = $outfilemap;
287 }
288
289 defined($fileinfo->{titles}->{$env}) ||
290 die "unknown environment '$env'";
291
292 my $title = $fileinfo->{titles}->{$env}->{$infile} or
293 die "unable to get title for '$infile'$env\n";
294
295 debug("compile $title");
296
297 my $leveloffset = 0;
298
299 my $doctype = $fileinfo->{doctype}->{$env}->{$infile};
300
301 die "unable to get document type for '$infile'\n"
302 if !defined($doctype);
303
304 $leveloffset = - $doctype;
305
306 my $date;
307 if (defined($ENV{SOURCE_DATE_EPOCH})) {
308 $date = `date -d "\@$ENV{SOURCE_DATE_EPOCH}"`;
309 } else {
310 $date = `date`;
311 }
312 chomp $date;
313
314 my $attributes = {
315 $env => undef,
316 leveloffset => $leveloffset,
317 revnumber => $release,
318 revdate => $date,
319 'footer-style' => 'revdate',
320 };
321
322 my $mansection = $fileinfo->{mansection}->{$env}->{$infile};
323
324 if ($env eq 'wiki') {
325 } elsif ($env eq 'manvolnum') {
326 die "undefined man section" if !defined($mansection);
327 $attributes->{manvolnum} = $mansection;
328 } elsif ($env eq 'default') {
329 die "$infile: wrong doctype\n" if $doctype != 0;
330 $attributes->{toc} = undef;
331 }
332
333 if (!defined($outfile)) {
334 $outfile = $infile;
335 $outfile =~ s/\.adoc$//;
336 if ($env eq 'manvolnum') {
337 if (($man_target eq 'html') || ($man_target eq 'wiki')) {
338 $outfile .= ".$mansection.html";
339 } else {
340 $outfile .= ".$mansection";
341 }
342 } else {
343 $outfile .= ".html";
344 }
345 }
346
347 if (($env eq 'manvolnum') && ($man_target eq 'man')) {
348
349 # asciidoc /etc/asciidoc/docbook-xsl/manpage.xsl skip REFERENCES
350 # section like footnotes, so we cannot use a2x.
351 # We use xmlto instead.
352
353 my $cmd = ['asciidoc', '-dmanpage', '-bdocbook',
354 '-f', "$adoc_source_dir/asciidoc/asciidoc-pve.conf",
355 '-a', 'docinfo1'];
356
357 foreach my $key (keys %$attributes) {
358 my $value = $attributes->{$key};
359 if (defined($value)) {
360 push @$cmd, '-a', "$key=$value";
361 } else {
362 push @$cmd, '-a', $key;
363 }
364 }
365
366 push @$cmd, '--verbose' if $verbose;
367
368 my $tmpxmlfile = "${outfile}.xml.tmp";
369
370 push @$cmd, '--out-file', $tmpxmlfile;
371
372 push @$files_for_cleanup, $tmpxmlfile;
373
374 my $new_infile = prepare_adoc_file($env, $infile, $attributes);
375
376 push @$cmd, $new_infile;
377
378 debug("run " . join(' ', @$cmd));
379
380 system(@$cmd) == 0 or
381 die "aciidoc error";
382
383 $cmd = ['xmlto', 'man', $tmpxmlfile];
384
385 push @$cmd, '-v' if $verbose;
386
387 debug("run " . join(' ', @$cmd));
388
389 system(@$cmd) == 0 or
390 die "xmlto error";
391
392 } else {
393
394 $attributes->{icons} = undef;
395 $attributes->{'data-uri'} = undef;
396
397 my $cmd = ['asciidoc',
398 '-f', "$adoc_source_dir/asciidoc/asciidoc-pve.conf",
399 ];
400
401 if (($env eq 'wiki') ||
402 (($env eq 'manvolnum') && ($man_target eq 'wiki'))) {
403
404 push @$cmd, '-b', "$adoc_source_dir/asciidoc/mediawiki";
405 } else {
406 push @$cmd, '-b', "$adoc_source_dir/asciidoc/pve-html";
407 }
408
409 foreach my $key (keys %$attributes) {
410 my $value = $attributes->{$key};
411 if (defined($value)) {
412 push @$cmd, '-a', "$key=$value";
413 } else {
414 push @$cmd, '-a', $key;
415 }
416 }
417
418 push @$cmd, '--verbose' if $verbose;
419
420 push @$cmd, '--out-file', $outfile;
421
422 my $new_infile = prepare_adoc_file($env, $infile, $attributes);
423
424 push @$cmd, $new_infile;
425
426 debug("run " . join(' ', @$cmd));
427
428 system(@$cmd) == 0 or
429 die "aciidoc error";
430 }
431 }
432
433 sub get_links {
434
435 my $data = {};
436
437 foreach my $blockid (sort keys %{$fileinfo->{blockid_target}->{default}}) {
438 my $link = $fileinfo->{blockid_target}->{default}->{$blockid};
439 my $reftitle = $fileinfo->{reftitle}->{default}->{$blockid};
440 my $reftext = $fileinfo->{reftext}->{default}->{$blockid};
441 die "internal error" if $link !~ m/^link:/;
442 $link =~ s/^link://;
443
444 my $file = $fileinfo->{blockid}->{default}->{$blockid};
445 die "internal error - no filename" if ! defined($file);
446 my $title = $fileinfo->{titles}->{default}->{$file} ||
447 die "internal error - no title";
448
449 $data->{$blockid}->{title} = $title;
450 $data->{$blockid}->{link} = $link;
451 my $subtitle = $reftitle || $reftext;
452 $data->{$blockid}->{subtitle} = $subtitle
453 if $subtitle && ($title ne $subtitle);
454 }
455
456 return $data;
457 }
458
459 sub scan_extjs_file {
460 my ($filename, $res_data) = @_;
461
462 my $fh = IO::File->new($filename, "r") ||
463 die "unable to open '$filename' - $!\n";
464
465 debug("scan-extjs $filename");
466
467 while(defined(my $line = <$fh>)) {
468 if ($line =~ m/\s+onlineHelp:\s*[\'\"](.*?)[\'\"]/) {
469 my $blockid = $1;
470 my $link = $fileinfo->{blockid_target}->{default}->{$blockid};
471 die "undefined blockid '$blockid' ($filename, line $.)\n"
472 if !(defined($link) || defined($online_help_links->{$blockid}));
473
474 $res_data->{$blockid} = 1;
475 }
476 }
477 }
478
479 if ($clicmd eq 'compile-wiki') {
480
481 eval { compile_asciidoc('wiki'); };
482 my $err = $@;
483
484 cleanup();
485
486 die $err if $err;
487
488 } elsif ($clicmd eq 'compile-chapter') {
489
490 eval { compile_asciidoc('default'); };
491 my $err = $@;
492
493 cleanup();
494
495 die $err if $err;
496
497 } elsif ($clicmd eq 'compile-man-html') {
498
499 $man_target = 'html';
500
501 eval { compile_asciidoc('manvolnum'); };
502 my $err = $@;
503
504 cleanup();
505
506 die $err if $err;
507
508 } elsif ($clicmd eq 'compile-man-wiki') {
509
510 $man_target = 'wiki';
511
512 eval { compile_asciidoc('manvolnum'); };
513 my $err = $@;
514
515 cleanup();
516
517 die $err if $err;
518
519 } elsif ($clicmd eq 'compile-man') {
520
521 eval { compile_asciidoc('manvolnum'); };
522 my $err = $@;
523
524 cleanup();
525
526 die $err if $err;
527
528 } elsif ($clicmd eq 'print-links') {
529
530 my $outfile;
531
532 GetOptions("outfile=s" => \$outfile,
533 "verbose" => \$verbose) or
534 die("Error in command line arguments\n");
535
536 scalar(@ARGV) == 0 or
537 die "too many arguments...\n";
538
539 my $data = get_links();
540
541 my $res = to_json($data, { pretty => 1, canonical => 1 } );
542
543 if (defined($outfile)) {
544 my $outfh = IO::File->new("$outfile", "w") or
545 die "unable to open temporary file '$outfile'\n";
546
547 print $outfh $res;
548
549 } else {
550
551 print $res;
552 }
553
554 } elsif ($clicmd eq 'scan-extjs') {
555
556 GetOptions("verbose" => \$verbose) or
557 die("Error in command line arguments\n");
558
559 my $link_hash = {};
560 my $scanned_files = {};
561 while (my $filename = shift) {
562 die "got strange file name '$filename'\n"
563 if $filename !~ m/\.js$/;
564 next if $scanned_files->{$filename};
565
566 scan_extjs_file($filename, $link_hash);
567 $scanned_files->{$filename} = 1;
568 }
569
570 my $data = get_links();
571
572 my $res_data = {};
573
574 foreach my $blockid (keys %$link_hash) {
575 $res_data->{$blockid} = $data->{$blockid} || $online_help_links->{$blockid} ||
576 die "internal error - no data for '$blockid'";
577 }
578
579 my $data_str = to_json($res_data, { pretty => 1, canonical => 1 });
580 chomp $data_str;
581
582 print "var pveOnlineHelpInfo = ${data_str};\n";
583
584 } elsif ($clicmd eq 'chapter-table') {
585
586 print '[width="100%",options="header"]' . "\n";
587 print "|====\n";
588 print "|Title|Link\n";
589
590 my $filelist = $fileinfo->{outfile}->{default};
591 foreach my $sourcefile (sort keys %$filelist) {
592 my $target = $filelist->{$sourcefile};
593 next if $target eq 'pve-admin-guide.html';
594 my $title = $fileinfo->{titles}->{default}->{$sourcefile} ||
595 die "not title for '$sourcefile'";
596 print "|$title|link:$target\[\]\n";
597 }
598
599 print "|====\n";
600
601 } elsif ($clicmd =~ m/^man([158])page-table$/) {
602
603 my $section = $1;
604 print '[width="100%",cols="5*d",options="header"]' . "\n";
605 print "|====\n";
606 print "|Name 3+|Title|Link\n";
607
608 my $filelist = $fileinfo->{outfile}->{manvolnum};
609 foreach my $manpage (sort keys %$filelist) {
610 next if $section ne $fileinfo->{mansection}->{manvolnum}->{$manpage};
611 my $mantitle = $fileinfo->{titles}->{manvolnum}->{$manpage} ||
612 die "not manual title for '$manpage'";
613 my $title = $fileinfo->{titles}->{default}->{$manpage} ||
614 die "not title for '$manpage'";
615
616 # hack - remove command name prefix from titles
617 $title =~ s/^[a-z]+\s*-\s*//;
618
619 my $target = $filelist->{$manpage};
620 print "|$mantitle 3+|$title|link:$target.html\[$target\]\n";
621 }
622
623 print "|====\n";
624
625 } else {
626
627 die "unknown command '$clicmd'\n";
628
629 }
630
631
632 exit 0;
633
634 __END__