2 Einträge, 1 Seite |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package HTML::Template::Extension::HEAD_BODY;
$VERSION = "0.22";
sub Version { $VERSION; }
use Carp;
use strict;
use HTML::TokeParser;
my %fields_parent =
(
autoDeleteHeader => 0,
);
sub init {
my $self = shift;
while (my ($key,$val) = each(%fields_parent)) {
$self->{$key} = $self->{$key} || $val;
}
&push_filter($self);
}
sub push_filter {
my $self = shift;
push @{$self->{filter}},@{_get_filter($self)};
}
sub _get_filter {
my $self = shift;
my @ret;
if ($self->{autoDeleteHeader}) {
push @ret, sub {
my $tmpl = shift;
my $header;
###if ($$tmpl =~s{^.+?<body([^>'"]*|".*?"|'.*?')+>}{}msi) {
if ($$tmpl =~s{(^.+?<body(?:[^>'"]*|".*?"|'.*?')+>)}{}msi) {
###$self->{header} = $&;
$self->{header} = $1;
&tokenizer_header($self);
} else {
# header doesn't exist
undef $self->{header};
undef $self->{tokens};
}
$$tmpl =~ s{</body>.+}{}msi;
};
}
return \@ret;
}
sub autoDeleteHeader {
my $s=shift;
if (@_) {
$s->{autoDeleteHeader}=shift;
# reload local filter
$s->reloadFilter;
$s->{_auto_parse} = 1;
};
my $ret = $s->{autoDeleteHeader};
return
}
sub tokenizer_header {
# prende l'header contenuto in $self->{header} e ne estrae i
# token fondamentali inserendoli in $self->{tokens}
my $self = shift;
my $header = $self->{header};
$header =~m|<head>(.*?)</head>|smi;
$header = $1;
my $p = HTML::TokeParser->new(\$header);
$self->{tokens} = {};
while (my $token = $p->get_tag()) {
my $tag = $token->[0];
my $type = substr($tag,0,1) eq '/' ? 'E' : 'S';
my $tag_text;
if ($type eq 'S') {
$tag_text = $token->[3];
my $text = $p->get_text();
my $struct = [$tag_text,$text,undef];
push @{$self->{tokens}->{$tag}},$struct;
} elsif ($type eq 'E') {
$tag = substr($tag,1,length($tag)-1);
$tag_text = $token->[1];
my $last_idx = scalar @{$self->{tokens}->{$tag}}-1;
$self->{tokens}->{$tag}->[$last_idx]->[2] = $tag_text;
}
}
}
sub header {my $s = shift;return exists($s->{header}) ? $s->{header} : ''};
sub js_header { return &header_js(shift); }
sub header_js {
# ritorna il codice javascript presente nell'header
my $self = shift;
my $ret;
#my $re_init = q|<\s*script(?:\s*\s+language\s*=\s*['"]?\s*javascript(?:.*?)['"]\s*.*?)?>|;
#my $re_end = q|<\s*\/script\s*>|;
#while (s/$re_init.*?$re_end//msxi) {
# $ret .= $&;
#}
my $js_token = $self->{tokens}->{script};
foreach (@{$js_token}) {
$ret .= $_->[0] . $_->[1] . $_->[2];
}
return $ret;
}
sub header_css {
# ritorna i css presenti nell'header
my $self = shift;
my $ret;
my $style_token = $self->{tokens}->{style};
foreach (@{$style_token}) {
$ret .= $_->[0] . $_->[1] . $_->[2];
}
return $ret;
}
sub body_attributes {
# ritorna gli attributi interni al campo body
my $self = shift;
my $h = $self->{header};
my $re_init = q|<\s*body(.*?)>|;
$h=~/$re_init/msxi;
return $1;
}
sub header_tokens {
# ritorna un riferimento ad un hash che contiene
# come chiavi tutti i tag presenti nell'header <HEAD>...</HEAD>
# ogni elemento dell'hash e' un riferimento ad un array.
# Ogni array e' a sua volta un riferimento ad array di tre elementi
# tag_init - testo contenuto tra il tag e l'eventuale fine tag o successivo tag - eventuale fine tag o undef
my $self = shift;
return $self->{tokens};
}
1;
if ($$tmpl =~s{(^.+?<body(?:[^>'"]*|".*?"|'.*?')+>)}{}msi)
$header =~m|<head>(.*?)</head>|smi;
2 Einträge, 1 Seite |