comparison Yuki/DiffText.pm @ 0:a2f0a2c135cf

hg init
author Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
date Sun, 06 Jun 2010 22:00:38 +0900
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:a2f0a2c135cf
1 package Yuki::DiffText;
2 use strict;
3 use Algorithm::Diff qw(traverse_sequences);
4 use vars qw($VERSION @EXPORT_OK @ISA);
5 use vars qw($diff_text $diff_msgrefA $diff_msgrefB @diff_deleted @diff_added);
6 require Exporter;
7 @ISA = qw(Exporter);
8 @EXPORT_OK = qw(difftext);
9 $VERSION = '0.1';
10
11 =head1 NAME
12
13 Yuki::DiffText - A wrapper of Algorithm::Diff for YukiWiki.
14
15 =head1 SYNOPSIS
16
17 use strict;
18 use Yuki::DiffText qw(difftext);
19
20 my @array1 = ( "Alice", "Bobby", "Chris", );
21 my @array2 = ( "Alice", "Chris", "Diana", );
22 my $difftext = difftext(\@array1, \@array2);
23 print $difftext;
24
25 # Result:
26 # =Alice
27 # -Bobby
28 # =Chris
29 # +Diana
30
31 =head1 SEE ALSO
32
33 =over 4
34
35 =item L<Algorithm::Diff>
36
37 =back
38
39 =head1 AUTHOR
40
41 Hiroshi Yuki <hyuki@hyuki.com> http://www.hyuki.com/
42
43 =head1 LICENSE
44
45 Copyright (C) 2002 by Hiroshi Yuki.
46
47 This program is free software; you can redistribute it and/or
48 modify it under the same terms as Perl itself.
49
50 =cut
51
52 sub difftext {
53 ($diff_msgrefA, $diff_msgrefB) = @_;
54 undef $diff_text;
55 undef @diff_deleted;
56 undef @diff_added;
57 traverse_sequences(
58 $diff_msgrefA, $diff_msgrefB,
59 {
60 MATCH => \&df_match,
61 DISCARD_A => \&df_delete,
62 DISCARD_B => \&df_add,
63 }
64 );
65 &diff_flush;
66 return $diff_text;
67 }
68
69 sub diff_flush {
70 $diff_text .= join('', map { "-$_\n" } splice(@diff_deleted));
71 $diff_text .= join('', map { "+$_\n" } splice(@diff_added));
72 }
73
74 sub df_match {
75 my ($a, $b) = @_;
76 &diff_flush;
77 $diff_text .= "=$diff_msgrefA->[$a]\n";
78 }
79
80 sub df_delete {
81 my ($a, $b) = @_;
82 push(@diff_deleted, $diff_msgrefA->[$a]);
83 }
84
85 sub df_add {
86 my ($a, $b) = @_;
87 push(@diff_added, $diff_msgrefB->[$b]);
88 }
89
90 1;