-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtemplate.pm
More file actions
136 lines (118 loc) · 3.11 KB
/
template.pm
File metadata and controls
136 lines (118 loc) · 3.11 KB
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
package template;
use strict;
my %fields = {
template => '',
markF => '',
markB => '',
keywords => '',
debug => 0,
};
# $template = new template ("filename");
sub new {
my ($that, $template) = @_;
my $class = ref($that) || $that;
my $self = {
_permitted => \%fields,
%fields,
};
bless $self, $class;
$self->_initialize($template);
return $self;
}
sub _initialize {
my ($self, $template) = @_;
$self->_loadTemplate($template);
}
sub _loadTemplate {
my ($self, $filename) = @_;
if (open (TEMPLATE, "<" . $filename)) {
binmode TEMPLATE;
while (read TEMPLATE, my $buffer, 1024) {
$self->{template} .= $buffer;
}
close FILE;
}
}
# $replacement = $template->replace(\%keywords, $markF, $markB);
sub replace {
my ($self, $keywords, $markF, $markB) = @_;
my $replacement = $self->{template};
$self->{keywords} = $keywords;
$self->{markF} = quotemeta $markF;
$self->{markB} = quotemeta $markB;
$markF = $self->{markF};
$markB = $self->{markB};
my @arrays;
$replacement =~ s/\?/\x08/sg;
my @keys = keys %{$keywords};
foreach my $key (@keys) {
my $value = $keywords->{$key};
pos($replacement) = 0;
if (ref($value) eq 'ARRAY') {
push(@arrays, $key) if ($replacement =~ m/$markF$key$markB/sg);
} else {
$replacement =~ s/$markF$key$markB/$value/sg;
}
}
pos($replacement) = 0;
my (@startOffsets, @endOffsets);
my $startLoop = 'startLoop';
my $endLoop = 'endLoop';
while ($replacement =~ /$markF$startLoop$markB/g) {
push @startOffsets, ((pos $replacement) - length '{startLoop}');
$replacement =~ /$markF$endLoop$markB/g;
push @endOffsets, (pos $replacement);
}
my @replacements;
for (my $i; $i < @startOffsets; $i++) {
push @replacements, substr $replacement, $startOffsets[$i], $endOffsets[$i]-$startOffsets[$i];
}
for (my $i; $i < @replacements; $i++) {
my $replace = $replacements[$i];
my $text = $self->_expand($replace, \@arrays);
$replacement =~ s/$replace/$text/sg;
}
$replacement =~ s/\x08/\?/sg;
return $replacement;
}
# $expanded = _expand($text, \@keys);
sub _expand {
my ($self, $text, $keys) = @_;
my $keywords = $self->{keywords};
my $markF = $self->{markF};
my $markB = $self->{markB};
my $replacement;
my $expanded;
my $firstFound;
for (my $i; $i < @{$keys}; $i++) {
my $key = $keys->[$i];
if (quotemeta $text =~ /$markF$key$markB/sg) {
$firstFound = $key;
last;
}
}
my $array = $keywords->{$firstFound};
my $i;
foreach my $value (@{$array}) {
$replacement = $text;
$replacement =~ s/\{startLoop\}//sg;
$replacement =~ s/\{endLoop\}//sg;
next if !($replacement =~ s/$markF$firstFound$markB/$value/sg);
foreach my $key (@{$keys}) {
next if ($key eq $firstFound);
$replacement =~ s/$markF$key$markB/$keywords->{$key}->[$i]/sg;
}
$expanded .= $replacement;
$i++;
}
if ($expanded) {
return $expanded;
} else {
foreach my $key (@{$keys}) {
$text =~ s/\{startLoop\}//sg;
$text =~ s/\{endLoop\}//sg;
$text =~ s/$markF$key$markB/none/sg;;
}
return $text;
}
}