run perlcritic against booker.pl

This commit is contained in:
Francois Perrad 2017-12-08 06:49:00 +01:00
parent 99057f6759
commit eb0a4b1f93
2 changed files with 71 additions and 58 deletions

View File

@ -9,21 +9,28 @@
#This preprocessor will then open "file" and insert it as a verbatim copy. #This preprocessor will then open "file" and insert it as a verbatim copy.
# #
#Tom St Denis #Tom St Denis
use strict;
#get graphics type #get graphics type
my $graph;
if (shift =~ /PDF/) { if (shift =~ /PDF/) {
$graph = ""; $graph = "";
} else { } else {
$graph = ".ps"; $graph = ".ps";
} }
open(IN,"<tommath.src") or die "Can't open source file"; open(my $in, '<', 'tommath.src') or die "Can't open source file";
open(OUT,">tommath.tex") or die "Can't open destination file"; open(my $out, '>', 'tommath.tex') or die "Can't open destination file";
print "Scanning for sections\n"; print "Scanning for sections\n";
$chapter = $section = $subsection = 0; my $chapter = 0;
$x = 0; my $section = 0;
while (<IN>) { my $subsection = 0;
my $x = 0;
my %index1;
my %index2;
my %index3;
while (<$in>) {
print "."; print ".";
if (!(++$x % 80)) { print "\n"; } if (!(++$x % 80)) { print "\n"; }
#update the headings #update the headings
@ -40,25 +47,29 @@ while (<IN>) {
} }
if ($_ =~ m/MARK/) { if ($_ =~ m/MARK/) {
@m = split(",",$_); my @m = split ',', $_;
chomp(@m[1]); chomp $m[1];
$index1{@m[1]} = $chapter; $index1{$m[1]} = $chapter;
$index2{@m[1]} = $section; $index2{$m[1]} = $section;
$index3{@m[1]} = $subsection; $index3{$m[1]} = $subsection;
} }
} }
close(IN); close $in;
open(IN,"<tommath.src") or die "Can't open source file"; open($in, '<', 'tommath.src') or die "Can't open source file";
$readline = $wroteline = 0; my $readline = 0;
$srcline = 0; my $wroteline = 0;
my $srcline = 0;
my $totlines;
my @text;
while (<IN>) { while (<$in>) {
++$readline; ++$readline;
++$srcline; ++$srcline;
if ($_ =~ m/MARK/) { if ($_ =~ m/MARK/) {
} elsif ($_ =~ m/EXAM/ || $_ =~ m/LIST/) { } elsif ($_ =~ m/EXAM/ || $_ =~ m/LIST/) {
my $skipheader;
if ($_ =~ m/EXAM/) { if ($_ =~ m/EXAM/) {
$skipheader = 1; $skipheader = 1;
} else { } else {
@ -67,28 +78,28 @@ while (<IN>) {
# EXAM,file # EXAM,file
chomp($_); chomp($_);
@m = split(",",$_); my @m = split ',', $_;
open(SRC,"<../$m[1]") or die "Error:$srcline:Can't open source file $m[1]"; open(my $src, '<', "../$m[1]") or die "Error:$srcline:Can't open source file $m[1]";
print "$srcline:Inserting $m[1]:"; print "$srcline:Inserting $m[1]:";
$line = 0; my $line = 0;
$tmp = $m[1]; my $tmp = $m[1];
$tmp =~ s/_/"\\_"/ge; $tmp =~ s/_/"\\_"/ge;
print OUT "\\vspace{+3mm}\\begin{small}\n\\hspace{-5.1mm}{\\bf File}: $tmp\n\\vspace{-3mm}\n\\begin{alltt}\n"; print {$out} "\\vspace{+3mm}\\begin{small}\n\\hspace{-5.1mm}{\\bf File}: $tmp\n\\vspace{-3mm}\n\\begin{alltt}\n";
$wroteline += 5; $wroteline += 5;
if ($skipheader == 1) { if ($skipheader == 1) {
# scan till next end of comment, e.g. skip license # scan till next end of comment, e.g. skip license
while (<SRC>) { while (<$src>) {
$text[$line++] = $_; $text[$line++] = $_;
last if ($_ =~ /libtom\.org/); last if ($_ =~ /libtom\.org/);
} }
<SRC>; <$src>;
} }
$inline = 0; my $inline = 0;
while (<SRC>) { while (<$src>) {
next if ($_ =~ /\$Source/); next if ($_ =~ /\$Source/);
next if ($_ =~ /\$Revision/); next if ($_ =~ /\$Revision/);
next if ($_ =~ /\$Date/); next if ($_ =~ /\$Date/);
@ -101,34 +112,38 @@ while (<IN>) {
$_ =~ s/\\/'\symbol{92}'/ge; $_ =~ s/\\/'\symbol{92}'/ge;
$_ =~ s/\^/"\\"/ge; $_ =~ s/\^/"\\"/ge;
printf OUT ("%03d ", $line); printf {$out} ("%03d ", $line);
for ($x = 0; $x < length($_); $x++) { for ($x = 0; $x < length($_); $x++) {
print OUT chr(vec($_, $x, 8)); print {$out} chr(vec($_, $x, 8));
if ($x == 75) { if ($x == 75) {
print OUT "\n "; print {$out} "\n ";
++$wroteline; ++$wroteline;
} }
} }
print OUT "\n"; print {$out} "\n";
++$wroteline; ++$wroteline;
} }
$totlines = $line; $totlines = $line;
print OUT "\\end{alltt}\n\\end{small}\n"; print {$out} "\\end{alltt}\n\\end{small}\n";
close(SRC); close $src;
print "$inline lines\n"; print "$inline lines\n";
$wroteline += 2; $wroteline += 2;
} elsif ($_ =~ m/@\d+,.+@/) { } elsif ($_ =~ m/@\d+,.+@/) {
# line contains [number,text] # line contains [number,text]
# e.g. @14,for (ix = 0)@ # e.g. @14,for (ix = 0)@
$txt = $_; my $txt = $_;
while ($txt =~ m/@\d+,.+@/) { while ($txt =~ m/@\d+,.+@/) {
@m = split("@",$txt); # splits into text, one, two my @m = split '@', $txt; # splits into text, one, two
@parms = split(",",$m[1]); # splits one,two into two elements my @parms = split ',', $m[1]; # splits one,two into two elements
# now search from $parms[0] down for $parms[1] # now search from $parms[0] down for $parms[1]
$found1 = 0; my $found;
$found2 = 0; my $found1 = 0;
for ($i = $parms[0]; $i < $totlines && $found1 == 0; $i++) { my $found2 = 0;
my $foundline;
my $foundline1;
my $foundline2;
for (my $i = $parms[0]; $i < $totlines && $found1 == 0; $i++) {
if ($text[$i] =~ m/\Q$parms[1]\E/) { if ($text[$i] =~ m/\Q$parms[1]\E/) {
$foundline1 = $i + 1; $foundline1 = $i + 1;
$found1 = 1; $found1 = 1;
@ -136,7 +151,7 @@ while (<IN>) {
} }
# now search backwards # now search backwards
for ($i = $parms[0] - 1; $i >= 0 && $found2 == 0; $i--) { for (my $i = $parms[0] - 1; $i >= 0 && $found2 == 0; $i--) {
if ($text[$i] =~ m/\Q$parms[1]\E/) { if ($text[$i] =~ m/\Q$parms[1]\E/) {
$foundline2 = $i + 1; $foundline2 = $i + 1;
$found2 = 1; $found2 = 1;
@ -163,7 +178,7 @@ while (<IN>) {
# if found replace # if found replace
if ($found == 1) { if ($found == 1) {
$delta = $parms[0] - $foundline; my $delta = $parms[0] - $foundline;
print "Found replacement tag for \"$parms[1]\" on line $srcline which refers to line $foundline (delta $delta)\n"; print "Found replacement tag for \"$parms[1]\" on line $srcline which refers to line $foundline (delta $delta)\n";
$_ =~ s/@\Q$m[1]\E@/$foundline/; $_ =~ s/@\Q$m[1]\E@/$foundline/;
} else { } else {
@ -171,32 +186,31 @@ while (<IN>) {
} }
# remake the rest of the line # remake the rest of the line
$cnt = @m;
$txt = ""; $txt = "";
for ($i = 2; $i < $cnt; $i++) { for (my $i = 2; $i < scalar(@m); $i++) {
$txt = $txt . $m[$i] . "@"; $txt = $txt . $m[$i] . "@";
} }
} }
print OUT $_; print {$out} $_;
++$wroteline; ++$wroteline;
} elsif ($_ =~ /~.+~/) { } elsif ($_ =~ /~.+~/) {
# line contains a ~text~ pair used to refer to indexing :-) # line contains a ~text~ pair used to refer to indexing :-)
$txt = $_; my $txt = $_;
while ($txt =~ /~.+~/) { while ($txt =~ /~.+~/) {
@m = split("~", $txt); my @m = split '~', $txt;
# word is the second position # word is the second position
$word = @m[1]; my $word = $m[1];
$a = $index1{$word}; my $a = $index1{$word};
$b = $index2{$word}; my $b = $index2{$word};
$c = $index3{$word}; my $c = $index3{$word};
# if chapter (a) is zero it wasn't found # if chapter (a) is zero it wasn't found
if ($a == 0) { if ($a == 0) {
print "ERROR: the tag \"$word\" on line $srcline was not found previously marked.\n"; print "ERROR: the tag \"$word\" on line $srcline was not found previously marked.\n";
} else { } else {
# format the tag as x, x.y or x.y.z depending on the values # format the tag as x, x.y or x.y.z depending on the values
$str = $a; my $str = $a;
$str = $str . ".$b" if ($b != 0); $str = $str . ".$b" if ($b != 0);
$str = $str . ".$c" if ($c != 0); $str = $str . ".$c" if ($c != 0);
@ -239,29 +253,28 @@ while (<IN>) {
} }
# remake rest of the line # remake rest of the line
$cnt = @m;
$txt = ""; $txt = "";
for ($i = 2; $i < $cnt; $i++) { for (my $i = 2; $i < scalar(@m); $i++) {
$txt = $txt . $m[$i] . "~"; $txt = $txt . $m[$i] . "~";
} }
} }
print OUT $_; print {$out} $_;
++$wroteline; ++$wroteline;
} elsif ($_ =~ m/FIGU/) { } elsif ($_ =~ m/FIGU/) {
# FIGU,file,caption # FIGU,file,caption
chomp($_); chomp($_);
@m = split(",", $_); my @m = split ',', $_;
print OUT "\\begin{center}\n\\begin{figure}[h]\n\\includegraphics{pics/$m[1]$graph}\n"; print {$out} "\\begin{center}\n\\begin{figure}[h]\n\\includegraphics{pics/$m[1]$graph}\n";
print OUT "\\caption{$m[2]}\n\\label{pic:$m[1]}\n\\end{figure}\n\\end{center}\n"; print {$out} "\\caption{$m[2]}\n\\label{pic:$m[1]}\n\\end{figure}\n\\end{center}\n";
$wroteline += 4; $wroteline += 4;
} else { } else {
print OUT $_; print {$out} $_;
++$wroteline; ++$wroteline;
} }
} }
print "Read $readline lines, wrote $wroteline lines\n"; print "Read $readline lines, wrote $wroteline lines\n";
close (OUT); close $out;
close (IN); close $in;
system('perl -pli -e "s/\s*$//" tommath.tex'); system('perl -pli -e "s/\s*$//" tommath.tex');

View File

@ -144,7 +144,7 @@ new_file:
perl dep.pl perl dep.pl
perlcritic: perlcritic:
perlcritic *.pl perlcritic *.pl doc/*.pl
astyle: astyle:
astyle --options=astylerc $(OBJECTS:.o=.c) tommath*.h demo/*.c etc/*.c mtest/mtest.c astyle --options=astylerc $(OBJECTS:.o=.c) tommath*.h demo/*.c etc/*.c mtest/mtest.c