#!/usr/local/bin/ruby #search.rb #tDiaryのログファイルを直接検索し、該当記事をリストアップするスクリプト # #tDiaryのログディレクトリを指定 log_dir='/hoge/tdiary' #tDiaryのURLをフルパスで指定 url='http://hogehoge/tdiary/' require "kconv" require "cgi-lib" input = CGI.new #フォームからの値を受け取る keyword = input["keyword"] print "Content-type:text/html\n\n" #ヘッダの表示 print < Garbage Collection - Search
Garbage Collection - Search     back

EOS keyword = keyword.strip if keyword != "" then keyword = keyword.toeuc keyword = CGI::escapeHTML(keyword) dir_list=Array.new #ログのディレクトリをリスト file_list=Dir::entries("#{log_dir}") #ログディレクトリを抽出(4文字の数字からなるディレクトリ) file_list.each{|f| if /^\d\d\d\d/=~ f then dir_list.push(f) end } resalts=Array.new marged_resalts=Array.new #ログファイルを読み込む dir_list.each{|d| Dir.glob("#{log_dir}/#{d}/*.td2").each{|f| file=open("#{f}") dd="" tmp_dd="" tt="" resalts=Array.new #ログファイル内を検索 file.each{|line| line="#{line.gsub(/<%(.*?)%>/,'')}" line="#{line.gsub(/<(.*?)>/,'')}" if /"TDIARY2.00.00"/=~ line then next elsif /^Last-Modified:/=~ line then next elsif /^Visible: /=~ line then next elsif /^Format: /=~ line then next elsif /^Date:/=~ line then dd="#{line}" next end if /^Title:/=~ line then tt="#{line}" end if /#{keyword}/=~ line then dd="#{dd.gsub(/^Date:(.*?)/,'\1')}" dd=dd.strip tt="#{tt.gsub(/^Title:(.*?)/,'\1')}" tt=tt.strip if tt == "" then tt ="no title" end if tmp_dd != dd then resalts.push("#{dd}::#{tt}::#{line}") tmp_dd = dd end end } file.close marged_resalts.concat(resalts) } } #検索結果を表示 if marged_resalts != "" then puts "\"#{keyword}\"の検索結果

" marged_resalts.each{|resalts| a=resalts.split(/::/) date=a[0] title=a[1] content=a[2] year=date.slice(0,4) month=date.slice(4,2) day=date.slice(6,2) if /^Title:/=~ content then title="#{title.gsub(/(#{keyword})/,'\1')}" content="" else content="#{content.gsub(/(#{keyword})/,'\1')}" end print <#{year}年#{month}月#{day}日 : #{title}
#{url}?date=#{date}
#{content} ...

EOS } else print <no search resalts.


EOS end end #フッタの表示 print <

EOS