root/ttdiff/trunk/ttdiff

Revision 611 (checked in by miyagawa, 18 years ago)

--dump option

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1 #!/usr/local/bin/perl
2 use strict;
3 use vars qw($VERSION);
4 $VERSION = 0.02;
5
6 =head1 NAME
7
8 ttdiff - find difference between structures of two TT templates
9
10 =head1 SYNOPSIS
11
12   ttdiff file1 file2
13   ttdiff --dump file
14
15 =head1 DESCRIPTION
16
17 C<ttdiff> is a command line tool to find structured difference between
18 two TT templates. It may be quite useful for checking before your
19 co-working designer fixes up your bare-bone template into production
20 ready design. What you should check is just run C<ttdiff> against your
21 template and the final template to see any typos or losing in
22 migration are there.
23
24 =head1 PREREQUISTICS
25
26 =over 4
27
28 =item *
29
30 Template-Toolkit 2.07
31
32 =item *
33
34 Text::Diff 0.32
35
36 =back
37
38 =head1 AUTHOR
39
40 Tatsuhiko Miyagawa E<lt>miyagawa@bulknews.netE<gt>
41
42 This library is free software; you can redistribute it and/or modify
43 it under the same terms as Perl itself.
44
45 =head1 SEE ALSO
46
47 L<Template>, L<Text::Diff>
48
49 =cut
50
51 package TTDiffApp;
52 use IO::File;
53 use Template::Parser;
54 use Text::Diff;
55
56 sub new {
57     my($class, %p) = @_;
58     bless \%p, $class;
59 }
60
61 sub run {
62     my $self = shift;
63     if ($self->{dump}) {
64         $self->dump_structure;
65     } else {
66         my @tokens = map $self->make_tokens($_), @{$self->{files}};
67         $self->check_directives(@tokens);
68     }
69 }
70
71 sub dump_structure {
72     my $self = shift;
73     my $structure = $self->make_bone($self->make_tokens($self->{dump}));
74     print $structure;
75 }
76
77 sub make_tokens {
78     my($self, $file) = @_;
79     my $text = $self->slurp($file);
80     my $parser = Template::Parser->new;
81     my $tokens = $parser->split_text($text);
82     return [ grep ref, @$tokens ]; # refernce only
83 }
84
85 sub slurp {
86     my($self, $file) = @_;
87     my $io   = IO::File->new($file) or die "$file: $!";
88     local $/;
89     return <$io>;
90 }
91
92 sub check_directives {
93     my($self, @tokens) = @_;
94     my($bone1, $bone2) = map $self->make_bone($_), @tokens;
95     my $diff = diff \$bone1, \$bone2;
96     print $diff if $diff;
97 }
98
99 sub make_bone {
100     my($self, $token) = @_;
101     return join '', map "[% $_->[0] %]\n", @$token;
102 }
103
104 package main;
105
106 sub usage {
107     (my $me = $0) =~ s@.*/@@;
108     warn "Usage: $me file1 file2 / $me --dump file\n";
109 }
110
111 @ARGV == 2 or usage(), exit;
112 my $app = $ARGV[0] eq '--dump'
113     ? TTDiffApp->new(dump => $ARGV[1])
114     : TTDiffApp->new(files => \@ARGV);
115 $app->run;
116
117
Note: See TracBrowser for help on using the browser.