iso2022jp.pl untested

filter: Statementstmt 0.0% 0 / 88 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 3 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 151 CC 27 SCAR 53.7
CC
27
Cov
0%
clip
272.0
jp2022_to_html
156.0
str2html
2.0
CRAP 214.7
1 ##---------------------------------------------------------------------------##
2 ## File:
3 ## $Id: iso2022jp.pl,v 1.10 2009/05/03 20:38:32 ehood Exp $
4 ## Author(s):
5 ## Earl Hood mhonarc@mhonarc.org
6 ## NIIBE Yutaka gniibe@mri.co.jp
7 ## Takashi P.KATOH p-katoh@shiratori.riec.tohoku.ac.jp
8 ## Description:
9 ## Library defines routine to process iso-2022-jp data.
10 ##---------------------------------------------------------------------------##
11 ## Copyright (C) 1995-2002
12 ## Earl Hood, mhonarc@mhonarc.org
13 ## NIIBE Yutaka, gniibe@mri.co.jp
14 ## Takashi P.KATOH, p-katoh@shiratori.riec.tohoku.ac.jp
15 ##
16 ## This program is free software; you can redistribute it and/or modify
17 ## it under the terms of the GNU General Public License as published by
18 ## the Free Software Foundation; either version 2 of the License, or
19 ## (at your option) any later version.
20 ##
21 ## This program is distributed in the hope that it will be useful,
22 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ## GNU General Public License for more details.
25 ##
26 ## You should have received a copy of the GNU General Public License
27 ## along with this program; if not, write to the Free Software
28 ## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
29 ## 02111-1307, USA
30 ##---------------------------------------------------------------------------##
31  
32 package iso_2022_jp;
33  
34 $Url = '(http://|https://|ftp://|afs://|wais://|telnet://|ldap://'
35 . '|gopher://|news:|nntp:|mid:|cid:|mailto:|prospero:)';
36 $UrlExp = $Url . q%[^\s\(\)\|<>"']*[^\.?!;,"'\|\[\]\(\)\s<>]%;
37 $HUrlExp = $Url . q%[^\s\(\)\|<>"'\&]*[^\.?!;,"'\|\[\]\(\)\s<>\&]%;
38  
39 ##---------------------------------------------------------------------------##
40 ## str2html(): Convert an iso-2022-jp string into HTML. Function
41 ## interface similiar as iso8859.pl function.
42 ##
43 sub str2html { jp2022_to_html($_[0], 1); }
44  
45 ##---------------------------------------------------------------------------##
46 ## Function to convert ISO-2022-JP data into HTML. Function is based
47 ## on the following RFCs:
48 ##
49 ## RFC-1468 I
50 ## J. Murai, M. Crispin, E. van der Poel, "Japanese Character
51 ## Encoding for Internet Messages", 06/04/1993. (Pages=6)
52 ##
53 ## RFC-1554 I
54 ## M. Ohta, K. Handa, "ISO-2022-JP-2: Multilingual Extension of
55 ## ISO-2022-JP", 12/23/1993. (Pages=6)
56 ##
57 sub jp2022_to_html {
58 my ($body) = shift;
59 my ($nourl) = shift;
60 my (@lines) = split(/\r?\n/, $body);
61 my ($ret, $ascii_text);
62 local ($_);
63  
64 $ret = "";
65 my $cnt = scalar(@lines);
66 my $i = 0;
67 foreach (@lines) {
68 my $line = "";
69 # a trick to process preceding ASCII text
70 $_ = "\033(B" . $_ unless /^\033/;
71  
72 # Process Each Segment
73 while (1) {
74 if (s/^(\033\([BJ])//) { # Single Byte Segment
75 $line .= $1;
76 while (1) {
77 if (s/^([^\033]+)//) { # ASCII plain text
78 $ascii_text = $1;
79  
80 # Replace meta characters in ASCII plain text
81 $ascii_text =~ s%\&%\&amp;%g;
82 $ascii_text =~ s%<%\&lt;%g;
83 $ascii_text =~ s%>%\&gt;%g;
84 ## Convert URLs to hyperlinks
85 $ascii_text =~ s%($HUrlExp)%<a href="$1">$1</a>%gio
86 unless $nourl;
87  
88 $line .= $ascii_text;
89 } elsif (s/(\033\.[A-F])//) { # G2 Designate Sequence
90 $line .= $1;
91 } elsif (s/(\033N[ -])//) { # Single Shift Sequence
92 $line .= $1;
93 } else {
94 last;
95 }
96 }
97 } elsif (s/^(\033\$[\@AB]|\033\$\([CD])//) { # Double Byte Segment
98 $line .= $1;
99 while (1) {
100 if (s/^([!-~][!-~]+)//) { # Double Char plain text
101 $line .= $1;
102 } elsif (s/(\033\.[A-F])//) { # G2 Designate Sequence
103 $line .= $1;
104 } elsif (s/(\033N[ -])//) { # Single Shift Sequence
105 $line .= $1;
106 } else {
107 last;
108 }
109 }
110 } else {
111 # Something wrong in text
112 $line .= $_;
113 last;
114 }
115 }
116  
117 # remove a `trick'
118 $line =~ s/^\033\(B//;
119  
120 $ret .= $line;
121  
122 # add back eol
123 $ret .= "\n" unless (++$i >= $cnt);
124 }
125  
126 ($ret);
127 }
128  
129 ##---------------------------------------------------------------------------##
130 ## clip($str, $length, $is_html, $has_tags): Clip an iso-2022-jp string.
131 ##
132 ## The last argument $is_html specifies '&' should be treated
133 ## as HTML character or not.
134 ## (i.e., the length of '&amp;' will be 1 if $is_html).
135 ##
136 sub clip { # &clip($str, 10, 1, 1);
137 my ($str) = shift;
138 my ($length) = shift;
139 my ($is_html) = shift;
140 my ($has_tags) = shift;
141 my ($ret, $inascii);
142 local ($_) = $str;
143  
144 $ret = "";
145 # a trick to process preceding ASCII text
146 $_ = "\033(B" . $_ unless /^\033/;
147  
148 # Process Each Segment
149 CLIP: while (1) {
150 if (s/^(\033\([BJ])//) { # Single Byte Segment
151 $inascii = 1;
152 $ret .= $1;
153 while (1) {
154 if (s/^([^\033])//) { # ASCII plain text
155 if ($is_html) {
156 if (($1 eq '<') && $has_tags) {
157 s/^[^>\033]*>//;
158 } else {
159 if ($1 eq '&') {
160 s/^([^\;]*\;)//;
161 $ret .= "&$1";
162 } else {
163 $ret .= $1;
164 }
165 $length--;
166 }
167 } else {
168 $ret .= $1;
169 $length--;
170 }
171 } elsif (s/(\033\.[A-F])//) { # G2 Designate Sequence
172 $ret .= $1;
173 } elsif (s/(\033N[ -])//) { # Single Shift Sequence
174 $ret .= $1;
175 $length--;
176 } else {
177 last;
178 }
179 last CLIP if ($length <= 0);
180 }
181 } elsif (s/^(\033\$[\@AB]|\033\$\([CD])//) { # Double Byte Segment
182 $inascii = 0;
183 $ret .= $1;
184 while (1) {
185 if (s/^([!-~][!-~])//) { # Double Char plain text
186 $ret .= $1;
187 # The length of a double-byte-char is assumed 2.
188 # If we consider compatibility with UTF-8, it should be 1.
189 $length -= 2;
190 } elsif (s/(\033\.[A-F])//) { # G2 Designate Sequence
191 $ret .= $1;
192 } elsif (s/(\033N[ -])//) { # Single Shift Sequence
193 $ret .= $1;
194 $length--;
195 } else {
196 last;
197 }
198 last CLIP if ($length <= 0);
199 }
200 } else {
201 # Something wrong in text
202 $ret .= $_;
203 last;
204 }
205 }
206  
207 # remove a `trick'
208 $ret =~ s/^\033\(B//;
209  
210 # Shuold we check the last \033\([BJ] sequence?
211 # (I believe it is too paranoid).
212 $ret .= "\033(B" unless $inascii;
213  
214 ($ret);
215 }
216 ##---------------------------------------------------------------------------##
217 1;