line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Filter::HereDocIndent;
|
2
|
1
|
|
|
1
|
|
2177
|
use strict;
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
30
|
|
3
|
1
|
|
|
1
|
|
579051
|
use Filter::Util::Call;
|
|
1
|
|
|
|
|
1942
|
|
|
1
|
|
|
|
|
90
|
|
4
|
|
|
|
|
|
|
# use Debug::ShowStuff ':all';
|
5
|
1
|
|
|
1
|
|
10
|
use re 'taint';
|
|
1
|
|
|
|
|
6
|
|
|
1
|
|
|
|
|
59
|
|
6
|
1
|
|
|
1
|
|
5
|
use vars qw($VERSION $debug);
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
78
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
# documentation at end of file
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
# version
|
12
|
|
|
|
|
|
|
$VERSION = '0.91';
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
# constants
|
15
|
1
|
|
|
1
|
|
6
|
use constant REG => 1;
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
93
|
|
16
|
1
|
|
|
1
|
|
6
|
use constant HEREDOC => 2;
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
42
|
|
17
|
1
|
|
|
1
|
|
6
|
use constant POD => 3; # reserved for later improvement
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
1073
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
#------------------------------------------------------------------
|
21
|
|
|
|
|
|
|
# new
|
22
|
|
|
|
|
|
|
#
|
23
|
|
|
|
|
|
|
sub new {
|
24
|
1
|
|
|
1
|
0
|
3
|
my ($class, %opts) = @_;
|
25
|
1
|
|
|
|
|
3
|
my $self = bless({}, $class);
|
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
# default INDENT_CONTENT
|
28
|
1
|
50
|
|
|
|
6
|
defined($opts{'INDENT_CONTENT'}) or $opts{'INDENT_CONTENT'} = 1;
|
29
|
1
|
|
|
|
|
9
|
$self->{'INDENT_CONTENT'} = $opts{'INDENT_CONTENT'};
|
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
# default state
|
32
|
1
|
|
|
|
|
3
|
$self->{'state'} = REG;
|
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
# return object
|
35
|
1
|
|
|
|
|
6
|
return $self;
|
36
|
|
|
|
|
|
|
}
|
37
|
|
|
|
|
|
|
#
|
38
|
|
|
|
|
|
|
# new
|
39
|
|
|
|
|
|
|
#------------------------------------------------------------------
|
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
#------------------------------------------------------------------
|
43
|
|
|
|
|
|
|
# import routine: creates filter object and adds it
|
44
|
|
|
|
|
|
|
# to the filters array
|
45
|
|
|
|
|
|
|
#
|
46
|
|
|
|
|
|
|
sub import {
|
47
|
1
|
|
|
1
|
|
11
|
my ($class, %opts) = @_;
|
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
# add filter if set to do so
|
50
|
1
|
50
|
|
|
|
10
|
if ( defined($opts{'filter_add'}) ? $opts{'filter_add'} : 1 ) {
|
|
|
50
|
|
|
|
|
|
51
|
1
|
|
|
|
|
4
|
filter_add($class->new(%opts));
|
52
|
|
|
|
|
|
|
}
|
53
|
|
|
|
|
|
|
}
|
54
|
|
|
|
|
|
|
#
|
55
|
|
|
|
|
|
|
# import routine
|
56
|
|
|
|
|
|
|
#------------------------------------------------------------------
|
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
#------------------------------------------------------------------
|
60
|
|
|
|
|
|
|
# filter: this sub is run for every line in the calling script
|
61
|
|
|
|
|
|
|
#
|
62
|
|
|
|
|
|
|
sub filter {
|
63
|
57
|
|
|
57
|
0
|
7656
|
my $self = shift;
|
64
|
57
|
|
|
|
|
317
|
my $status = filter_read() ;
|
65
|
57
|
|
|
|
|
85
|
my $line = $_;
|
66
|
|
|
|
|
|
|
|
67
|
57
|
|
|
|
|
118
|
($status, $line) = $self->process_line($status, $line);
|
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
# set line and return value
|
70
|
57
|
|
|
|
|
86
|
$_= $line;
|
71
|
57
|
|
|
|
|
2723
|
$status;
|
72
|
|
|
|
|
|
|
}
|
73
|
|
|
|
|
|
|
#
|
74
|
|
|
|
|
|
|
# filter
|
75
|
|
|
|
|
|
|
#------------------------------------------------------------------
|
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
#------------------------------------------------------------------
|
80
|
|
|
|
|
|
|
# filter_block
|
81
|
|
|
|
|
|
|
#
|
82
|
|
|
|
|
|
|
sub filter_block {
|
83
|
0
|
|
|
0
|
0
|
0
|
my ($self, $block) = @_;
|
84
|
0
|
|
|
|
|
0
|
my (@lines, $status);
|
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
# parse block into lines
|
87
|
0
|
|
|
|
|
0
|
@lines = split("\n", $block);
|
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
# loop through lines
|
90
|
|
|
|
|
|
|
LINE_LOOP:
|
91
|
0
|
|
|
|
|
0
|
foreach my $line (@lines) {
|
92
|
0
|
|
|
|
|
0
|
($status, $line) = $self->process_line(1, "$line\n");
|
93
|
|
|
|
|
|
|
}
|
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
# return
|
96
|
0
|
|
|
|
|
0
|
return join('', @lines);
|
97
|
|
|
|
|
|
|
}
|
98
|
|
|
|
|
|
|
#
|
99
|
|
|
|
|
|
|
# filter_block
|
100
|
|
|
|
|
|
|
#------------------------------------------------------------------
|
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
#------------------------------------------------------------------
|
105
|
|
|
|
|
|
|
# process_line
|
106
|
|
|
|
|
|
|
#
|
107
|
|
|
|
|
|
|
sub process_line {
|
108
|
57
|
|
|
57
|
0
|
80
|
my ($self, $status, $line) = @_;
|
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
# if we're at the end of the file
|
111
|
57
|
100
|
|
|
|
253
|
if (! $status) {
|
|
|
100
|
|
|
|
|
|
112
|
|
|
|
|
|
|
# for debugging this module
|
113
|
1
|
50
|
|
|
|
5
|
if ($debug)
|
|
0
|
|
|
|
|
0
|
|
114
|
|
|
|
|
|
|
{print STDERR "\n--------------------------\n"}
|
115
|
|
|
|
|
|
|
}
|
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
# if in here doc
|
118
|
|
|
|
|
|
|
elsif ($self->{'state'} == HEREDOC) {
|
119
|
|
|
|
|
|
|
# if this is the end of the heredoc
|
120
|
8
|
100
|
|
|
|
69
|
if ($line =~ m|^(\s*)$self->{'del_regex'}\s*$|) {
|
121
|
2
|
|
|
|
|
4
|
my $len = length($1);
|
122
|
|
|
|
|
|
|
|
123
|
2
|
50
|
|
|
|
7
|
if ($self->{'INDENT_CONTENT'}) {
|
124
|
2
|
|
|
|
|
2
|
foreach my $el (@{$self->{'lines'}}) {
|
|
2
|
|
|
|
|
8
|
|
125
|
6
|
100
|
|
|
|
79
|
$el =~ s|^\s{$len}|| or $el =~ s|^\s+||;
|
126
|
6
|
50
|
|
|
|
26
|
$el eq '' and $el = "\n";
|
127
|
|
|
|
|
|
|
}
|
128
|
|
|
|
|
|
|
}
|
129
|
|
|
|
|
|
|
|
130
|
2
|
|
|
|
|
3
|
$line = join('', @{$self->{'lines'}}, $self->{'del'}, "\n");
|
|
2
|
|
|
|
|
8
|
|
131
|
2
|
|
|
|
|
5
|
$self->{'state'} = REG;
|
132
|
|
|
|
|
|
|
}
|
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
# else add to lines array
|
135
|
|
|
|
|
|
|
else {
|
136
|
6
|
|
|
|
|
7
|
push @{$self->{'lines'}}, $line;
|
|
6
|
|
|
|
|
16
|
|
137
|
6
|
|
|
|
|
11
|
$line = '';
|
138
|
|
|
|
|
|
|
}
|
139
|
|
|
|
|
|
|
}
|
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
# else in regular code
|
142
|
|
|
|
|
|
|
else {
|
143
|
|
|
|
|
|
|
# if this line starts a heredoc
|
144
|
48
|
100
|
|
|
|
152
|
if ($line =~ m/
|
145
|
|
|
|
|
|
|
^ # start of line
|
146
|
|
|
|
|
|
|
[^#]* # anything except a comment marker
|
147
|
|
|
|
|
|
|
<<
|
148
|
|
|
|
|
|
|
\s*
|
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
(
|
151
|
|
|
|
|
|
|
'[^']+'
|
152
|
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
"[^"]+"
|
154
|
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
\w+
|
156
|
|
|
|
|
|
|
)
|
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
[^'"]*
|
159
|
|
|
|
|
|
|
;
|
160
|
|
|
|
|
|
|
\s*
|
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
/sx
|
163
|
|
|
|
|
|
|
) {
|
164
|
|
|
|
|
|
|
|
165
|
2
|
|
|
|
|
8
|
$self->{'del'} = $1;
|
166
|
2
|
50
|
|
|
|
18
|
$self->{'del'} =~ s|^'(.*)'$|$1| or $self->{'del'} =~ s|^"(.*)"$|$1|;
|
167
|
2
|
|
|
|
|
8
|
$self->{'del_regex'} = quotemeta($self->{'del'});
|
168
|
|
|
|
|
|
|
|
169
|
2
|
|
|
|
|
29
|
$self->{'lines'} = [];
|
170
|
2
|
|
|
|
|
5
|
$self->{'state'} = HEREDOC;
|
171
|
|
|
|
|
|
|
}
|
172
|
|
|
|
|
|
|
}
|
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
# for debugging this module
|
175
|
57
|
50
|
|
|
|
115
|
print STDERR $line if $debug;
|
176
|
|
|
|
|
|
|
|
177
|
57
|
|
|
|
|
171
|
return ($status, $line);
|
178
|
|
|
|
|
|
|
}
|
179
|
|
|
|
|
|
|
#
|
180
|
|
|
|
|
|
|
# process_line
|
181
|
|
|
|
|
|
|
#------------------------------------------------------------------
|
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
# return true
|
186
|
|
|
|
|
|
|
1;
|
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
__END__
|