小一時間で作った超着陸版の検索エンジン

小一時間で作った超着陸版の検索エンジン
数千のオーダーならこの程度のエンジンで十分

 #!/usr/bin/ruby -Ks
 require 'cgi'
 cgi=CGI.new
 if cgi.has_key?('key') && cgi['key'][0]!=""
 
   ###########################
   ## keyがあるときのページ ##
   ###########################
   #searchkey=['service','object','function','timestamp']
   keyarray=cgi['key'][0].split(/ | /)
   index=Hash.new
   0.upto(keyarray.size-1){|i|
     if keyarray[i]=~/([^:]+):(.+)/
       index[$1]=$2
       keyarray.delete_at(i)
     end
   }
   temp =""
   results=[]
   Dir.glob("/var/www/katana/htdocs/manual/*"){|filename|
     File.open(filename){|file|
       all = file.readlines.join
       flag=true
       keyarray.each{|i|
 	if flag==true && (all !~ /#{i}/m)
 	  flag=false
 	end
       }
       if flag
 	results.push("<a href=\"#{filename.gsub(/\/var\/www\/katana\/htdocs/,&#39;&#39;)}\">#{/<title>([^<]+)<\/title>/m.match(all)[1]}</a>")
       end
     }
   }
 
 print <<_RESULTS_
 content-type: text/html
 
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 <html>
  <head>
   <meta http-equiv="content-type" content="text/html; charset=Shift_JIS">
   <title>検索結果</title>
  </head>
  <body>
   <form action="/cgi-bin/manual/search.cgi" method=get>
    <input type=text name="key" size=30>
    <input type=submit name="u" value="再検索">
   </form>
   <dl>
    <dt>keyの値</dt><dd>"#{cgi[&#39;key&#39;].join(&#39;","&#39;)}"</dd>
    <dt>検索キーワード</dt><dd>"#{keyarray.join(&#39;","&#39;)}"</dd>
    <dt>インデックス</dt><dd>#{index}</dd>
   </dl>
   <p>
   #{ results.sort.join(&#39;<br>&#39;) }
   </p>