]> git.proxmox.com Git - pve-docs.git/blob - asciidoc-pve.in
asciidoc-pve: cleanup temporary files
[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
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_html = 0;
37 my $env_stack = [];
38 my $env_skip = 0;
39
40 sub push_environment {
41 my ($env, $skip) = @_;
42
43 $skip = 1 if $env_skip;
44 $skip = 0 if !defined($skip);
45
46 push @$env_stack, [$env, $skip];
47
48 $env_skip = $skip;
49 }
50
51 sub pop_environment {
52 my ($env) = @_;
53
54 my $last_stack_entry = pop @$env_stack;
55 die "unable to pop env '$env'" if !defined($last_stack_entry);
56
57 my ($last_env, $skip) = @$last_stack_entry;
58 die "environment missmatch (${last_env} != $env)\n" if $last_env ne $env;
59
60 if (!scalar(@$env_stack)) {
61 $env_skip = 0;
62 } else {
63 my (undef, $skip) = @{$env_stack->[-1]};
64 $env_skip = $skip;
65 }
66 }
67
68 my $files_for_cleanup = [];
69
70 sub cleanup {
71
72 return if $keep_artifacts;
73
74 foreach my $file (@$files_for_cleanup) {
75 unlink $file;
76 }
77 }
78
79 sub replace_wiki_xref {
80 my ($blockid, $text) = @_;
81
82 my $link = $fileinfo->{blockid_target}->{wiki}->{$blockid};
83 my $reftext = $fileinfo->{reftext}->{wiki}->{$blockid};
84
85 die "unable to resolve wiki link (xref:$blockid)\n"
86 if !defined($link);
87
88 $text = $reftext if !length($text);
89
90 die "xref: no text for wiki link '$blockid'\n" if !$text;
91
92 return "$link\[$text\]";
93 }
94
95 sub replace_default_xref {
96 my ($blockid, $text) = @_;
97
98 my $link = $fileinfo->{blockid_target}->{default}->{$blockid};
99 my $reftext = $fileinfo->{reftext}->{default}->{$blockid};
100
101 die "unable to resolve chapter link (xref:$blockid)\n"
102 if !defined($link);
103
104 $text = $reftext if !length($text);
105
106 die "xref: no text for chapter link '$blockid'\n" if !$text;
107
108 return "$link\[$text\]";
109 }
110
111 sub replace_man_xref {
112 my ($blockid, $text) = @_;
113
114 my $link = $fileinfo->{blockid_target}->{manvolnum}->{$blockid};
115 my $reftext = $fileinfo->{reftext}->{manvolnum}->{$blockid};
116
117 die "unable to resolve man page link (xref:$blockid)\n"
118 if !defined($link);
119
120 $text = $reftext if !length($text);
121
122 die "xref: no text for man page link '$blockid'\n" if !$text;
123
124 my $section = $fileinfo->{mansection}->{manvolnum}->{$link};
125 die "link target is not a manual page" if !defined($section);
126
127
128 if (0 && $man_target_html) {
129 my $target = $link;
130 $target =~ s/\.adoc//;
131 $target .= ".$section";
132 return "link:${target}.html#${blockid}\[$text\]";
133 } else {
134 my $command = $link;
135 $command =~ s/\.adoc//;
136 return "\*${text}\* (man \*${command}\*($section))";
137 }
138 }
139
140 sub replace_xref {
141 my ($env, $blockid, $text) = @_;
142
143 if ($env eq 'wiki') {
144 return replace_wiki_xref($blockid, $text);
145 } elsif ($env eq 'manvolnum') {
146 return replace_man_xref($blockid, $text);
147 } elsif ($env eq 'default') {
148 return replace_default_xref($blockid, $text);
149 } else {
150 die "internal error";
151 }
152 }
153
154 sub prepare_adoc_file {
155 my ($target_env, $filename, $attributes) = @_;
156
157 return $prepared_files->{$filename} if defined($prepared_files->{$filename});
158
159 print "PREPARE $filename\n";
160
161 my $dirname = dirname($filename);
162 my $basename = basename($filename);
163
164 my $outfilename = "$dirname/${tmpprefix}$basename";
165
166 $prepared_files->{$filename} = $outfilename;
167
168 my $fh = IO::File->new("$filename", "r") or
169 die "unable to open file '$filename' - $!\n";
170
171 my $outfh = IO::File->new("$outfilename", "w") or
172 die "unable to open temporary file '$outfilename'\n";
173
174 push @$files_for_cleanup, $outfilename;
175
176 while (defined (my $line = <$fh>)) {
177 chomp $line;
178 if ($line =~ m/^if(n?)def::(\S+)\[(.*)\]\s*$/) {
179 my ($not, $env, $text) = ($1, $2, $3);
180 die "unsuported ifdef usage - implement me" if $text;
181
182 my $skip = !exists($attributes->{$env}) ? 1 : 0;
183 $skip = ($skip ? 0 : 1 ) if $not;
184
185 push_environment($env, $skip);
186 next;
187 } elsif ($line =~ m/^endif::(\S+)\[(.*)\]\s*$/) {
188 my ($env, $text) = ($1, $2);
189 die "unsuported ifdef usage - implement me" if $text;
190 pop_environment($env);
191 next;
192 }
193
194 next if $env_skip;
195
196 if ($line =~ m/^include::(\S+)(\[.*\]\s*)$/) {
197 my ($fn, $rest) = ($1, $2);
198 print "INCLUDE: $fn\n";
199 my $new_fn = prepare_adoc_file($target_env, $fn, $attributes);
200
201 print $outfh "include::${new_fn}$rest\n";
202 next;
203 }
204
205 # fix xrefs
206 $line =~ s/xref:([^\s\[\]]+)\[([^\]]*)\]/replace_xref(${target_env},$1,$2)/ge;
207
208 $line =~ s/<<([^\s,\[\]]+)(?:,(.*?))?>>/replace_xref(${target_env},$1,$2)/ge;
209
210 print $outfh $line . "\n";
211 }
212
213 return $outfilename;
214 }
215
216 sub compile_asciidoc {
217 my ($env) = @_;
218
219 my $outfile;
220
221 GetOptions ("outfile=s" => \$outfile,
222 "keep-artifacts" => \$keep_artifacts,
223 "verbose" => \$verbose) or
224 die("Error in command line arguments\n");
225
226 my $infile = shift(@ARGV) or
227 die "no input file specified\n";
228
229 scalar(@ARGV) == 0 or
230 die "too many arguments...\n";
231
232 defined($fileinfo->{titles}->{$env}) ||
233 die "unknown environment '$env'";
234
235 my $title = $fileinfo->{titles}->{$env}->{$infile} or
236 die "unable to get title for '$infile'$env\n";
237
238 print "compile: $title\n";
239
240 my $leveloffset = 0;
241
242 my $doctype = $fileinfo->{doctype}->{$env}->{$infile};
243
244 die "unable to get document type for '$infile'\n"
245 if !defined($doctype);
246
247 $leveloffset = - $doctype;
248
249 my $date = `date`;
250 chomp $date;
251
252 my $attributes = {
253 $env => undef,
254 leveloffset => $leveloffset,
255 };
256
257 my $mansection = $fileinfo->{mansection}->{$env}->{$infile};
258
259 if ($env eq 'wiki') {
260 } elsif ($env eq 'manvolnum') {
261 die "undefined man section" if !defined($mansection);
262 $attributes->{manvolnum} = $mansection;
263 } elsif ($env eq 'default') {
264 die "$infile: wrong doctype\n" if $doctype != 0;
265 $attributes->{toc} = undef;
266 }
267
268 if (!defined($outfile)) {
269 $outfile = $infile;
270 $outfile =~ s/\.adoc$//;
271 if ($env eq 'manvolnum') {
272 if ($man_target_html) {
273 $outfile .= ".$mansection.html";
274 } else {
275 $outfile .= ".$mansection";
276 }
277 } else {
278 $outfile .= ".html";
279 }
280 }
281
282 if (($env eq 'manvolnum') && !$man_target_html) {
283
284 # asciidoc /etc/asciidoc/docbook-xsl/manpage.xsl skip REFERENCES
285 # section like footnotes, so we cannot use a2x.
286 # We use xmlto instead.
287
288 my $cmd = ['asciidoc', '-dmanpage', '-bdocbook', '-a', 'docinfo1'];
289
290 foreach my $key (keys %$attributes) {
291 my $value = $attributes->{$key};
292 if (defined($value)) {
293 push @$cmd, '-a', "$key=$value";
294 } else {
295 push @$cmd, '-a', $key;
296 }
297 }
298
299 push @$cmd, '--verbose' if $verbose;
300
301 my $tmpxmlfile = "${outfile}.xml.tmp";
302
303 push @$cmd, '--out-file', $tmpxmlfile;
304
305 push @$files_for_cleanup, $tmpxmlfile;
306
307 my $new_infile = prepare_adoc_file($env, $infile, $attributes);
308
309 push @$cmd, $new_infile;
310
311 print "RUN " . join(' ', @$cmd) . "\n";
312
313 system(@$cmd) == 0 or
314 die "aciidoc error";
315
316 $cmd = ['xmlto', 'man', $tmpxmlfile];
317
318 push @$cmd, '-v' if $verbose;
319
320 print "RUN " . join(' ', @$cmd) . "\n";
321
322 system(@$cmd) == 0 or
323 die "xmlto error";
324
325 } else {
326
327 $attributes->{icons} = undef;
328 $attributes->{'data-uri'} = undef;
329 $attributes->{revnumber} = $release;
330
331 my $cmd = ['asciidoc'];
332
333 push @$cmd, '-s' if $env eq 'wiki';
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 push @$cmd, '--out-file', $outfile;
347
348 my $new_infile = prepare_adoc_file($env, $infile, $attributes);
349
350 push @$cmd, $new_infile;
351
352 print "RUN " . join(' ', @$cmd) . "\n";
353
354 system(@$cmd) == 0 or
355 die "aciidoc error";
356 }
357 }
358
359 if ($clicmd eq 'compile-wiki') {
360
361 eval { compile_asciidoc('wiki'); };
362 my $err = $@;
363
364 cleanup();
365
366 die $err if $err;
367
368 } elsif ($clicmd eq 'compile-chapter') {
369
370 eval { compile_asciidoc('default'); };
371 my $err = $@;
372
373 cleanup();
374
375 die $err if $err;
376
377 } elsif ($clicmd eq 'compile-man-html') {
378
379 $man_target_html = 1;
380
381 eval { compile_asciidoc('manvolnum'); };
382 my $err = $@;
383
384 cleanup();
385
386 die $err if $err;
387
388 } elsif ($clicmd eq 'compile-man') {
389
390 eval { compile_asciidoc('manvolnum'); };
391 my $err = $@;
392
393 cleanup();
394
395 die $err if $err;
396
397 } else {
398
399 die "unknown command '$clicmd'\n";
400
401 }
402
403
404
405
406
407
408 exit 0;
409
410 __END__