:#!/usr/bin/env ruby : :# テキストファイル検索 CGI けんさくー :# Last Modified Sep. 27 2005 :# :# ・JavaScript でインクリメンタル grep 検索 :# (http://namazu.org/~satoru/blog/archives/000008.html) :# を元に作りました。 :# :# ・テキストボックスに文字列を入れるとマッチした行だけが表示されます。 :# ・←にある ":" をクリックすると全体が表示され、その行にジャンプします。 :# ・URL っぽい文字列があるとリンクを張ります。 :# ・めんどいのでキャッシュはしてません。 :# :# ・w3m など javascript が動かない場合は条件を入れた後 search ボタン(*)を押してください。 :# ほとんど同じ動きをすると思います。 :# * w3m 等で見ると出てきます。 :# :# ・ほとんど gonzui のパクリになったのでライセンスは GPL で。 :# ・IE 6, Firefox 1.0.7, w3m 0.5.1 で動作確認(たぶん)。 :# ・無保証なので使う人は気を付けて。 : :require "erb" :require "cgi" : :FileName = "kensakuu.rb" # ここに読み込むファイル名を書く :UseRegexp = true # 正規表現を使うなら true に : :HTML = <<'EOB' :<html> : <head> : <title>kensakuu</title> : <script type=text/javascript> :<!-- :function isearch (string) { : function quotemeta (string) { : return string.replace(/(\W)/, "\\$1"); : } : <%= : if UseRegexp : 'var pattern = new RegExp(string, "i");' : else : 'var pattern = new RegExp(quotemeta(string), "i");' : end : %> : span = document.getElementsByTagName("span"); : for (var i = 0; i < span.length; i += 1) { : var e = span[i]; : if (e.className == "line") { : if (e.title.match(pattern)) { : e.style.display = "inline"; : } else { : e.style.display = "none"; : } : } : } :} : :function before_jump (linenum) { : isearch(""); : : pattern = document.getElementById('pattern'); : pattern.value = ""; :} ://--> : </script> : </head> :<body> :<form name="search" action="./<%=self_filename%>" method="GET" onsubmit="return false;"> :pattern: <input type="text" onkeyup=isearch(this.value) id="pattern" name="pattern" value="<%=CGI.escapeHTML(cgi["pattern"])%>"> :<noscript> <input type="submit" value="search"> </noscript> :</form> :<hr> :<pre style="line-height: 100%;"> :<%=contents%> :</pre> :</body> :</html> :EOB : :cgi = CGI.new :self_filename = $0.split('/')[-1] :contents = String.new : :pattern = cgi["pattern"] :pattern = Regexp.quote(pattern) if !UseRegexp : :linenum = 0 :File.read(FileName).each do |line| : linenum += 1 : : next if pattern.length > 0 && line !~ /#{pattern}/i : : contents += %Q[<span class="line" title="#{CGI.escapeHTML(line)}">] : contents += %Q[<span style="padding-right: 0.5em;"><a style="color: gray; text-decoration: none;" name="l#{linenum}" href="./#{self_filename}#l#{linenum}" onclick="before_jump(#{linenum});">:</a></span>] : : escaped = line.gsub(%r$(https?://[A-Za-z0-9\+\$\;\?\.%,!#~*/:@&=_-]+)$){ "<a href=#{$1}>#{CGI.escapeHTML($1)}</a>"} : escaped = CGI.escapeHTML(line) if escaped == line : : contents += %Q[#{escaped}</span>] :end : :cgi.out({"type" => "text/html", "charset" => "EUC-JP"}){ERB.new(HTML).result}