#!/usr/local/bin/perl use File::Basename; $program=basename($0); ( @ARGV==1 )||( @ARGV==2)||die <<"__USAGE__"; 名前 $program - 指定された html file の index を生成する 形式 $program [-n][-i] file 機能説明 引数で指定された html file の中から barという形式の行 を取り出し、index を生成します。 index を挿入したい部分を 囲んでおく必要があります。(上記のタグが見つからなければ、何もしません) -n オプションを指定すると、ファイルの書き換えは行わずに、書き換えた結果 を標準出力に出力します。 -i オプションを指定すると、ファイルの書き換えは行わずに、生成した index のみを標準出力に出力します。 -n オプションと -i オプションを同時に指定することはできません。 __USAGE__ # コマンドラインの解釈 for( @ARGV ){ if( /^(-n|--debug)$/ ){ $DEBUG=1; }elsif( /^(-i|--index_only)$/ ){ $INDEX_ONLY=1; }else{ $file=$_; } } # 指定されたファイルを読み込む ( -f $file )||die "Cannot find file: $file"; open( FILE,"< $file" )||die "Cannot open file to read: $file"; @lines=; close FILE; for( $i=0;$_=$lines[$i];$i++ ){ next unless //i; my $name=$1; $name=~s/\s+$//; # 行末の改行文字を削除 for( $j=$i;$_=$lines[$j];$j++ ){ last if /<\/a>/i; } my $line=(split( //i,join("",@lines[$i..$j]),2 ))[1]; #" $line=(split( /<\/a>/i,$line ))[0]; $line=~s/<\/?h\d?>//gi; # 文字サイズを変更するタグを削除 $line=~s/\s+$//; # 行末の改行文字を削除 if( $index{$name} ){ warn " is duplicated: item=\"$index{$name}\"\n"; }else{ push( @key,$name ); } $index{$name}=$line; } if( $INDEX_ONLY ){ print "\n"; exit 0; } if( $DEBUG==0 ){ open( FILE,"> $file" )||die "Cannot open file to write: $file"; select( FILE ); } for( $i=0;$_=$lines[$i];$i++ ){ if( /^$/ ){ $index=1; print $_; print "\n"; }elsif( /^$/ ){ $index=0; print $_; }elsif( $index ){ next; }else{ print $_; } } close FILE if $DEBUG==0;