From 69cf1b3a8ba087cbb8f2ffb5f25778a7ac399273 Mon Sep 17 00:00:00 2001 From: bluesaxman Date: Fri, 3 May 2019 11:54:39 -0600 Subject: [PATCH] menu refactored --- index.pl | 75 ++++++++++++++++++++++++++++++++------------------------ 1 file changed, 43 insertions(+), 32 deletions(-) diff --git a/index.pl b/index.pl index 0f1b5c8..05a42fc 100755 --- a/index.pl +++ b/index.pl @@ -87,7 +87,7 @@ sub html_tag { sub soft_die { print http_status(500,"text/html; charset=utf-8"); print "\n"; - print html_tag("html",html_tag("body",$site500)); + print html_tag("html",html_tag("body",$site500.shift)); exit; } @@ -181,17 +181,6 @@ sub get_page { } } -############## Menu needs refactoring ################### -sub getmenu { - open(MENUFILE, "<", $data_dir."menucont") or return "There is no menu yet"; - while(my $item = ) { - push(@_,$item); - } - close(MENUFILE); - chomp @_; - @_; -} - sub menu_item { my $name = $_[0]; if ($_[0] =~ m/\//){$name = (split("/",$_[0]))[1]} @@ -215,32 +204,55 @@ sub render_menu { return $menu_content; } -sub submenu { - my $page = (split("/",shift(@_)))[1]; - $page =~ s/_dir$//g; - my @list = []; - opendir(DIR,$pages_dir.$page."_dir/") or return ""; - while (my $file = readdir(DIR)) { - next if ($file =~ m/^\./); - push(@list,$page."_dir/".$file); +sub menu { + my $current_page = (split("/",shift))[1] ? (split("/",shift))[1] : ""; + $current_page =~ s/_dir$//g; + my %menus = (); + # Handle main menu + if (-e $data_dir."menucont") { + open(MENUFILE, "<", $data_dir."menucont") or return "Problem accessing menu file:".$!; + while(my $item = ) { + push(@{$menus{"main"}},$item); + } + close(MENUFILE); + chomp @{$menus{"main"}}; + } else { + opendir(my $dir,$pages_dir) or return "Problem parsing the pages directory:".$!; + while (my $file = readdir($dir)) { + next if ($file =~ m/^\./); + next if ($file =~ m/_dir$/); + push (@{$menus{"main"}}, $file); + } + close($dir); } - closedir(DIR); - shift(@list); - return render_menu("sub",@list); + # Get any additional menus. currently supports 1 sub menu, eventually recursive + if (-d $pages_dir.$current_page."_dir") { + $menus{$current_page} = (); + opendir(my $dir,$pages_dir.$current_page."_dir") or return "Problem parsing the ".$current_page."_dir directory:".$!; + while (my $file = readdir($dir)) { + next if ($file =~ m/^\./); + next if ($file =~ m/_dir$/); + push (@{$menus{$current_page}}, $file); + } + close($dir); + } + my $buffer = ""; + # main needs to be output first then the rest of the menus can be looped through + $buffer .= render_menu("main",@{$menus{"main"}}); + delete($menus{"main"}); + for my $menu_name (sort keys %menus) { + $buffer .= render_menu($menu_name,@{$menus{$menu_name}}); + } + return $buffer; } -######################### /menu ########################### sub page_template { my $content = join("\n",@_); my $page_content = html_tag( 'div', $content -# For the main sites command prompt -# .html_tag('span'," ", {attribute=>'id', value=>"prompt"}) -# .html_tag('span',"█", {attribute=>'class', value=>"cursor"}) ,{attribute=>'id', value=>'screen'}); - $page_content .= render_menu("menu",getmenu()); #will need to me changed after menu refactor -# $page_content .= submenu(clean_input($request[4])); #comment this out if the site doesn't have submenus. + $page_content .= menu(clean_input($request[4])); $page_content .= html_tag('div',"Murkfall.net",{attribute=>'id',value=>"footer"}); return $page_content; } @@ -255,9 +267,8 @@ sub build_page { render_logo($site_symbol). page_template(@content). include(@footer_includes), -#for the main site -# {attribute=>'onkeydown',value=>"HandleStroke(event)"} - )); + ) + ); } build_page(clean_input($request[4]));