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