]> git.proxmox.com Git - pve-docs.git/blob - asciidoc-pve.in
878fe51772ce82481fa2fbe465046def340e624b
[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 "attributes.txt" && -f "pve-admin-guide.adoc") {
32 $adoc_source_dir = "."
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 die "link target is not a manual page" if !defined($section);
150
151
152 if ($man_target eq 'html') {
153 my $target = $link;
154 $target =~ s/\.adoc//;
155 $target .= ".$section";
156 return "link:${target}.html#${blockid}\[$text\]";
157 } elsif ($man_target eq 'man') {
158 my $command = $link;
159 $command =~ s/\.adoc//;
160 return "\*${text}\* (man \*${command}\*($section))";
161 } else {
162 die "internal error"
163 }
164 }
165
166 sub replace_xref {
167 my ($env, $blockid, $text) = @_;
168
169 if ($env eq 'wiki') {
170 return replace_wiki_xref($blockid, $text);
171 } elsif ($env eq 'manvolnum') {
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 }
179 } elsif ($env eq 'default') {
180 return replace_default_xref($blockid, $text);
181 } else {
182 die "internal error";
183 }
184 }
185
186 sub prepare_adoc_file {
187 my ($target_env, $filename, $attributes) = @_;
188
189 return $prepared_files->{$filename} if defined($prepared_files->{$filename});
190
191 debug("prepare $filename");
192
193 my $dirname = dirname($filename);
194 my $basename = basename($filename);
195
196 my $outfilename = "$dirname/${tmpprefix}$basename";
197
198 $prepared_files->{$filename} = $outfilename;
199
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
206 push @$files_for_cleanup, $outfilename;
207
208 while (defined (my $line = <$fh>)) {
209 chomp $line;
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);
230 debug("include $fn");
231 my $new_fn = prepare_adoc_file($target_env, $fn, $attributes);
232
233 print $outfh "include::${new_fn}$rest\n";
234 next;
235 }
236
237 # fix xrefs
238 $line =~ s/xref:([^\s\[\]]+)\[([^\]]*)\]/replace_xref(${target_env},$1,$2)/ge;
239
240 $line =~ s/<<([^\s,\[\]]+)(?:,(.*?))?>>/replace_xref(${target_env},$1,$2)/ge;
241
242 print $outfh $line . "\n";
243 }
244
245 return $outfilename;
246 }
247
248 sub compile_asciidoc {
249 my ($env) = @_;
250
251 my $outfile;
252
253 GetOptions ("outfile=s" => \$outfile,
254 "keep-artifacts" => \$keep_artifacts,
255 "verbose" => \$verbose) or
256 die("Error in command line arguments\n");
257
258 my $infile = shift(@ARGV) or
259 die "no input file specified\n";
260
261 scalar(@ARGV) == 0 or
262 die "too many arguments...\n";
263
264 my $outfilemap = $fileinfo->{outfile}->{$env}->{$infile} ||
265 die "no output file mapping '$infile => $outfile' ($env)";
266
267 if ($man_target eq 'html') {
268 $outfilemap .= '.html';
269 } elsif ($man_target eq 'wiki') {
270 $outfilemap .= '-plain.html';
271 }
272
273 die "wrong output file name '$outfile != $outfilemap' ($env)"
274 if $outfile ne $outfilemap;
275
276 defined($fileinfo->{titles}->{$env}) ||
277 die "unknown environment '$env'";
278
279 my $title = $fileinfo->{titles}->{$env}->{$infile} or
280 die "unable to get title for '$infile'$env\n";
281
282 debug("compile $title");
283
284 my $leveloffset = 0;
285
286 my $doctype = $fileinfo->{doctype}->{$env}->{$infile};
287
288 die "unable to get document type for '$infile'\n"
289 if !defined($doctype);
290
291 $leveloffset = - $doctype;
292
293 my $date = `date`;
294 chomp $date;
295
296 my $attributes = {
297 $env => undef,
298 leveloffset => $leveloffset,
299 revnumber => $release,
300 revdate => $date,
301 };
302
303 my $mansection = $fileinfo->{mansection}->{$env}->{$infile};
304
305 if ($env eq 'wiki') {
306 } elsif ($env eq 'manvolnum') {
307 die "undefined man section" if !defined($mansection);
308 $attributes->{manvolnum} = $mansection;
309 } elsif ($env eq 'default') {
310 die "$infile: wrong doctype\n" if $doctype != 0;
311 $attributes->{toc} = undef;
312 }
313
314 if (!defined($outfile)) {
315 $outfile = $infile;
316 $outfile =~ s/\.adoc$//;
317 if ($env eq 'manvolnum') {
318 if (($man_target eq 'html') || ($man_target eq 'wiki')) {
319 $outfile .= ".$mansection.html";
320 } else {
321 $outfile .= ".$mansection";
322 }
323 } else {
324 $outfile .= ".html";
325 }
326 }
327
328 if (($env eq 'manvolnum') && ($man_target eq 'man')) {
329
330 # asciidoc /etc/asciidoc/docbook-xsl/manpage.xsl skip REFERENCES
331 # section like footnotes, so we cannot use a2x.
332 # We use xmlto instead.
333
334 my $cmd = ['asciidoc', '-dmanpage', '-bdocbook', '-a', 'docinfo1'];
335
336 foreach my $key (keys %$attributes) {
337 my $value = $attributes->{$key};
338 if (defined($value)) {
339 push @$cmd, '-a', "$key=$value";
340 } else {
341 push @$cmd, '-a', $key;
342 }
343 }
344
345 push @$cmd, '--verbose' if $verbose;
346
347 my $tmpxmlfile = "${outfile}.xml.tmp";
348
349 push @$cmd, '--out-file', $tmpxmlfile;
350
351 push @$files_for_cleanup, $tmpxmlfile;
352
353 my $new_infile = prepare_adoc_file($env, $infile, $attributes);
354
355 push @$cmd, $new_infile;
356
357 debug("run " . join(' ', @$cmd));
358
359 system(@$cmd) == 0 or
360 die "aciidoc error";
361
362 $cmd = ['xmlto', 'man', $tmpxmlfile];
363
364 push @$cmd, '-v' if $verbose;
365
366 debug("run " . join(' ', @$cmd));
367
368 system(@$cmd) == 0 or
369 die "xmlto error";
370
371 } else {
372
373 $attributes->{icons} = undef;
374 $attributes->{'data-uri'} = undef;
375
376 my $cmd = ['asciidoc'];
377
378 if (($env eq 'wiki') ||
379 (($env eq 'manvolnum') && ($man_target eq 'wiki'))) {
380
381 push @$cmd, '-s';
382
383 if (-f "./asciidoc/mediawiki.conf") {
384 my $cwd = getcwd();
385 push @$cmd, '-b', "$cwd/asciidoc/mediawiki";
386 } else {
387 push @$cmd, '-b', "mediawiki";
388 }
389 }
390
391 foreach my $key (keys %$attributes) {
392 my $value = $attributes->{$key};
393 if (defined($value)) {
394 push @$cmd, '-a', "$key=$value";
395 } else {
396 push @$cmd, '-a', $key;
397 }
398 }
399
400 push @$cmd, '--verbose' if $verbose;
401
402 push @$cmd, '--out-file', $outfile;
403
404 my $new_infile = prepare_adoc_file($env, $infile, $attributes);
405
406 push @$cmd, $new_infile;
407
408 debug("run " . join(' ', @$cmd));
409
410 system(@$cmd) == 0 or
411 die "aciidoc error";
412 }
413 }
414
415 sub get_links {
416
417 my $data = {};
418
419 foreach my $blockid (sort keys %{$fileinfo->{blockid_target}->{default}}) {
420 my $link = $fileinfo->{blockid_target}->{default}->{$blockid};
421 my $reftitle = $fileinfo->{reftitle}->{default}->{$blockid};
422 my $reftext = $fileinfo->{reftext}->{default}->{$blockid};
423 die "internal error" if $link !~ m/^link:/;
424 $link =~ s/^link://;
425
426 my $file = $fileinfo->{blockid}->{default}->{$blockid};
427 die "internal error - no filename" if ! defined($file);
428 my $title = $fileinfo->{titles}->{default}->{$file} ||
429 die "internal error - no title";
430
431 $data->{$blockid}->{title} = $title;
432 $data->{$blockid}->{link} = $link;
433 my $subtitle = $reftitle || $reftext;
434 $data->{$blockid}->{subtitle} = $subtitle
435 if $subtitle && ($title ne $subtitle);
436 }
437
438 return $data;
439 }
440
441 sub scan_extjs_file {
442 my ($filename, $res_data) = @_;
443
444 my $fh = IO::File->new($filename, "r") ||
445 die "unable to open '$filename' - $!\n";
446
447 debug("scan-extjs $filename");
448
449 while(defined(my $line = <$fh>)) {
450 if ($line =~ m/\s+onlineHelp:\s*[\'\"](.*?)[\'\"]/) {
451 my $blockid = $1;
452 my $link = $fileinfo->{blockid_target}->{default}->{$blockid};
453 die "undefined blockid '$blockid' ($filename, line $.)\n"
454 if !(defined($link) || defined($online_help_links->{$blockid}));
455
456 $res_data->{$blockid} = 1;
457 }
458 }
459 }
460
461 if ($clicmd eq 'compile-wiki') {
462
463 eval { compile_asciidoc('wiki'); };
464 my $err = $@;
465
466 cleanup();
467
468 die $err if $err;
469
470 } elsif ($clicmd eq 'compile-chapter') {
471
472 eval { compile_asciidoc('default'); };
473 my $err = $@;
474
475 cleanup();
476
477 die $err if $err;
478
479 } elsif ($clicmd eq 'compile-man-html') {
480
481 $man_target = 'html';
482
483 eval { compile_asciidoc('manvolnum'); };
484 my $err = $@;
485
486 cleanup();
487
488 die $err if $err;
489
490 } elsif ($clicmd eq 'compile-man-wiki') {
491
492 $man_target = 'wiki';
493
494 eval { compile_asciidoc('manvolnum'); };
495 my $err = $@;
496
497 cleanup();
498
499 die $err if $err;
500
501 } elsif ($clicmd eq 'compile-man') {
502
503 eval { compile_asciidoc('manvolnum'); };
504 my $err = $@;
505
506 cleanup();
507
508 die $err if $err;
509
510 } elsif ($clicmd eq 'print-links') {
511
512 my $outfile;
513
514 GetOptions("outfile=s" => \$outfile,
515 "verbose" => \$verbose) or
516 die("Error in command line arguments\n");
517
518 scalar(@ARGV) == 0 or
519 die "too many arguments...\n";
520
521 my $data = get_links();
522
523 my $res = to_json($data, { pretty => 1, canonical => 1 } );
524
525 if (defined($outfile)) {
526 my $outfh = IO::File->new("$outfile", "w") or
527 die "unable to open temporary file '$outfile'\n";
528
529 print $outfh $res;
530
531 } else {
532
533 print $res;
534 }
535
536 } elsif ($clicmd eq 'scan-extjs') {
537
538 GetOptions("verbose" => \$verbose) or
539 die("Error in command line arguments\n");
540
541 my $link_hash = {};
542 my $scanned_files = {};
543 while (my $filename = shift) {
544 die "got strange file name '$filename'\n"
545 if $filename !~ m/\.js$/;
546 next if $scanned_files->{$filename};
547
548 scan_extjs_file($filename, $link_hash);
549 $scanned_files->{$filename} = 1;
550 }
551
552 my $data = get_links();
553
554 my $res_data = {};
555
556 foreach my $blockid (keys %$link_hash) {
557 $res_data->{$blockid} = $data->{$blockid} || $online_help_links->{$blockid} ||
558 die "internal error - no data for '$blockid'";
559 }
560
561 my $data_str = to_json($res_data, { pretty => 1, canonical => 1 });
562 chomp $data_str;
563
564 print "var pveOnlineHelpInfo = ${data_str};\n";
565
566 } else {
567
568 die "unknown command '$clicmd'\n";
569
570 }
571
572
573 exit 0;
574
575 __END__