]> git.proxmox.com Git - pve-docs.git/blob - asciidoc-pve.in
582267db3bbc9c8afb8b4b950add1312ac2b9ee5
[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 = 'man';
37 my $env_stack = [];
38 my $env_skip = 0;
39
40 sub debug {
41 my $msg = shift;
42
43 return if !$verbose;
44
45 print STDERR "asciidoc-pve: $msg\n";
46 }
47
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
76 my $files_for_cleanup = [];
77
78 sub cleanup {
79
80 return if $keep_artifacts;
81
82 foreach my $file (@$files_for_cleanup) {
83 unlink $file;
84 }
85 }
86
87 sub replace_wiki_xref {
88 my ($blockid, $text) = @_;
89
90 my $link = $fileinfo->{blockid_target}->{wiki}->{$blockid};
91 my $reftext = $fileinfo->{reftext}->{wiki}->{$blockid};
92
93 die "unable to resolve wiki link (xref:$blockid)\n"
94 if !defined($link);
95
96 $text = $reftext if !length($text);
97
98 die "xref: no text for wiki link '$blockid'\n" if !$text;
99
100 return "$link\[$text\]";
101 }
102
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 ($man_target eq 'html') {
137 my $target = $link;
138 $target =~ s/\.adoc//;
139 $target .= ".$section";
140 return "link:${target}.html#${blockid}\[$text\]";
141 } elsif ($man_target eq 'man') {
142 my $command = $link;
143 $command =~ s/\.adoc//;
144 return "\*${text}\* (man \*${command}\*($section))";
145 } else {
146 die "internal error"
147 }
148 }
149
150 sub replace_xref {
151 my ($env, $blockid, $text) = @_;
152
153 if ($env eq 'wiki') {
154 return replace_wiki_xref($blockid, $text);
155 } elsif ($env eq 'manvolnum') {
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 }
163 } elsif ($env eq 'default') {
164 return replace_default_xref($blockid, $text);
165 } else {
166 die "internal error";
167 }
168 }
169
170 sub prepare_adoc_file {
171 my ($target_env, $filename, $attributes) = @_;
172
173 return $prepared_files->{$filename} if defined($prepared_files->{$filename});
174
175 debug("prepare $filename");
176
177 my $dirname = dirname($filename);
178 my $basename = basename($filename);
179
180 my $outfilename = "$dirname/${tmpprefix}$basename";
181
182 $prepared_files->{$filename} = $outfilename;
183
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
190 push @$files_for_cleanup, $outfilename;
191
192 while (defined (my $line = <$fh>)) {
193 chomp $line;
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);
214 debug("include $fn");
215 my $new_fn = prepare_adoc_file($target_env, $fn, $attributes);
216
217 print $outfh "include::${new_fn}$rest\n";
218 next;
219 }
220
221 # fix xrefs
222 $line =~ s/xref:([^\s\[\]]+)\[([^\]]*)\]/replace_xref(${target_env},$1,$2)/ge;
223
224 $line =~ s/<<([^\s,\[\]]+)(?:,(.*?))?>>/replace_xref(${target_env},$1,$2)/ge;
225
226 print $outfh $line . "\n";
227 }
228
229 return $outfilename;
230 }
231
232 sub compile_asciidoc {
233 my ($env) = @_;
234
235 my $outfile;
236
237 GetOptions ("outfile=s" => \$outfile,
238 "keep-artifacts" => \$keep_artifacts,
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
248 defined($fileinfo->{titles}->{$env}) ||
249 die "unknown environment '$env'";
250
251 my $title = $fileinfo->{titles}->{$env}->{$infile} or
252 die "unable to get title for '$infile'$env\n";
253
254 debug("compile $title");
255
256 my $leveloffset = 0;
257
258 my $doctype = $fileinfo->{doctype}->{$env}->{$infile};
259
260 die "unable to get document type for '$infile'\n"
261 if !defined($doctype);
262
263 $leveloffset = - $doctype;
264
265 my $date = `date`;
266 chomp $date;
267
268 my $attributes = {
269 $env => undef,
270 leveloffset => $leveloffset,
271 };
272
273 my $mansection = $fileinfo->{mansection}->{$env}->{$infile};
274
275 if ($env eq 'wiki') {
276 } elsif ($env eq 'manvolnum') {
277 die "undefined man section" if !defined($mansection);
278 $attributes->{manvolnum} = $mansection;
279 } elsif ($env eq 'default') {
280 die "$infile: wrong doctype\n" if $doctype != 0;
281 $attributes->{toc} = undef;
282 }
283
284 if (!defined($outfile)) {
285 $outfile = $infile;
286 $outfile =~ s/\.adoc$//;
287 if ($env eq 'manvolnum') {
288 if (($man_target eq 'html') || ($man_target eq 'wiki')) {
289 $outfile .= ".$mansection.html";
290 } else {
291 $outfile .= ".$mansection";
292 }
293 } else {
294 $outfile .= ".html";
295 }
296 }
297
298 if (($env eq 'manvolnum') && ($man_target eq 'man')) {
299
300 # asciidoc /etc/asciidoc/docbook-xsl/manpage.xsl skip REFERENCES
301 # section like footnotes, so we cannot use a2x.
302 # We use xmlto instead.
303
304 my $cmd = ['asciidoc', '-dmanpage', '-bdocbook', '-a', 'docinfo1'];
305
306 foreach my $key (keys %$attributes) {
307 my $value = $attributes->{$key};
308 if (defined($value)) {
309 push @$cmd, '-a', "$key=$value";
310 } else {
311 push @$cmd, '-a', $key;
312 }
313 }
314
315 push @$cmd, '--verbose' if $verbose;
316
317 my $tmpxmlfile = "${outfile}.xml.tmp";
318
319 push @$cmd, '--out-file', $tmpxmlfile;
320
321 push @$files_for_cleanup, $tmpxmlfile;
322
323 my $new_infile = prepare_adoc_file($env, $infile, $attributes);
324
325 push @$cmd, $new_infile;
326
327 debug("run " . join(' ', @$cmd));
328
329 system(@$cmd) == 0 or
330 die "aciidoc error";
331
332 $cmd = ['xmlto', 'man', $tmpxmlfile];
333
334 push @$cmd, '-v' if $verbose;
335
336 debug("run " . join(' ', @$cmd));
337
338 system(@$cmd) == 0 or
339 die "xmlto error";
340
341 } else {
342
343 $attributes->{icons} = undef;
344 $attributes->{'data-uri'} = undef;
345 $attributes->{revnumber} = $release;
346
347 my $cmd = ['asciidoc'];
348
349 push @$cmd, '-s' if ($env eq 'wiki') ||
350 (($env eq 'manvolnum') && ($man_target eq 'wiki'));
351
352 foreach my $key (keys %$attributes) {
353 my $value = $attributes->{$key};
354 if (defined($value)) {
355 push @$cmd, '-a', "$key=$value";
356 } else {
357 push @$cmd, '-a', $key;
358 }
359 }
360
361 push @$cmd, '--verbose' if $verbose;
362
363 push @$cmd, '--out-file', $outfile;
364
365 my $new_infile = prepare_adoc_file($env, $infile, $attributes);
366
367 push @$cmd, $new_infile;
368
369 debug("run " . join(' ', @$cmd));
370
371 system(@$cmd) == 0 or
372 die "aciidoc error";
373 }
374 }
375
376 if ($clicmd eq 'compile-wiki') {
377
378 eval { compile_asciidoc('wiki'); };
379 my $err = $@;
380
381 cleanup();
382
383 die $err if $err;
384
385 } elsif ($clicmd eq 'compile-chapter') {
386
387 eval { compile_asciidoc('default'); };
388 my $err = $@;
389
390 cleanup();
391
392 die $err if $err;
393
394 } elsif ($clicmd eq 'compile-man-html') {
395
396 $man_target = 'html';
397
398 eval { compile_asciidoc('manvolnum'); };
399 my $err = $@;
400
401 cleanup();
402
403 die $err if $err;
404
405 } elsif ($clicmd eq 'compile-man-wiki') {
406
407 $man_target = 'wiki';
408
409 eval { compile_asciidoc('manvolnum'); };
410 my $err = $@;
411
412 cleanup();
413
414 die $err if $err;
415
416 } elsif ($clicmd eq 'compile-man') {
417
418 eval { compile_asciidoc('manvolnum'); };
419 my $err = $@;
420
421 cleanup();
422
423 die $err if $err;
424
425 } else {
426
427 die "unknown command '$clicmd'\n";
428
429 }
430
431
432
433
434
435
436 exit 0;
437
438 __END__