/[suikacvs]/markup/html/html5/spec-ja/find.cgi
Suika

Contents of /markup/html/html5/spec-ja/find.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (hide annotations) (download)
Sun Jul 20 07:45:21 2008 UTC (16 years, 3 months ago) by wakaba
Branch: MAIN
Find program first version

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/</&lt;/g;
21     $s =~ s/>/&gt;/g;
22     $s =~ s/"/&quot;/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     my $has_match;
73     for_each_data_file (sub ($) {
74     my $data_file_name = shift;
75     load_data_file ($data_file_name, my $exact_data = {}, my $pattern_data = {});
76     $pattern_data = unescape_patterns ($pattern_data);
77     $has_match |= print_matches ($data_file_name, $exact_data, $pattern_data);
78     });
79    
80     unless ($has_match) {
81     print q[<p>No match found.];
82     }
83    
84     sub unescape_patterns ($) {
85     my $pattern_data = shift;
86     my $new_data = {};
87     for (keys %$pattern_data) {
88     my $w = $_;
89     my $v = $_;
90     $v =~ s/\(\.\+\)/*/g;
91     $v =~ s/\\([\s\S])/$1/g;
92     $new_data->{$v} = $pattern_data->{$w};
93     }
94     return $new_data;
95     } # unescape_patterns
96    
97     sub print_matches ($$$) {
98     my ($file_name, $exact_data, $pattern_data) = @_;
99    
100     my $file_id = $file_name;
101     if ($file_name =~ /([0-9A-Za-z-]+)\.dat$/) {
102     $file_id = $1;
103     }
104    
105     my $has_match;
106     my $r = qq[<h2>File "<a href="edit/@{[htescape $file_id]}"><code class=file>@{[htescape $file_id]}</code></a>"</h2><table>];
107     my $result = 0;
108     my $added = {};
109     my %en2ja = (%$exact_data, %$pattern_data);
110     for (sort {$a cmp $b} keys %en2ja) {
111     if (/$pattern/) {
112     $has_match = 1;
113     $added->{$_} = 1;
114     $r .= get_match_text ($_, $en2ja{$_}, $pattern);
115     $r .= q[<tr><td colspan=2>...] and last if ++$result == $max_result;
116     }
117     }
118     $result = 0;
119     my %ja2en = reverse %en2ja;
120     for (sort {$a cmp $b} keys %ja2en) {
121     if (/$pattern/) {
122     next if $added->{$ja2en{$_}};
123     $has_match = 1;
124     $r .= get_match_text ($ja2en{$_}, $_, $pattern);
125     $r .= q[<tr><td colspan=2>...] and last if ++$result == $max_result;
126     }
127     }
128     $r .= q[</table>];
129     print $r if $has_match;
130     return $has_match;
131     } # print_matches
132    
133     sub get_match_text ($$) {
134     my ($en, $ja) = @_;
135    
136     ## NOTE: Marking will not work well if it contains &, <, >, or ", or
137     ## the pattern matches with charrefs, e.g. "t" (part of &lt; and &quot;).
138    
139     my $r = q[<tr><td lang=en>];
140     my $v = htescape ($en);
141     $v =~ s[($pattern)][<mark>$1</mark>]g;
142     $v =~ s[(&lt;[\s\S]+?&gt;)][<span class=tag>$1</span>]g;
143     $v =~ s[(&amp;[#0-9A-Za-z]+;)][<span class=ref>$1</span>]g;
144     $v =~ s[\*][<var class=pattern-star>*</var>]g;
145     $r .= $v;
146     $r .= q[<td lang=ja>];
147     my $v = htescape ($ja);
148     $v =~ s[($pattern)][<mark>$1</mark>]g;
149     $v =~ s[(&lt;[\s\S]+?&gt;)][<span class=tag>$1</span>]g;
150     $v =~ s[(&amp;[#0-9A-Za-z]+;)][<span class=ref>$1</span>]g;
151     $v =~ s{(\[\[[^\[\]]+\]\])}[<span class=rfc2119>$1</span>]g;
152     $v =~ s[(\$[0-9]+)][<var class=pattern-var>$1</var>]g;
153     $r .= $v;
154    
155     return $r;
156     } # get_match_text

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24