1 | #! /usr/bin/perl -w |
---|
2 | |
---|
3 | # Spell Checker Plugin for HTMLArea-3.0 |
---|
4 | # Sponsored by www.americanbible.org |
---|
5 | # Implementation by Mihai Bazon, http://dynarch.com/mishoo/ |
---|
6 | # |
---|
7 | # (c) dynarch.com 2003. |
---|
8 | # Distributed under the same terms as HTMLArea itself. |
---|
9 | # This notice MUST stay intact for use (see license.txt). |
---|
10 | # |
---|
11 | |
---|
12 | use strict; |
---|
13 | use utf8; |
---|
14 | use Encode; |
---|
15 | use Text::Aspell; |
---|
16 | use XML::DOM; |
---|
17 | use CGI; |
---|
18 | |
---|
19 | my $TIMER_start = undef; |
---|
20 | eval { |
---|
21 | use Time::HiRes qw( gettimeofday tv_interval ); |
---|
22 | $TIMER_start = [gettimeofday()]; |
---|
23 | }; |
---|
24 | # use POSIX qw( locale_h ); |
---|
25 | |
---|
26 | binmode STDIN, ':utf8'; |
---|
27 | binmode STDOUT, ':utf8'; |
---|
28 | |
---|
29 | my $debug = 0; |
---|
30 | |
---|
31 | my $speller = new Text::Aspell; |
---|
32 | my $cgi = new CGI; |
---|
33 | |
---|
34 | my $total_words = 0; |
---|
35 | my $total_mispelled = 0; |
---|
36 | my $total_suggestions = 0; |
---|
37 | my $total_words_suggested = 0; |
---|
38 | |
---|
39 | # FIXME: report a nice error... |
---|
40 | die "Can't create speller!" unless $speller; |
---|
41 | |
---|
42 | my $dict = $cgi->param('dictionary') || $cgi->cookie('dictionary') || 'en'; |
---|
43 | |
---|
44 | # add configurable option for this |
---|
45 | $speller->set_option('lang', $dict); |
---|
46 | $speller->set_option('encoding', 'UTF-8'); |
---|
47 | #setlocale(LC_CTYPE, $dict); |
---|
48 | |
---|
49 | # ultra, fast, normal, bad-spellers |
---|
50 | # bad-spellers seems to cause segmentation fault |
---|
51 | $speller->set_option('sug-mode', 'normal'); |
---|
52 | |
---|
53 | my %suggested_words = (); |
---|
54 | keys %suggested_words = 128; |
---|
55 | |
---|
56 | my $file_content = decode('UTF-8', $cgi->param('content')); |
---|
57 | $file_content = parse_with_dom($file_content); |
---|
58 | |
---|
59 | my $ck_dictionary = $cgi->cookie(-name => 'dictionary', |
---|
60 | -value => $dict, |
---|
61 | -expires => '+30d'); |
---|
62 | |
---|
63 | print $cgi->header(-type => 'text/html; charset: utf-8', |
---|
64 | -cookie => $ck_dictionary); |
---|
65 | |
---|
66 | my $js_suggested_words = make_js_hash(\%suggested_words); |
---|
67 | my $js_spellcheck_info = make_js_hash_from_array |
---|
68 | ([ |
---|
69 | [ 'Total words' , $total_words ], |
---|
70 | [ 'Mispelled words' , $total_mispelled . ' in dictionary \"'.$dict.'\"' ], |
---|
71 | [ 'Total suggestions' , $total_suggestions ], |
---|
72 | [ 'Total words suggested' , $total_words_suggested ], |
---|
73 | [ 'Spell-checked in' , defined $TIMER_start ? (tv_interval($TIMER_start) . ' seconds') : 'n/a' ] |
---|
74 | ]); |
---|
75 | |
---|
76 | print qq^<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
---|
77 | <html> |
---|
78 | <head> |
---|
79 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
---|
80 | <link rel="stylesheet" type="text/css" media="all" href="spell-check-style.css" /> |
---|
81 | <script type="text/javascript"> |
---|
82 | var suggested_words = { $js_suggested_words }; |
---|
83 | var spellcheck_info = { $js_spellcheck_info }; </script> |
---|
84 | </head> |
---|
85 | <body onload="window.parent.finishedSpellChecking();">^; |
---|
86 | |
---|
87 | print $file_content; |
---|
88 | if ($cgi->param('init') eq '1') { |
---|
89 | my @dicts = $speller->dictionary_info(); |
---|
90 | my $dictionaries = ''; |
---|
91 | foreach my $i (@dicts) { |
---|
92 | next if $i->{jargon}; |
---|
93 | my $name = $i->{name}; |
---|
94 | if ($name eq $dict) { |
---|
95 | $name = '@'.$name; |
---|
96 | } |
---|
97 | $dictionaries .= ',' . $name; |
---|
98 | } |
---|
99 | $dictionaries =~ s/^,//; |
---|
100 | print qq^<div id="HA-spellcheck-dictionaries">$dictionaries</div>^; |
---|
101 | } |
---|
102 | |
---|
103 | print '</body></html>'; |
---|
104 | |
---|
105 | # Perl is beautiful. |
---|
106 | sub spellcheck { |
---|
107 | my $node = shift; |
---|
108 | my $doc = $node->getOwnerDocument; |
---|
109 | my $check = sub { # called for each word in the text |
---|
110 | # input is in UTF-8 |
---|
111 | my $word = shift; |
---|
112 | my $already_suggested = defined $suggested_words{$word}; |
---|
113 | ++$total_words; |
---|
114 | if (!$already_suggested && $speller->check($word)) { |
---|
115 | return undef; |
---|
116 | } else { |
---|
117 | # we should have suggestions; give them back to browser in UTF-8 |
---|
118 | ++$total_mispelled; |
---|
119 | if (!$already_suggested) { |
---|
120 | # compute suggestions for this word |
---|
121 | my @suggestions = $speller->suggest($word); |
---|
122 | my $suggestions = decode($speller->get_option('encoding'), join(',', @suggestions)); |
---|
123 | $suggested_words{$word} = $suggestions; |
---|
124 | ++$total_suggestions; |
---|
125 | $total_words_suggested += scalar @suggestions; |
---|
126 | } |
---|
127 | # HA-spellcheck-error |
---|
128 | my $err = $doc->createElement('span'); |
---|
129 | $err->setAttribute('class', 'HA-spellcheck-error'); |
---|
130 | my $tmp = $doc->createTextNode; |
---|
131 | $tmp->setNodeValue($word); |
---|
132 | $err->appendChild($tmp); |
---|
133 | return $err; |
---|
134 | } |
---|
135 | }; |
---|
136 | while ($node->getNodeValue =~ /([\p{IsWord}']+)/) { |
---|
137 | my $word = $1; |
---|
138 | my $before = $`; |
---|
139 | my $after = $'; |
---|
140 | my $df = &$check($word); |
---|
141 | if (!$df) { |
---|
142 | $before .= $word; |
---|
143 | } |
---|
144 | { |
---|
145 | my $parent = $node->getParentNode; |
---|
146 | my $n1 = $doc->createTextNode; |
---|
147 | $n1->setNodeValue($before); |
---|
148 | $parent->insertBefore($n1, $node); |
---|
149 | $parent->insertBefore($df, $node) if $df; |
---|
150 | $node->setNodeValue($after); |
---|
151 | } |
---|
152 | } |
---|
153 | }; |
---|
154 | |
---|
155 | sub check_inner_text { |
---|
156 | my $node = shift; |
---|
157 | my $text = ''; |
---|
158 | for (my $i = $node->getFirstChild; defined $i; $i = $i->getNextSibling) { |
---|
159 | if ($i->getNodeType == TEXT_NODE) { |
---|
160 | spellcheck($i); |
---|
161 | } |
---|
162 | } |
---|
163 | }; |
---|
164 | |
---|
165 | sub parse_with_dom { |
---|
166 | my ($text) = @_; |
---|
167 | $text = '<spellchecker>'.$text.'</spellchecker>'; |
---|
168 | |
---|
169 | my $parser = new XML::DOM::Parser; |
---|
170 | if ($debug) { |
---|
171 | open(FOO, '>:utf8', '/tmp/foo'); |
---|
172 | print FOO $text; |
---|
173 | close FOO; |
---|
174 | } |
---|
175 | my $doc = $parser->parse($text); |
---|
176 | my $nodes = $doc->getElementsByTagName('*'); |
---|
177 | my $n = $nodes->getLength; |
---|
178 | |
---|
179 | for (my $i = 0; $i < $n; ++$i) { |
---|
180 | my $node = $nodes->item($i); |
---|
181 | if ($node->getNodeType == ELEMENT_NODE) { |
---|
182 | check_inner_text($node); |
---|
183 | } |
---|
184 | } |
---|
185 | |
---|
186 | my $ret = $doc->toString; |
---|
187 | $ret =~ s{<spellchecker>(.*)</spellchecker>}{$1}sg; |
---|
188 | return $ret; |
---|
189 | }; |
---|
190 | |
---|
191 | sub make_js_hash { |
---|
192 | my ($hash) = @_; |
---|
193 | my $js_hash = ''; |
---|
194 | while (my ($key, $val) = each %$hash) { |
---|
195 | $js_hash .= ',' if $js_hash; |
---|
196 | $js_hash .= '"'.$key.'":"'.$val.'"'; |
---|
197 | } |
---|
198 | return $js_hash; |
---|
199 | }; |
---|
200 | |
---|
201 | sub make_js_hash_from_array { |
---|
202 | my ($array) = @_; |
---|
203 | my $js_hash = ''; |
---|
204 | foreach my $i (@$array) { |
---|
205 | $js_hash .= ',' if $js_hash; |
---|
206 | $js_hash .= '"'.$i->[0].'":"'.$i->[1].'"'; |
---|
207 | } |
---|
208 | return $js_hash; |
---|
209 | }; |
---|