]>
Commit | Line | Data |
---|---|---|
aa99b349 DM |
1 | #!/usr/bin/perl |
2 | ||
3 | use strict; | |
4 | use warnings; | |
835dd63b DM |
5 | use Getopt::Long; |
6 | use File::Path; | |
7 | use File::Basename; | |
8 | use IO::File; | |
9 | ||
aa99b349 DM |
10 | use JSON; |
11 | ||
034b35c8 DM |
12 | my $verbose; |
13 | my $keep_artifacts; | |
14 | ||
835dd63b DM |
15 | my $release = '@RELEASE@'; |
16 | ||
17 | my $clicmd = shift or | |
18 | die "no command specified\n"; | |
19 | ||
aa99b349 DM |
20 | my $data_str = ""; |
21 | while (<main::DATA>) { $data_str .= $_; } | |
22 | ||
23 | my $fileinfo = decode_json($data_str); | |
aa99b349 | 24 | |
835dd63b DM |
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 | ||
4ff677c9 | 36 | my $man_target = 'man'; |
835dd63b DM |
37 | my $env_stack = []; |
38 | my $env_skip = 0; | |
39 | ||
a297b96e DM |
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 | ||
94a60df7 DM |
55 | sub debug { |
56 | my $msg = shift; | |
57 | ||
58 | return if !$verbose; | |
59 | ||
60 | print STDERR "asciidoc-pve: $msg\n"; | |
61 | } | |
62 | ||
835dd63b DM |
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 | ||
034b35c8 DM |
91 | my $files_for_cleanup = []; |
92 | ||
17d8be0c DM |
93 | sub cleanup { |
94 | ||
034b35c8 DM |
95 | return if $keep_artifacts; |
96 | ||
97 | foreach my $file (@$files_for_cleanup) { | |
98 | unlink $file; | |
99 | } | |
17d8be0c DM |
100 | } |
101 | ||
835dd63b DM |
102 | sub replace_wiki_xref { |
103 | my ($blockid, $text) = @_; | |
104 | ||
105 | my $link = $fileinfo->{blockid_target}->{wiki}->{$blockid}; | |
45930831 | 106 | my $reftext = $fileinfo->{reftext}->{wiki}->{$blockid}; |
835dd63b DM |
107 | |
108 | die "unable to resolve wiki link (xref:$blockid)\n" | |
109 | if !defined($link); | |
110 | ||
45930831 DM |
111 | $text = $reftext if !length($text); |
112 | ||
c38115e9 | 113 | die "xref: no text for wiki link '$blockid'\n" if !$text; |
508e0012 | 114 | |
835dd63b DM |
115 | return "$link\[$text\]"; |
116 | } | |
117 | ||
c38115e9 DM |
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 | ||
4ff677c9 | 151 | if ($man_target eq 'html') { |
c38115e9 DM |
152 | my $target = $link; |
153 | $target =~ s/\.adoc//; | |
154 | $target .= ".$section"; | |
155 | return "link:${target}.html#${blockid}\[$text\]"; | |
4ff677c9 | 156 | } elsif ($man_target eq 'man') { |
c38115e9 DM |
157 | my $command = $link; |
158 | $command =~ s/\.adoc//; | |
159 | return "\*${text}\* (man \*${command}\*($section))"; | |
4ff677c9 DM |
160 | } else { |
161 | die "internal error" | |
c38115e9 DM |
162 | } |
163 | } | |
164 | ||
a22a4a80 DM |
165 | sub replace_xref { |
166 | my ($env, $blockid, $text) = @_; | |
835dd63b | 167 | |
a22a4a80 DM |
168 | if ($env eq 'wiki') { |
169 | return replace_wiki_xref($blockid, $text); | |
c38115e9 | 170 | } elsif ($env eq 'manvolnum') { |
4ff677c9 DM |
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 | } | |
c38115e9 DM |
178 | } elsif ($env eq 'default') { |
179 | return replace_default_xref($blockid, $text); | |
a22a4a80 | 180 | } else { |
c38115e9 | 181 | die "internal error"; |
a22a4a80 DM |
182 | } |
183 | } | |
184 | ||
185 | sub prepare_adoc_file { | |
186 | my ($target_env, $filename, $attributes) = @_; | |
508e0012 | 187 | |
c38115e9 | 188 | return $prepared_files->{$filename} if defined($prepared_files->{$filename}); |
835dd63b | 189 | |
94a60df7 | 190 | debug("prepare $filename"); |
835dd63b | 191 | |
835dd63b DM |
192 | my $dirname = dirname($filename); |
193 | my $basename = basename($filename); | |
194 | ||
195 | my $outfilename = "$dirname/${tmpprefix}$basename"; | |
196 | ||
c38115e9 DM |
197 | $prepared_files->{$filename} = $outfilename; |
198 | ||
835dd63b DM |
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 | ||
034b35c8 DM |
205 | push @$files_for_cleanup, $outfilename; |
206 | ||
835dd63b | 207 | while (defined (my $line = <$fh>)) { |
c38115e9 | 208 | chomp $line; |
835dd63b DM |
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); | |
94a60df7 | 229 | debug("include $fn"); |
a22a4a80 | 230 | my $new_fn = prepare_adoc_file($target_env, $fn, $attributes); |
c38115e9 DM |
231 | |
232 | print $outfh "include::${new_fn}$rest\n"; | |
233 | next; | |
835dd63b DM |
234 | } |
235 | ||
236 | # fix xrefs | |
a22a4a80 | 237 | $line =~ s/xref:([^\s\[\]]+)\[([^\]]*)\]/replace_xref(${target_env},$1,$2)/ge; |
835dd63b | 238 | |
a22a4a80 | 239 | $line =~ s/<<([^\s,\[\]]+)(?:,(.*?))?>>/replace_xref(${target_env},$1,$2)/ge; |
835dd63b | 240 | |
c38115e9 | 241 | print $outfh $line . "\n"; |
835dd63b DM |
242 | } |
243 | ||
244 | return $outfilename; | |
245 | } | |
246 | ||
a22a4a80 DM |
247 | sub compile_asciidoc { |
248 | my ($env) = @_; | |
835dd63b | 249 | |
835dd63b | 250 | my $outfile; |
835dd63b DM |
251 | |
252 | GetOptions ("outfile=s" => \$outfile, | |
034b35c8 | 253 | "keep-artifacts" => \$keep_artifacts, |
835dd63b DM |
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 | ||
c38115e9 | 263 | defined($fileinfo->{titles}->{$env}) || |
a22a4a80 | 264 | die "unknown environment '$env'"; |
a22a4a80 | 265 | |
835dd63b | 266 | my $title = $fileinfo->{titles}->{$env}->{$infile} or |
c38115e9 | 267 | die "unable to get title for '$infile'$env\n"; |
835dd63b | 268 | |
94a60df7 | 269 | debug("compile $title"); |
835dd63b DM |
270 | |
271 | my $leveloffset = 0; | |
272 | ||
c38115e9 DM |
273 | my $doctype = $fileinfo->{doctype}->{$env}->{$infile}; |
274 | ||
275 | die "unable to get document type for '$infile'\n" | |
276 | if !defined($doctype); | |
277 | ||
b489b02c | 278 | $leveloffset = - $doctype; |
835dd63b DM |
279 | |
280 | my $date = `date`; | |
281 | chomp $date; | |
282 | ||
283 | my $attributes = { | |
284 | $env => undef, | |
835dd63b | 285 | leveloffset => $leveloffset, |
b2a47ab2 DM |
286 | revnumber => $release, |
287 | revdate => $date, | |
835dd63b DM |
288 | }; |
289 | ||
c38115e9 DM |
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; | |
a22a4a80 DM |
298 | $attributes->{toc} = undef; |
299 | } | |
300 | ||
c38115e9 DM |
301 | if (!defined($outfile)) { |
302 | $outfile = $infile; | |
303 | $outfile =~ s/\.adoc$//; | |
304 | if ($env eq 'manvolnum') { | |
4ff677c9 | 305 | if (($man_target eq 'html') || ($man_target eq 'wiki')) { |
c38115e9 DM |
306 | $outfile .= ".$mansection.html"; |
307 | } else { | |
308 | $outfile .= ".$mansection"; | |
309 | } | |
835dd63b | 310 | } else { |
c38115e9 | 311 | $outfile .= ".html"; |
835dd63b DM |
312 | } |
313 | } | |
314 | ||
4ff677c9 | 315 | if (($env eq 'manvolnum') && ($man_target eq 'man')) { |
835dd63b | 316 | |
c38115e9 DM |
317 | # asciidoc /etc/asciidoc/docbook-xsl/manpage.xsl skip REFERENCES |
318 | # section like footnotes, so we cannot use a2x. | |
319 | # We use xmlto instead. | |
835dd63b | 320 | |
c38115e9 | 321 | my $cmd = ['asciidoc', '-dmanpage', '-bdocbook', '-a', 'docinfo1']; |
835dd63b | 322 | |
c38115e9 DM |
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 | } | |
835dd63b | 331 | |
c38115e9 | 332 | push @$cmd, '--verbose' if $verbose; |
835dd63b | 333 | |
c38115e9 | 334 | my $tmpxmlfile = "${outfile}.xml.tmp"; |
835dd63b | 335 | |
c38115e9 DM |
336 | push @$cmd, '--out-file', $tmpxmlfile; |
337 | ||
034b35c8 DM |
338 | push @$files_for_cleanup, $tmpxmlfile; |
339 | ||
c38115e9 DM |
340 | my $new_infile = prepare_adoc_file($env, $infile, $attributes); |
341 | ||
342 | push @$cmd, $new_infile; | |
343 | ||
94a60df7 | 344 | debug("run " . join(' ', @$cmd)); |
c38115e9 DM |
345 | |
346 | system(@$cmd) == 0 or | |
347 | die "aciidoc error"; | |
348 | ||
349 | $cmd = ['xmlto', 'man', $tmpxmlfile]; | |
350 | ||
351 | push @$cmd, '-v' if $verbose; | |
352 | ||
94a60df7 | 353 | debug("run " . join(' ', @$cmd)); |
c38115e9 DM |
354 | |
355 | system(@$cmd) == 0 or | |
356 | die "xmlto error"; | |
357 | ||
358 | } else { | |
835dd63b | 359 | |
c38115e9 DM |
360 | $attributes->{icons} = undef; |
361 | $attributes->{'data-uri'} = undef; | |
c38115e9 DM |
362 | |
363 | my $cmd = ['asciidoc']; | |
364 | ||
4ff677c9 DM |
365 | push @$cmd, '-s' if ($env eq 'wiki') || |
366 | (($env eq 'manvolnum') && ($man_target eq 'wiki')); | |
c38115e9 DM |
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 | ||
94a60df7 | 385 | debug("run " . join(' ', @$cmd)); |
c38115e9 DM |
386 | |
387 | system(@$cmd) == 0 or | |
388 | die "aciidoc error"; | |
389 | } | |
390 | } | |
a22a4a80 | 391 | |
a297b96e DM |
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 | ||
b489b02c | 438 | if ($clicmd eq 'compile-wiki') { |
835dd63b | 439 | |
a22a4a80 | 440 | eval { compile_asciidoc('wiki'); }; |
835dd63b DM |
441 | my $err = $@; |
442 | ||
17d8be0c | 443 | cleanup(); |
835dd63b DM |
444 | |
445 | die $err if $err; | |
446 | ||
c38115e9 DM |
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 | ||
4ff677c9 DM |
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'; | |
c38115e9 DM |
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 | ||
a297b96e | 487 | } elsif ($clicmd eq 'print-links') { |
dd284d25 DM |
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 | ||
a297b96e | 498 | my $data = get_links(); |
dd284d25 DM |
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 | ||
a297b96e | 513 | } elsif ($clicmd eq 'scan-extjs') { |
835dd63b | 514 | |
a297b96e DM |
515 | GetOptions("verbose" => \$verbose) or |
516 | die("Error in command line arguments\n"); | |
835dd63b | 517 | |
a297b96e DM |
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 | } | |
835dd63b | 528 | |
a297b96e | 529 | my $data = get_links(); |
835dd63b | 530 | |
a297b96e | 531 | my $res_data = {}; |
835dd63b | 532 | |
a297b96e DM |
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"; | |
835dd63b | 546 | |
a297b96e | 547 | } |
835dd63b DM |
548 | |
549 | ||
550 | exit 0; | |
aa99b349 DM |
551 | |
552 | __END__ |