1 |
wakaba |
1.1 |
#!/usr/bin/perl |
2 |
|
|
use strict; |
3 |
|
|
|
4 |
|
|
BEGIN { require 'common.pl' } |
5 |
|
|
|
6 |
|
|
require Encode; |
7 |
|
|
|
8 |
|
|
my $max_result = 30; |
9 |
|
|
|
10 |
|
|
sub decode_url ($) { |
11 |
|
|
my $s = shift; |
12 |
|
|
$s =~ tr/+/ /; |
13 |
|
|
$s =~ s/%([0-9A-Fa-f]{2})/pack 'C', hex $1/ge; |
14 |
|
|
return Encode::decode ('utf-8', $s); |
15 |
|
|
} # decode_url |
16 |
|
|
|
17 |
|
|
sub htescape ($) { |
18 |
|
|
my $s = shift; |
19 |
|
|
$s =~ s/&/&/g; |
20 |
|
|
$s =~ s/</</g; |
21 |
|
|
$s =~ s/>/>/g; |
22 |
|
|
$s =~ s/"/"/g; |
23 |
|
|
return $s; |
24 |
|
|
} # htescape |
25 |
|
|
|
26 |
|
|
my $param = {}; |
27 |
|
|
for (split /[&;]/, $ENV{QUERY_STRING} || '') { |
28 |
|
|
my ($name, $value) = split /=/, $_, 2; |
29 |
|
|
$param->{decode_url ($name)} = decode_url ($value); |
30 |
|
|
} |
31 |
|
|
|
32 |
|
|
my $pattern = quotemeta normalize $param->{word}; |
33 |
|
|
my $eword = htescape $param->{word}; |
34 |
|
|
|
35 |
|
|
print qq[Content-Type: text/html ; charset=utf-8 |
36 |
|
|
|
37 |
|
|
<!DOCTYPE HTML> |
38 |
|
|
<html lang=en> |
39 |
|
|
<head> |
40 |
|
|
<title>Search result for "$eword"</title> |
41 |
|
|
<link rel=stylesheet href="/www/style/html/xhtml"> |
42 |
|
|
<style> |
43 |
|
|
mark { |
44 |
|
|
background-color: yellow; |
45 |
|
|
} |
46 |
|
|
.tag { |
47 |
|
|
color: maroon; |
48 |
|
|
} |
49 |
|
|
.ref { |
50 |
|
|
color: orange; |
51 |
|
|
} |
52 |
|
|
.rfc2119 { |
53 |
|
|
font-weight: bolder; |
54 |
|
|
} |
55 |
|
|
.pattern-star { |
56 |
|
|
font-style: normal; |
57 |
|
|
color: gray; |
58 |
|
|
text-decoration: none; |
59 |
|
|
border-style: none; |
60 |
|
|
} |
61 |
|
|
.pattern-var { |
62 |
|
|
font-style: italic; |
63 |
|
|
color: gray; |
64 |
|
|
text-decoration: none; |
65 |
|
|
border-style: none; |
66 |
|
|
} |
67 |
|
|
</style> |
68 |
|
|
</head> |
69 |
|
|
<body> |
70 |
|
|
<h1>Search result for "$eword"</h1>]; |
71 |
|
|
|
72 |
wakaba |
1.2 |
print_input_form (); |
73 |
|
|
|
74 |
|
|
print q[<article>]; |
75 |
wakaba |
1.1 |
my $has_match; |
76 |
|
|
for_each_data_file (sub ($) { |
77 |
|
|
my $data_file_name = shift; |
78 |
|
|
load_data_file ($data_file_name, my $exact_data = {}, my $pattern_data = {}); |
79 |
|
|
$pattern_data = unescape_patterns ($pattern_data); |
80 |
|
|
$has_match |= print_matches ($data_file_name, $exact_data, $pattern_data); |
81 |
|
|
}); |
82 |
|
|
|
83 |
|
|
unless ($has_match) { |
84 |
wakaba |
1.2 |
print q[<p>No match found.</article>]; |
85 |
|
|
} else { |
86 |
|
|
print q[</article>]; |
87 |
|
|
print_input_form (); |
88 |
wakaba |
1.1 |
} |
89 |
|
|
|
90 |
|
|
sub unescape_patterns ($) { |
91 |
|
|
my $pattern_data = shift; |
92 |
|
|
my $new_data = {}; |
93 |
|
|
for (keys %$pattern_data) { |
94 |
|
|
my $w = $_; |
95 |
|
|
my $v = $_; |
96 |
|
|
$v =~ s/\(\.\+\)/*/g; |
97 |
|
|
$v =~ s/\\([\s\S])/$1/g; |
98 |
|
|
$new_data->{$v} = $pattern_data->{$w}; |
99 |
|
|
} |
100 |
|
|
return $new_data; |
101 |
|
|
} # unescape_patterns |
102 |
|
|
|
103 |
|
|
sub print_matches ($$$) { |
104 |
|
|
my ($file_name, $exact_data, $pattern_data) = @_; |
105 |
|
|
|
106 |
|
|
my $file_id = $file_name; |
107 |
|
|
if ($file_name =~ /([0-9A-Za-z-]+)\.dat$/) { |
108 |
|
|
$file_id = $1; |
109 |
|
|
} |
110 |
|
|
|
111 |
|
|
my $has_match; |
112 |
|
|
my $r = qq[<h2>File "<a href="edit/@{[htescape $file_id]}"><code class=file>@{[htescape $file_id]}</code></a>"</h2><table>]; |
113 |
|
|
my $result = 0; |
114 |
|
|
my $added = {}; |
115 |
|
|
my %en2ja = (%$exact_data, %$pattern_data); |
116 |
|
|
for (sort {$a cmp $b} keys %en2ja) { |
117 |
|
|
if (/$pattern/) { |
118 |
|
|
$has_match = 1; |
119 |
|
|
$added->{$_} = 1; |
120 |
|
|
$r .= get_match_text ($_, $en2ja{$_}, $pattern); |
121 |
|
|
$r .= q[<tr><td colspan=2>...] and last if ++$result == $max_result; |
122 |
|
|
} |
123 |
|
|
} |
124 |
|
|
$result = 0; |
125 |
|
|
my %ja2en = reverse %en2ja; |
126 |
|
|
for (sort {$a cmp $b} keys %ja2en) { |
127 |
|
|
if (/$pattern/) { |
128 |
|
|
next if $added->{$ja2en{$_}}; |
129 |
|
|
$has_match = 1; |
130 |
|
|
$r .= get_match_text ($ja2en{$_}, $_, $pattern); |
131 |
|
|
$r .= q[<tr><td colspan=2>...] and last if ++$result == $max_result; |
132 |
|
|
} |
133 |
|
|
} |
134 |
|
|
$r .= q[</table>]; |
135 |
|
|
print $r if $has_match; |
136 |
|
|
return $has_match; |
137 |
|
|
} # print_matches |
138 |
|
|
|
139 |
|
|
sub get_match_text ($$) { |
140 |
|
|
my ($en, $ja) = @_; |
141 |
|
|
|
142 |
|
|
## NOTE: Marking will not work well if it contains &, <, >, or ", or |
143 |
|
|
## the pattern matches with charrefs, e.g. "t" (part of < and "). |
144 |
|
|
|
145 |
|
|
my $r = q[<tr><td lang=en>]; |
146 |
|
|
my $v = htescape ($en); |
147 |
|
|
$v =~ s[($pattern)][<mark>$1</mark>]g; |
148 |
|
|
$v =~ s[(<[\s\S]+?>)][<span class=tag>$1</span>]g; |
149 |
|
|
$v =~ s[(&[#0-9A-Za-z]+;)][<span class=ref>$1</span>]g; |
150 |
|
|
$v =~ s[\*][<var class=pattern-star>*</var>]g; |
151 |
|
|
$r .= $v; |
152 |
|
|
$r .= q[<td lang=ja>]; |
153 |
|
|
my $v = htescape ($ja); |
154 |
|
|
$v =~ s[($pattern)][<mark>$1</mark>]g; |
155 |
|
|
$v =~ s[(<[\s\S]+?>)][<span class=tag>$1</span>]g; |
156 |
|
|
$v =~ s[(&[#0-9A-Za-z]+;)][<span class=ref>$1</span>]g; |
157 |
|
|
$v =~ s{(\[\[[^\[\]]+\]\])}[<span class=rfc2119>$1</span>]g; |
158 |
|
|
$v =~ s[(\$[0-9]+)][<var class=pattern-var>$1</var>]g; |
159 |
|
|
$r .= $v; |
160 |
|
|
|
161 |
|
|
return $r; |
162 |
|
|
} # get_match_text |
163 |
wakaba |
1.2 |
|
164 |
|
|
sub print_input_form () { |
165 |
|
|
print qq[<section><form action=find accept-charset=utf-8 method=get>]; |
166 |
|
|
print qq[<p><input type=text name=word value="@{[htescape $param->{word}]}">]; |
167 |
|
|
print qq[<p><label><input type=checkbox name=cs @{[$param->{cs} ? 'checked' : '']}> Case-sensitive</label> |
168 |
|
|
<label><input type=checkbox name=aw @{[$param->{aw} ? 'checked' : '']}> As word(s)</label>]; |
169 |
|
|
print qq[</form></section>]; |
170 |
|
|
} # print_input_form |