line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------------- |
2
|
|
|
|
|
|
|
# MKDoc::XML::Stripper |
3
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------------- |
4
|
|
|
|
|
|
|
# Author : Jean-Michel Hiver. |
5
|
|
|
|
|
|
|
# Copyright : (c) MKDoc Holdings Ltd, 2003 |
6
|
|
|
|
|
|
|
# |
7
|
|
|
|
|
|
|
# This module removes user-defined markup from an existing XML file / variable. |
8
|
|
|
|
|
|
|
# |
9
|
|
|
|
|
|
|
# This module is distributed under the same license as Perl itself. |
10
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------------- |
11
|
|
|
|
|
|
|
package MKDoc::XML::Stripper; |
12
|
3
|
|
|
3
|
|
119883
|
use MKDoc::XML::Tokenizer; |
|
3
|
|
|
|
|
13
|
|
|
3
|
|
|
|
|
111
|
|
13
|
3
|
|
|
3
|
|
20
|
use File::Spec; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
82
|
|
14
|
3
|
|
|
3
|
|
15
|
use strict; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
105
|
|
15
|
3
|
|
|
3
|
|
436
|
use warnings; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
7321
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
## |
19
|
|
|
|
|
|
|
# $class->new(); |
20
|
|
|
|
|
|
|
# -------------- |
21
|
|
|
|
|
|
|
# Returns a new MKDoc::XML::Stripper object. |
22
|
|
|
|
|
|
|
## |
23
|
|
|
|
|
|
|
sub new |
24
|
|
|
|
|
|
|
{ |
25
|
3
|
|
|
3
|
1
|
372
|
my $class = shift; |
26
|
3
|
|
|
|
|
16
|
my $self = bless { @_ }, $class; |
27
|
3
|
|
|
|
|
12
|
return $self; |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub load_def |
32
|
|
|
|
|
|
|
{ |
33
|
1
|
|
|
1
|
1
|
5
|
my $self = shift; |
34
|
1
|
|
|
|
|
2
|
my $file = shift; |
35
|
|
|
|
|
|
|
|
36
|
1
|
50
|
|
|
|
5
|
$file =~ /\// and return $self->_load_def ($file); |
37
|
1
|
50
|
|
|
|
4
|
$file =~ /\./ and return $self->_load_def ($file); |
38
|
|
|
|
|
|
|
|
39
|
1
|
|
|
|
|
4
|
$file .= '.txt'; |
40
|
1
|
|
|
|
|
3
|
for (@INC) |
41
|
|
|
|
|
|
|
{ |
42
|
2
|
|
|
|
|
46
|
my $path = File::Spec->catfile ($_, qw /MKDoc XML Stripper/, $file); |
43
|
2
|
100
|
66
|
|
|
76
|
-e $path and -f $path and return $self->_load_def ($path); |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
0
|
|
|
|
|
0
|
warn "Cannot read-open $file. Reason: Doesn't seem to be anywhere in \@INC"; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub _load_def |
51
|
|
|
|
|
|
|
{ |
52
|
1
|
|
|
1
|
|
2
|
my $self = shift; |
53
|
1
|
|
|
|
|
3
|
my $file = shift; |
54
|
|
|
|
|
|
|
|
55
|
1
|
|
33
|
|
|
44
|
open FP, "<$file" || do { |
56
|
|
|
|
|
|
|
warn "Cannot read-open $file. Reason: $!"; |
57
|
|
|
|
|
|
|
return; |
58
|
|
|
|
|
|
|
}; |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
# clean $self |
61
|
1
|
|
|
|
|
2
|
for (keys %{$self}) { delete $self->{$_} } |
|
1
|
|
|
|
|
11
|
|
|
0
|
|
|
|
|
0
|
|
62
|
1
|
|
|
|
|
40
|
while () { |
63
|
893
|
|
|
|
|
1218
|
chomp(); |
64
|
893
|
|
|
|
|
1592
|
s/\#.*$//; |
65
|
893
|
|
|
|
|
2357
|
s/^\s*//; |
66
|
893
|
|
|
|
|
3906
|
s/\s*$//; |
67
|
893
|
100
|
|
|
|
2448
|
next unless ($_ ne ''); |
68
|
|
|
|
|
|
|
|
69
|
543
|
|
|
|
|
1634
|
my @l = split /\s+/, $_; |
70
|
543
|
|
|
|
|
1258
|
$self->allow (@l); |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
1
|
|
|
|
|
674
|
close FP; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
## |
78
|
|
|
|
|
|
|
# $self->allow ($tag, @attributes); |
79
|
|
|
|
|
|
|
# --------------------------------- |
80
|
|
|
|
|
|
|
# Allows the tag $tag to be present along with a list of attributes, |
81
|
|
|
|
|
|
|
# i.e. |
82
|
|
|
|
|
|
|
# |
83
|
|
|
|
|
|
|
# $self->allow (qw /p class id/); |
84
|
|
|
|
|
|
|
## |
85
|
|
|
|
|
|
|
sub allow |
86
|
|
|
|
|
|
|
{ |
87
|
545
|
|
|
545
|
1
|
1086
|
my $self = shift; |
88
|
545
|
|
|
|
|
1171
|
my $tag = shift; |
89
|
545
|
|
100
|
|
|
10197
|
$self->{$tag} ||= {}; |
90
|
545
|
|
|
|
|
927
|
for (@_) { $self->{$tag}->{$_} = 1 }; |
|
547
|
|
|
|
|
9420
|
|
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
## |
95
|
|
|
|
|
|
|
# $self->disallow ($tag, @attributes); |
96
|
|
|
|
|
|
|
# ------------------------------------ |
97
|
|
|
|
|
|
|
# Disallows the tag $tag to be present. |
98
|
|
|
|
|
|
|
## |
99
|
|
|
|
|
|
|
sub disallow |
100
|
|
|
|
|
|
|
{ |
101
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
102
|
0
|
|
|
|
|
0
|
my $tag = shift; |
103
|
0
|
|
|
|
|
0
|
delete $self->{$tag}; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
## |
108
|
|
|
|
|
|
|
# $self->process_data ($data); |
109
|
|
|
|
|
|
|
# ---------------------------- |
110
|
|
|
|
|
|
|
# Strips tags on $data and returns the stripped result. |
111
|
|
|
|
|
|
|
## |
112
|
|
|
|
|
|
|
sub process_data |
113
|
|
|
|
|
|
|
{ |
114
|
1
|
|
|
1
|
1
|
7
|
my $self = shift; |
115
|
1
|
|
|
|
|
2
|
my $data = shift; |
116
|
1
|
|
|
|
|
16
|
my $tokens = MKDoc::XML::Tokenizer->process_data ($data); |
117
|
1
|
|
|
|
|
2
|
my @result = map { $self->strip ($_) } @{$tokens}; |
|
7
|
|
|
|
|
19
|
|
|
1
|
|
|
|
|
2
|
|
118
|
1
|
|
|
|
|
3
|
return join '', map { $$_ } @result; |
|
5
|
|
|
|
|
27
|
|
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
## |
123
|
|
|
|
|
|
|
# $self->process_file ($file); |
124
|
|
|
|
|
|
|
# ---------------------------- |
125
|
|
|
|
|
|
|
# Strips tags on $file and returns the stripped result. |
126
|
|
|
|
|
|
|
## |
127
|
|
|
|
|
|
|
sub process_file |
128
|
|
|
|
|
|
|
{ |
129
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
130
|
0
|
|
|
|
|
0
|
my $file = shift; |
131
|
0
|
|
|
|
|
0
|
my $tokens = MKDoc::XML::Tokenizer->process_file ($file); |
132
|
0
|
|
|
|
|
0
|
my @result = map { $self->strip ($_) } @{$tokens}; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
133
|
0
|
|
|
|
|
0
|
return join '', map { $$_ } @result; |
|
0
|
|
|
|
|
0
|
|
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
## |
138
|
|
|
|
|
|
|
# $self->strip ($token); |
139
|
|
|
|
|
|
|
# ---------------------- |
140
|
|
|
|
|
|
|
# Returns this token stripped out of the stuff which we don't want. |
141
|
|
|
|
|
|
|
# Returns an empty list if the token is not allowed. |
142
|
|
|
|
|
|
|
## |
143
|
|
|
|
|
|
|
sub strip |
144
|
|
|
|
|
|
|
{ |
145
|
18
|
|
|
18
|
0
|
57
|
my $self = shift; |
146
|
18
|
|
|
|
|
20
|
my $token = shift; |
147
|
18
|
|
|
|
|
75
|
my $node = $token->tag(); |
148
|
18
|
100
|
|
|
|
50
|
defined $node || return $token; |
149
|
|
|
|
|
|
|
|
150
|
13
|
|
|
|
|
21
|
my $tag = $node->{_tag}; |
151
|
13
|
100
|
|
|
|
65
|
return unless ( $self->{$tag} ); |
152
|
|
|
|
|
|
|
|
153
|
4
|
|
|
|
|
6
|
for (keys %{$node}) |
|
4
|
|
|
|
|
17
|
|
154
|
|
|
|
|
|
|
{ |
155
|
17
|
100
|
|
|
|
54
|
/^_/ and next; |
156
|
5
|
100
|
|
|
|
22
|
delete $node->{$_} unless $self->{$tag}->{$_}; |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
|
159
|
4
|
|
|
|
|
14
|
return new MKDoc::XML::Token ( _node_to_tag ($node) ); |
160
|
|
|
|
|
|
|
} |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
sub _node_to_tag |
164
|
|
|
|
|
|
|
{ |
165
|
16
|
|
|
16
|
|
4077
|
my $node = shift; |
166
|
16
|
|
|
|
|
30
|
my $tag = $node->{_tag}; |
167
|
16
|
|
|
|
|
23
|
my $open = $node->{_open}; |
168
|
16
|
|
|
|
|
22
|
my $close = $node->{_close}; |
169
|
16
|
100
|
|
|
|
20
|
my %attr = map { /^_/ ? () : ($_ => $node->{$_}) } keys %{$node}; |
|
52
|
|
|
|
|
179
|
|
|
16
|
|
|
|
|
50
|
|
170
|
12
|
|
|
|
|
15
|
my $attr = join ' ', map { |
171
|
16
|
|
|
|
|
40
|
my $key = $_; |
172
|
12
|
|
|
|
|
19
|
my $val = $attr{$key}; |
173
|
12
|
100
|
|
|
|
58
|
($val =~ /\"/) ? "$key='$val'" : "$key=\"$val\"" |
174
|
|
|
|
|
|
|
} keys %attr; |
175
|
|
|
|
|
|
|
|
176
|
16
|
|
|
|
|
27
|
my $res = '<'; |
177
|
16
|
100
|
100
|
|
|
61
|
$res .= '/' if ($close and not $open); |
178
|
16
|
|
|
|
|
23
|
$res .= $tag; |
179
|
16
|
100
|
100
|
|
|
93
|
$res .= " $attr" if ($attr and $open); |
180
|
16
|
100
|
100
|
|
|
63
|
$res .= ' /' if ($open and $close); |
181
|
16
|
|
|
|
|
20
|
$res .= '>'; |
182
|
16
|
|
|
|
|
66
|
return $res; |
183
|
|
|
|
|
|
|
} |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
1; |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
__END__ |