File: recent_namazu.rb

package info (click to toggle)
tdiary 5.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,092 kB
  • sloc: ruby: 23,031; javascript: 1,029; xml: 325; makefile: 26; sh: 2
file content (49 lines) | stat: -rw-r--r-- 1,313 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# recent_namazu.rb
#
# recent_namazu: Namazu検索語新しい順
# 		 namazi.cgiが作成する検索キーワードログ(NMZ.slog)から
#		 最新xx件分の検索語を表示します。
# パラメタ:
#   file:       検索キーワードログファイル名(絶対パス表記)
#   namazu:     なまずcgi名
#   limit:      表示件数(未指定時:5)
#   sep:        セパレータ(未指定時:空白)
#   make_link:  <a>を生成するか?(未指定時:生成する)
#
#
# Copyright (c) 2002 Hiroyuki Ikezoe <zoe@kasumi.sakura.ne.jp>
# Distributed under the GPL2 or any later version.

def recent_namazu(file, namazu, limit = 5, sep='&nbsp;', make_link = true)
	begin
		lines = []
		log = open(file)
		if log.stat.size > 300 * limit then
			log.seek(-300 * limit,IO::SEEK_END)
		end
		log.each_line do |line|
			lines << line
		end

		result = []
		lines.reverse.each_with_index do |line,idx|
			break if idx >= limit
			word = line.split(/\t/)[0]
			if make_link
				result << %Q[<a href="#{h( namazu )}?query=#{u( word )}">#{h( word )}</a>]
			else
				result << h( word )
			end
		end
		result.join( sep )
	rescue
		%Q[<p class="message">#$! (#{$!.class})<br>cannot read #{file}.</p>]
	end
end

# Local Variables:
# mode: ruby
# indent-tabs-mode: t
# tab-width: 3
# ruby-indent-level: 3
# End: