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