lib/Encode/Arabic/CGI.pm untested

filter: Statementstmt 0.0% 0 / 184 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 255 CC 13 SCAR 40.9
CC
13
Cov
0%
recode
156.0
run
6.0
setup
2.0
CRAP 60.0
1 # ###################################################################### Otakar Smrz, 2003/01/23
2 #
3 # Encode Arabic Online #########################################################################
4  
5 package Encode::Arabic::CGI;
6  
7 our $VERSION = '14.1';
8  
9 use strict;
10  
11 use base 'CGI::Application::FastCGI';
12  
13 use CGI::Fast ':standard';
14  
15 use Benchmark;
16  
17 use Encode::Arabic;
18 use Encode::Arabic::ArabTeX ':xml';
19 use Encode::Arabic::ArabTeX::ZDMG ':xml';
20 use Encode::Arabic::Buckwalter ':xml';
21  
22  
23 our $session;
24  
25 our %enc_hash = ( 'ArabTeX', 'ArabTeX',
26 'Buckwalter', 'Buckwalter',
27 'Habash-Soudi-Buckwalter', 'Habash',
28 'Parkinson', 'Parkinson',
29 'Unicode', 'UTF-8' );
30  
31 our @dec_list = ('ArabTeX', 'Buckwalter', 'Habash-Soudi-Buckwalter', 'Parkinson', 'Unicode');
32  
33 our @enc_list = ('Unicode', 'Buckwalter', 'Habash-Soudi-Buckwalter', 'Parkinson', 'ArabTeX');
34  
35  
36 our %url_hash = ();
37  
38 foreach (keys %enc_hash) {
39  
40 my $url = $enc_hash{$_};
41  
42 $url =~ tr[-][/];
43  
44 $url_hash{$_} = 'http://search.cpan.org/dist/Encode-Arabic/lib/Encode/Arabic/' . $url . '.pm';
45 }
46  
47 $url_hash{'Unicode'} = 'http://search.cpan.org/dist/Encode/Encode.pm';
48  
49  
50 sub setup {
51  
52 my $c = shift;
53  
54 $c->mode_param('runmode');
55  
56 $c->start_mode('recode');
57 $c->error_mode('recode');
58  
59 $c->run_modes(map { $_ => $_ } qw 'recode');
60 }
61  
62 sub cgiapp_prerun {
63  
64 $session++;
65 }
66  
67 # use base 'CGI::Application::FastCGI';
68  
69 sub run {
70 my $self = shift;
71 my $request = FCGI::Request();
72 $self->fastcgi($request);
73 while ($request->Accept >= 0) {
74 $self->reset_query;
75 $self->CGI::Application::run;
76 last if $self->reinit();
77 }
78 }
79  
80 sub reinit {
81  
82 return -M $0 < 0;
83 }
84  
85  
86 sub escape ($) {
87  
88 my $x = shift;
89  
90 $x =~ s/\&/\&amp;/g;
91 $x =~ s/\</\&lt;/g;
92 $x =~ s/\>/\&gt;/g;
93  
94 return $x;
95 }
96  
97 sub timer (@) {
98  
99 return sprintf "%04d/%02d/%02d %02d:%02d:%02d", $_[5] + 1900, $_[4] + 1, @_[3, 2, 1, 0];
100 }
101  
102  
103 sub display_header ($) {
104  
105 my $c = shift;
106 my $q = $c->query();
107 my $r;
108  
109 $q->charset('utf-8');
110  
111 $r .= $q->start_html(-title => "Encode Arabic Online Interface", '-encoding' => $q->charset(),
112 -style => {-src => 'http://quest.ms.mff.cuni.cz/encode/encode.css', '-type' => 'text/css'},
113 -script => {-src => 'http://quest.ms.mff.cuni.cz/encode/encode.js', -type => 'text/javascript'});
114  
115 return $r;
116 }
117  
118 sub display_headline ($) {
119  
120 my $c = shift;
121 my $q = $c->query();
122 my $r;
123  
124 $r .= $q->h1($q->a({'href' => 'http://sourceforge.net/projects/encode-arabic/'}, "Encode Arabic"), 'Online Interface');
125  
126 return $r;
127 }
128  
129 sub display_welcome ($) {
130  
131 my $c = shift;
132 my $q = $c->query();
133 my $r;
134  
135 $r .= $q->p("Welcome to the online interface to", $q->a({-href => 'http://sourceforge.net/projects/encode-arabic/'}, "Encode Arabic") .
136 ", a library for processing various encodings and notations of Arabic with",
137 $q->a({-href => 'http://search.cpan.org/dist/Encode-Arabic/'}, "Perl"), "or",
138 $q->a({-href => 'http://hackage.haskell.org/cgi-bin/hackage-scripts/package/Encode/'}, "Haskell") . ".");
139  
140 $r .= $q->p('You must have Unicode fonts installed to appreciate this site. If you need some, try the',
141 $q->a({'href' => 'http://sourceforge.net/projects/dejavu/'}, 'DejaVu Fonts'), 'from SourceForge.');
142  
143 return $r;
144 }
145  
146 sub display_footline ($) {
147  
148 my $c = shift;
149 my $q = $c->query();
150 my $r;
151  
152 $r .= $q->br();
153  
154 $r .= $q->p("(C) Otakar Smr\x{017E} 2012-2003. GNU General Public License", $q->a({-href => 'http://www.gnu.org/licenses/'}, "GNU GPL 3") . ".");
155  
156 $r .= $q->p("Encode Arabic is an", $q->a({-href => 'http://sourceforge.net/projects/encode-arabic/'}, "open-source online"), "project.",
157 "You can contribute to its development with your suggestions!");
158  
159 $r .= $q->p("Contact", $q->a({-href => 'http://otakar-smrz.users.sf.net/'}, "otakar-smrz users.sf.net") . ",",
160 "Institute of Formal and Applied Linguistics, Charles University in Prague.");
161  
162 return $r;
163 }
164  
165 sub display_footer ($) {
166  
167 my $c = shift;
168 my $q = $c->query();
169 my $r;
170  
171 $r .= $q->p({'style' => 'text-align: right;'},
172 '<a href="http://validator.w3.org/check?uri=referer"><img border="0"
173 src="http://www.w3.org/Icons/valid-xhtml10"
174 alt="Valid XHTML 1.0 Transitional" height="31" width="88" /></a>',
175 '<a href="http://jigsaw.w3.org/css-validator/check?uri=referer"><img border="0"
176 src="http://www.w3.org/Icons/valid-css2"
177 alt="Valid CSS level 2.1" height="31" width="88" /></a>');
178  
179 $r .= $q->script({-type => 'text/javascript', -src => 'http://api.yamli.com/js/yamli_api.js'}, "");
180  
181 $r .= $q->script({-type => 'text/javascript'}, join ' ', split ' ', q {
182  
183 if (typeof(Yamli) == "object") {
184  
185 Yamli.init({ uiLanguage: "en", startMode: "onOrUserDefault",
186 settingsPlacement: 'inside',
187 showTutorialLink: false, showDirectionLink: true });
188  
189 encodeYamli('');
190 }
191 });
192  
193 $r .= $q->end_html();
194  
195 return $r;
196 }
197  
198  
199 sub recode {
200  
201 my $c = shift;
202  
203 my $q = $c->query();
204  
205 my $r = '';
206  
207 my @tick = ();
208  
209 $q->param($c->mode_param(), 'recode');
210  
211 $r .= display_header $c;
212  
213 $r .= display_headline $c;
214  
215 my @example = ( [ 'ArabTeX', "\\cap al-waqtu al-'Ana " . (timer gmtime time) . " bi-tawqIti <GMT>, 'ah\"laN wa-sah\"laN!" ],
216 [ 'ArabTeX', "\\cap iqra' h_a_dA an-na.s.sa bi-intibAhiN: li-al-laylaTayni yusAwI li-llaylatayni, wa-lA li-a|l-laylaT-|ayni." ],
217 [ 'ArabTeX', "iqra'-i ad-darsa al-'awwala" ],
218 [ 'Buckwalter', "AqrO Aldrs AlOwl" ],
219 [ 'Buckwalter', "yEtbr mDy}A" ],
220 [ 'Unicode', decode "buckwalter", "AqrO Aldrs AlOwl" ],
221 [ 'Unicode', decode "buckwalter", "Aldrs AlOwl" ],
222 [ 'Unicode', decode "buckwalter", "yEtbr mDy}A" ] );
223  
224 if (defined $q->param('submit') and $q->param('submit') eq 'Example') {
225  
226 my $idx = rand @example;
227  
228 $q->param('text', $example[$idx][1]);
229 $q->param('decode', $example[$idx][0]);
230 }
231 else {
232  
233 if (defined $q->param('text') and $q->param('text') != /^\s*$/) {
234  
235 $q->param('text', decode "utf8", $q->param('text'));
236 $q->param('decode', @dec_list) unless defined $q->param('decode');
237 }
238 else {
239  
240 $q->param('text', $example[0][1]);
241 $q->param('decode', $example[0][0]);
242 }
243 }
244  
245 $q->param('encode', @enc_list) unless defined $q->param('encode');
246  
247 $r .= display_welcome $c;
248  
249 $r .= $q->h2('Your Request');
250  
251 $r .= $q->start_form('-method' => 'POST');
252  
253 $r .= $q->table({-border => 0},
254 Tr({-align => 'left', -valign => 'middle'},
255 [
256 td({-colspan => 3},
257 [$q->textfield(-name => 'text',
258 -default => $q->param('text'),
259 -size => 120,
260 -maxlength => 200,
261 )]),
262  
263 td({-colspan => 3},
264 table({-border => 0, -width => "100%"},
265 Tr({-align => 'left', -valign => 'top'},
266 [
267 td({-align => 'left'}, 'Decode Setting') .
268 td({-align => 'center'},
269 [$q->radio_group(-name => 'decode',
270 -onchange => "encodeYamli('text')",
271 -values => [@dec_list],
272 -default => [$q->param('decode')]),
273 ]),
274 td({-align => 'left'}, 'Encode Setting') .
275 td({-align => 'center'},
276 [$q->checkbox_group(-name => 'encode',
277 -values => [@enc_list],
278 -default => [$q->param('encode')]),
279 ]),
280 ])
281 )),
282 ]),
283  
284 Tr({-align => 'left', -valign => 'middle'},
285 td({-align => 'left'}, $q->submit(-name => 'submit', -value => 'Submit')),
286 td({-align => 'center'}, $q->reset('Reset')),
287 td({-align => 'right'}, $q->submit(-name => 'submit', -value => 'Example')),
288 ));
289  
290 $r .= $q->hidden( -name => $c->mode_param(), -value => $q->param($c->mode_param()) );
291  
292 $r .= $q->end_form();
293  
294  
295 $r .= $q->h2('Decode');
296  
297 $r .= $q->h3($q->a({'href' => $url_hash{$q->param('decode')}}, $q->param('decode')));
298  
299 $r .= $q->p({'class' => $q->param('text') =~ /\p{Arabic}/ ? 'arabic' : ''}, $q->param('text') ne '' ? escape $q->param('text') : '&nbsp;');
300  
301 my $decode = decode $enc_hash{$q->param('decode')}, encode "utf8", $q->param('text');
302  
303  
304 $r .= $q->h2('Encode');
305  
306 foreach ($q->param('encode')) {
307  
308 $r .= $q->h3($q->a({'href' => $url_hash{$_}}, $_));
309  
310 my $encode = decode "utf8", encode $enc_hash{$_}, $decode;
311  
312 $r .= $q->p({'class' => $encode =~ /\p{Arabic}/ ? 'arabic' : ''}, $encode ne '' ? escape $encode : '&nbsp;');
313  
314 if ($_ eq 'Unicode' and $q->param('decode') eq 'ArabTeX') {
315  
316 my $encode = decode "arabtex-zdmg", encode "utf8", $q->param('text');
317  
318 $r .= $q->p({'class' => $encode =~ /\p{Arabic}/ ? 'arabic' : ''}, $encode ne '' ? escape $encode : '&nbsp;');
319 }
320 }
321  
322 $r .= display_footline $c;
323  
324 $r .= display_footer $c;
325  
326 return encode "utf8", $r;
327 }
328  
329  
330 1;