-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathamon2-tt2tx.pl
executable file
·140 lines (103 loc) · 3.41 KB
/
amon2-tt2tx.pl
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
137
138
139
140
#!/usr/bin/env perl
use strict;
use warnings;
use Pod::Usage;
use File::Copy;
use Path::Class;
use File::Path;
use File::Basename;
pod2usage() if scalar @ARGV;
my $bkup_dir = 'tt_org';
warn "backup-dir: $bkup_dir\n";
mkdir $bkup_dir if ! -d $bkup_dir;
tt_tx(
root => 'lib',
#target => qr{/Web\.pm$}, # - Amon2-3.66
target => qr{/Web/View\.pm$}, # Amon2-3.67-
replace => [
sub{ $_[0] =~ s/TTerse/Kolon/ },
],
);
tt_tx(
root => 'lib',
target => qr/\/Dispatcher\.pm$/,
replace => [
sub{ $_[0] =~ s/index\.tt/index.tx/ },
],
);
tmpl_tt_tx('tmpl');
exit;
sub tt_tx {
my %args = @_;
dir($args{root})->recurse(callback => sub{
my $tt_file = shift;
return if $tt_file->is_dir;
return if $tt_file !~ $args{target};
warn "target: $tt_file\n";
my $tt_path = $bkup_dir.'/'.$tt_file;
warn " copy $tt_file -> $tt_path\n";
mkpath( dirname($tt_path), { verbose => 1 });
copy $tt_file, $tt_path;
my $tx_path = $tt_file;
warn " read $tt_path\n";
warn " write $tx_path\n";
open my $tt_fh, "<", $tt_path or die "open error $tt_path. $!";
open my $tx_fh, ">", $tx_path or die "open error $tx_path. $!";
while(my $buf = <$tt_fh>){
for my $replace (@{$args{replace}}){
$replace->($buf);
}
print $tx_fh $buf;
}
close $tt_fh;
close $tx_fh;
});
}
sub tmpl_tt_tx {
my $dir = shift || 'tmpl';
dir($dir)->recurse(callback => sub{
my $tt_file = shift;
return if $tt_file->is_dir;
return if $tt_file !~ /\.tt$/;
warn "target: $tt_file\n";
my $tt_path = $bkup_dir.'/'.$tt_file;
warn " move $tt_file -> $tt_path\n";
mkpath( dirname($tt_path), { verbose => 1 });
#copy $tt_file, $tt_path;
move $tt_file, $tt_path;
(my $tx_path = $tt_file) =~ s/tt$/tx/;
warn " read $tt_path\n";
warn " write $tx_path\n";
open my $tt_fh, "<", $tt_path or die "open error $tt_path. $!";
open my $tx_fh, ">", $tx_path or die "open error $tx_path. $!";
while(my $buf = <$tt_fh>){
$buf =~ s/\[% IF bodyID %]/<: if \$bodyID { :>/ig;
$buf =~ s/\[% bodyID %]/<: \$bodyID :>/ig;
$buf =~ s/^\s?\[% END %]\s?$/: }/ig;
$buf =~ s/\[% END %]/<: } :>/ig;
$buf =~ s/\[% WRAPPER 'include\/layout.tt' %]/: cascade 'include\/layout.tx'\n: around body -> {/ig;
$buf =~ s/\[% content %]/: block body -> {}/ig;
$buf =~ s/\[% title (.*) %]/<: \$title $1 :>/ig;
$buf =~ s/\[% content %]/: block body -> {}/ig;
$buf =~ s/\[%/<:/ig;
$buf =~ s/%]/:>/ig;
print $tx_fh $buf;
}
close $tt_fh;
close $tx_fh;
});
}
__END__
=head1 NAME
amon2-tt2tx.pl - change Xslate engine TTerse to Kolon after amon2-setup.pl
=head1 SYNOPSIS
% amon2-setup.pl MyApp
% amon2-tt_tx.pl
=head1 DESCRIPTION
amon2-steup.pl is created TTerse format.
this script change to Kolon format.
=head1 SUPPORTS
https://github.com/onopm/Amon2_tt2tx
=head1 AUTHOR
Takafumi Ono
=cut