]> Sergey Matveev's repositories - swg.git/commitdiff
Info output
authorSergey Matveev <stargrave@stargrave.org>
Mon, 12 May 2025 15:14:26 +0000 (18:14 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Mon, 12 May 2025 15:14:26 +0000 (18:14 +0300)
USAGE
zk

diff --git a/USAGE b/USAGE
index 6e0c5257ffbef1d43070886fe23fc83d9970654e..77b185bec71a8c61796789e6ee3508ecc8e3cd95 100644 (file)
--- a/USAGE
+++ b/USAGE
@@ -8,6 +8,8 @@
   Render current directory to a bunch of HTMLs.
 * zk dot | dot -Tpng >relations.png
   Generates Graphviz'es dot graph with internal links relationships.
+* zk info >out.info
+  Generates Info output.
 
 If directory does not have "index" file, then it will be autogenerated
 with all files in the directory (category) and all backlinks to it.
diff --git a/zk b/zk
index 8c4c471404bc176c1a7c3b51f7f1cac35aabc4cb..4a66592a154959ca469d83dbcb70f15af7250b02 100755 (executable)
--- a/zk
+++ b/zk
@@ -14,7 +14,8 @@ sub usage {
 \t$0 backs PAGE
 \t$0 cats [PREFIX]
 \t$0 htmls DIR
-\t$0 dot | dot -Tpng >png
+\t$0 dot | dot -Tpng >relations.png
+\t$0 info >out.info
 
 By default ZK_DO_BACKS=1 is set.
 Do not forget about .zkignore with regular expressions.
@@ -173,6 +174,15 @@ sub genIndex {
     print $out "do-backs\r\n";
 }
 
+sub genIndex2Buf {
+    my $p = shift;
+    my $buf = "";
+    open(my $fh, ">:encoding(UTF-8)", \$buf) or die "$!";
+    genIndex $fh, $p;
+    close $fh;
+    return $buf;
+}
+
 sub htmlescape {
     $_ = shift;
     s/&/\&amp;/g;
@@ -308,13 +318,75 @@ sub genHTML {
     return $backsWereGenerated;
 }
 
-sub genIndex2Buf {
-    my $p = shift;
-    my $buf = "";
-    open(my $fh, ">:encoding(UTF-8)", \$buf) or die "$!";
-    genIndex $fh, $p;
+sub nodename {
+    $_ = $_[0];
+    tr/./-/;
+    return $_;
+}
+
+sub genInfo {
+    my $page = shift;
+    my %links;
+    if ($page =~ /\/$/) {
+        foreach ((@{$CatFiles{$page}}, @{$CatDirs{$page}})) {
+            $links{$_} = 1;
+        }
+    } elsif (exists $Links{noindex $page}) {
+        foreach (@{$Links{noindex $page}}) {
+            $links{$_} = 1;
+        }
+    }
+    open(my $fh, "<:encoding(UTF-8)", $page) or die "$!";
+    while (<$fh>) {
+        chop;
+        if (/\r$/) {
+            chop;
+            /^\s*(.*)$/;
+            if ($1 =~ /^do-backs/) {
+                next;
+            }
+            if ($1 =~ /^#/) {
+                next;
+            }
+        }
+        s/\[([^]]+)\]/my $n = nodename $1; (exists $links{$1}) ? ("*note ${n}::") : $1/ge;
+        print "$_\n";
+    }
     close $fh;
-    return $buf;
+    my @backs = sort keys %{$Backs{noindex $page}};
+    if ($#backs != -1) {
+        print "\n* Menu:\nBacklinks:\n";
+        foreach (@backs) {
+            print "* " . (nodename $_) . ": " . (nodename $_) . ".\n";
+        }
+        print "\n";
+    }
+}
+
+sub genInfoIndex {
+    my $page = shift;
+    my $pth;
+    print "* Menu:\n\n";
+    foreach (sort @{$CatFiles{$page}}) {
+        print "* " . (nodename $_) . ": " . (nodename $_) . ".\n";
+    }
+    print "\n";
+    my @links = sort @{$CatDirs{$page}};
+    if ($#links != -1) {
+        print "\n* Menu:\nSubcategories:\n";
+        foreach (@links) {
+            print "* " . (nodename $_) . ": " . (nodename $_) . ".\n";
+        }
+        print "\n";
+    }
+    @links = sort keys %{$Backs{noindex $page}};
+    if ($#links != -1) {
+        print "\n* Menu:\nBacklinks:\n";
+        foreach (@links) {
+            print "* " . (nodename $_) . ": " . (nodename $_) . ".\n";
+        }
+        print "\n";
+    }
 }
 
 if ($ARGV[0] eq "dump") {
@@ -385,6 +457,49 @@ if ($ARGV[0] eq "dump") {
         copy $pth, "$ARGV[1]/$pth.txt" or die "$!";
         utime $s[9], $s[9], "$ARGV[1]/$pth.txt";
     }
+} elsif ($ARGV[0] eq "info") {
+    print "Autogenerated by zk\n";
+    my $sep = "\n\1f\n";
+    print "${sep}File: self, Node: Top, Up: (dir)\n\n";
+    if (exists $Mtimes{"index"}) {
+        genInfo "index";
+        delete $Mtimes{"index"};
+        delete $CatFiles{"/"};
+        delete $CatDirs{"/"};
+    } else {
+        genInfoIndex "/";
+        delete $CatFiles{"/"};
+        delete $CatDirs{"/"};
+    }
+    sub up {
+        my $p = dirname noindex $_[0];
+        if (($p eq ".") or ($p eq "/")) {
+            return "Top";
+        }
+        return (nodename $p) . "/";
+    }
+    foreach my $cat (keys %CatFiles) {
+        next if (exists $Mtimes{"${cat}index"});
+        print "${sep}File: self, Node: " . (nodename $cat) .
+            ", Up: " . (up $cat) . "\n\n";
+        genInfoIndex $cat;
+    }
+    foreach my $page (keys %Mtimes) {
+        print "${sep}File: self, Node: " . (nodename noindex $page) .
+            ", Up: " . (up $page) . "\n\n";
+        genInfo $page;
+    }
+    print "${sep}File: self, Node: index, Up: Top\n\n";
+    print "\0\b[index\0\b]\n* Menu:\n";
+    foreach my $cat (keys %CatFiles) {
+        print "* " . (nodename $cat) . ": " .
+            (nodename $cat) . ". (line 0)\n";
+    }
+    foreach my $page (keys %Mtimes) {
+        print "* " . (nodename noindex $page) . ": " .
+            (nodename noindex $page) . ". (line 0)\n";
+    }
+    print "${sep}Local Variables:\ncoding: utf-8\nEnd:\n";
 } elsif ($ARGV[0] eq "dot") {
     print "digraph d {\n";
     print "rankdir=LR\n";