]> git.proxmox.com Git - pve-docs.git/blobdiff - scan-adoc-refs
pveceph: add pg_num for easier search
[pve-docs.git] / scan-adoc-refs
index 13e6ae088a438e9cb30f4632eff92faa6adb32b2..9252701634d239e832bac4f72c14c4fac30edb8b 100755 (executable)
@@ -2,11 +2,18 @@
 
 use strict;
 use warnings;
+use Getopt::Long;
 use IO::File;
 use JSON;
 
 use Data::Dumper;
 
+my $generate_depends;
+
+GetOptions("depends=s" => \$generate_depends) or
+    die("Error in command line arguments\n");
+
+
 my $environments = {
     default => 1,
     wiki => 1,
@@ -14,27 +21,7 @@ my $environments = {
     pvelogo => 0, # ignore
 };
 
-my $fileinfo = {
-    outfile => {
-       default => {
-           "pve-admin-guide.adoc" => "pve-admin-guide.html",
-           "datacenter.cfg.adoc" => "datacenter.cfg.5.html",
-           "ha-manager.adoc" => "chapter-ha-manager.html",
-           "pct.adoc" => "chapter-pct.html",
-           "pve-bibliography.adoc" => "chapter-pve-bibliography.html",
-           "pve-firewall.adoc" => "chapter-pve-firewall.html",
-           "pve-installation.adoc" => "chapter-pve-installation.html",
-           "pvecm.adoc" => "chapter-pvecm.html",
-           "pvesm.adoc" => "chapter-pvesm.html",
-           "pveum.adoc" => "chapter-pveum.html",
-           "qm.adoc" => "chapter-qm.html",
-           "sysadmin.adoc" => "chapter-sysadmin.html",
-           "vzdump.adoc" => "chapter-vzdump.html",
-           "pmxcfs.adoc" => "chapter-pmxcfs.html",
-           "pve-faq.adoc" => "chapter-pve-faq.html",
-       },
-    },
-};
+my $fileinfo = {};
 
 my $start_env = [];
 foreach my $e (keys %$environments) {
@@ -89,8 +76,6 @@ sub pop_environment {
 sub register_include {
     my ($filename, $include_filename, $env_list) = @_;
 
-    return if $include_filename !~ m/\.adoc$/; # skip attributes.txt
-
     foreach my $e (@$env_list) {
        $fileinfo->{include}->{$e}->{$filename}->{$include_filename} = 1;
     }
@@ -127,11 +112,14 @@ sub register_title {
            die "unable to change title (no doctype)"
                if !defined($fileinfo->{doctype}->{$env}->{$filename});
        }
+    } elsif (!defined($doctype)) {
+       # change title via :title: attribute
+       $fileinfo->{titles}->{$env}->{$filename} = $title;
+    }
 
-       if (defined($doctype) && ($env eq 'manvolnum') && ($doctype == 0)) {
-           if ($title =~ m/.*\(([1-8])\)\s*$/) {
-               $fileinfo->{mansection}->{$env}->{$filename} = $1;
-           }
+    if (defined($doctype) && ($env eq 'manvolnum') && ($doctype == 0)) {
+       if ($title =~ m/.*\(([1-8])\)\s*$/) {
+           $fileinfo->{mansection}->{$env}->{$filename} = $1;
        }
     }
 
@@ -173,16 +161,16 @@ sub scan_adoc_file {
 
        # try to detect titles
        foreach my $e (@{$env_stack->[-1]}) {
-           if ($line =~ m/^=====+$/) {
+           if ($line =~ m/^===+$/) {
                register_title($filename, $e, 0, $env_last_line->{$e},
                               $env_last_blockid->{$e});
-           } elsif ($line =~ m/^-----+$/) {
+           } elsif ($line =~ m/^---+$/) {
                register_title($filename, $e, 1, $env_last_line->{$e},
                               $env_last_blockid->{$e});
-           } elsif ($line =~ m/^~~~~~+$/) {
+           } elsif ($line =~ m/^~~~+$/) {
                register_title($filename, $e, 2, $env_last_line->{$e},
                               $env_last_blockid->{$e});
-           } elsif ($line =~ m/^\^\^\^\^\^+$/) {
+           } elsif ($line =~ m/^\^\^\^+$/) {
                register_title($filename, $e, 3, $env_last_line->{$e},
                               $env_last_blockid->{$e});
            } elsif ($line =~ m/^= +(\S.*?)( +=)?$/) {
@@ -245,6 +233,7 @@ sub scan_adoc_file {
 my $scanned_files = {};
 while (my $filename = shift) {
     next if $filename !~ m/\.adoc$/; # skip attributes.txt
+    next if $filename =~ m/-(opts|synopsis)\.adoc$/;
     next if $scanned_files->{$filename};
 
     scan_adoc_file($filename);
@@ -291,9 +280,24 @@ foreach my $e (@$start_env) {
        } elsif ($e eq 'manvolnum') {
            my $realfn = $fn;
            $realfn =~ s/\.adoc$//;
-           die "toplevel file '$fn' is marhes as manual page!" if !$mansection;
+           die "toplevel file '$fn' is not marked as manual page!" if !$mansection;
            $realfn .= ".$mansection";
            $fileinfo->{outfile}->{$e}->{$fn} = $realfn;
+       } elsif ($e eq 'default') {
+           my $realfn = $fn;
+           $realfn =~ s/\.adoc$//;
+           if (defined($mansection) && ($mansection == 5)) {
+               $realfn .= ".$mansection";
+               $realfn = "$realfn.html";
+           } else {
+               if (($fn ne 'pve-admin-guide.adoc') &&
+                   $fileinfo->{doctype}->{$e}->{$fn} == 0) {
+                   $realfn = "chapter-$realfn.html";
+               } else {
+                   $realfn = "$realfn.html";
+               }
+           }
+           $fileinfo->{outfile}->{$e}->{$fn} = $realfn;
        }
     }
 }
@@ -326,5 +330,163 @@ foreach my $e (@$start_env) {
     }
 }
 
+my $makevar_hash = {};
+
+sub makevar_define {
+    my ($varname) = @_;
+
+    die "makefile variable '$varname' already defined\n"
+       if exists($makevar_hash->{$varname});
+
+    $makevar_hash->{$varname} = {};
+}
+
+sub makevar_list_add {
+    my ($varname, $value) = @_;
+
+    die "makefile variable '$varname' not defined\n"
+       if !exists($makevar_hash->{$varname});
+
+    $makevar_hash->{$varname}->{$value} = 1;
+}
+
+sub makevar_dump {
+    my $txt = '';
+
+    foreach my $varname (sort keys %$makevar_hash) {
+       $txt .= "$varname =";
+       foreach my $value (sort keys %{$makevar_hash->{$varname}}) {
+           $txt .= " \\\n\t$value";
+       }
+       $txt .= "\n\n";
+    }
+
+    return $txt;
+}
+
+if ($generate_depends) {
+
+    my $tmpfilename = "${generate_depends}.tmp";
+    my $outfh = IO::File->new($tmpfilename, "w") ||
+       die "unable to open temporary file '$tmpfilename'\n";
+
+    my $depends = {};
+    foreach my $e (@$start_env) {
+       my $env_data = $fileinfo->{include}->{$e};
+
+       my $add_depends;
+
+       $add_depends = sub {
+           my ($fn, $dep) = @_;
+
+           $depends->{$fn}->{$dep} = 1;
+           foreach my $nd (keys %{$env_data->{$dep}}) {
+               &$add_depends($fn, $nd);
+           }
+       };
+
+       foreach my $fn (keys %$env_data) {
+           foreach my $dep (keys %{$env_data->{$fn}}) {
+               &$add_depends($fn, $dep);
+           }
+       }
+    }
+
+    my $depend_varname_hash = {};
+
+    foreach my $fn (sort keys %$depends) {
+       my $basename = uc($fn);
+       $basename =~s/\.adoc$//i;
+       $basename =~s/[^A-Za-z0-9]/_/g;
+
+       my $varname1 = "${basename}_ADOCSOURCES";
+       my $varname2 = "${basename}_ADOCDEPENDS";
+
+       makevar_define($varname1);
+       makevar_define($varname2);
+
+       $depend_varname_hash->{$fn} = $varname2;
+
+       makevar_list_add($varname1, $fn);
+       makevar_list_add($varname2, "\$\{$varname1\}");
+
+       foreach my $dep (sort keys %{$depends->{$fn}}) {
+           if ($dep =~ m/-(opts|synopsis).adoc$/) {
+               makevar_list_add($varname2, $dep);
+           } else {
+               makevar_list_add($varname1, $dep);
+           }
+       }
+    }
+
+    my $man_sources_hash = {};
+    foreach my $sourcefile (keys %{$fileinfo->{outfile}->{manvolnum}}) {
+       $man_sources_hash->{$sourcefile} = 1;
+       my $ihash = $fileinfo->{include}->{manvolnum}->{$sourcefile};
+       foreach my $include (keys %$ihash) {
+           if ($include !~ m/-(opts|synopsis).adoc$/) {
+               $man_sources_hash->{$include} = 1;
+           }
+       }
+    }
+
+    my $varname = "MANUAL_SOURCES";
+    makevar_define($varname);
+    foreach my $sourcefile (sort keys %$man_sources_hash) {
+       makevar_list_add($varname, $sourcefile);
+    }
+
+    $varname = "CHAPTER_LIST";
+    makevar_define($varname);
+    my $filelist = $fileinfo->{outfile}->{default};
+    foreach my $sourcefile (sort keys %$filelist) {
+       my $target = $filelist->{$sourcefile};
+       makevar_list_add($varname, $target);
+    }
+
+    $varname = "MANUAL_PAGES";
+    makevar_define($varname);
+    $filelist = $fileinfo->{outfile}->{manvolnum};
+    foreach my $manpage (sort keys %$filelist) {
+       my $target = $filelist->{$manpage};
+       makevar_list_add($varname, $target);
+    }
+
+    $varname = "WIKI_IMPORTS";
+    makevar_define($varname);
+    $filelist = $fileinfo->{outfile}->{wiki};
+    foreach my $sourcefile (sort keys %$filelist) {
+       my $target = $filelist->{$sourcefile};
+       makevar_list_add($varname, $target);
+    }
+
+    my $res = makevar_dump();
+
+    my $make_targets = {};
+    foreach my $e (@$start_env) {
+       my $filelist = $fileinfo->{outfile}->{$e};
+       foreach my $sourcefile (sort keys %$filelist) {
+           my $varname = $depend_varname_hash->{$sourcefile};
+           next if !defined($varname);
+           my $target = $filelist->{$sourcefile};
+           my $dep = "\$\{$varname\}";
+           $make_targets->{$target} = $dep;
+           if ($e eq 'manvolnum') {
+               $make_targets->{"$target.html"} = $dep;
+               $make_targets->{"$target-plain.html"} = $dep;
+           }
+       }
+    }
+    foreach my $target (sort keys%$make_targets) {
+       my $dep = $make_targets->{$target};
+       $res .= "$target: $dep\n\n";
+    }
+
+    print $outfh $res;
+    close($outfh);
+
+    rename($tmpfilename, $generate_depends) ||
+       die "rename failed - $!";
+}
 
 print to_json($fileinfo, { pretty => 1,  canonical => 1 } );