]>
git.proxmox.com Git - pve-docs.git/blob - asciidoc-pve.in
16 my $release = '@RELEASE@';
19 die "no command specified\n";
22 while (<main
::DATA
>) { $data_str .= $_; }
24 my $fileinfo = decode_json
($data_str);
26 my $tmpprefix = '.asciidoc-pve-tmp'.$$.'_';
28 my $adoc_source_dir = "/usr/share/pve-doc-generator";
30 # inside pve-docs source dir?
31 if (-f
"asciidoc-pve.in" && -f
"pve-admin-guide.adoc") {
32 $adoc_source_dir = getcwd
();
35 my $prepared_files = {};
37 my $man_target = 'man';
41 my $online_help_links = {
42 'pve_service_daemons' => {
43 link => '/pve-docs/index.html#_service_daemons',
44 title
=> 'Service Daemons',
46 'pve_documentation_index' => {
47 link => '/pve-docs/index.html',
48 title
=> 'Proxmox VE Documentation Index',
50 'pve_admin_guide' => {
51 link => '/pve-docs/pve-admin-guide.html',
52 title
=> 'Proxmox VE Administration Guide',
61 print STDERR
"asciidoc-pve: $msg\n";
64 sub push_environment
{
65 my ($env, $skip) = @_;
67 $skip = 1 if $env_skip;
68 $skip = 0 if !defined($skip);
70 push @$env_stack, [$env, $skip];
78 my $last_stack_entry = pop @$env_stack;
79 die "unable to pop env '$env'" if !defined($last_stack_entry);
81 my ($last_env, $skip) = @$last_stack_entry;
82 die "environment missmatch (${last_env} != $env)\n" if $last_env ne $env;
84 if (!scalar(@$env_stack)) {
87 my (undef, $skip) = @{$env_stack->[-1]};
92 my $files_for_cleanup = [];
96 return if $keep_artifacts;
98 foreach my $file (@$files_for_cleanup) {
103 sub replace_wiki_xref
{
104 my ($blockid, $text) = @_;
106 my $link = $fileinfo->{blockid_target
}->{wiki
}->{$blockid};
107 my $reftext = $fileinfo->{reftext
}->{wiki
}->{$blockid};
109 die "unable to resolve wiki link (xref:$blockid)\n"
112 $text = $reftext if !length($text);
114 die "xref: no text for wiki link '$blockid'\n" if !$text;
116 return "$link\[$text\]";
119 sub replace_default_xref
{
120 my ($blockid, $text) = @_;
122 my $link = $fileinfo->{blockid_target
}->{default}->{$blockid};
123 my $reftext = $fileinfo->{reftext
}->{default}->{$blockid};
125 die "unable to resolve chapter link (xref:$blockid)\n"
128 $text = $reftext if !length($text);
130 die "xref: no text for chapter link '$blockid'\n" if !$text;
132 return "$link\[$text\]";
135 sub replace_man_xref
{
136 my ($blockid, $text) = @_;
138 my $link = $fileinfo->{blockid_target
}->{manvolnum
}->{$blockid};
139 my $reftext = $fileinfo->{reftext
}->{manvolnum
}->{$blockid};
141 die "unable to resolve man page link (xref:$blockid)\n"
144 $text = $reftext if !length($text);
146 die "xref: no text for man page link '$blockid'\n" if !$text;
148 my $section = $fileinfo->{mansection
}->{manvolnum
}->{$link};
149 if (!defined($section)) {
150 warn "link '$blockid' target '$link' is not a manual page, ignoring\n";
155 if ($man_target eq 'html') {
157 $target =~ s/\.adoc//;
158 $target .= ".$section";
159 return "link:${target}.html#${blockid}\[$text\]";
160 } elsif ($man_target eq 'man') {
162 $command =~ s/\.adoc//;
163 return "\*${text}\* (man \*${command}\*($section))";
170 my ($env, $blockid, $text) = @_;
172 if ($env eq 'wiki') {
173 return replace_wiki_xref
($blockid, $text);
174 } elsif ($env eq 'manvolnum') {
175 if (($man_target eq 'man') || ($man_target eq 'html')) {
176 return replace_man_xref
($blockid, $text);
177 } elsif ($man_target eq 'wiki') {
178 return replace_wiki_xref
($blockid, $text);
182 } elsif ($env eq 'default') {
183 return replace_default_xref
($blockid, $text);
185 die "internal error";
189 sub prepare_adoc_file
{
190 my ($target_env, $filename, $attributes) = @_;
192 return $prepared_files->{$filename} if defined($prepared_files->{$filename});
194 debug
("prepare $filename");
196 my $dirname = dirname
($filename);
197 my $basename = basename
($filename);
199 my $outfilename = "$dirname/${tmpprefix}$basename";
201 $prepared_files->{$filename} = $outfilename;
203 my $fh = IO
::File-
>new("$filename", "r") or
204 die "unable to open file '$filename' - $!\n";
206 my $outfh = IO
::File-
>new("$outfilename", "w") or
207 die "unable to open temporary file '$outfilename'\n";
209 push @$files_for_cleanup, $outfilename;
211 while (defined (my $line = <$fh>)) {
213 if ($line =~ m/^if(n?)def::(\S+)\[(.*)\]\s*$/) {
214 my ($not, $env, $text) = ($1, $2, $3);
215 die "unsuported ifdef usage - implement me" if $text;
217 my $skip = !exists($attributes->{$env}) ?
1 : 0;
218 $skip = ($skip ?
0 : 1 ) if $not;
220 push_environment
($env, $skip);
222 } elsif ($line =~ m/^endif::(\S+)\[(.*)\]\s*$/) {
223 my ($env, $text) = ($1, $2);
224 die "unsuported ifdef usage - implement me" if $text;
225 pop_environment
($env);
231 if ($line =~ m/^include::(\S+)(\[.*\]\s*)$/) {
232 my ($fn, $rest) = ($1, $2);
233 debug
("include $fn");
234 my $new_fn = prepare_adoc_file
($target_env, $fn, $attributes);
236 print $outfh "include::${new_fn}$rest\n";
240 if ($line =~ m/xref:\S+?\[[^\]]*$/) {
241 die "possible xref spanning multiple lines in '$filename':\n(line $.): $line\n";
243 if ($line =~ m/<<((?!\>\>).)*$/) {
244 die "possible xref spanning multiple lines in '$filename':\n(line $.): $line\n";
247 $line =~ s/xref:([^\s\[\]]+)\[([^\]]*)\]/replace_xref(${target_env},$1,$2)/ge;
249 $line =~ s/<<([^\s,\[\]]+)(?:,(.*?))?>>/replace_xref(${target_env},$1,$2)/ge;
251 print $outfh $line . "\n";
257 sub compile_asciidoc
{
263 "outfile=s" => \
$outfile,
264 "keep-artifacts" => \
$keep_artifacts,
265 "verbose" => \
$verbose
266 ) or die("Error in command line arguments\n");
268 my $infile = shift(@ARGV) or die "no input file specified\n";
269 scalar(@ARGV) == 0 or die "too many arguments...\n";
271 my $outfilemap = $fileinfo->{outfile
}->{$env}->{$infile} ||
272 die "no output file mapping for '$infile' ($env)";
274 if ($man_target eq 'html') {
275 $outfilemap .= '.html';
276 } elsif ($man_target eq 'wiki') {
277 $outfilemap .= '-plain.html';
280 if (defined($outfile)) {
281 die "wrong output file name '$outfile != $outfilemap' ($env)" if $outfile ne $outfilemap;
283 $outfile = $outfilemap;
286 defined($fileinfo->{titles
}->{$env}) || die "unknown environment '$env'";
288 my $title = $fileinfo->{titles
}->{$env}->{$infile} or
289 die "unable to get title for '$infile'$env\n";
291 debug
("compile $title");
295 my $doctype = $fileinfo->{doctype
}->{$env}->{$infile};
297 die "unable to get document type for '$infile'\n"
298 if !defined($doctype);
300 $leveloffset = - $doctype;
303 if (defined($ENV{SOURCE_DATE_EPOCH
})) {
304 $date = `date -d "\@$ENV{SOURCE_DATE_EPOCH}"`;
312 leveloffset
=> $leveloffset,
313 revnumber
=> $release,
315 'footer-style' => 'revdate',
318 my $mansection = $fileinfo->{mansection
}->{$env}->{$infile};
320 if ($env eq 'wiki') {
321 } elsif ($env eq 'manvolnum') {
322 die "undefined man section" if !defined($mansection);
323 $attributes->{manvolnum
} = $mansection;
324 } elsif ($env eq 'default') {
325 die "$infile: wrong doctype\n" if $doctype != 0;
326 $attributes->{toc2
} = undef;
329 if (!defined($outfile)) {
331 $outfile =~ s/\.adoc$//;
332 if ($env eq 'manvolnum') {
333 if (($man_target eq 'html') || ($man_target eq 'wiki')) {
334 $outfile .= ".$mansection.html";
336 $outfile .= ".$mansection";
343 if (($env eq 'manvolnum') && ($man_target eq 'man')) {
345 # asciidoc /etc/asciidoc/docbook-xsl/manpage.xsl skip REFERENCES
346 # section like footnotes, so we cannot use a2x.
347 # We use xmlto instead.
353 '-f', "$adoc_source_dir/asciidoc/asciidoc-pve.conf",
357 foreach my $key (keys %$attributes) {
358 my $value = $attributes->{$key};
359 if (defined($value)) {
360 push @$cmd, '-a', "$key=$value";
362 push @$cmd, '-a', $key;
366 push @$cmd, '--verbose' if $verbose;
368 my $tmpxmlfile = "${outfile}.xml.tmp";
370 push @$cmd, '--out-file', $tmpxmlfile;
372 push @$files_for_cleanup, $tmpxmlfile;
374 my $new_infile = prepare_adoc_file
($env, $infile, $attributes);
376 push @$cmd, $new_infile;
378 debug
("run " . join(' ', @$cmd));
380 system(@$cmd) == 0 or die "aciidoc error";
382 $cmd = ['xmlto', 'man', $tmpxmlfile];
384 push @$cmd, '-v' if $verbose;
386 debug
("run " . join(' ', @$cmd));
388 system(@$cmd) == 0 or die "xmlto error";
392 $attributes->{icons
} = undef;
393 $attributes->{'data-uri'} = undef;
395 my $cmd = [ 'asciidoc', '-f', "$adoc_source_dir/asciidoc/asciidoc-pve.conf" ];
397 if (($env eq 'wiki') ||
398 (($env eq 'manvolnum') && ($man_target eq 'wiki'))) {
400 push @$cmd, '-b', "$adoc_source_dir/asciidoc/mediawiki";
402 push @$cmd, '-b', "$adoc_source_dir/asciidoc/pve-html";
405 foreach my $key (keys %$attributes) {
406 my $value = $attributes->{$key};
407 if (defined($value)) {
408 push @$cmd, '-a', "$key=$value";
410 push @$cmd, '-a', $key;
414 push @$cmd, '--verbose' if $verbose;
416 push @$cmd, '--out-file', $outfile;
418 my $new_infile = prepare_adoc_file
($env, $infile, $attributes);
420 push @$cmd, $new_infile;
422 debug
("run " . join(' ', @$cmd));
424 system(@$cmd) == 0 or die "aciidoc error";
432 foreach my $blockid (sort keys %{$fileinfo->{blockid_target
}->{default}}) {
433 my $link = $fileinfo->{blockid_target
}->{default}->{$blockid};
434 my $reftitle = $fileinfo->{reftitle
}->{default}->{$blockid};
435 my $reftext = $fileinfo->{reftext
}->{default}->{$blockid};
436 die "internal error" if $link !~ m/^link:/;
439 my $file = $fileinfo->{blockid
}->{default}->{$blockid};
440 die "internal error - no filename" if ! defined($file);
441 my $title = $fileinfo->{titles
}->{default}->{$file} ||
442 die "internal error - no title";
444 $data->{$blockid}->{title
} = $title;
445 $data->{$blockid}->{link} = $link;
446 my $subtitle = $reftitle || $reftext;
447 $data->{$blockid}->{subtitle
} = $subtitle
448 if $subtitle && ($title ne $subtitle);
454 sub scan_extjs_file
{
455 my ($filename, $res_data) = @_;
457 my $fh = IO
::File-
>new($filename, "r") ||
458 die "unable to open '$filename' - $!\n";
460 debug
("scan-extjs $filename");
462 while(defined(my $line = <$fh>)) {
463 if ($line =~ m/\s+onlineHelp:\s*[\'\"]([^{}\[\]\'\"]+)[\'\"]/) {
465 my $link = $fileinfo->{blockid_target
}->{default}->{$blockid};
466 die "undefined blockid '$blockid' ($filename, line $.)\n"
467 if !(defined($link) || defined($online_help_links->{$blockid}));
469 $res_data->{$blockid} = 1;
474 if ($clicmd eq 'compile-wiki') {
476 eval { compile_asciidoc
('wiki'); };
483 } elsif ($clicmd eq 'compile-chapter') {
485 eval { compile_asciidoc
('default'); };
492 } elsif ($clicmd eq 'compile-man-html') {
494 $man_target = 'html';
496 eval { compile_asciidoc
('manvolnum'); };
503 } elsif ($clicmd eq 'compile-man-wiki') {
505 $man_target = 'wiki';
507 eval { compile_asciidoc
('manvolnum'); };
514 } elsif ($clicmd eq 'compile-man') {
516 eval { compile_asciidoc
('manvolnum'); };
523 } elsif ($clicmd eq 'print-links') {
527 GetOptions
("outfile=s" => \
$outfile,
528 "verbose" => \
$verbose) or
529 die("Error in command line arguments\n");
531 scalar(@ARGV) == 0 or
532 die "too many arguments...\n";
534 my $data = get_links
();
536 my $res = to_json
($data, { pretty
=> 1, canonical
=> 1 } );
538 if (defined($outfile)) {
539 my $outfh = IO
::File-
>new("$outfile", "w") or
540 die "unable to open temporary file '$outfile'\n";
549 } elsif ($clicmd eq 'scan-extjs') {
551 GetOptions
("verbose" => \
$verbose) or
552 die("Error in command line arguments\n");
555 my $scanned_files = {};
556 while (my $filename = shift) {
557 die "got strange file name '$filename'\n"
558 if $filename !~ m/\.js$/;
559 next if $scanned_files->{$filename};
561 scan_extjs_file
($filename, $link_hash);
562 $scanned_files->{$filename} = 1;
565 my $data = get_links
();
569 foreach my $blockid (keys %$link_hash) {
570 $res_data->{$blockid} = $data->{$blockid} || $online_help_links->{$blockid} ||
571 die "internal error - no data for '$blockid'";
574 my $data_str = to_json
($res_data, { pretty
=> 1, canonical
=> 1 });
577 print "const pveOnlineHelpInfo = ${data_str};\n";
579 } elsif ($clicmd eq 'chapter-table') {
581 print '[width="100%",options="header"]' . "\n";
583 print "|Title|Link\n";
585 my $filelist = $fileinfo->{outfile
}->{default};
586 foreach my $sourcefile (sort keys %$filelist) {
587 my $target = $filelist->{$sourcefile};
588 next if $target eq 'pve-admin-guide.html';
589 my $title = $fileinfo->{titles
}->{default}->{$sourcefile} ||
590 die "not title for '$sourcefile'";
591 print "|$title|link:$target\[\]\n";
596 } elsif ($clicmd =~ m/^man([158])page-table$/) {
599 print '[width="100%",cols="5*d",options="header"]' . "\n";
601 print "|Name 3+|Title|Link\n";
603 my $filelist = $fileinfo->{outfile
}->{manvolnum
};
604 foreach my $manpage (sort keys %$filelist) {
605 next if $section ne $fileinfo->{mansection
}->{manvolnum
}->{$manpage};
606 my $mantitle = $fileinfo->{titles
}->{manvolnum
}->{$manpage} ||
607 die "not manual title for '$manpage'";
608 my $title = $fileinfo->{titles
}->{default}->{$manpage} ||
609 die "not title for '$manpage'";
611 # hack - remove command name prefix from titles
612 $title =~ s/^[a-z]+\s*-\s*//;
614 my $target = $filelist->{$manpage};
615 print "|$mantitle 3+|$title|link:$target.html\[$target\]\n";
622 die "unknown command '$clicmd'\n";