]> git.proxmox.com Git - pve-docs.git/blobdiff - asciidoc-pve.in
asciidoc-pve.in: use new mediawiki.conf to render wiki pages
[pve-docs.git] / asciidoc-pve.in
index 0e17b7a39cdb4737ffc5a6cd83a2190f04a47722..878fe51772ce82481fa2fbe465046def340e624b 100644 (file)
@@ -6,9 +6,13 @@ use Getopt::Long;
 use File::Path;
 use File::Basename;
 use IO::File;
+use Cwd;
 
 use JSON;
 
+my $verbose;
+my $keep_artifacts;
+
 my $release = '@RELEASE@';
 
 my $clicmd = shift or
@@ -30,9 +34,33 @@ if (-f "attributes.txt" && -f "pve-admin-guide.adoc") {
 
 my $prepared_files = {};
 
+my $man_target = 'man';
 my $env_stack = [];
 my $env_skip = 0;
 
+my $online_help_links = {
+    'pve_service_daemons' => {
+       link => '/pve-docs/index.html#_service_daemons',
+       title => 'Service Daemons',
+    },
+    'pve_documentation_index' => {
+       link => '/pve-docs/index.html',
+       title => 'Proxmox VE Documentation Index',
+    },
+    'pve_admin_guide' => {
+       link => '/pve-docs/pve-admin-guide.html',
+       title => 'Proxmox VE Administration Guide',
+    },
+};
+
+sub debug {
+    my $msg = shift;
+
+    return if !$verbose;
+
+    print STDERR "asciidoc-pve: $msg\n";
+}
+
 sub push_environment {
     my ($env, $skip) = @_;
 
@@ -61,9 +89,15 @@ sub pop_environment {
     }
 }
 
+my $files_for_cleanup = [];
+
 sub cleanup {
 
-    # TODO: anything ?
+    return if $keep_artifacts;
+
+    foreach my $file (@$files_for_cleanup) {
+       unlink $file;
+    }
 }
 
 sub replace_wiki_xref {
@@ -77,33 +111,102 @@ sub replace_wiki_xref {
 
     $text = $reftext if !length($text);
 
-    die "xref: no text for '$blockid'\n" if !$text;
+    die "xref: no text for wiki link '$blockid'\n" if !$text;
 
     return "$link\[$text\]";
 }
 
-sub prepare_adoc_file {
-    my ($filename, $attributes) = @_;
+sub replace_default_xref {
+    my ($blockid, $text) = @_;
+
+    my $link = $fileinfo->{blockid_target}->{default}->{$blockid};
+    my $reftext = $fileinfo->{reftext}->{default}->{$blockid};
+
+    die "unable to resolve chapter link (xref:$blockid)\n"
+       if !defined($link);
+
+    $text = $reftext if !length($text);
+
+    die "xref: no text for chapter link '$blockid'\n" if !$text;
+
+    return "$link\[$text\]";
+}
+
+sub replace_man_xref {
+    my ($blockid, $text) = @_;
+
+    my $link = $fileinfo->{blockid_target}->{manvolnum}->{$blockid};
+    my $reftext = $fileinfo->{reftext}->{manvolnum}->{$blockid};
 
+    die "unable to resolve man page link (xref:$blockid)\n"
+       if !defined($link);
+
+    $text = $reftext if !length($text);
+
+    die "xref: no text for man page link '$blockid'\n" if !$text;
+
+    my $section = $fileinfo->{mansection}->{manvolnum}->{$link};
+    die "link target is not a manual page" if !defined($section);
+
+
+    if ($man_target eq 'html') {
+       my $target = $link;
+       $target =~ s/\.adoc//;
+       $target .= ".$section";
+       return "link:${target}.html#${blockid}\[$text\]";
+    } elsif ($man_target eq 'man') {
+       my $command = $link;
+       $command =~ s/\.adoc//;
+       return "\*${text}\* (man \*${command}\*($section))";
+    } else {
+       die "internal error"
+    }
+}
 
-    return if $prepared_files->{$filename};
+sub replace_xref {
+    my ($env, $blockid, $text) = @_;
 
-    print "PREPARE $filename\n";
+    if ($env eq 'wiki') {
+       return replace_wiki_xref($blockid, $text);
+    } elsif ($env eq 'manvolnum') {
+       if (($man_target eq 'man') || ($man_target eq 'html')) {
+           return replace_man_xref($blockid, $text);
+       } elsif ($man_target eq 'wiki') {
+           return replace_wiki_xref($blockid, $text);
+       } else {
+           die "internal error"
+       }
+    } elsif ($env eq 'default') {
+       return replace_default_xref($blockid, $text);
+    } else {
+       die "internal error";
+    }
+}
 
-    $prepared_files->{$filename} = 1;
+sub prepare_adoc_file {
+    my ($target_env, $filename, $attributes) = @_;
+
+    return $prepared_files->{$filename} if defined($prepared_files->{$filename});
+
+    debug("prepare $filename");
 
     my $dirname = dirname($filename);
     my $basename = basename($filename);
 
     my $outfilename = "$dirname/${tmpprefix}$basename";
 
+    $prepared_files->{$filename} = $outfilename;
+
     my $fh = IO::File->new("$filename", "r") or
        die "unable to open file '$filename' - $!\n";
 
     my $outfh = IO::File->new("$outfilename", "w") or
        die "unable to open temporary file '$outfilename'\n";
 
+    push @$files_for_cleanup, $outfilename;
+
     while (defined (my $line = <$fh>)) {
+       chomp $line;
        if ($line =~ m/^if(n?)def::(\S+)\[(.*)\]\s*$/) {
            my ($not, $env, $text) = ($1, $2, $3);
            die "unsuported ifdef usage - implement me" if $text;
@@ -124,29 +227,31 @@ sub prepare_adoc_file {
 
        if ($line =~ m/^include::(\S+)(\[.*\]\s*)$/) {
            my ($fn, $rest) = ($1, $2);
-           print "INCLUDE: $fn\n";
-           my $new_fn = prepare_adoc_file($fn, $attributes);
-           $line = "include::${new_fn}$rest\n";
+           debug("include $fn");
+           my $new_fn = prepare_adoc_file($target_env, $fn, $attributes);
+
+           print $outfh "include::${new_fn}$rest\n";
+           next;
        }
 
        # fix xrefs
-       $line =~ s/xref:([^\s\[\]]+)\[([^\]]*)\]/replace_wiki_xref($1,$2)/ge;
+       $line =~ s/xref:([^\s\[\]]+)\[([^\]]*)\]/replace_xref(${target_env},$1,$2)/ge;
 
-       $line =~ s/<<([^\s,\[\]]+)(?:,(.*?))?>>/replace_wiki_xref($1,$2)/ge;
+       $line =~ s/<<([^\s,\[\]]+)(?:,(.*?))?>>/replace_xref(${target_env},$1,$2)/ge;
 
-       print $outfh $line;
+       print $outfh $line . "\n";
     }
 
     return $outfilename;
 }
 
-sub compile_wiki {
+sub compile_asciidoc {
+    my ($env) = @_;
 
-    my $verbose;
     my $outfile;
-    my $target_env;
 
     GetOptions ("outfile=s" => \$outfile,
+               "keep-artifacts" => \$keep_artifacts,
                "verbose"   => \$verbose) or
                    die("Error in command line arguments\n");
 
@@ -156,18 +261,33 @@ sub compile_wiki {
     scalar(@ARGV) == 0 or
        die "too many arguments...\n";
 
-    my $env = 'wiki';
+    my $outfilemap = $fileinfo->{outfile}->{$env}->{$infile} ||
+       die "no output file mapping '$infile => $outfile' ($env)";
 
+    if ($man_target eq 'html') {
+       $outfilemap .= '.html';
+    } elsif ($man_target eq 'wiki') {
+       $outfilemap .= '-plain.html';
+    }
 
-    my $title = $fileinfo->{titles}->{$env}->{$infile} or
-       die "unable to get title for '$infile'\n";
+    die "wrong output file name '$outfile != $outfilemap' ($env)"
+       if $outfile ne $outfilemap;
 
+    defined($fileinfo->{titles}->{$env}) ||
+       die "unknown environment '$env'";
 
-    print "compile: $title\n";
+    my $title = $fileinfo->{titles}->{$env}->{$infile} or
+       die "unable to get title for '$infile'$env\n";
+
+    debug("compile $title");
 
     my $leveloffset = 0;
 
-    my $doctype = $fileinfo->{doctype}->{$env}->{$infile} // 0;
+    my $doctype = $fileinfo->{doctype}->{$env}->{$infile};
+
+    die "unable to get document type for '$infile'\n"
+       if !defined($doctype);
+
     $leveloffset = - $doctype;
 
     my $date = `date`;
@@ -175,62 +295,279 @@ sub compile_wiki {
 
     my $attributes = {
        $env => undef,
-       icons => undef,
-       'data-uri' => undef,
-       date => $date,
        leveloffset => $leveloffset,
        revnumber => $release,
+       revdate => $date,
     };
 
-    my $cmd = ['asciidoc', '-s'];
+    my $mansection = $fileinfo->{mansection}->{$env}->{$infile};
 
-    foreach my $key (keys %$attributes) {
-       my $value = $attributes->{$key};
-       if (defined($value)) {
-           push @$cmd, '-a', "$key=$value";
-       } else {
-           push @$cmd, '-a', $key;
-       }
+    if ($env eq 'wiki') {
+    } elsif ($env eq 'manvolnum') {
+       die "undefined man section" if !defined($mansection);
+       $attributes->{manvolnum} = $mansection;
+    } elsif ($env eq 'default') {
+       die "$infile: wrong doctype\n" if $doctype != 0;
+       $attributes->{toc} = undef;
     }
 
-    push @$cmd, '--verbose' if $verbose;
-
     if (!defined($outfile)) {
        $outfile = $infile;
        $outfile =~ s/\.adoc$//;
-       $outfile .= ".html";
+       if ($env eq 'manvolnum') {
+           if (($man_target eq 'html') || ($man_target eq 'wiki')) {
+               $outfile .= ".$mansection.html";
+           } else {
+               $outfile .= ".$mansection";
+           }
+       } else {
+           $outfile .= ".html";
+       }
     }
 
-    push @$cmd, '--out-file', $outfile;
+    if (($env eq 'manvolnum') && ($man_target eq 'man')) {
+
+       # asciidoc /etc/asciidoc/docbook-xsl/manpage.xsl skip REFERENCES
+       # section like footnotes, so we cannot use a2x.
+       # We use xmlto instead.
+
+       my $cmd = ['asciidoc', '-dmanpage', '-bdocbook', '-a', 'docinfo1'];
+
+       foreach my $key (keys %$attributes) {
+           my $value = $attributes->{$key};
+           if (defined($value)) {
+               push @$cmd, '-a', "$key=$value";
+           } else {
+               push @$cmd, '-a', $key;
+           }
+       }
+
+       push @$cmd, '--verbose' if $verbose;
+
+       my $tmpxmlfile = "${outfile}.xml.tmp";
 
-    my $new_infile = prepare_adoc_file($infile, $attributes);
+       push @$cmd, '--out-file', $tmpxmlfile;
+
+       push @$files_for_cleanup, $tmpxmlfile;
+
+       my $new_infile = prepare_adoc_file($env, $infile, $attributes);
+
+       push @$cmd, $new_infile;
+
+       debug("run " . join(' ', @$cmd));
+
+       system(@$cmd) == 0 or
+           die "aciidoc error";
+
+       $cmd = ['xmlto', 'man', $tmpxmlfile];
+
+       push @$cmd, '-v' if $verbose;
+
+       debug("run " . join(' ', @$cmd));
+
+       system(@$cmd) == 0 or
+           die "xmlto error";
+
+    } else {
+
+       $attributes->{icons} = undef;
+       $attributes->{'data-uri'} = undef;
+
+       my $cmd = ['asciidoc'];
+
+       if (($env eq 'wiki') ||
+           (($env eq 'manvolnum') && ($man_target eq 'wiki'))) {
+
+           push @$cmd, '-s';
+
+           if (-f "./asciidoc/mediawiki.conf") {
+               my $cwd = getcwd();
+               push @$cmd, '-b', "$cwd/asciidoc/mediawiki";
+           } else {
+               push @$cmd, '-b', "mediawiki";
+           }
+       }
+
+       foreach my $key (keys %$attributes) {
+           my $value = $attributes->{$key};
+           if (defined($value)) {
+               push @$cmd, '-a', "$key=$value";
+           } else {
+               push @$cmd, '-a', $key;
+           }
+       }
 
-    push @$cmd, $new_infile;
+       push @$cmd, '--verbose' if $verbose;
 
-    print "RUN " . join(' ', @$cmd) . "\n";
+       push @$cmd, '--out-file', $outfile;
 
-    system(@$cmd) == 0 or
-       die "aciidoc error";
+       my $new_infile = prepare_adoc_file($env, $infile, $attributes);
+
+       push @$cmd, $new_infile;
+
+       debug("run " . join(' ', @$cmd));
+
+       system(@$cmd) == 0 or
+           die "aciidoc error";
+    }
+}
+
+sub get_links {
+
+    my $data = {};
+
+    foreach my $blockid (sort keys %{$fileinfo->{blockid_target}->{default}}) {
+       my $link = $fileinfo->{blockid_target}->{default}->{$blockid};
+       my $reftitle = $fileinfo->{reftitle}->{default}->{$blockid};
+       my $reftext = $fileinfo->{reftext}->{default}->{$blockid};
+       die "internal error" if $link !~ m/^link:/;
+       $link =~ s/^link://;
+
+       my $file = $fileinfo->{blockid}->{default}->{$blockid};
+       die "internal error - no filename" if ! defined($file);
+       my $title =  $fileinfo->{titles}->{default}->{$file} ||
+           die "internal error - no title";
+
+       $data->{$blockid}->{title} = $title;
+       $data->{$blockid}->{link} = $link;
+       my $subtitle = $reftitle || $reftext;
+       $data->{$blockid}->{subtitle} = $subtitle
+           if $subtitle && ($title ne $subtitle);
+    }
+
+    return $data;
+}
+
+sub scan_extjs_file {
+    my ($filename, $res_data) = @_;
+
+    my $fh = IO::File->new($filename, "r") ||
+       die "unable to open '$filename' - $!\n";
+
+    debug("scan-extjs $filename");
+
+    while(defined(my $line = <$fh>)) {
+       if ($line =~ m/\s+onlineHelp:\s*[\'\"](.*?)[\'\"]/) {
+           my $blockid = $1;
+           my $link = $fileinfo->{blockid_target}->{default}->{$blockid};
+           die "undefined blockid '$blockid' ($filename, line $.)\n"
+               if !(defined($link) || defined($online_help_links->{$blockid}));
+
+           $res_data->{$blockid} = 1;
+       }
+    }
 }
 
 if ($clicmd eq 'compile-wiki') {
 
-    eval { compile_wiki(); };
+    eval { compile_asciidoc('wiki'); };
     my $err = $@;
 
     cleanup();
 
     die $err if $err;
 
-} else {
+} elsif ($clicmd eq 'compile-chapter') {
 
-    die "unknown command '$clicmd'\n";
+    eval { compile_asciidoc('default'); };
+    my $err = $@;
 
-}
+    cleanup();
+
+    die $err if $err;
+
+} elsif ($clicmd eq 'compile-man-html') {
+
+    $man_target = 'html';
+
+    eval { compile_asciidoc('manvolnum'); };
+    my $err = $@;
+
+    cleanup();
+
+    die $err if $err;
+
+} elsif ($clicmd eq 'compile-man-wiki') {
+
+    $man_target = 'wiki';
+
+    eval { compile_asciidoc('manvolnum'); };
+    my $err = $@;
 
+    cleanup();
+
+    die $err if $err;
+
+} elsif ($clicmd eq 'compile-man') {
+
+    eval { compile_asciidoc('manvolnum'); };
+    my $err = $@;
+
+    cleanup();
+
+    die $err if $err;
 
+} elsif ($clicmd eq 'print-links') {
 
+    my $outfile;
+
+    GetOptions("outfile=s" => \$outfile,
+              "verbose"   => \$verbose) or
+                  die("Error in command line arguments\n");
+
+    scalar(@ARGV) == 0 or
+       die "too many arguments...\n";
 
+    my $data = get_links();
+
+    my $res = to_json($data, { pretty => 1,  canonical => 1 } );
+
+    if (defined($outfile)) {
+       my $outfh = IO::File->new("$outfile", "w") or
+           die "unable to open temporary file '$outfile'\n";
+
+       print $outfh $res;
+
+    } else {
+
+       print $res;
+    }
+
+} elsif ($clicmd eq 'scan-extjs') {
+
+    GetOptions("verbose" => \$verbose) or
+       die("Error in command line arguments\n");
+
+    my $link_hash = {};
+    my $scanned_files = {};
+    while (my $filename = shift) {
+       die "got strange file name '$filename'\n"
+           if $filename !~ m/\.js$/;
+       next if $scanned_files->{$filename};
+
+       scan_extjs_file($filename, $link_hash);
+       $scanned_files->{$filename} = 1;
+    }
+
+    my $data = get_links();
+
+    my $res_data = {};
+
+    foreach my $blockid (keys %$link_hash) {
+       $res_data->{$blockid} = $data->{$blockid} || $online_help_links->{$blockid} ||
+           die "internal error - no data for '$blockid'";
+    }
+
+    my $data_str =  to_json($res_data, { pretty => 1,  canonical => 1 });
+    chomp $data_str;
+
+    print "var pveOnlineHelpInfo = ${data_str};\n";
+
+} else {
+
+    die "unknown command '$clicmd'\n";
+
+}
 
 
 exit 0;