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