]>
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
"attributes.txt" && -f
"pve-admin-guide.adoc") {
32 $adoc_source_dir = "."
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 die "link target is not a manual page" if !defined($section);
152 if ($man_target eq 'html') {
154 $target =~ s/\.adoc//;
155 $target .= ".$section";
156 return "link:${target}.html#${blockid}\[$text\]";
157 } elsif ($man_target eq 'man') {
159 $command =~ s/\.adoc//;
160 return "\*${text}\* (man \*${command}\*($section))";
167 my ($env, $blockid, $text) = @_;
169 if ($env eq 'wiki') {
170 return replace_wiki_xref
($blockid, $text);
171 } elsif ($env eq 'manvolnum') {
172 if (($man_target eq 'man') || ($man_target eq 'html')) {
173 return replace_man_xref
($blockid, $text);
174 } elsif ($man_target eq 'wiki') {
175 return replace_wiki_xref
($blockid, $text);
179 } elsif ($env eq 'default') {
180 return replace_default_xref
($blockid, $text);
182 die "internal error";
186 sub prepare_adoc_file
{
187 my ($target_env, $filename, $attributes) = @_;
189 return $prepared_files->{$filename} if defined($prepared_files->{$filename});
191 debug
("prepare $filename");
193 my $dirname = dirname
($filename);
194 my $basename = basename
($filename);
196 my $outfilename = "$dirname/${tmpprefix}$basename";
198 $prepared_files->{$filename} = $outfilename;
200 my $fh = IO
::File-
>new("$filename", "r") or
201 die "unable to open file '$filename' - $!\n";
203 my $outfh = IO
::File-
>new("$outfilename", "w") or
204 die "unable to open temporary file '$outfilename'\n";
206 push @$files_for_cleanup, $outfilename;
208 while (defined (my $line = <$fh>)) {
210 if ($line =~ m/^if(n?)def::(\S+)\[(.*)\]\s*$/) {
211 my ($not, $env, $text) = ($1, $2, $3);
212 die "unsuported ifdef usage - implement me" if $text;
214 my $skip = !exists($attributes->{$env}) ?
1 : 0;
215 $skip = ($skip ?
0 : 1 ) if $not;
217 push_environment
($env, $skip);
219 } elsif ($line =~ m/^endif::(\S+)\[(.*)\]\s*$/) {
220 my ($env, $text) = ($1, $2);
221 die "unsuported ifdef usage - implement me" if $text;
222 pop_environment
($env);
228 if ($line =~ m/^include::(\S+)(\[.*\]\s*)$/) {
229 my ($fn, $rest) = ($1, $2);
230 debug
("include $fn");
231 my $new_fn = prepare_adoc_file
($target_env, $fn, $attributes);
233 print $outfh "include::${new_fn}$rest\n";
237 if ($line =~ m/xref:\S+?\[[^\]]*$/) {
238 die "possible xref spanning multiple lines in '$filename':\n(line $.): $line\n";
240 if ($line =~ m/<<((?!\>\>).)*$/) {
241 die "possible xref spanning multiple lines in '$filename':\n(line $.): $line\n";
244 $line =~ s/xref:([^\s\[\]]+)\[([^\]]*)\]/replace_xref(${target_env},$1,$2)/ge;
246 $line =~ s/<<([^\s,\[\]]+)(?:,(.*?))?>>/replace_xref(${target_env},$1,$2)/ge;
248 print $outfh $line . "\n";
254 sub compile_asciidoc
{
259 GetOptions
("outfile=s" => \
$outfile,
260 "keep-artifacts" => \
$keep_artifacts,
261 "verbose" => \
$verbose) or
262 die("Error in command line arguments\n");
264 my $infile = shift(@ARGV) or
265 die "no input file specified\n";
267 scalar(@ARGV) == 0 or
268 die "too many arguments...\n";
270 my $outfilemap = $fileinfo->{outfile
}->{$env}->{$infile} ||
271 die "no output file mapping for '$infile' ($env)";
273 if ($man_target eq 'html') {
274 $outfilemap .= '.html';
275 } elsif ($man_target eq 'wiki') {
276 $outfilemap .= '-plain.html';
279 if (defined($outfile)) {
280 die "wrong output file name '$outfile != $outfilemap' ($env)"
281 if $outfile ne $outfilemap;
283 $outfile = $outfilemap;
286 defined($fileinfo->{titles
}->{$env}) ||
287 die "unknown environment '$env'";
289 my $title = $fileinfo->{titles
}->{$env}->{$infile} or
290 die "unable to get title for '$infile'$env\n";
292 debug
("compile $title");
296 my $doctype = $fileinfo->{doctype
}->{$env}->{$infile};
298 die "unable to get document type for '$infile'\n"
299 if !defined($doctype);
301 $leveloffset = - $doctype;
308 leveloffset
=> $leveloffset,
309 revnumber
=> $release,
313 my $mansection = $fileinfo->{mansection
}->{$env}->{$infile};
315 if ($env eq 'wiki') {
316 } elsif ($env eq 'manvolnum') {
317 die "undefined man section" if !defined($mansection);
318 $attributes->{manvolnum
} = $mansection;
319 } elsif ($env eq 'default') {
320 die "$infile: wrong doctype\n" if $doctype != 0;
321 $attributes->{toc
} = undef;
324 if (!defined($outfile)) {
326 $outfile =~ s/\.adoc$//;
327 if ($env eq 'manvolnum') {
328 if (($man_target eq 'html') || ($man_target eq 'wiki')) {
329 $outfile .= ".$mansection.html";
331 $outfile .= ".$mansection";
338 if (($env eq 'manvolnum') && ($man_target eq 'man')) {
340 # asciidoc /etc/asciidoc/docbook-xsl/manpage.xsl skip REFERENCES
341 # section like footnotes, so we cannot use a2x.
342 # We use xmlto instead.
344 my $cmd = ['asciidoc', '-dmanpage', '-bdocbook', '-a', 'docinfo1'];
346 foreach my $key (keys %$attributes) {
347 my $value = $attributes->{$key};
348 if (defined($value)) {
349 push @$cmd, '-a', "$key=$value";
351 push @$cmd, '-a', $key;
355 push @$cmd, '--verbose' if $verbose;
357 my $tmpxmlfile = "${outfile}.xml.tmp";
359 push @$cmd, '--out-file', $tmpxmlfile;
361 push @$files_for_cleanup, $tmpxmlfile;
363 my $new_infile = prepare_adoc_file
($env, $infile, $attributes);
365 push @$cmd, $new_infile;
367 debug
("run " . join(' ', @$cmd));
369 system(@$cmd) == 0 or
372 $cmd = ['xmlto', 'man', $tmpxmlfile];
374 push @$cmd, '-v' if $verbose;
376 debug
("run " . join(' ', @$cmd));
378 system(@$cmd) == 0 or
383 $attributes->{icons
} = undef;
384 $attributes->{'data-uri'} = undef;
386 my $cmd = ['asciidoc'];
388 if (($env eq 'wiki') ||
389 (($env eq 'manvolnum') && ($man_target eq 'wiki'))) {
391 if (-f
"./asciidoc/mediawiki.conf") {
393 push @$cmd, '-b', "$cwd/asciidoc/mediawiki";
395 push @$cmd, '-b', "mediawiki";
399 foreach my $key (keys %$attributes) {
400 my $value = $attributes->{$key};
401 if (defined($value)) {
402 push @$cmd, '-a', "$key=$value";
404 push @$cmd, '-a', $key;
408 push @$cmd, '--verbose' if $verbose;
410 push @$cmd, '--out-file', $outfile;
412 my $new_infile = prepare_adoc_file
($env, $infile, $attributes);
414 push @$cmd, $new_infile;
416 debug
("run " . join(' ', @$cmd));
418 system(@$cmd) == 0 or
427 foreach my $blockid (sort keys %{$fileinfo->{blockid_target
}->{default}}) {
428 my $link = $fileinfo->{blockid_target
}->{default}->{$blockid};
429 my $reftitle = $fileinfo->{reftitle
}->{default}->{$blockid};
430 my $reftext = $fileinfo->{reftext
}->{default}->{$blockid};
431 die "internal error" if $link !~ m/^link:/;
434 my $file = $fileinfo->{blockid
}->{default}->{$blockid};
435 die "internal error - no filename" if ! defined($file);
436 my $title = $fileinfo->{titles
}->{default}->{$file} ||
437 die "internal error - no title";
439 $data->{$blockid}->{title
} = $title;
440 $data->{$blockid}->{link} = $link;
441 my $subtitle = $reftitle || $reftext;
442 $data->{$blockid}->{subtitle
} = $subtitle
443 if $subtitle && ($title ne $subtitle);
449 sub scan_extjs_file
{
450 my ($filename, $res_data) = @_;
452 my $fh = IO
::File-
>new($filename, "r") ||
453 die "unable to open '$filename' - $!\n";
455 debug
("scan-extjs $filename");
457 while(defined(my $line = <$fh>)) {
458 if ($line =~ m/\s+onlineHelp:\s*[\'\"](.*?)[\'\"]/) {
460 my $link = $fileinfo->{blockid_target
}->{default}->{$blockid};
461 die "undefined blockid '$blockid' ($filename, line $.)\n"
462 if !(defined($link) || defined($online_help_links->{$blockid}));
464 $res_data->{$blockid} = 1;
469 if ($clicmd eq 'compile-wiki') {
471 eval { compile_asciidoc
('wiki'); };
478 } elsif ($clicmd eq 'compile-chapter') {
480 eval { compile_asciidoc
('default'); };
487 } elsif ($clicmd eq 'compile-man-html') {
489 $man_target = 'html';
491 eval { compile_asciidoc
('manvolnum'); };
498 } elsif ($clicmd eq 'compile-man-wiki') {
500 $man_target = 'wiki';
502 eval { compile_asciidoc
('manvolnum'); };
509 } elsif ($clicmd eq 'compile-man') {
511 eval { compile_asciidoc
('manvolnum'); };
518 } elsif ($clicmd eq 'print-links') {
522 GetOptions
("outfile=s" => \
$outfile,
523 "verbose" => \
$verbose) or
524 die("Error in command line arguments\n");
526 scalar(@ARGV) == 0 or
527 die "too many arguments...\n";
529 my $data = get_links
();
531 my $res = to_json
($data, { pretty
=> 1, canonical
=> 1 } );
533 if (defined($outfile)) {
534 my $outfh = IO
::File-
>new("$outfile", "w") or
535 die "unable to open temporary file '$outfile'\n";
544 } elsif ($clicmd eq 'scan-extjs') {
546 GetOptions
("verbose" => \
$verbose) or
547 die("Error in command line arguments\n");
550 my $scanned_files = {};
551 while (my $filename = shift) {
552 die "got strange file name '$filename'\n"
553 if $filename !~ m/\.js$/;
554 next if $scanned_files->{$filename};
556 scan_extjs_file
($filename, $link_hash);
557 $scanned_files->{$filename} = 1;
560 my $data = get_links
();
564 foreach my $blockid (keys %$link_hash) {
565 $res_data->{$blockid} = $data->{$blockid} || $online_help_links->{$blockid} ||
566 die "internal error - no data for '$blockid'";
569 my $data_str = to_json
($res_data, { pretty
=> 1, canonical
=> 1 });
572 print "var pveOnlineHelpInfo = ${data_str};\n";
574 } elsif ($clicmd eq 'chapter-table') {
576 print '[width="100%",options="header"]' . "\n";
578 print "|Title|Link\n";
580 my $filelist = $fileinfo->{outfile
}->{default};
581 foreach my $sourcefile (sort keys %$filelist) {
582 my $target = $filelist->{$sourcefile};
583 next if $target eq 'pve-admin-guide.html';
584 my $title = $fileinfo->{titles
}->{default}->{$sourcefile} ||
585 die "not title for '$sourcefile'";
586 print "|$title|link:$target\[\]\n";
591 } elsif ($clicmd =~ m/^man([158])page-table$/) {
594 print '[width="100%",cols="5*d",options="header"]' . "\n";
596 print "|Name 3+|Title|Link\n";
598 my $filelist = $fileinfo->{outfile
}->{manvolnum
};
599 foreach my $manpage (sort keys %$filelist) {
600 next if $section ne $fileinfo->{mansection
}->{manvolnum
}->{$manpage};
601 my $mantitle = $fileinfo->{titles
}->{manvolnum
}->{$manpage} ||
602 die "not manual title for '$manpage'";
603 my $title = $fileinfo->{titles
}->{default}->{$manpage} ||
604 die "not title for '$manpage'";
606 # hack - remove command name prefix from titles
607 $title =~ s/^[a-z]+\s*-\s*//;
609 my $target = $filelist->{$manpage};
610 print "|$mantitle 3+|$title|link:$target.html\[$target\]\n";
617 die "unknown command '$clicmd'\n";