~woffs/wcm

d7de68560e731f7d1ab34acaa61a5dc84ec75220 — Frank Doepper 10 years ago 0b36d05 20131111
expose hidden subdirs if hit
1 files changed, 23 insertions(+), 19 deletions(-)

M index.pl
M index.pl => index.pl +23 -19
@@ 4,9 4,9 @@
# http://woffs.de/WCM
# <wcm@woffs.de>
#
# Version 20130404
# Version 20131111
# 
# Copyright © 2010-2012 WofFS
# Copyright © 2010-2013 WofFS
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as


@@ 36,8 36,7 @@ my $menulevel=1;               # how many menulevels to show

### SUB ###

my $fourzerofour='page not found';
my $content=$fourzerofour;     # content fill-in
my $content='page not found';  # content fill-in
my $current='';                # selected page
my $lm=0;                      # last modified
my $redir='';                  # redirect?


@@ 49,9 48,7 @@ my $pagetitle='';              # title for HTML
sub walktree ($$$);            # prototype needed because of recursion

sub walktree ($$$) {
  my $dir=shift;
  my $indent=shift;
  my $hiddendir=shift;
  my ($dir,$indent,$hiddendir)=@_;
  opendir (my $dh,$dir) or return '';
  my $lnavi='';
  my $hit;


@@ 69,6 66,7 @@ sub walktree ($$$) {
    my $selected='';                       # highlight selected link
    (my $bhref=$href)=~s,.*/,,;
    $fallback=$href if lc $bhref eq lc $bcurrent;
    my $isdir=-d "$dir/$file";

    #
    # look for $content


@@ 78,7 76,7 @@ sub walktree ($$$) {
        or ($current eq '' and $href eq 'index')) {  # root directory index
      $selected=' class="selected"';
      $pagetitle=" - $title" unless $title eq 'index';
      if (-f "$dir/$file") {
      if (-f _) {
        #
        # *.pl, *.sh, *.bash, *.cgi, *.php, *.mphp: system()
        # other: just slurp in


@@ 122,21 120,27 @@ sub walktree ($$$) {
    #
    # fill $navi
    #
    unless ($hidden or $hiddendir) {
      $lnavi.=" "." "x$indent."<li class=\"indent$indent\">"
            ."<a$selected href=\"$abs$href\">$title</a>";
    # recurse into subtree and get sublist
    my ($subhit,$subnavi);
    if ($isdir) {
      ($subhit,$subnavi)=walktree("$dir/$file",($indent+1),($hidden or $hiddendir));
      $hit||=$subhit;
    }
    my $submenu=walktree("$dir/$file",($indent+1),($hidden or $hiddendir)) if -d "$dir/$file";
    if ($submenu) {
      $hit=1;
      $lnavi.=$submenu;
    # add entry for this file if not hidden, and include sublist
    # expose hidden subtree if we hit a non-hidden file inside
    if (!$hidden or ($hidden and $isdir and !$hiddendir and $subhit)) {
      $lnavi.=" "." "x$indent."<li class=\"indent$indent\">"
              ."<a$selected href=\"$abs$href\">$title</a>";
      if ($subnavi and ($indent<$menulevel or $subhit)) {
        $lnavi.=$subnavi;
      }
      $lnavi.="</li>\n";
    }
    $lnavi.="</li>\n" unless $hidden or $hiddendir;
  }
  closedir $dh;
  # nice indentation in HTML source
  $lnavi="\n"." "x$indent."<ul>\n".$lnavi." "x$indent."</ul>" if $lnavi;
  return ($hit or $indent <= $menulevel) ? $lnavi : undef;
  return ($hit,$lnavi);
}

### BEGIN ###


@@ 176,7 180,7 @@ $abs.='?page=' if $abs =~ /index\.pl$/;
$abs.='page=' if $abs =~ /index\.pl\?$/;

# traverse directories and fill $navi and $content
my $navi=walktree($src,0,0);
my ($found,$navi)=(walktree($src,0,0));

# spit it out
if ($redir) {


@@ 184,7 188,7 @@ if ($redir) {
  print 'Location: '.($redir=~/^http/ ? '' : $abs)."$redir\n\n";
} else {
  print "Content-Type: text/html; charset=utf-8\n";
  if ($content eq $fourzerofour) {
  if (!$found) {
    if ($fallback) {
      $fallback=~s,^/,,;
      print "Location: $abs$fallback\n";