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