line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Hub::Parse::HtmlParser; |
2
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
32
|
|
3
|
1
|
|
|
1
|
|
5
|
use Hub qw/:lib/; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
5
|
|
4
|
|
|
|
|
|
|
our $has_imagelib; |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
BEGIN { |
7
|
1
|
|
|
1
|
|
78
|
eval 'use Image::Size qw(imgsize)'; |
|
1
|
|
|
1
|
|
466
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
8
|
1
|
|
|
|
|
1325
|
our $has_imagelib = !$@; |
9
|
|
|
|
|
|
|
} |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
push our @ISA, qw(Hub::Parse::StandardParser); |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our %EVALUATORS; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub get_evaluator { |
16
|
0
|
0
|
|
0
|
1
|
|
return defined $EVALUATORS{$_[1]} |
17
|
|
|
|
|
|
|
? $EVALUATORS{$_[1]} |
18
|
|
|
|
|
|
|
: &Hub::Parse::StandardParser::get_evaluator(@_); |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
$EVALUATORS{'url'} = sub { |
22
|
|
|
|
|
|
|
my ($self, $params, $result) = @_; |
23
|
|
|
|
|
|
|
my ($outer_str, $fields, $pos, $text, $parents, $valdata) = @$params; |
24
|
|
|
|
|
|
|
my ($opts, %directive) = Hub::opts($fields); |
25
|
|
|
|
|
|
|
$result->{'value'} = ''; |
26
|
|
|
|
|
|
|
my $literal = $directive{'url'}; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
# Get the site URL. This is... |
29
|
|
|
|
|
|
|
# my $script_filename = $$Hub{'/sys/ENV/SCRIPT_FILENAME'}; |
30
|
|
|
|
|
|
|
# my $relative_filename = substr $script_filename, length($$Hub{'/sys/ENV/WORKING_DIR'}); |
31
|
|
|
|
|
|
|
#warn "script_filename: $script_filename\n"; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
my $relative_filename = Hub::getaddr($ENV{'SCRIPT_FILENAME'}); |
34
|
|
|
|
|
|
|
my $script_name = $$Hub{'/sys/ENV/SCRIPT_NAME'}; |
35
|
|
|
|
|
|
|
my $site_url = substr($script_name, 0, - length($relative_filename)) || ''; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
#warn "relative_filename: $relative_filename\n"; |
38
|
|
|
|
|
|
|
#warn "script_name: $script_name\n"; |
39
|
|
|
|
|
|
|
#warn "site_url: $site_url\n"; |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
#warn "--url--: $literal\n"; |
42
|
|
|
|
|
|
|
if ($$opts{'relative'}) { |
43
|
|
|
|
|
|
|
# Figure out script directory |
44
|
|
|
|
|
|
|
my $script_dir = Hub::getpath($$Hub{'/sys/ENV/SCRIPT_FILENAME'}); |
45
|
|
|
|
|
|
|
my $work_dir = $$Hub{'/sys/ENV/WORKING_DIR'}; |
46
|
|
|
|
|
|
|
if (index($script_dir, $work_dir) == 0 ) { |
47
|
|
|
|
|
|
|
#warn " trim: $work_dir\n"; |
48
|
|
|
|
|
|
|
$script_dir = substr($script_dir, length($work_dir)); |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
if ($script_dir eq $work_dir) { |
51
|
|
|
|
|
|
|
$script_dir = '/'; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
$script_dir ||= '/'; |
54
|
|
|
|
|
|
|
# Find the relative path |
55
|
|
|
|
|
|
|
my $relative_path = Hub::relpath($literal, $script_dir); |
56
|
|
|
|
|
|
|
#warn " from: $script_dir\n"; |
57
|
|
|
|
|
|
|
#warn " to: $literal\n"; |
58
|
|
|
|
|
|
|
#warn " =: $relative_path\n"; |
59
|
|
|
|
|
|
|
$$result{'value'} = $relative_path; |
60
|
|
|
|
|
|
|
} else { |
61
|
|
|
|
|
|
|
$$result{'value'} = $literal =~ /^\// ? $site_url . $literal : $literal; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
}; |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
$EVALUATORS{'image'} = sub { |
66
|
|
|
|
|
|
|
my ($self, $params, $result) = @_; |
67
|
|
|
|
|
|
|
my ($outer_str, $fields, $pos, $text, $parents, $valdata) = @$params; |
68
|
|
|
|
|
|
|
my ($opts, %directive) = Hub::opts($fields); |
69
|
|
|
|
|
|
|
$result->{'value'} = ''; |
70
|
|
|
|
|
|
|
my $relpath = $directive{'image'} || ''; |
71
|
|
|
|
|
|
|
my %args = %directive; # copy |
72
|
|
|
|
|
|
|
my $path = Hub::srcpath($relpath) || $relpath; |
73
|
|
|
|
|
|
|
unless ($path) { |
74
|
|
|
|
|
|
|
warn "Image path not found: $args{'image'}"; |
75
|
|
|
|
|
|
|
return; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
delete $args{'image'}; |
78
|
|
|
|
|
|
|
if ($has_imagelib) { |
79
|
|
|
|
|
|
|
# my ($x,$y,$error) = imgsize($path); |
80
|
|
|
|
|
|
|
my ($x,$y) = _getdims($path, -opts => $opts); |
81
|
|
|
|
|
|
|
$args{'width'} ||= $x; |
82
|
|
|
|
|
|
|
$args{'height'} ||= $y; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
$args{'src'} = $$self{'var_begin'} |
85
|
|
|
|
|
|
|
. "url \"$relpath\"" . $$self{'var_end'}; |
86
|
|
|
|
|
|
|
$$result{'value'} = ''; |
87
|
|
|
|
|
|
|
}; |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
90
|
|
|
|
|
|
|
# _getdims - Get image dimensions |
91
|
|
|
|
|
|
|
# _getdims $path, [options] |
92
|
|
|
|
|
|
|
# |
93
|
|
|
|
|
|
|
# options: |
94
|
|
|
|
|
|
|
# |
95
|
|
|
|
|
|
|
# -max_x=n Maximum width |
96
|
|
|
|
|
|
|
# -max_y=n Maximum height |
97
|
|
|
|
|
|
|
# -min_x=n Minimum width |
98
|
|
|
|
|
|
|
# -min_y=n Minimum height |
99
|
|
|
|
|
|
|
# |
100
|
|
|
|
|
|
|
# my ($w,$h) = _getdims( "/images/laura.jpg", -max_x => 50, -max_y => 50 ); |
101
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
sub _getdims { |
104
|
|
|
|
|
|
|
|
105
|
0
|
|
|
0
|
|
|
my ($opts, $file) = Hub::opts(\@_, { |
106
|
|
|
|
|
|
|
'max_x' => 0, |
107
|
|
|
|
|
|
|
'min_x' => 0, |
108
|
|
|
|
|
|
|
'max_y' => 0, |
109
|
|
|
|
|
|
|
'min_y' => 0, |
110
|
|
|
|
|
|
|
}); |
111
|
|
|
|
|
|
|
|
112
|
0
|
|
|
|
|
|
my $nx = 0; |
113
|
0
|
|
|
|
|
|
my $ny = 0; |
114
|
0
|
|
|
|
|
|
my $w = 0; |
115
|
0
|
|
|
|
|
|
my $h = 0; |
116
|
|
|
|
|
|
|
|
117
|
0
|
|
|
|
|
|
($nx,$ny) = imgsize($file); |
118
|
0
|
|
|
|
|
|
$w = $nx; |
119
|
0
|
|
|
|
|
|
$h = $ny; |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
# Expand |
122
|
0
|
0
|
0
|
|
|
|
if( $nx > 0 && $$opts{'min_x'} > 0 ) { |
123
|
0
|
0
|
|
|
|
|
if( $nx < $$opts{'min_x'} ) { |
124
|
0
|
|
|
|
|
|
my $ratio = $ny/$nx; |
125
|
0
|
|
|
|
|
|
my $expandX = $$opts{'min_x'} - $nx; |
126
|
0
|
|
|
|
|
|
my $expandY = int($expandX*$ratio); |
127
|
0
|
|
|
|
|
|
$w = $nx + $expandX; |
128
|
0
|
|
|
|
|
|
$h = $ny + $expandY; |
129
|
0
|
|
|
|
|
|
$nx = $w; |
130
|
0
|
|
|
|
|
|
$ny = $h; |
131
|
|
|
|
|
|
|
}#if |
132
|
|
|
|
|
|
|
}#if |
133
|
0
|
0
|
0
|
|
|
|
if( $ny > 0 && $$opts{'min_y'} > 0 ) { |
134
|
0
|
0
|
|
|
|
|
if( $ny < $$opts{'min_y'} ) { |
135
|
0
|
|
|
|
|
|
my $ratio = $nx/$ny; |
136
|
0
|
|
|
|
|
|
my $expandY = $$opts{'min_y'} - $ny; |
137
|
0
|
|
|
|
|
|
my $expandX = int($expandY*$ratio); |
138
|
0
|
|
|
|
|
|
$w = $nx + $expandX; |
139
|
0
|
|
|
|
|
|
$h = $ny + $expandY; |
140
|
0
|
|
|
|
|
|
$nx = $w; |
141
|
0
|
|
|
|
|
|
$ny = $h; |
142
|
|
|
|
|
|
|
}#if |
143
|
|
|
|
|
|
|
}#if |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
# Reduce |
146
|
0
|
0
|
|
|
|
|
if( $$opts{'max_x'} > 0 ) { |
147
|
0
|
0
|
|
|
|
|
if( $nx > $$opts{'max_x'} ) { |
148
|
0
|
|
|
|
|
|
my $ratio = $ny/$nx; |
149
|
0
|
|
|
|
|
|
my $reduceX = $nx - $$opts{'max_x'}; |
150
|
0
|
|
|
|
|
|
my $reduceY = int($reduceX*$ratio); |
151
|
0
|
|
|
|
|
|
$w = $nx - $reduceX; |
152
|
0
|
|
|
|
|
|
$h = $ny - $reduceY; |
153
|
0
|
|
|
|
|
|
$nx = $w; |
154
|
0
|
|
|
|
|
|
$ny = $h; |
155
|
|
|
|
|
|
|
}#if |
156
|
|
|
|
|
|
|
}#if |
157
|
0
|
0
|
|
|
|
|
if( $$opts{'max_y'} > 0 ) { |
158
|
0
|
0
|
|
|
|
|
if( $ny > $$opts{'max_y'} ) { |
159
|
0
|
|
|
|
|
|
my $ratio = $nx/$ny; |
160
|
0
|
|
|
|
|
|
my $reduceY = $ny - $$opts{'max_y'}; |
161
|
0
|
|
|
|
|
|
my $reduceX = int($reduceY*$ratio); |
162
|
0
|
|
|
|
|
|
$w = $nx - $reduceX; |
163
|
0
|
|
|
|
|
|
$h = $ny - $reduceY; |
164
|
0
|
|
|
|
|
|
$nx = $w; |
165
|
0
|
|
|
|
|
|
$ny = $h; |
166
|
|
|
|
|
|
|
}#if |
167
|
|
|
|
|
|
|
}#if |
168
|
|
|
|
|
|
|
|
169
|
0
|
|
|
|
|
|
return ($nx, $ny); |
170
|
|
|
|
|
|
|
}#_getdims |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
$EVALUATORS{'jsvar'} = sub { |
174
|
|
|
|
|
|
|
my ($self, $params, $result) = @_; |
175
|
|
|
|
|
|
|
my ($outer_str, $fields, $pos, $text, $parents, $valdata) = @$params; |
176
|
|
|
|
|
|
|
my ($opts, %directive) = Hub::opts($fields); |
177
|
|
|
|
|
|
|
my $value = $self->get_value($directive{'jsvar'}, $valdata, $fields); |
178
|
|
|
|
|
|
|
return unless defined $value; |
179
|
|
|
|
|
|
|
$$result{'value'} = _jsvar($value, $self, $valdata); |
180
|
|
|
|
|
|
|
}; |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
sub _jsvar { |
183
|
0
|
|
|
0
|
|
|
my $item = shift; |
184
|
0
|
0
|
|
|
|
|
return isa($item, 'HASH') |
|
|
0
|
|
|
|
|
|
185
|
|
|
|
|
|
|
? '{' . join(', ', _jsvar_hash($item, @_)) . '}' |
186
|
|
|
|
|
|
|
: isa($item, 'ARRAY') |
187
|
|
|
|
|
|
|
? '[' . join(', ', _jsvar_array($item, @_)) . ']' |
188
|
|
|
|
|
|
|
: "'" . Hub::jsstr(_jsvar_scalar($item, @_)) . "'"; |
189
|
|
|
|
|
|
|
} |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
sub _jsvar_hash { |
192
|
0
|
|
|
0
|
|
|
my $item = shift; |
193
|
0
|
|
|
|
|
|
my @args = @_; |
194
|
0
|
|
|
|
|
|
map {Hub::safestr($_) . ": " . _jsvar($$item{$_}, @args)} keys %$item |
|
0
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
} |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
sub _jsvar_array { |
198
|
0
|
|
|
0
|
|
|
my $item = shift; |
199
|
0
|
|
|
|
|
|
my @args = @_; |
200
|
0
|
|
|
|
|
|
map {_jsvar($_, @args)} @$item; |
|
0
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
} |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
sub _jsvar_scalar { |
204
|
0
|
|
|
0
|
|
|
my ($item, $self, $valdata) = @_; |
205
|
0
|
|
|
|
|
|
my $value = $self->_populate(-text => $item, @$valdata); |
206
|
0
|
|
|
|
|
|
$self->{'*depth'}--; |
207
|
0
|
|
|
|
|
|
return $$value; |
208
|
|
|
|
|
|
|
}; |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
1; |