]>
git.proxmox.com Git - pve-docs.git/blob - asciidoc-pve.in
15 my $release = '@RELEASE@';
18 die "no command specified\n";
21 while (<main
::DATA
>) { $data_str .= $_; }
23 my $fileinfo = decode_json
($data_str);
25 my $tmpprefix = ".asciidoc-pve-tmp_";
27 my $adoc_source_dir = "/usr/share/pve-doc-generator";
29 # inside pve-docs source dir?
30 if (-f
"attributes.txt" && -f
"pve-admin-guide.adoc") {
31 $adoc_source_dir = "."
34 my $prepared_files = {};
36 my $man_target = 'man';
40 my $online_help_links = {
41 'pve_service_daemons' => {
42 link => '/pve-docs/index.html#_service_daemons',
43 title
=> 'Service Daemons',
45 'pve_documentation_index' => {
46 link => '/pve-docs/index.html',
47 title
=> 'Proxmox VE Documentation Index',
49 'pve_admin_guide' => {
50 link => '/pve-docs/pve-admin-guide.html',
51 title
=> 'Proxmox VE Administration Guide',
60 print STDERR
"asciidoc-pve: $msg\n";
63 sub push_environment
{
64 my ($env, $skip) = @_;
66 $skip = 1 if $env_skip;
67 $skip = 0 if !defined($skip);
69 push @$env_stack, [$env, $skip];
77 my $last_stack_entry = pop @$env_stack;
78 die "unable to pop env '$env'" if !defined($last_stack_entry);
80 my ($last_env, $skip) = @$last_stack_entry;
81 die "environment missmatch (${last_env} != $env)\n" if $last_env ne $env;
83 if (!scalar(@$env_stack)) {
86 my (undef, $skip) = @{$env_stack->[-1]};
91 my $files_for_cleanup = [];
95 return if $keep_artifacts;
97 foreach my $file (@$files_for_cleanup) {
102 sub replace_wiki_xref
{
103 my ($blockid, $text) = @_;
105 my $link = $fileinfo->{blockid_target
}->{wiki
}->{$blockid};
106 my $reftext = $fileinfo->{reftext
}->{wiki
}->{$blockid};
108 die "unable to resolve wiki link (xref:$blockid)\n"
111 $text = $reftext if !length($text);
113 die "xref: no text for wiki link '$blockid'\n" if !$text;
115 return "$link\[$text\]";
118 sub replace_default_xref
{
119 my ($blockid, $text) = @_;
121 my $link = $fileinfo->{blockid_target
}->{default}->{$blockid};
122 my $reftext = $fileinfo->{reftext
}->{default}->{$blockid};
124 die "unable to resolve chapter link (xref:$blockid)\n"
127 $text = $reftext if !length($text);
129 die "xref: no text for chapter link '$blockid'\n" if !$text;
131 return "$link\[$text\]";
134 sub replace_man_xref
{
135 my ($blockid, $text) = @_;
137 my $link = $fileinfo->{blockid_target
}->{manvolnum
}->{$blockid};
138 my $reftext = $fileinfo->{reftext
}->{manvolnum
}->{$blockid};
140 die "unable to resolve man page link (xref:$blockid)\n"
143 $text = $reftext if !length($text);
145 die "xref: no text for man page link '$blockid'\n" if !$text;
147 my $section = $fileinfo->{mansection
}->{manvolnum
}->{$link};
148 die "link target is not a manual page" if !defined($section);
151 if ($man_target eq 'html') {
153 $target =~ s/\.adoc//;
154 $target .= ".$section";
155 return "link:${target}.html#${blockid}\[$text\]";
156 } elsif ($man_target eq 'man') {
158 $command =~ s/\.adoc//;
159 return "\*${text}\* (man \*${command}\*($section))";
166 my ($env, $blockid, $text) = @_;
168 if ($env eq 'wiki') {
169 return replace_wiki_xref
($blockid, $text);
170 } elsif ($env eq 'manvolnum') {
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);
178 } elsif ($env eq 'default') {
179 return replace_default_xref
($blockid, $text);
181 die "internal error";
185 sub prepare_adoc_file
{
186 my ($target_env, $filename, $attributes) = @_;
188 return $prepared_files->{$filename} if defined($prepared_files->{$filename});
190 debug
("prepare $filename");
192 my $dirname = dirname
($filename);
193 my $basename = basename
($filename);
195 my $outfilename = "$dirname/${tmpprefix}$basename";
197 $prepared_files->{$filename} = $outfilename;
199 my $fh = IO
::File-
>new("$filename", "r") or
200 die "unable to open file '$filename' - $!\n";
202 my $outfh = IO
::File-
>new("$outfilename", "w") or
203 die "unable to open temporary file '$outfilename'\n";
205 push @$files_for_cleanup, $outfilename;
207 while (defined (my $line = <$fh>)) {
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;
213 my $skip = !exists($attributes->{$env}) ?
1 : 0;
214 $skip = ($skip ?
0 : 1 ) if $not;
216 push_environment
($env, $skip);
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);
227 if ($line =~ m/^include::(\S+)(\[.*\]\s*)$/) {
228 my ($fn, $rest) = ($1, $2);
229 debug
("include $fn");
230 my $new_fn = prepare_adoc_file
($target_env, $fn, $attributes);
232 print $outfh "include::${new_fn}$rest\n";
237 $line =~ s/xref:([^\s\[\]]+)\[([^\]]*)\]/replace_xref(${target_env},$1,$2)/ge;
239 $line =~ s/<<([^\s,\[\]]+)(?:,(.*?))?>>/replace_xref(${target_env},$1,$2)/ge;
241 print $outfh $line . "\n";
247 sub compile_asciidoc
{
252 GetOptions
("outfile=s" => \
$outfile,
253 "keep-artifacts" => \
$keep_artifacts,
254 "verbose" => \
$verbose) or
255 die("Error in command line arguments\n");
257 my $infile = shift(@ARGV) or
258 die "no input file specified\n";
260 scalar(@ARGV) == 0 or
261 die "too many arguments...\n";
263 my $outfilemap = $fileinfo->{outfile
}->{$env}->{$infile} ||
264 die "no output file mapping '$infile => $outfile' ($env)";
266 if ($man_target eq 'html') {
267 $outfilemap .= '.html';
268 } elsif ($man_target eq 'wiki') {
269 $outfilemap .= '-plain.html';
272 die "wrong output file name '$outfile != $outfilemap' ($env)"
273 if $outfile ne $outfilemap;
275 defined($fileinfo->{titles
}->{$env}) ||
276 die "unknown environment '$env'";
278 my $title = $fileinfo->{titles
}->{$env}->{$infile} or
279 die "unable to get title for '$infile'$env\n";
281 debug
("compile $title");
285 my $doctype = $fileinfo->{doctype
}->{$env}->{$infile};
287 die "unable to get document type for '$infile'\n"
288 if !defined($doctype);
290 $leveloffset = - $doctype;
297 leveloffset
=> $leveloffset,
298 revnumber
=> $release,
302 my $mansection = $fileinfo->{mansection
}->{$env}->{$infile};
304 if ($env eq 'wiki') {
305 } elsif ($env eq 'manvolnum') {
306 die "undefined man section" if !defined($mansection);
307 $attributes->{manvolnum
} = $mansection;
308 } elsif ($env eq 'default') {
309 die "$infile: wrong doctype\n" if $doctype != 0;
310 $attributes->{toc
} = undef;
313 if (!defined($outfile)) {
315 $outfile =~ s/\.adoc$//;
316 if ($env eq 'manvolnum') {
317 if (($man_target eq 'html') || ($man_target eq 'wiki')) {
318 $outfile .= ".$mansection.html";
320 $outfile .= ".$mansection";
327 if (($env eq 'manvolnum') && ($man_target eq 'man')) {
329 # asciidoc /etc/asciidoc/docbook-xsl/manpage.xsl skip REFERENCES
330 # section like footnotes, so we cannot use a2x.
331 # We use xmlto instead.
333 my $cmd = ['asciidoc', '-dmanpage', '-bdocbook', '-a', 'docinfo1'];
335 foreach my $key (keys %$attributes) {
336 my $value = $attributes->{$key};
337 if (defined($value)) {
338 push @$cmd, '-a', "$key=$value";
340 push @$cmd, '-a', $key;
344 push @$cmd, '--verbose' if $verbose;
346 my $tmpxmlfile = "${outfile}.xml.tmp";
348 push @$cmd, '--out-file', $tmpxmlfile;
350 push @$files_for_cleanup, $tmpxmlfile;
352 my $new_infile = prepare_adoc_file
($env, $infile, $attributes);
354 push @$cmd, $new_infile;
356 debug
("run " . join(' ', @$cmd));
358 system(@$cmd) == 0 or
361 $cmd = ['xmlto', 'man', $tmpxmlfile];
363 push @$cmd, '-v' if $verbose;
365 debug
("run " . join(' ', @$cmd));
367 system(@$cmd) == 0 or
372 $attributes->{icons
} = undef;
373 $attributes->{'data-uri'} = undef;
375 my $cmd = ['asciidoc', '-b', 'html5'];
377 push @$cmd, '-s' if ($env eq 'wiki') ||
378 (($env eq 'manvolnum') && ($man_target eq 'wiki'));
380 foreach my $key (keys %$attributes) {
381 my $value = $attributes->{$key};
382 if (defined($value)) {
383 push @$cmd, '-a', "$key=$value";
385 push @$cmd, '-a', $key;
389 push @$cmd, '--verbose' if $verbose;
391 push @$cmd, '--out-file', $outfile;
393 my $new_infile = prepare_adoc_file
($env, $infile, $attributes);
395 push @$cmd, $new_infile;
397 debug
("run " . join(' ', @$cmd));
399 system(@$cmd) == 0 or
408 foreach my $blockid (sort keys %{$fileinfo->{blockid_target
}->{default}}) {
409 my $link = $fileinfo->{blockid_target
}->{default}->{$blockid};
410 my $reftitle = $fileinfo->{reftitle
}->{default}->{$blockid};
411 my $reftext = $fileinfo->{reftext
}->{default}->{$blockid};
412 die "internal error" if $link !~ m/^link:/;
415 my $file = $fileinfo->{blockid
}->{default}->{$blockid};
416 die "internal error - no filename" if ! defined($file);
417 my $title = $fileinfo->{titles
}->{default}->{$file} ||
418 die "internal error - no title";
420 $data->{$blockid}->{title
} = $title;
421 $data->{$blockid}->{link} = $link;
422 my $subtitle = $reftitle || $reftext;
423 $data->{$blockid}->{subtitle
} = $subtitle
424 if $subtitle && ($title ne $subtitle);
430 sub scan_extjs_file
{
431 my ($filename, $res_data) = @_;
433 my $fh = IO
::File-
>new($filename, "r") ||
434 die "unable to open '$filename' - $!\n";
436 debug
("scan-extjs $filename");
438 while(defined(my $line = <$fh>)) {
439 if ($line =~ m/\s+onlineHelp:\s*[\'\"](.*?)[\'\"]/) {
441 my $link = $fileinfo->{blockid_target
}->{default}->{$blockid};
442 die "undefined blockid '$blockid' ($filename, line $.)\n"
443 if !(defined($link) || defined($online_help_links->{$blockid}));
445 $res_data->{$blockid} = 1;
450 if ($clicmd eq 'compile-wiki') {
452 eval { compile_asciidoc
('wiki'); };
459 } elsif ($clicmd eq 'compile-chapter') {
461 eval { compile_asciidoc
('default'); };
468 } elsif ($clicmd eq 'compile-man-html') {
470 $man_target = 'html';
472 eval { compile_asciidoc
('manvolnum'); };
479 } elsif ($clicmd eq 'compile-man-wiki') {
481 $man_target = 'wiki';
483 eval { compile_asciidoc
('manvolnum'); };
490 } elsif ($clicmd eq 'compile-man') {
492 eval { compile_asciidoc
('manvolnum'); };
499 } elsif ($clicmd eq 'print-links') {
503 GetOptions
("outfile=s" => \
$outfile,
504 "verbose" => \
$verbose) or
505 die("Error in command line arguments\n");
507 scalar(@ARGV) == 0 or
508 die "too many arguments...\n";
510 my $data = get_links
();
512 my $res = to_json
($data, { pretty
=> 1, canonical
=> 1 } );
514 if (defined($outfile)) {
515 my $outfh = IO
::File-
>new("$outfile", "w") or
516 die "unable to open temporary file '$outfile'\n";
525 } elsif ($clicmd eq 'scan-extjs') {
527 GetOptions
("verbose" => \
$verbose) or
528 die("Error in command line arguments\n");
531 my $scanned_files = {};
532 while (my $filename = shift) {
533 die "got strange file name '$filename'\n"
534 if $filename !~ m/\.js$/;
535 next if $scanned_files->{$filename};
537 scan_extjs_file
($filename, $link_hash);
538 $scanned_files->{$filename} = 1;
541 my $data = get_links
();
545 foreach my $blockid (keys %$link_hash) {
546 $res_data->{$blockid} = $data->{$blockid} || $online_help_links->{$blockid} ||
547 die "internal error - no data for '$blockid'";
550 my $data_str = to_json
($res_data, { pretty
=> 1, canonical
=> 1 });
553 print "var pveOnlineHelpInfo = ${data_str};\n";
557 die "unknown command '$clicmd'\n";