From 863d653b326c6aca4c958dcc1ecd90fd79fca918 Mon Sep 17 00:00:00 2001 From: Sergey Matveev Date: Thu, 15 May 2025 16:36:17 +0300 Subject: [PATCH] Simplify walker and relative paths calculations --- zk | 108 ++++++++++++++++++++++++++++--------------------------------- 1 file changed, 50 insertions(+), 58 deletions(-) diff --git a/zk b/zk index 5ce87c3..b5d9a01 100755 --- a/zk +++ b/zk @@ -37,18 +37,10 @@ my $DoBacks = ((not exists $ENV{ZK_DO_BACKS}) or ($ENV{ZK_DO_BACKS} eq q{1})) ? 1 : 0; my %Mtimes; -my %Lines; my %CatFiles; my %CatDirs; -sub noindex { - $_ = shift; - /(.*\/)index$/; - if (defined $1) { - return $1; - } - return $_; -} +use File::Basename; { my @ignores; @@ -60,48 +52,37 @@ sub noindex { } close $fh or croak "$!"; } - sub isignored { - my $p = shift; - foreach my $re (@ignores) { - return 1 if $p =~ m/$re/; - } - return 0; - } use File::Find; use POSIX qw(strftime); my $pth; + my $cat; my $mtime; sub wanted { my $fn = $_; - return if (($fn =~ /^[.]/) && ($fn ne q{.})); - $pth = $File::Find::name; - $pth = decode q{UTF-8}, $pth; + $pth = decode q{UTF-8}, $File::Find::name; $pth =~ s/^[.]\/?//; + foreach (split /\//, $pth) { + return if (/^[.]/); + } if (-d $fn) { - return if isignored "$pth/"; - opendir my $dh, $fn or croak "$!"; - my @_files; - my @_dirs; - while (readdir $dh) {; - next if /^[.]/; - $_ = decode q{UTF-8}, $_; - if (-d "$fn/$_") { - $_ = "$pth/$_/"; - s/^\///; - next if isignored $_; - push @_dirs, $_; - } else { - $_ = "$pth/$_"; - s/^\///; - next if isignored $_; - push @_files, $_; - } + $pth .= q{/}; + } + foreach (@ignores) { + return if $pth =~ /$_/; + } + $cat = dirname $pth; + $cat = ($cat eq q{.}) ? q{/} : "$cat/"; + if (-d $fn) { + if (not defined $CatFiles{$pth}) { + my @files; + my @dirs; + $CatFiles{$pth} = \@files; + $CatDirs{$pth} = \@dirs; } - closedir $dh or croak "$!"; - $CatFiles{"$pth/"} = \@_files; - $CatDirs{"$pth/"} = \@_dirs; + return if $pth eq q{/}; + push @{$CatDirs{$cat}}, $pth; } else { - return if isignored $pth; + push @{$CatFiles{$cat}}, $pth; (undef, undef, undef, undef, undef, undef, undef, undef, undef, $mtime, undef, undef, undef) = stat $fn or croak "$!"; $Mtimes{$pth} = strftime q{%Y-%m-%d %H:%M:%S}, gmtime $mtime; @@ -112,6 +93,15 @@ sub noindex { find(\%opts, q{.}); } +sub noindex { + my $l = shift; + if ($l =~ /(.*\/)index$/) { + return $1; + } + return $l; +} + +my %Lines; my %Links; my %Backs; for my $pth (keys %Mtimes) { @@ -208,15 +198,14 @@ sub genIndex2Buf { } sub htmlescape { - $_ = shift; - s/&/\&/g; - s//\>/g; - return $_; + my $s = shift; + $s =~ s/&/\&/g; + $s =~ s//\>/g; + return $s; } use File::Spec; -use File::Basename; sub genHTML { my $out = shift; @@ -234,16 +223,19 @@ sub genHTML { my sub makerels { @rels = (); my $rel; - foreach (@links) { - $rel = ($page eq q{/}) ? "$_" : File::Spec->abs2rel($_, $page); - if ($page !~ /\/$/) { - $rel = (length $rel > 2) ? (substr $rel, 3) : q{}; - } + my $base = $page; + if ($page =~ /\/$/) { + $base .= q{index}; + } + $base = dirname $base; + foreach (sort @links) { + $rel = File::Spec->abs2rel($_, $base); if (-d) { - if ($rel !~ /\/$/) { - $rel .= q{/}; + if ($rel eq q{.}) { + $rel = q{index}; + } else { + $rel .= q{/index}; } - $rel .= q{index}; } push @rels, $rel; } @@ -352,9 +344,9 @@ END_HTML } sub nodename { - $_ = shift; - tr'():,.'-----'; - return $_; + my $n = shift; + $n =~ tr'():,.'-----'; + return $n; } sub printMenuEntry { -- 2.48.1