line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package File::Mangle; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
54108
|
use 5.008001; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
42
|
|
4
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
42
|
|
5
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
1037
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
require Exporter; |
8
|
|
|
|
|
|
|
require File::Slurp; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
# Items to export into callers namespace by default. Note: do not export |
13
|
|
|
|
|
|
|
# names by default without a very good reason. Use EXPORT_OK instead. |
14
|
|
|
|
|
|
|
# Do not simply export all your public functions/methods/constants. |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
# This allows declaration use File::Mangle ':all'; |
17
|
|
|
|
|
|
|
# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK |
18
|
|
|
|
|
|
|
# will save memory. |
19
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( 'all' => [ qw() ] ); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
our @EXPORT = qw(replace_block); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub fetch_block { |
28
|
3
|
|
|
3
|
1
|
1399
|
my ($filename, $marker) = @_; |
29
|
|
|
|
|
|
|
|
30
|
3
|
|
|
|
|
12
|
my $data = File::Slurp::read_file($filename); |
31
|
|
|
|
|
|
|
|
32
|
3
|
|
|
|
|
248
|
my $start = _format_start_marker($marker); |
33
|
3
|
|
|
|
|
12
|
my $end = _format_end_marker($marker); |
34
|
|
|
|
|
|
|
|
35
|
3
|
100
|
66
|
|
|
124
|
return unless $data =~ m{ \Q$start\E }xms and $data =~ m{ \Q$end\E }xms; |
36
|
|
|
|
|
|
|
|
37
|
2
|
50
|
|
|
|
69
|
if ( $data =~ m{ |
38
|
|
|
|
|
|
|
^ .* \Q$start\E .* $ # start marker |
39
|
|
|
|
|
|
|
((?s: .*? )) # existing content |
40
|
|
|
|
|
|
|
^ .* \Q$end\E .* $ # end marker |
41
|
|
|
|
|
|
|
}xm ) { |
42
|
2
|
|
|
|
|
21
|
return $1; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
else { |
45
|
0
|
|
|
|
|
0
|
return; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub replace_block { |
50
|
2
|
|
|
2
|
1
|
1957
|
my ($filename, $marker, $replacement, $line_comment_marker) = @_; |
51
|
|
|
|
|
|
|
|
52
|
2
|
|
50
|
|
|
13
|
$line_comment_marker ||= '#'; |
53
|
2
|
|
100
|
|
|
10
|
$replacement ||= ''; |
54
|
|
|
|
|
|
|
|
55
|
2
|
|
|
|
|
8
|
my $data = File::Slurp::read_file($filename); |
56
|
|
|
|
|
|
|
|
57
|
2
|
|
|
|
|
167
|
my $start = _format_start_marker($marker); |
58
|
2
|
|
|
|
|
5
|
my $end = _format_end_marker($marker); |
59
|
|
|
|
|
|
|
|
60
|
2
|
100
|
66
|
|
|
41
|
unless ( $data =~ /$start/ and $data =~ /$end/ ) { |
61
|
1
|
|
|
|
|
6
|
$data .= "\n$line_comment_marker $start\n$line_comment_marker $end\n"; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
2
|
|
|
|
|
6
|
chomp $replacement; |
65
|
|
|
|
|
|
|
|
66
|
2
|
|
|
|
|
77
|
$data =~ s{ |
67
|
|
|
|
|
|
|
^ ( .* \Q$start\E .* ) $ # start marker |
68
|
|
|
|
|
|
|
((?s: .*? )) # existing content |
69
|
|
|
|
|
|
|
^ ( .* \Q$end\E .* ) $ # end marker |
70
|
|
|
|
|
|
|
}{$1\n$replacement\n$3\n}xm; |
71
|
|
|
|
|
|
|
|
72
|
2
|
|
|
|
|
11
|
File::Slurp::write_file($filename, $data); |
73
|
|
|
|
|
|
|
|
74
|
2
|
|
|
|
|
317
|
return $2; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub insert_block_before { |
78
|
1
|
|
|
1
|
1
|
2383
|
my ($filename, $marker, $placement, $line_comment_marker) = @_; |
79
|
|
|
|
|
|
|
|
80
|
1
|
|
50
|
|
|
8
|
$line_comment_marker ||= '#'; |
81
|
|
|
|
|
|
|
|
82
|
1
|
|
|
|
|
4
|
my $data = File::Slurp::read_file($filename); |
83
|
|
|
|
|
|
|
|
84
|
1
|
|
|
|
|
72
|
my $start = _format_start_marker($marker); |
85
|
1
|
|
|
|
|
4
|
my $end = _format_end_marker($marker); |
86
|
|
|
|
|
|
|
|
87
|
1
|
50
|
33
|
|
|
31
|
return if $data =~ m{ \Q$start\E }xms and $data =~ m{ \Q$end\E }xms; |
88
|
|
|
|
|
|
|
|
89
|
1
|
50
|
|
|
|
49
|
if ( $data =~ m{ |
90
|
|
|
|
|
|
|
( |
91
|
|
|
|
|
|
|
\A (?s: .* ) |
92
|
|
|
|
|
|
|
) |
93
|
|
|
|
|
|
|
( |
94
|
|
|
|
|
|
|
^ .* $placement .* $ |
95
|
|
|
|
|
|
|
(?s: .* ) \z |
96
|
|
|
|
|
|
|
) |
97
|
|
|
|
|
|
|
}xm |
98
|
|
|
|
|
|
|
) { |
99
|
1
|
|
|
|
|
3
|
$data = $1; |
100
|
1
|
|
|
|
|
4
|
$data .= $line_comment_marker . ' ' . $start . "\n"; |
101
|
1
|
|
|
|
|
4
|
$data .= $line_comment_marker . ' ' . $end . "\n"; |
102
|
1
|
|
|
|
|
4
|
$data .= $2; |
103
|
|
|
|
|
|
|
|
104
|
1
|
|
|
|
|
4
|
File::Slurp::write_file($filename, $data); |
105
|
|
|
|
|
|
|
|
106
|
1
|
|
|
|
|
130
|
return 1; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
else { |
109
|
0
|
|
|
|
|
0
|
return; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
sub insert_block_after { |
114
|
1
|
|
|
1
|
1
|
815
|
my ($filename, $marker, $placement, $line_comment_marker) = @_; |
115
|
|
|
|
|
|
|
|
116
|
1
|
|
50
|
|
|
10
|
$line_comment_marker ||= '#'; |
117
|
|
|
|
|
|
|
|
118
|
1
|
|
|
|
|
6
|
my $data = File::Slurp::read_file($filename); |
119
|
|
|
|
|
|
|
|
120
|
1
|
|
|
|
|
75
|
my $start = _format_start_marker($marker); |
121
|
1
|
|
|
|
|
4
|
my $end = _format_end_marker($marker); |
122
|
|
|
|
|
|
|
|
123
|
1
|
50
|
33
|
|
|
34
|
return if $data =~ m{ \Q$start\E }xms and $data =~ m{ \Q$end\E }xms; |
124
|
|
|
|
|
|
|
|
125
|
1
|
50
|
|
|
|
38
|
if ( $data =~ m{ |
126
|
|
|
|
|
|
|
( |
127
|
|
|
|
|
|
|
\A (?s: .* ) |
128
|
|
|
|
|
|
|
^ .* $placement .* $ |
129
|
|
|
|
|
|
|
) |
130
|
|
|
|
|
|
|
( |
131
|
|
|
|
|
|
|
(?s: .* ) \z |
132
|
|
|
|
|
|
|
) |
133
|
|
|
|
|
|
|
}xm |
134
|
|
|
|
|
|
|
) { |
135
|
1
|
|
|
|
|
4
|
$data = $1; |
136
|
1
|
|
|
|
|
4
|
$data .= "\n" . $line_comment_marker . ' ' . $start . "\n"; |
137
|
1
|
|
|
|
|
4
|
$data .= $line_comment_marker . ' ' . $end . "\n"; |
138
|
1
|
|
|
|
|
3
|
$data .= $2; |
139
|
|
|
|
|
|
|
|
140
|
1
|
|
|
|
|
3
|
File::Slurp::write_file($filename, $data); |
141
|
|
|
|
|
|
|
|
142
|
1
|
|
|
|
|
151
|
return 1; |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
else { |
145
|
0
|
|
|
|
|
0
|
return; |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
sub _format_start_marker { |
150
|
7
|
|
|
7
|
|
16
|
my $marker = shift; |
151
|
7
|
|
|
|
|
21
|
return '###:START:' . $marker . ':###'; |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
sub _format_end_marker { |
154
|
7
|
|
|
7
|
|
9
|
my $marker = shift; |
155
|
7
|
|
|
|
|
20
|
return '###:END:' . $marker . ':###'; |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
1; |
159
|
|
|
|
|
|
|
__END__ |