File: random_google.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 (158 lines) | stat: -rw-r--r-- 6,703 bytes parent folder | download | duplicates (3)
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# random_google.rb $Revision: 1.4 $
#
# random_google: ФñgoogleǸ󥯤
#
# Ȥ:
#   tDiary1.5.5ʹߤƳ줿conf_procбƤΤǡtDiary̤ɤ
#
# Copyright (c) 2003 Junichiro KITA <kita@kitaj.no-ip.com>
# Distributed under the GPL
#

def random_google_pickup_word(date)
	analyzer = @conf['random_google.analyzer']
	analyzer = @conf['random_google.analyzer_path'] if analyzer == "user_defined"
	analyzer = "internal" if analyzer == ""
	if analyzer == "internal"
		m = self.methods
		url_regexp = %r<(((http[s]{0,1}|ftp)://[\(\)%#!/0-9a-zA-Z_$@.&+-,'"*=;?:~-]+)|([0-9a-zA-Z_.-]+@[\(\)%!0-9a-zA-Z_$.&+-,'"*-]+\.[\(\)%!0-9a-zA-Z_$.&+-,'"*-]+))>
		@diaries[date.strftime('%Y%m%d')].to_src.
			gsub(url_regexp, '').
			scan(/(?:[-]{2,}|[-]{2,}|[0-9A-Za-z]{2,})/).sort.uniq.reject {|i| m.include?(i)}
	else
		require 'open3'
		inn, out, err = Open3.popen3("#{analyzer} | sort | uniq")
		inn.puts @diaries[date.strftime('%Y%m%d')].to_src
		inn.close
		m = self.methods
		r = out.read.map do |l|
			word = l.split[0]
			if /\s(̾|̤θ)/.match(l) and
				!/(\W|\d)/.match(word) and            # Ȥפʤ
				!/\A[-]{1,2}\z/.match(word) and   # 2ʸʲΤҤ餬ʤפǤ
				!m.include?(word)          # PluginΥ᥽åɤϽ
				word
			else
				nil
			end
		end.compact
		out.close
		err.close
		r
	end
end

def random_google_init
	@conf['random_google.n'] ||= 2
	@conf['random_google.caption'] ||= '⤷Ϣ뤫⤷ʤڡ'
	@conf['random_google.popup'] ||= 'Τ: $1'
	@conf['random_google.append'] ||= ''
	@conf['random_google.exception'] ||= ''
	if @conf.secure
		@conf['random_google.analyzer'] = "internal"
	else
		@conf['random_google.analyzer'] ||= "internal"
		@conf['random_google.analyzer_path'] ||= ""
	end
end

if /(latest|day)/ === @mode and ! @conf.bot? and ! @conf.mobile_agent?
	add_body_enter_proc do |date|
		random_google_init

		exception = @conf['random_google.exception'].split
		words = random_google_pickup_word(date) - exception
		if words.empty?
			''
		else
			r = []
			@conf['random_google.n'].times do |i|
				r << words.delete_at(rand(words.size))
			end
			append = @conf['random_google.append'].split
			rr = (r.compact.map {|i| CGI::escape(i)} + append).join('+')
			rrr = r.compact.map {|i| CGI::escapeHTML(i)}.join(' ')
			caption = @conf['random_google.caption'].gsub(/\$1/, rrr)
			popup = @conf['random_google.popup'].gsub(/\$1/, rrr)
			<<-HTML
			<div class="body-enter">
			[<a href="http://www.google.com/search?lr=lang_ja&amp;ie=euc-jp&amp;q=#{rr}" title="#{popup}">
			#{caption}
			</a>]
			</div>
			HTML
		end
	end
end

def saveconf_random_google
	if @mode == 'saveconf' then
		@conf['random_google.n'] = @cgi.params['random_google_n'][0].to_i
		@conf['random_google.caption'] = @cgi.params['random_google_caption'][0]
		@conf['random_google.popup'] = @cgi.params['random_google_popup'][0]
		@conf['random_google.append'] = @cgi.params['random_google_append'][0]
		@conf['random_google.exception'] = @cgi.params['random_google_exception'][0]
		if @conf.secure
			@conf['random_google.analyzer'] = "internal"
		else
			@conf['random_google.analyzer'] = @cgi.params['random_google_analyzer'][0]
			@conf['random_google.analyzer_path'] = @cgi.params['random_google_analyzer_path'][0]
		end
	end
end

add_conf_proc('RandomGoogle', 'Ϣ뤫⤷ʤڡ') do
	saveconf_random_google
	random_google_init

	r = <<-HTML
	<h3 class="subtitle">ɤο</h3>
	#{"<p>ʸФ륭ɤο</p>" unless @conf.mobile_agent?}
	<p><select name="random_google_n">
	HTML

	1.upto(9) do |i|
		r << %Q|		<option value="#{i}"#{@conf['random_google.n'] == i ? " selected" : ""}>#{i}</option>\n|
	end
	r << <<-HTML
	</select></p>

	<h3 class="subtitle">ɽϢ</h3>
	#{"<p>googleؤΥ󥯤򼨤ʸȥ󥯤ξ˥ޥݥ󥿤֤˥ݥåץåפʸꤷޤʸ $1 Ф줿ɤִޤ</p>" unless @conf.mobile_agent?}
	<p>󥯡<input name="random_google_caption" size="70" value="#{@conf['random_google.caption']}"></p>
	<p>ݥåץåס<input name="random_google_popup" size="70" value="#{@conf['random_google.popup']}"></p>

	<h3 class="subtitle">ɲä륭</h3>
	#{"<p>ɲäɤꤷޤʣꤹϥɤ򥹥ڡǶڤäƻꤷƤ</p>" unless @conf.mobile_agent?}
	#{"<p>)</p><pre>-site:example.com</pre>" unless @conf.mobile_agent?}
	<p><input name="random_google_append" size="70" value="#{@conf['random_google.append']}"></p>

	<h3 class="subtitle">륭</h3>
	#{"<p>ɤꤷޤʣꤹϥɤ򥹥ڡǶڤäƻꤷƤ</p>" unless @conf.mobile_agent?}
	#{"<p>)</p><pre>a the</pre>" unless @conf.mobile_agent?}
	<p><input name="random_google_exception" size="70" value="#{@conf['random_google.exception']}"></p>
	HTML

	unless @conf.secure
		r << <<-HTML
	<h3 class="subtitle">Dzϴ</h3>
	#{"<p>ɤФ˷DzϴѤ뤫ɤꤷޤDzϴѤʤ/([-]{2,}|[-]{2,}|[0-9A-Za-z]{2,})/ȤɽǥɤФޤޤ٤Ϥ褯ޤ</p>" unless @conf.mobile_agent?}
	#{"<p>ChasenMeCabɸѥ˥󥹥ȡ뤵Ƥʤϡ־ꤹפ򤷡ΡַDzϴΥѥפǷDzϴХѥǻꤷƲ</p>" unless @conf.mobile_agent?}
	#{"<p>ChasenMeCabʳηDzϴѤƱͤˡ־ꤹפ򤷡ΡַDzϴΥѥפǷDzϴХѥǻꤷƲǽΥ˷ǤϤ졤ƱԤˤηǤʻ(̾)Ϥ褦ʷDzϴǤѲǽǤ</p>" unless @conf.mobile_agent?}
	<p><select name="random_google_analyzer">
		<option value="chasen"#{@conf['random_google.analyzer'] == "chasen" ? " selected" : ""}>ChaSenѤ</option>
		<option value="mecab"#{@conf['random_google.analyzer'] == "mecab" ? " selected" : ""}>MeCabѤ</option>
		<option value="user_defined"#{@conf['random_google.analyzer'] == "user_defined" ? " selected" : ""}>ꤹ</option>
		<option value="internal"#{@conf['random_google.analyzer'] == "internal" ? " selected" : ""}>Ѥʤ</option>
 	</select></p>

	<h3 class="subtitle">DzϴΥѥ</h3>
	#{"<p>ѤDzϴХѥǻꤷޤ</p>" unless @conf.mobile_agent?}
	#{"<p>)</p><pre>/usr/local/bin/chasen</pre>" unless @conf.mobile_agent?}
	<p><input name="random_google_analyzer_path" size="70" value="#{@conf['random_google.analyzer_path']}"></p>
	HTML
	end
	r
end

# vim: ts=3