| 1 | package Filter::HereDocIndent; | ||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | 1 | ▶ | use Filter::Util::Call; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | # use Debug::ShowStuff ':all'; | ||||||||||||||
| 5 | 1 | ▶ | use re 'taint'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 1 | ▶ | use vars qw($VERSION $debug); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | |||||||||||||||
| 8 | # documentation at end of file | ||||||||||||||
| 9 | |||||||||||||||
| 10 | |||||||||||||||
| 11 | # version | ||||||||||||||
| 12 | 1 | $VERSION = '0.91'; | |||||||||||||
| 13 | |||||||||||||||
| 14 | # constants | ||||||||||||||
| 15 | 1 | ▶ | use constant REG => 1; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 16 | 1 | ▶ | use constant HEREDOC => 2; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 17 | 1 | ▶ | use constant POD => 3; # reserved for later improvement | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 18 | |||||||||||||||
| 19 | |||||||||||||||
| 20 | #------------------------------------------------------------------ | ||||||||||||||
| 21 | # new | ||||||||||||||
| 22 | # | ||||||||||||||
| 23 | sub new { | ||||||||||||||
| 24 | 1 | ▶ | my ($class, %opts) = @_; | ||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||
| 25 | 1 | my $self = bless({}, $class); | |||||||||||||
| 26 | |||||||||||||||
| 27 | # default INDENT_CONTENT | ||||||||||||||
| 28 | 1 | ▶ | defined($opts{'INDENT_CONTENT'}) or $opts{'INDENT_CONTENT'} = 1; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 29 | 1 | $self->{'INDENT_CONTENT'} = $opts{'INDENT_CONTENT'}; | |||||||||||||
| 30 | |||||||||||||||
| 31 | # default state | ||||||||||||||
| 32 | 1 | $self->{'state'} = REG; | |||||||||||||
| 33 | |||||||||||||||
| 34 | # return object | ||||||||||||||
| 35 | 1 | 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 | ▶ | my ($class, %opts) = @_; | ||||||||||||
|
Subroutine
import: called CC=3 SCAR=11.7
| |||||||||||||||
| 48 | |||||||||||||||
| 49 | # add filter if set to do so | ||||||||||||||
| 50 | 1 | ▶ | if ( defined($opts{'filter_add'}) ? $opts{'filter_add'} : 1 ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 51 | 1 | 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 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
filter: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 64 | 57 | my $status = filter_read() ; | |||||||||||||
| 65 | 57 | my $line = $_; | |||||||||||||
| 66 | |||||||||||||||
| 67 | 57 | ($status, $line) = $self->process_line($status, $line); | |||||||||||||
| 68 | |||||||||||||||
| 69 | # set line and return value | ||||||||||||||
| 70 | 57 | $_= $line; | |||||||||||||
| 71 | 57 | $status; | |||||||||||||
| 72 | } | ||||||||||||||
| 73 | # | ||||||||||||||
| 74 | # filter | ||||||||||||||
| 75 | #------------------------------------------------------------------ | ||||||||||||||
| 76 | |||||||||||||||
| 77 | |||||||||||||||
| 78 | |||||||||||||||
| 79 | #------------------------------------------------------------------ | ||||||||||||||
| 80 | # filter_block | ||||||||||||||
| 81 | # | ||||||||||||||
| 82 | sub filter_block { | ||||||||||||||
| 83 | 0 | ▶ | my ($self, $block) = @_; | ||||||||||||
|
Subroutine
filter_block: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 84 | 0 | my (@lines, $status); | |||||||||||||
| 85 | |||||||||||||||
| 86 | # parse block into lines | ||||||||||||||
| 87 | 0 | @lines = split("\n", $block); | |||||||||||||
| 88 | |||||||||||||||
| 89 | # loop through lines | ||||||||||||||
| 90 | LINE_LOOP: | ||||||||||||||
| 91 | 0 | foreach my $line (@lines) { | |||||||||||||
| 92 | 0 | ($status, $line) = $self->process_line(1, "$line\n"); | |||||||||||||
| 93 | } | ||||||||||||||
| 94 | |||||||||||||||
| 95 | # return | ||||||||||||||
| 96 | 0 | return join('', @lines); | |||||||||||||
| 97 | } | ||||||||||||||
| 98 | # | ||||||||||||||
| 99 | # filter_block | ||||||||||||||
| 100 | #------------------------------------------------------------------ | ||||||||||||||
| 101 | |||||||||||||||
| 102 | |||||||||||||||
| 103 | |||||||||||||||
| 104 | #------------------------------------------------------------------ | ||||||||||||||
| 105 | # process_line | ||||||||||||||
| 106 | # | ||||||||||||||
| 107 | sub process_line { | ||||||||||||||
| 108 | 57 | ▶ | my ($self, $status, $line) = @_; | ||||||||||||
|
Subroutine
process_line: called CC=12 SCAR=25.1
Pod
undocumented
| |||||||||||||||
| 109 | |||||||||||||||
| 110 | # if we're at the end of the file | ||||||||||||||
| 111 | 57 | ▶ | if (! $status) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 112 | # for debugging this module | ||||||||||||||
| 113 | 1 | ▶ | if ($debug) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 114 | 0 | {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 | ▶ | if ($line =~ m|^(\s*)$self->{'del_regex'}\s*$|) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 121 | 2 | my $len = length($1); | |||||||||||||
| 122 | |||||||||||||||
| 123 | 2 | ▶ | if ($self->{'INDENT_CONTENT'}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 124 | 2 | foreach my $el (@{$self->{'lines'}}) { | |||||||||||||
| 125 | 6 | ▶ | $el =~ s|^\s{$len}|| or $el =~ s|^\s+||; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 126 | 6 | ▶ | $el eq '' and $el = "\n"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 127 | } | ||||||||||||||
| 128 | } | ||||||||||||||
| 129 | |||||||||||||||
| 130 | 2 | $line = join('', @{$self->{'lines'}}, $self->{'del'}, "\n"); | |||||||||||||
| 131 | 2 | $self->{'state'} = REG; | |||||||||||||
| 132 | } | ||||||||||||||
| 133 | |||||||||||||||
| 134 | # else add to lines array | ||||||||||||||
| 135 | else { | ||||||||||||||
| 136 | 6 | push @{$self->{'lines'}}, $line; | |||||||||||||
| 137 | 6 | $line = ''; | |||||||||||||
| 138 | } | ||||||||||||||
| 139 | } | ||||||||||||||
| 140 | |||||||||||||||
| 141 | # else in regular code | ||||||||||||||
| 142 | else { | ||||||||||||||
| 143 | # if this line starts a heredoc | ||||||||||||||
| 144 | 48 | ▶ | if ($line =~ m/ | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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 | $self->{'del'} = $1; | |||||||||||||
| 166 | 2 | ▶ | $self->{'del'} =~ s|^'(.*)'$|$1| or $self->{'del'} =~ s|^"(.*)"$|$1|; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 167 | 2 | $self->{'del_regex'} = quotemeta($self->{'del'}); | |||||||||||||
| 168 | |||||||||||||||
| 169 | 2 | $self->{'lines'} = []; | |||||||||||||
| 170 | 2 | $self->{'state'} = HEREDOC; | |||||||||||||
| 171 | } | ||||||||||||||
| 172 | } | ||||||||||||||
| 173 | |||||||||||||||
| 174 | # for debugging this module | ||||||||||||||
| 175 | 57 | ▶ | print STDERR $line if $debug; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 176 | |||||||||||||||
| 177 | 57 | return ($status, $line); | |||||||||||||
| 178 | } | ||||||||||||||
| 179 | # | ||||||||||||||
| 180 | # process_line | ||||||||||||||
| 181 | #------------------------------------------------------------------ | ||||||||||||||
| 182 | |||||||||||||||
| 183 | |||||||||||||||
| 184 | |||||||||||||||
| 185 | # return true | ||||||||||||||
| 186 | 1 | 1; | |||||||||||||
| 187 | |||||||||||||||
| 188 | __END__ | ||||||||||||||