Read from both STDIN and named files
[project-aon.git] / common / scripts / gbfixencoding.pl
1 #!/usr/bin/perl -Tw
2 #
3 # Uses ANSI color escapes to highlight text and for cursor movement
4 #
5
6 use strict;
7
8 my $usage = "Usage:\n\tgbfixquotes.pl INFILE OUTFILE\n";
9
10 my $lineNumber = 1;
11
12 my ($infile, $outfile);
13
14 if( $#ARGV == 1 ) {
15   $infile = shift @ARGV;
16   $outfile = shift @ARGV;
17 }
18 else {
19   die $usage;
20 }
21
22 if( $infile =~ m{(^.*$)} && -f $1 ) {
23   open( INFILE, "<$1" ) or die "Error: unable to read from \"$infile\": $!\n";
24 }
25 else {
26   die "Error: bad input file\n";
27 }
28
29 if( $outfile =~ m{(^.*$)} ) {
30   open( OUTFILE, ">$1" ) or die "Error: unable to write to \"$outfile\": $!\n";
31 }
32 else {
33   die "Error: bad output file\n";
34 }
35
36 while( my $line = <INFILE> ) {
37   $line = &encodify( $line );
38
39   print OUTFILE $line;
40   ++$lineNumber;
41 }
42
43 close OUTFILE;
44 close INFILE;
45
46 ################################################################################
47
48 sub encodify {
49   my ($line) = @_;
50   my $modified = $line;
51   my $replacements = 0;
52
53   if( $modified =~ s{ ([[:space:]]) \& ([[:space:]]) }{$1<ch.ampersand/>$2}xg ) { $replacements = 1; }
54   if( $modified =~ s{ [[:space:]]+ - [[:space:]]+ }{<ch.emdash/>}xg ) { $replacements = 1; }
55   if( $modified =~ s{ (?<!\!) ([[:space:]])* -- ([[:space:]])* (?!>) }{$1<ch.emdash/>$2}xg ) { $replacements = 1; }
56   if( $modified =~ s{ [[:space:]]* \227 [[:space:]]* }{<ch.emdash/>}xg ) { $replacements = 1; }
57   if( $modified =~ s{ ([[:digit:]]) - ([[:digit:]]) }{$1<ch.endash/>$2}xg ) { $replacements = 1; }
58   if( $modified =~ s{ [[:space:]]* \227 [[:space:]]* }{<ch.endash/>}xg ) { $replacements = 1; }
59   if( $modified =~ s{ > [[:space:]]* \. [[:space:]]* \. ([[:space:]]* \.)? }{><ch.lellips/>}xg ) { $replacements = 1; }
60   if( $modified =~ s{ [[:space:]]* \. [[:space:]]* \. ([[:space:]]* \.)? }{<ch.ellips/>}xg ) { $replacements = 1; }
61   if( $modified =~ s{ (</?quote>) \1 }{<ch.thinspace/>}xg ) { $replacements = 1; }
62   if( $modified =~ s{ <quote> \&apos; }{<quote><ch.thinspace/><ch.apos/>}xg ) { $replacements = 1; }
63   if( $modified =~ s{ \&apos; </quote> }{<ch.apos/><ch.thinspace/></quote>}xg ) { $replacements = 1; }
64   if( $modified =~ s{ __+ }{<ch.blankline/>}xg ) { $replacements = 1; }
65   if( $modified =~ s{\%}{<ch.percent/>}xg ) { $replacements = 1; }
66
67   if( $replacements ) {
68     print "\033[2J";
69     print &highlight( $line ) . "\n";
70     print &highlight( $modified );
71     print "\033[7m    (a)ccept, (r)eject, (q)uit: [accept]\033[0m >> ";
72
73     my $response = <STDIN>;
74     chomp $response;
75     if( $response =~ m/^[aA]$/ || $response eq "" ) { $line = $modified; }
76     elsif( $response =~ m/^[qQ]$/ ) {
77       print OUTFILE $line;
78       while( $line = <INFILE> ) {
79         print OUTFILE $line;
80       }
81       exit( 0 );
82     }
83     return $line;
84   }
85   else { return $line; }
86 }
87
88 sub highlight {
89   my ($text) = @_;
90
91   my $start = "\033[45;30m";
92   my $encodedStart = "\033[40;35m";
93   my $dashStart = "\033[46;30m";
94   my $encodedDashStart = "\033[40;36m";
95   my $stop  = "\033[0m";
96
97   $text =~ s{^[[:space:]]+}{}g;
98   $text =~ s{ ([[:space:]]) \& ([[:space:]]) }{$1$start\&$stop$2}xg;
99   $text =~ s{(<ch.ampersand/>)}{$encodedStart$1$stop}g;
100   $text =~ s{(<ch.emdash/>)}{$encodedDashStart$1$stop}g;
101   $text =~ s{ [[:space:]] (\&) [[:space:]] }{$dashStart$1$stop}xg;
102   $text =~ s{ ([[:space:]]+ - [[:space:]]+) }{$dashStart$1$stop}xg;
103   $text =~ s{ (?<!\!) ([[:space:]]* -- [[:space:]]*) }{$dashStart$1$stop}xg;
104   $text =~ s{ ([[:space:]]* \227 [[:space:]]*) }{$dashStart$1$stop}xg;
105   $text =~ s{(<ch.endash/>)}{$encodedDashStart$1$stop}g;
106   $text =~ s{ ([[:digit:]]) - ([[:digit:]]) }{$1$dashStart-$stop$2}xg;
107   $text =~ s{ ([[:space:]]* \226 [[:space:]]*) }{$dashStart$1$stop}xg;
108   $text =~ s{(<ch.lellips/>)}{$encodedStart$1$stop}g;
109   $text =~ s{ > ([[:space:]]* \. [[:space:]]* \. ([[:space:]]* \.)?) }{>$start$1$stop}xg;
110   $text =~ s{(<ch.ellips/>)}{$encodedStart$1$stop}g;
111   $text =~ s{ ([[:space:]]* \. [[:space:]]* \. ([[:space:]]* \.)?) }{$start$1$stop}xg;
112   $text =~ s{(<ch.thinspace/>)}{$encodedStart$1$stop}g;
113   $text =~ s{ (</?quote> \1) }{$start$1$stop}xg;
114   $text =~ s{ (<quote> <ch.apos/>) }{$start$1$stop}xg;
115   $text =~ s{ (<ch.apos/> </quote>) }{$start$1$stop}xg;
116   $text =~ s{(<ch.blankline/>)}{$encodedStart$1$stop}g;
117   $text =~ s{ (__+) }{$start$1$stop}xg;
118   $text =~ s{(<ch.percent/>)}{$encodedStart$1$stop}g;
119   $text =~ s{(\%)}{$start$1$stop}xg;
120
121   return $text;
122 }