root/ttdiff/trunk/ttdiff

Revision 605 (checked in by miyagawa, 17 years ago)

Initial revision

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