]>
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 | ||
c38115e9 | 36 | my $man_target_html = 0; |
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 | ||
136 | if (0 && $man_target_html) { | |
137 | my $target = $link; | |
138 | $target =~ s/\.adoc//; | |
139 | $target .= ".$section"; | |
140 | return "link:${target}.html#${blockid}\[$text\]"; | |
141 | } else { | |
142 | my $command = $link; | |
143 | $command =~ s/\.adoc//; | |
144 | return "\*${text}\* (man \*${command}\*($section))"; | |
145 | } | |
146 | } | |
147 | ||
a22a4a80 DM |
148 | sub replace_xref { |
149 | my ($env, $blockid, $text) = @_; | |
835dd63b | 150 | |
a22a4a80 DM |
151 | if ($env eq 'wiki') { |
152 | return replace_wiki_xref($blockid, $text); | |
c38115e9 DM |
153 | } elsif ($env eq 'manvolnum') { |
154 | return replace_man_xref($blockid, $text); | |
155 | } elsif ($env eq 'default') { | |
156 | return replace_default_xref($blockid, $text); | |
a22a4a80 | 157 | } else { |
c38115e9 | 158 | die "internal error"; |
a22a4a80 DM |
159 | } |
160 | } | |
161 | ||
162 | sub prepare_adoc_file { | |
163 | my ($target_env, $filename, $attributes) = @_; | |
508e0012 | 164 | |
c38115e9 | 165 | return $prepared_files->{$filename} if defined($prepared_files->{$filename}); |
835dd63b | 166 | |
94a60df7 | 167 | debug("prepare $filename"); |
835dd63b | 168 | |
835dd63b DM |
169 | my $dirname = dirname($filename); |
170 | my $basename = basename($filename); | |
171 | ||
172 | my $outfilename = "$dirname/${tmpprefix}$basename"; | |
173 | ||
c38115e9 DM |
174 | $prepared_files->{$filename} = $outfilename; |
175 | ||
835dd63b DM |
176 | my $fh = IO::File->new("$filename", "r") or |
177 | die "unable to open file '$filename' - $!\n"; | |
178 | ||
179 | my $outfh = IO::File->new("$outfilename", "w") or | |
180 | die "unable to open temporary file '$outfilename'\n"; | |
181 | ||
034b35c8 DM |
182 | push @$files_for_cleanup, $outfilename; |
183 | ||
835dd63b | 184 | while (defined (my $line = <$fh>)) { |
c38115e9 | 185 | chomp $line; |
835dd63b DM |
186 | if ($line =~ m/^if(n?)def::(\S+)\[(.*)\]\s*$/) { |
187 | my ($not, $env, $text) = ($1, $2, $3); | |
188 | die "unsuported ifdef usage - implement me" if $text; | |
189 | ||
190 | my $skip = !exists($attributes->{$env}) ? 1 : 0; | |
191 | $skip = ($skip ? 0 : 1 ) if $not; | |
192 | ||
193 | push_environment($env, $skip); | |
194 | next; | |
195 | } elsif ($line =~ m/^endif::(\S+)\[(.*)\]\s*$/) { | |
196 | my ($env, $text) = ($1, $2); | |
197 | die "unsuported ifdef usage - implement me" if $text; | |
198 | pop_environment($env); | |
199 | next; | |
200 | } | |
201 | ||
202 | next if $env_skip; | |
203 | ||
204 | if ($line =~ m/^include::(\S+)(\[.*\]\s*)$/) { | |
205 | my ($fn, $rest) = ($1, $2); | |
94a60df7 | 206 | debug("include $fn"); |
a22a4a80 | 207 | my $new_fn = prepare_adoc_file($target_env, $fn, $attributes); |
c38115e9 DM |
208 | |
209 | print $outfh "include::${new_fn}$rest\n"; | |
210 | next; | |
835dd63b DM |
211 | } |
212 | ||
213 | # fix xrefs | |
a22a4a80 | 214 | $line =~ s/xref:([^\s\[\]]+)\[([^\]]*)\]/replace_xref(${target_env},$1,$2)/ge; |
835dd63b | 215 | |
a22a4a80 | 216 | $line =~ s/<<([^\s,\[\]]+)(?:,(.*?))?>>/replace_xref(${target_env},$1,$2)/ge; |
835dd63b | 217 | |
c38115e9 | 218 | print $outfh $line . "\n"; |
835dd63b DM |
219 | } |
220 | ||
221 | return $outfilename; | |
222 | } | |
223 | ||
a22a4a80 DM |
224 | sub compile_asciidoc { |
225 | my ($env) = @_; | |
835dd63b | 226 | |
835dd63b | 227 | my $outfile; |
835dd63b DM |
228 | |
229 | GetOptions ("outfile=s" => \$outfile, | |
034b35c8 | 230 | "keep-artifacts" => \$keep_artifacts, |
835dd63b DM |
231 | "verbose" => \$verbose) or |
232 | die("Error in command line arguments\n"); | |
233 | ||
234 | my $infile = shift(@ARGV) or | |
235 | die "no input file specified\n"; | |
236 | ||
237 | scalar(@ARGV) == 0 or | |
238 | die "too many arguments...\n"; | |
239 | ||
c38115e9 | 240 | defined($fileinfo->{titles}->{$env}) || |
a22a4a80 | 241 | die "unknown environment '$env'"; |
a22a4a80 | 242 | |
835dd63b | 243 | my $title = $fileinfo->{titles}->{$env}->{$infile} or |
c38115e9 | 244 | die "unable to get title for '$infile'$env\n"; |
835dd63b | 245 | |
94a60df7 | 246 | debug("compile $title"); |
835dd63b DM |
247 | |
248 | my $leveloffset = 0; | |
249 | ||
c38115e9 DM |
250 | my $doctype = $fileinfo->{doctype}->{$env}->{$infile}; |
251 | ||
252 | die "unable to get document type for '$infile'\n" | |
253 | if !defined($doctype); | |
254 | ||
b489b02c | 255 | $leveloffset = - $doctype; |
835dd63b DM |
256 | |
257 | my $date = `date`; | |
258 | chomp $date; | |
259 | ||
260 | my $attributes = { | |
261 | $env => undef, | |
835dd63b | 262 | leveloffset => $leveloffset, |
835dd63b DM |
263 | }; |
264 | ||
c38115e9 DM |
265 | my $mansection = $fileinfo->{mansection}->{$env}->{$infile}; |
266 | ||
267 | if ($env eq 'wiki') { | |
268 | } elsif ($env eq 'manvolnum') { | |
269 | die "undefined man section" if !defined($mansection); | |
270 | $attributes->{manvolnum} = $mansection; | |
271 | } elsif ($env eq 'default') { | |
272 | die "$infile: wrong doctype\n" if $doctype != 0; | |
a22a4a80 DM |
273 | $attributes->{toc} = undef; |
274 | } | |
275 | ||
c38115e9 DM |
276 | if (!defined($outfile)) { |
277 | $outfile = $infile; | |
278 | $outfile =~ s/\.adoc$//; | |
279 | if ($env eq 'manvolnum') { | |
280 | if ($man_target_html) { | |
281 | $outfile .= ".$mansection.html"; | |
282 | } else { | |
283 | $outfile .= ".$mansection"; | |
284 | } | |
835dd63b | 285 | } else { |
c38115e9 | 286 | $outfile .= ".html"; |
835dd63b DM |
287 | } |
288 | } | |
289 | ||
c38115e9 | 290 | if (($env eq 'manvolnum') && !$man_target_html) { |
835dd63b | 291 | |
c38115e9 DM |
292 | # asciidoc /etc/asciidoc/docbook-xsl/manpage.xsl skip REFERENCES |
293 | # section like footnotes, so we cannot use a2x. | |
294 | # We use xmlto instead. | |
835dd63b | 295 | |
c38115e9 | 296 | my $cmd = ['asciidoc', '-dmanpage', '-bdocbook', '-a', 'docinfo1']; |
835dd63b | 297 | |
c38115e9 DM |
298 | foreach my $key (keys %$attributes) { |
299 | my $value = $attributes->{$key}; | |
300 | if (defined($value)) { | |
301 | push @$cmd, '-a', "$key=$value"; | |
302 | } else { | |
303 | push @$cmd, '-a', $key; | |
304 | } | |
305 | } | |
835dd63b | 306 | |
c38115e9 | 307 | push @$cmd, '--verbose' if $verbose; |
835dd63b | 308 | |
c38115e9 | 309 | my $tmpxmlfile = "${outfile}.xml.tmp"; |
835dd63b | 310 | |
c38115e9 DM |
311 | push @$cmd, '--out-file', $tmpxmlfile; |
312 | ||
034b35c8 DM |
313 | push @$files_for_cleanup, $tmpxmlfile; |
314 | ||
c38115e9 DM |
315 | my $new_infile = prepare_adoc_file($env, $infile, $attributes); |
316 | ||
317 | push @$cmd, $new_infile; | |
318 | ||
94a60df7 | 319 | debug("run " . join(' ', @$cmd)); |
c38115e9 DM |
320 | |
321 | system(@$cmd) == 0 or | |
322 | die "aciidoc error"; | |
323 | ||
324 | $cmd = ['xmlto', 'man', $tmpxmlfile]; | |
325 | ||
326 | push @$cmd, '-v' if $verbose; | |
327 | ||
94a60df7 | 328 | debug("run " . join(' ', @$cmd)); |
c38115e9 DM |
329 | |
330 | system(@$cmd) == 0 or | |
331 | die "xmlto error"; | |
332 | ||
333 | } else { | |
835dd63b | 334 | |
c38115e9 DM |
335 | $attributes->{icons} = undef; |
336 | $attributes->{'data-uri'} = undef; | |
337 | $attributes->{revnumber} = $release; | |
338 | ||
339 | my $cmd = ['asciidoc']; | |
340 | ||
341 | push @$cmd, '-s' if $env eq 'wiki'; | |
342 | ||
343 | foreach my $key (keys %$attributes) { | |
344 | my $value = $attributes->{$key}; | |
345 | if (defined($value)) { | |
346 | push @$cmd, '-a', "$key=$value"; | |
347 | } else { | |
348 | push @$cmd, '-a', $key; | |
349 | } | |
350 | } | |
351 | ||
352 | push @$cmd, '--verbose' if $verbose; | |
353 | ||
354 | push @$cmd, '--out-file', $outfile; | |
355 | ||
356 | my $new_infile = prepare_adoc_file($env, $infile, $attributes); | |
357 | ||
358 | push @$cmd, $new_infile; | |
359 | ||
94a60df7 | 360 | debug("run " . join(' ', @$cmd)); |
c38115e9 DM |
361 | |
362 | system(@$cmd) == 0 or | |
363 | die "aciidoc error"; | |
364 | } | |
365 | } | |
a22a4a80 | 366 | |
b489b02c | 367 | if ($clicmd eq 'compile-wiki') { |
835dd63b | 368 | |
a22a4a80 | 369 | eval { compile_asciidoc('wiki'); }; |
835dd63b DM |
370 | my $err = $@; |
371 | ||
17d8be0c | 372 | cleanup(); |
835dd63b DM |
373 | |
374 | die $err if $err; | |
375 | ||
c38115e9 DM |
376 | } elsif ($clicmd eq 'compile-chapter') { |
377 | ||
378 | eval { compile_asciidoc('default'); }; | |
379 | my $err = $@; | |
380 | ||
381 | cleanup(); | |
382 | ||
383 | die $err if $err; | |
384 | ||
385 | } elsif ($clicmd eq 'compile-man-html') { | |
386 | ||
387 | $man_target_html = 1; | |
388 | ||
389 | eval { compile_asciidoc('manvolnum'); }; | |
390 | my $err = $@; | |
391 | ||
392 | cleanup(); | |
393 | ||
394 | die $err if $err; | |
395 | ||
396 | } elsif ($clicmd eq 'compile-man') { | |
397 | ||
398 | eval { compile_asciidoc('manvolnum'); }; | |
399 | my $err = $@; | |
400 | ||
401 | cleanup(); | |
402 | ||
403 | die $err if $err; | |
404 | ||
835dd63b DM |
405 | } else { |
406 | ||
407 | die "unknown command '$clicmd'\n"; | |
408 | ||
409 | } | |
410 | ||
411 | ||
412 | ||
413 | ||
414 | ||
415 | ||
416 | exit 0; | |
aa99b349 DM |
417 | |
418 | __END__ |