line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Hub::Misc::Transform; |
2
|
1
|
|
|
1
|
|
9
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
58
|
|
3
|
1
|
|
|
1
|
|
8
|
use Hub qw/:lib/; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
8
|
|
4
|
|
|
|
|
|
|
our $VERSION = '4.00043'; |
5
|
|
|
|
|
|
|
our @EXPORT = qw//; |
6
|
|
|
|
|
|
|
our @EXPORT_OK = qw/ |
7
|
|
|
|
|
|
|
populate |
8
|
|
|
|
|
|
|
jsstr |
9
|
|
|
|
|
|
|
hashtoattrs |
10
|
|
|
|
|
|
|
safestr |
11
|
|
|
|
|
|
|
unpack_safestr |
12
|
|
|
|
|
|
|
nbspstr |
13
|
|
|
|
|
|
|
packcgi |
14
|
|
|
|
|
|
|
unpackcgi |
15
|
|
|
|
|
|
|
pack_ncr |
16
|
|
|
|
|
|
|
unpack_ncr |
17
|
|
|
|
|
|
|
/; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
20
|
|
|
|
|
|
|
# safestr STRING |
21
|
|
|
|
|
|
|
# |
22
|
|
|
|
|
|
|
# Pack nogood characters into good ones. Good characters are letters, numbers, |
23
|
|
|
|
|
|
|
# and the underscore. |
24
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
25
|
|
|
|
|
|
|
#|test(match) safestr( 'Dogs (Waters, Gilmour) 17:06' ); |
26
|
|
|
|
|
|
|
#=Dogs_0x20__0x28_Waters_0x2c__0x20_Gilmour_0x29__0x20_17_0x3a_06 |
27
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub safestr { |
30
|
0
|
|
|
0
|
1
|
|
my $str = shift; |
31
|
0
|
|
|
|
|
|
$str =~ s/([^A-Za-z0-9_])/sprintf("_0x%2x_", unpack("C", $1))/eg; |
|
0
|
|
|
|
|
|
|
32
|
0
|
|
|
|
|
|
return $str; |
33
|
|
|
|
|
|
|
}#safestr |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
36
|
|
|
|
|
|
|
# unpack_safestr - Safe strings back into ASCII characters |
37
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
38
|
|
|
|
|
|
|
#|test(match) Hub::unpack_safestr('Dogs_0x20__0x28_Waters_0x2c__0x20_Gilmour_0x29__0x20_17_0x3a_06'); |
39
|
|
|
|
|
|
|
#=Dogs (Waters, Gilmour) 17:06 |
40
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub unpack_safestr { |
43
|
0
|
|
|
0
|
1
|
|
my $str = shift; |
44
|
0
|
|
|
|
|
|
$str =~ s/_0x([a-fA-F0-9][a-fA-F0-9])_/pack("C",hex($1))/eg; |
|
0
|
|
|
|
|
|
|
45
|
0
|
|
|
|
|
|
return $str |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
49
|
|
|
|
|
|
|
# pack_ncr - Non-alphanumeric ASCII characters to Numeric Character References. |
50
|
|
|
|
|
|
|
# pack_ncr $string |
51
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
52
|
|
|
|
|
|
|
#|test(match) Hub::pack_ncr("This is a # of tests & bl/ah"); |
53
|
|
|
|
|
|
|
#=This is a # of tests & bl/ah |
54
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub pack_ncr { |
57
|
0
|
|
|
0
|
1
|
|
my $str = shift; |
58
|
0
|
0
|
|
|
|
|
my $ptr = ref($str) eq 'SCALAR' ? $str : \$str; |
59
|
0
|
|
|
|
|
|
$$ptr =~ s/([^A-Za-z0-9_ ])/sprintf("%d;", ord($1))/eg; |
|
0
|
|
|
|
|
|
|
60
|
0
|
|
|
|
|
|
return $str |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
64
|
|
|
|
|
|
|
# unpack_ncr - Convert Numeric Character References into ASCII characters |
65
|
|
|
|
|
|
|
# unpack_ncr $string |
66
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
67
|
|
|
|
|
|
|
#|test(match) Hub::unpack_ncr('This is a # of tests & bl/ah'); |
68
|
|
|
|
|
|
|
#=This is a # of tests & bl/ah |
69
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub unpack_ncr { |
72
|
0
|
|
|
0
|
1
|
|
my $str = shift; |
73
|
0
|
|
|
|
|
|
$str =~ s/([0-9]+);/sprintf("%c",$1)/eg; |
|
0
|
|
|
|
|
|
|
74
|
0
|
|
|
|
|
|
return $str |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
78
|
|
|
|
|
|
|
# packcgi $string|\$string |
79
|
|
|
|
|
|
|
# |
80
|
|
|
|
|
|
|
# Pack characters into those used for passing by the cgi. |
81
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub packcgi { |
84
|
0
|
|
|
0
|
1
|
|
my $str = shift; |
85
|
0
|
0
|
|
|
|
|
my $ptr = ref($str) eq 'SCALAR' ? $str : \$str; |
86
|
0
|
|
|
|
|
|
$$ptr =~ s/([^A-Za-z0-9_])/sprintf("%%%X", ord($1))/eg; |
|
0
|
|
|
|
|
|
|
87
|
0
|
|
|
|
|
|
return $str |
88
|
|
|
|
|
|
|
}#packcgi |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
91
|
|
|
|
|
|
|
# unpackcgi QUERY |
92
|
|
|
|
|
|
|
# |
93
|
|
|
|
|
|
|
# Unpack cgi characters into a kv hashref |
94
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
sub unpackcgi { |
97
|
0
|
|
|
0
|
1
|
|
my $q = shift; |
98
|
0
|
|
|
|
|
|
my $p = {}; |
99
|
0
|
|
|
|
|
|
$q =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg; |
|
0
|
|
|
|
|
|
|
100
|
0
|
|
|
|
|
|
$q =~ tr/+/ /; |
101
|
0
|
|
|
|
|
|
my @pairs = split /[&;]/, $q; |
102
|
0
|
|
|
|
|
|
for( my $i = 0; $i <= $#pairs; $i++ ) { |
103
|
0
|
0
|
|
|
|
|
if( $pairs[$i] =~ /(.*)=(.*)/ ) { |
104
|
0
|
|
|
|
|
|
my ($l,$r) = ($1,$2); |
105
|
0
|
0
|
|
|
|
|
if( $r =~ /^\((?:.*,)*.*\)$/ ) { |
106
|
0
|
|
|
|
|
|
my @extract = split( /\,/, substr( $r, 1, (length $r) -1 ) ); |
107
|
0
|
|
|
|
|
|
$p->{$l} = \@extract; |
108
|
|
|
|
|
|
|
} else { |
109
|
0
|
|
|
|
|
|
$p->{$l} = $r; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
} |
113
|
0
|
|
|
|
|
|
return $p; |
114
|
|
|
|
|
|
|
}#unpackcgi |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
117
|
|
|
|
|
|
|
# nbspstr - Format a string, replacing spaces with ' ' |
118
|
|
|
|
|
|
|
# nbspstr - $text |
119
|
|
|
|
|
|
|
# |
120
|
|
|
|
|
|
|
# For example: |
121
|
|
|
|
|
|
|
# |
122
|
|
|
|
|
|
|
# nbspstr( "Hello world!" ) |
123
|
|
|
|
|
|
|
# |
124
|
|
|
|
|
|
|
# would return: |
125
|
|
|
|
|
|
|
# |
126
|
|
|
|
|
|
|
# "Hello World" |
127
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
sub nbspstr { |
130
|
0
|
|
0
|
0
|
1
|
|
my $s = shift || return; |
131
|
0
|
0
|
|
|
|
|
if( $s =~ /<.*>/ ) { |
132
|
0
|
|
|
|
|
|
my $p = 0; |
133
|
0
|
|
|
|
|
|
while( $p >= 0 ) { |
134
|
0
|
|
|
|
|
|
my $lb = index $s, '<', $p; |
135
|
0
|
|
|
|
|
|
my $rb = index $s, '>', $p; |
136
|
0
|
|
|
|
|
|
my $sp = index $s, ' ', $p; |
137
|
0
|
0
|
|
|
|
|
if( $sp >= 0 ) { |
138
|
0
|
0
|
0
|
|
|
|
if( ( $sp < $lb ) || ( $sp > $rb ) ) { |
139
|
0
|
|
|
|
|
|
substr $s, $sp, 1, " "; |
140
|
0
|
|
|
|
|
|
$p = $sp + length( " " ); |
141
|
|
|
|
|
|
|
} else { |
142
|
0
|
|
|
|
|
|
$p = $rb; |
143
|
|
|
|
|
|
|
}#if |
144
|
|
|
|
|
|
|
} else { |
145
|
0
|
|
|
|
|
|
$p = $sp; |
146
|
|
|
|
|
|
|
}#if |
147
|
|
|
|
|
|
|
}#while |
148
|
|
|
|
|
|
|
} else { |
149
|
0
|
|
|
|
|
|
$s =~ s/ / /g; |
150
|
|
|
|
|
|
|
}#if |
151
|
0
|
|
|
|
|
|
return $s; |
152
|
|
|
|
|
|
|
}#nbsp |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
155
|
|
|
|
|
|
|
# jsstr - Format as one long string for use as the rval in javascript |
156
|
|
|
|
|
|
|
# jsstr \$value |
157
|
|
|
|
|
|
|
# jsstr $value |
158
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
sub jsstr { |
161
|
0
|
0
|
|
0
|
1
|
|
my $str = isa($_[0], 'SCALAR') ? ${$_[0]} : $_[0]; |
|
0
|
|
|
|
|
|
|
162
|
0
|
|
|
|
|
|
$str =~ s/(?
|
163
|
0
|
|
|
|
|
|
return $str; |
164
|
|
|
|
|
|
|
}#jsstr |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
167
|
|
|
|
|
|
|
# populate - Populate template text with values |
168
|
|
|
|
|
|
|
# populate $text|\$text, \%values [,\%values...] [option] |
169
|
|
|
|
|
|
|
# |
170
|
|
|
|
|
|
|
# options: |
171
|
|
|
|
|
|
|
# |
172
|
|
|
|
|
|
|
# -as_ref=1 Return a reference |
173
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
174
|
|
|
|
|
|
|
#|test(match,mushroom) populate('mu[#foo]m', { foo => 'shroo' }); |
175
|
|
|
|
|
|
|
#|test(match,SCALAR) ref(populate('a[#b]c', { b => 'bee' }, '-as_ref=1')); |
176
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
sub populate { |
179
|
0
|
|
|
0
|
1
|
|
my $opts = Hub::opts(\@_, {'as_ref' => 0}); |
180
|
0
|
|
|
|
|
|
my $text = shift; |
181
|
0
|
0
|
|
|
|
|
croak("No template provided") unless defined $text; |
182
|
0
|
|
|
|
|
|
my $parser = Hub::mkinst( 'StandardParser', $text, -opts => $opts ); |
183
|
0
|
|
|
|
|
|
my $result = $parser->populate( @_ ); |
184
|
0
|
0
|
|
|
|
|
return $$opts{'as_ref'} ? $result : $$result; |
185
|
|
|
|
|
|
|
}#populate |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
188
|
|
|
|
|
|
|
# hashtoattrs - Turn the given hash into an key="value" string. |
189
|
|
|
|
|
|
|
# hashtoattrs \%hash, [\@ignore_keys] |
190
|
|
|
|
|
|
|
# |
191
|
|
|
|
|
|
|
# When C is provided, matching hash keys will not be converted. |
192
|
|
|
|
|
|
|
# |
193
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
194
|
|
|
|
|
|
|
#|test(match) |
195
|
|
|
|
|
|
|
#| my $hash = { |
196
|
|
|
|
|
|
|
#| 'class' => "foodidly", |
197
|
|
|
|
|
|
|
#| 'name' => "bobsmith", |
198
|
|
|
|
|
|
|
#| 'height' => "5px", |
199
|
|
|
|
|
|
|
#| 'junk' => "ignore me", |
200
|
|
|
|
|
|
|
#| }; |
201
|
|
|
|
|
|
|
#| hashtoattrs($hash, ['junk']); |
202
|
|
|
|
|
|
|
#~ |
203
|
|
|
|
|
|
|
#~ class=\"foodidly\" height=\"5px\" name=\"bobsmith\" |
204
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
sub hashtoattrs { |
207
|
|
|
|
|
|
|
|
208
|
0
|
|
|
0
|
1
|
|
my $hash = shift; |
209
|
0
|
|
|
|
|
|
my $iarray = shift; |
210
|
0
|
|
|
|
|
|
my @attrs = (); |
211
|
0
|
|
|
|
|
|
my $ignore = ''; |
212
|
|
|
|
|
|
|
|
213
|
0
|
0
|
|
|
|
|
if( ref($iarray) eq 'ARRAY' ) { |
214
|
0
|
|
|
|
|
|
$ignore = '^' . join( '|', @$iarray ) . '$'; |
215
|
|
|
|
|
|
|
}#if |
216
|
|
|
|
|
|
|
|
217
|
0
|
0
|
|
|
|
|
if( ref($hash) eq 'HASH' ) { |
218
|
0
|
|
|
|
|
|
keys %$hash; # reset internal iterator |
219
|
0
|
|
|
|
|
|
while( my($k,$v) = each %$hash ) { |
220
|
0
|
0
|
0
|
|
|
|
if($ignore && ($k =~ $ignore)) { |
221
|
0
|
|
|
|
|
|
next; |
222
|
|
|
|
|
|
|
}#if |
223
|
0
|
0
|
|
|
|
|
push(@attrs, "$k=\"$v\"") if defined $v; |
224
|
|
|
|
|
|
|
}#while |
225
|
|
|
|
|
|
|
}#if |
226
|
|
|
|
|
|
|
|
227
|
0
|
|
|
|
|
|
return join( ' ', sort @attrs ); |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
}#hashtoattrs |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
232
|
|
|
|
|
|
|
return 1; |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
__END__ |