]>
Commit | Line | Data |
---|---|---|
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 | my $outfilemap = $fileinfo->{outfile}->{$env}->{$infile} || | |
264 | die "no output file mapping '$infile => $outfile' ($env)"; | |
265 | ||
266 | if ($man_target eq 'html') { | |
267 | $outfilemap .= '.html'; | |
268 | } elsif ($man_target eq 'wiki') { | |
269 | $outfilemap .= '-plain.html'; | |
270 | } | |
271 | ||
272 | die "wrong output file name '$outfile != $outfilemap' ($env)" | |
273 | if $outfile ne $outfilemap; | |
274 | ||
275 | defined($fileinfo->{titles}->{$env}) || | |
276 | die "unknown environment '$env'"; | |
277 | ||
278 | my $title = $fileinfo->{titles}->{$env}->{$infile} or | |
279 | die "unable to get title for '$infile'$env\n"; | |
280 | ||
281 | debug("compile $title"); | |
282 | ||
283 | my $leveloffset = 0; | |
284 | ||
285 | my $doctype = $fileinfo->{doctype}->{$env}->{$infile}; | |
286 | ||
287 | die "unable to get document type for '$infile'\n" | |
288 | if !defined($doctype); | |
289 | ||
290 | $leveloffset = - $doctype; | |
291 | ||
292 | my $date = `date`; | |
293 | chomp $date; | |
294 | ||
295 | my $attributes = { | |
296 | $env => undef, | |
297 | leveloffset => $leveloffset, | |
298 | revnumber => $release, | |
299 | revdate => $date, | |
300 | }; | |
301 | ||
302 | my $mansection = $fileinfo->{mansection}->{$env}->{$infile}; | |
303 | ||
304 | if ($env eq 'wiki') { | |
305 | } elsif ($env eq 'manvolnum') { | |
306 | die "undefined man section" if !defined($mansection); | |
307 | $attributes->{manvolnum} = $mansection; | |
308 | } elsif ($env eq 'default') { | |
309 | die "$infile: wrong doctype\n" if $doctype != 0; | |
310 | $attributes->{toc} = undef; | |
311 | } | |
312 | ||
313 | if (!defined($outfile)) { | |
314 | $outfile = $infile; | |
315 | $outfile =~ s/\.adoc$//; | |
316 | if ($env eq 'manvolnum') { | |
317 | if (($man_target eq 'html') || ($man_target eq 'wiki')) { | |
318 | $outfile .= ".$mansection.html"; | |
319 | } else { | |
320 | $outfile .= ".$mansection"; | |
321 | } | |
322 | } else { | |
323 | $outfile .= ".html"; | |
324 | } | |
325 | } | |
326 | ||
327 | if (($env eq 'manvolnum') && ($man_target eq 'man')) { | |
328 | ||
329 | # asciidoc /etc/asciidoc/docbook-xsl/manpage.xsl skip REFERENCES | |
330 | # section like footnotes, so we cannot use a2x. | |
331 | # We use xmlto instead. | |
332 | ||
333 | my $cmd = ['asciidoc', '-dmanpage', '-bdocbook', '-a', 'docinfo1']; | |
334 | ||
335 | foreach my $key (keys %$attributes) { | |
336 | my $value = $attributes->{$key}; | |
337 | if (defined($value)) { | |
338 | push @$cmd, '-a', "$key=$value"; | |
339 | } else { | |
340 | push @$cmd, '-a', $key; | |
341 | } | |
342 | } | |
343 | ||
344 | push @$cmd, '--verbose' if $verbose; | |
345 | ||
346 | my $tmpxmlfile = "${outfile}.xml.tmp"; | |
347 | ||
348 | push @$cmd, '--out-file', $tmpxmlfile; | |
349 | ||
350 | push @$files_for_cleanup, $tmpxmlfile; | |
351 | ||
352 | my $new_infile = prepare_adoc_file($env, $infile, $attributes); | |
353 | ||
354 | push @$cmd, $new_infile; | |
355 | ||
356 | debug("run " . join(' ', @$cmd)); | |
357 | ||
358 | system(@$cmd) == 0 or | |
359 | die "aciidoc error"; | |
360 | ||
361 | $cmd = ['xmlto', 'man', $tmpxmlfile]; | |
362 | ||
363 | push @$cmd, '-v' if $verbose; | |
364 | ||
365 | debug("run " . join(' ', @$cmd)); | |
366 | ||
367 | system(@$cmd) == 0 or | |
368 | die "xmlto error"; | |
369 | ||
370 | } else { | |
371 | ||
372 | $attributes->{icons} = undef; | |
373 | $attributes->{'data-uri'} = undef; | |
374 | ||
375 | my $cmd = ['asciidoc']; | |
376 | ||
377 | push @$cmd, '-s' if ($env eq 'wiki') || | |
378 | (($env eq 'manvolnum') && ($man_target eq 'wiki')); | |
379 | ||
380 | foreach my $key (keys %$attributes) { | |
381 | my $value = $attributes->{$key}; | |
382 | if (defined($value)) { | |
383 | push @$cmd, '-a', "$key=$value"; | |
384 | } else { | |
385 | push @$cmd, '-a', $key; | |
386 | } | |
387 | } | |
388 | ||
389 | push @$cmd, '--verbose' if $verbose; | |
390 | ||
391 | push @$cmd, '--out-file', $outfile; | |
392 | ||
393 | my $new_infile = prepare_adoc_file($env, $infile, $attributes); | |
394 | ||
395 | push @$cmd, $new_infile; | |
396 | ||
397 | debug("run " . join(' ', @$cmd)); | |
398 | ||
399 | system(@$cmd) == 0 or | |
400 | die "aciidoc error"; | |
401 | } | |
402 | } | |
403 | ||
404 | sub get_links { | |
405 | ||
406 | my $data = {}; | |
407 | ||
408 | foreach my $blockid (sort keys %{$fileinfo->{blockid_target}->{default}}) { | |
409 | my $link = $fileinfo->{blockid_target}->{default}->{$blockid}; | |
410 | my $reftitle = $fileinfo->{reftitle}->{default}->{$blockid}; | |
411 | my $reftext = $fileinfo->{reftext}->{default}->{$blockid}; | |
412 | die "internal error" if $link !~ m/^link:/; | |
413 | $link =~ s/^link://; | |
414 | ||
415 | my $file = $fileinfo->{blockid}->{default}->{$blockid}; | |
416 | die "internal error - no filename" if ! defined($file); | |
417 | my $title = $fileinfo->{titles}->{default}->{$file} || | |
418 | die "internal error - no title"; | |
419 | ||
420 | $data->{$blockid}->{title} = $title; | |
421 | $data->{$blockid}->{link} = $link; | |
422 | my $subtitle = $reftitle || $reftext; | |
423 | $data->{$blockid}->{subtitle} = $subtitle | |
424 | if $subtitle && ($title ne $subtitle); | |
425 | } | |
426 | ||
427 | return $data; | |
428 | } | |
429 | ||
430 | sub scan_extjs_file { | |
431 | my ($filename, $res_data) = @_; | |
432 | ||
433 | my $fh = IO::File->new($filename, "r") || | |
434 | die "unable to open '$filename' - $!\n"; | |
435 | ||
436 | debug("scan-extjs $filename"); | |
437 | ||
438 | while(defined(my $line = <$fh>)) { | |
439 | if ($line =~ m/\s+onlineHelp:\s*[\'\"](.*?)[\'\"]/) { | |
440 | my $blockid = $1; | |
441 | my $link = $fileinfo->{blockid_target}->{default}->{$blockid}; | |
442 | die "undefined blockid '$blockid' ($filename, line $.)\n" | |
443 | if !(defined($link) || defined($online_help_links->{$blockid})); | |
444 | ||
445 | $res_data->{$blockid} = 1; | |
446 | } | |
447 | } | |
448 | } | |
449 | ||
450 | if ($clicmd eq 'compile-wiki') { | |
451 | ||
452 | eval { compile_asciidoc('wiki'); }; | |
453 | my $err = $@; | |
454 | ||
455 | cleanup(); | |
456 | ||
457 | die $err if $err; | |
458 | ||
459 | } elsif ($clicmd eq 'compile-chapter') { | |
460 | ||
461 | eval { compile_asciidoc('default'); }; | |
462 | my $err = $@; | |
463 | ||
464 | cleanup(); | |
465 | ||
466 | die $err if $err; | |
467 | ||
468 | } elsif ($clicmd eq 'compile-man-html') { | |
469 | ||
470 | $man_target = 'html'; | |
471 | ||
472 | eval { compile_asciidoc('manvolnum'); }; | |
473 | my $err = $@; | |
474 | ||
475 | cleanup(); | |
476 | ||
477 | die $err if $err; | |
478 | ||
479 | } elsif ($clicmd eq 'compile-man-wiki') { | |
480 | ||
481 | $man_target = 'wiki'; | |
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') { | |
491 | ||
492 | eval { compile_asciidoc('manvolnum'); }; | |
493 | my $err = $@; | |
494 | ||
495 | cleanup(); | |
496 | ||
497 | die $err if $err; | |
498 | ||
499 | } elsif ($clicmd eq 'print-links') { | |
500 | ||
501 | my $outfile; | |
502 | ||
503 | GetOptions("outfile=s" => \$outfile, | |
504 | "verbose" => \$verbose) or | |
505 | die("Error in command line arguments\n"); | |
506 | ||
507 | scalar(@ARGV) == 0 or | |
508 | die "too many arguments...\n"; | |
509 | ||
510 | my $data = get_links(); | |
511 | ||
512 | my $res = to_json($data, { pretty => 1, canonical => 1 } ); | |
513 | ||
514 | if (defined($outfile)) { | |
515 | my $outfh = IO::File->new("$outfile", "w") or | |
516 | die "unable to open temporary file '$outfile'\n"; | |
517 | ||
518 | print $outfh $res; | |
519 | ||
520 | } else { | |
521 | ||
522 | print $res; | |
523 | } | |
524 | ||
525 | } elsif ($clicmd eq 'scan-extjs') { | |
526 | ||
527 | GetOptions("verbose" => \$verbose) or | |
528 | die("Error in command line arguments\n"); | |
529 | ||
530 | my $link_hash = {}; | |
531 | my $scanned_files = {}; | |
532 | while (my $filename = shift) { | |
533 | die "got strange file name '$filename'\n" | |
534 | if $filename !~ m/\.js$/; | |
535 | next if $scanned_files->{$filename}; | |
536 | ||
537 | scan_extjs_file($filename, $link_hash); | |
538 | $scanned_files->{$filename} = 1; | |
539 | } | |
540 | ||
541 | my $data = get_links(); | |
542 | ||
543 | my $res_data = {}; | |
544 | ||
545 | foreach my $blockid (keys %$link_hash) { | |
546 | $res_data->{$blockid} = $data->{$blockid} || $online_help_links->{$blockid} || | |
547 | die "internal error - no data for '$blockid'"; | |
548 | } | |
549 | ||
550 | my $data_str = to_json($res_data, { pretty => 1, canonical => 1 }); | |
551 | chomp $data_str; | |
552 | ||
553 | print "var pveOnlineHelpInfo = ${data_str};\n"; | |
554 | ||
555 | } else { | |
556 | ||
557 | die "unknown command '$clicmd'\n"; | |
558 | ||
559 | } | |
560 | ||
561 | ||
562 | exit 0; | |
563 | ||
564 | __END__ |