File: kw.rb

package info (click to toggle)
tdiary 2.0.1-1sarge1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 7,220 kB
  • ctags: 1,667
  • sloc: ruby: 20,044; lisp: 476; makefile: 91; sql: 32; sh: 31
file content (135 lines) | stat: -rw-r--r-- 3,382 bytes parent folder | download
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# kw.rb $Revision: 1.8 $
#
# kw: keyword link generator
#   Parameters:
#     keyword: keyword or InterWikiName (separated by ':').
#     name: anchor string (optional).
#
# @options['kw.dic']
#   array of dictionary table array. an item of array is:
#
#       [key, URL, style]
#
#   key:   nil or string of key.
#          If keyword is 'foo', it has key nil.
#          If keyword is 'google:foo', it has key 'google'.
#   URL:   the URL for link. '$1' is replace by keyword.
#   style: encoding style as: 'euc-jp', 'sjis', 'jis' or nil.
#
#   if there isn't @options['kw.dic'], the plugin links to google.
#
# @options['kw.show_inter']
#   Show InterWikiName.
#   If this options is true, the keyword 'google:foo' shows 'google:foo'.
#   But it is false, that shows only 'foo'.
#   The default of this option is true.
#
# Copyright (C) 2003, TADA Tadashi <sho@spc.gr.jp>
# You can distribute this under GPL.
#

def kw_parse( str )
	kw_list = []
	str.each do |pair|
		k, u, s = pair.sub( /[\r\n]+/, '' ).split( /[ \t]+/, 3 )
		k = nil if k == '' or k == 'nil'
		s = nil if s != 'euc-jp' && s != 'sjis' && s != 'jis'
		kw_list << [k, u, s] if u
	end
	kw_list
end

def kw_generate_dic
	case @lang
	when 'en'
		kw_dic = {nil => ['http://www.google.com/search?q=$1', nil]}
	else
		kw_dic = {nil => ['http://www.google.com/search?ie=euc-jp&amp;q=$1', 'euc-jp']}
	end

	kw_list = []
	case @conf['kw.dic'].class.to_s
	when "String"
		kw_list = kw_parse( @conf['kw.dic'] )
	when "Array"
		kw_list = @conf['kw.dic']
	end
	kw_list.each do |pair|
		kw_dic[pair[0]] = pair[1..-1]
	end
	kw_dic
end

def kw( keyword, name = nil )
	@kw_dic = kw_generate_dic unless @kw_dic
	show_inter = @options['kw.show_inter'] == nil ? true : @options['kw.show_inter']

	inter, key = keyword.split( /:/, 2 )
	unless key then
		inter = nil
		key = keyword
	end
	keyword = key unless show_inter
	name = keyword unless name
	begin
		key = CGI::escape( case @kw_dic[inter][1]
			when 'euc-jp'
				#NKF::nkf( '-e', key )
				key
			when 'sjis'
				NKF::nkf( '-s', key )
			when 'jis'
				NKF::nkf( '-j', key )
			else # none
				key
		end )
	rescue NameError
		inter = nil
		retry
	end
	%Q[<a href="#{@kw_dic[inter][0].sub( /\$1/, key )}">#{name}</a>]
end

#
# config
#
unless @resource_loaded then
	def kw_label
		"ɥץ饰"
	end

	def kw_desc
		<<-HTML
		<h3>󥯥ꥹȤλ</h3>
		<p>ΥȤؤΥ󥯤򡢴ñʵҤ뤿Υץ饰(kw)Ǥ
		֥ URL 󥳡ɥפȶǶڤäƻꤷޤ㤨С</p>
		<pre>google http://www.google.com/search?ie=euc-jp&amp;q=$1 euc-jp</pre>
		<p>Ȼꤹȡ</p>
		<pre>&lt;%=kw('google:tdiary')%&gt;</pre>
		<p>Τ褦˽񤱤googletdiary򸡺󥯤ˤʤޤ
		(ˡϥˤäѤޤ)ʤnilꤹȡ
		google:פʬ񤫤ʤλ꤬Ǥޤ</p>
		HTML
	end
end

add_conf_proc( 'kw', kw_label ) do
	if @mode == 'saveconf' then
		kw_list = kw_parse( @conf.to_native( @cgi.params['kw.dic'][0] ) )
		if kw_list.empty? then
			@conf.delete( 'kw.dic' )
		else
			@conf['kw.dic'] = kw_list.collect{|a|a.join( ' ' )}.join( "\n" )
		end
	end
	dic = kw_generate_dic
	if dic[nil] then
		dic['nil'] = dic[nil]
		dic.delete( nil )
	end
	<<-HTML
	#{kw_desc}
	<p><textarea name="kw.dic" cols="70" rows="10">#{dic.collect{|a|a.flatten.join( " " )}.join( "\n" )}</textarea></p>
	HTML
end