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