サイトマップ(ディレクトリ内のファイルを調べる)
#! /usr/local/bin/perl $DIR = "../../sample"; #サイトマップを作るディレクトリ #表示しないディレクトリ @NO = ("img", "dat"); # HTML出力 print qq(Content-type: text/html; charset=Shift_JIS\n\n); print <<END; <html> <head> <title>サイトマップ</title> <style type="text/css"> <!-- table { width: 100%; font-size: 12px; text-align: left; border: 1px solid #999; } //--> </style> </head> <body> END openDir($DIR); #ディレクトリ内のHTMLファイルを表示 print <<END; </body> </html> END exit; #================================================ sub openDir { my ($i, $j, $title); print qq(<table>\n); print qq(<tr>); print qq(<th colspan="2">); my $FILE = "$_[0]/index.html"; if($_[0] eq $DIR) { #トップのディレクトリの場合の表示 print qq(サイトマップ); } elsif(-e $FILE) { #ディレクトリ内のindex.htmlのタイトルを表示 open(FILE, "<$FILE"); my @HTML = <FILE>; close(FILE); for($j = 0 ; $j < @HTML ; $j++) { if($HTML[$j] =~ /<title>(.+)<\/title>/) { $title = $1; last; } if($j + 1 == @HTML) { #タイトル無しの場合 ディレクトリ名を表示 my @dir = split(/\//, $_[0]); $title = $dir[@dir - 1]; } } print qq(<a href="$_[0]/">$title</a>); } else { #index.html無の場合 ディレクトリ名を表示 my @dir = split(/\//, $_[0]); print qq($dir[@dir - 1]); } print qq(</th>); print qq(</tr>\n); opendir(DIR, $_[0]); #ディレクトリ内にあるファイルを読み込む my @FILE = readdir(DIR); closedir(DIR); for($i = 0 ; $i < @FILE ; $i++) { #ディレクトリ内のファイルを確認 if($FILE[$i] eq "." or $FILE[$i] eq "..") { next; } my $FILE = "$_[0]/$FILE[$i]"; if(-d $FILE) { #ディレクトリの場合 for($j = 0 ; $j < @NO ; $j++) { #表示させないディレクトリを確認 if($FILE eq $DIR . "/" . $NO[$j]) { last; #一致の場合 } } if($j == @NO) { print qq(<tr>); print qq(<td width="50"></td>); print qq(<td>\n); openDir($FILE); #ディレクトリ内のHTMLファイルを表示 print qq(</td>); print qq(</tr>\n); } } elsif(-B $FILE) { #バイナリファイルの場合 表示しない } elsif($FILE[$i] =~ /\.s?html/ and !($FILE[$i] =~ /index.s?html$/)) { #HTMLファイルの場合 表示 open(FILE, "<$FILE"); my @HTML = <FILE>; close(FILE); for($j = 0 ; $j < @HTML ; $j++) { #HTMLファイル内からタイトルを探す if($HTML[$j] =~ /<title>(.+)<\/title>/) { $title = $1; print qq(<tr>); print qq(<td width="50"></td>); print qq(<td><a href="$FILE">$title</a></td>); print qq(</tr>\n); last; } } } } print qq(</table>\n); }
〔 実行する 〕