libtommath/doc/booker.pl

296 lines
8.4 KiB
Perl
Raw Normal View History

2003-05-17 12:33:54 +00:00
#!/bin/perl
#
#Used to prepare the book "tommath.src" for LaTeX by pre-processing it into a .tex file
#
#Essentially you write the "tommath.src" as normal LaTex except where you want code snippets you put
#
#EXAM,file
#
#This preprocessor will then open "file" and insert it as a verbatim copy.
#
#Tom St Denis
2017-12-08 05:49:00 +00:00
use strict;
2003-05-17 12:33:54 +00:00
#get graphics type
2017-12-08 05:49:00 +00:00
my $graph;
2003-05-17 12:33:54 +00:00
if (shift =~ /PDF/) {
$graph = "";
} else {
$graph = ".ps";
2014-09-02 00:14:38 +00:00
}
2003-05-17 12:33:54 +00:00
2017-12-08 05:49:00 +00:00
open(my $in, '<', 'tommath.src') or die "Can't open source file";
open(my $out, '>', 'tommath.tex') or die "Can't open destination file";
2003-05-17 12:33:54 +00:00
print "Scanning for sections\n";
2017-12-08 05:49:00 +00:00
my $chapter = 0;
my $section = 0;
my $subsection = 0;
my $x = 0;
my %index1;
my %index2;
my %index3;
while (<$in>) {
2003-05-17 12:33:54 +00:00
print ".";
if (!(++$x % 80)) { print "\n"; }
2014-09-02 00:14:38 +00:00
#update the headings
2003-05-17 12:33:54 +00:00
if (~($_ =~ /\*/)) {
if ($_ =~ /\\chapter\{.+}/) {
2003-05-17 12:33:54 +00:00
++$chapter;
$section = $subsection = 0;
} elsif ($_ =~ /\\section\{.+}/) {
2003-05-17 12:33:54 +00:00
++$section;
$subsection = 0;
} elsif ($_ =~ /\\subsection\{.+}/) {
2003-05-17 12:33:54 +00:00
++$subsection;
}
2014-09-02 00:14:38 +00:00
}
2003-05-17 12:33:54 +00:00
if ($_ =~ m/MARK/) {
2017-12-08 05:49:00 +00:00
my @m = split ',', $_;
chomp $m[1];
$index1{$m[1]} = $chapter;
$index2{$m[1]} = $section;
$index3{$m[1]} = $subsection;
2003-05-17 12:33:54 +00:00
}
}
2017-12-08 05:49:00 +00:00
close $in;
2003-05-17 12:33:54 +00:00
2017-12-08 05:49:00 +00:00
open($in, '<', 'tommath.src') or die "Can't open source file";
my $readline = 0;
my $wroteline = 0;
my $srcline = 0;
my $totlines;
my @text;
2003-05-17 12:33:54 +00:00
2017-12-08 05:49:00 +00:00
while (<$in>) {
2003-05-17 12:33:54 +00:00
++$readline;
++$srcline;
2014-09-02 00:14:38 +00:00
2003-05-17 12:33:54 +00:00
if ($_ =~ m/MARK/) {
} elsif ($_ =~ m/EXAM/ || $_ =~ m/LIST/) {
2017-12-08 05:49:00 +00:00
my $skipheader;
2003-05-17 12:33:54 +00:00
if ($_ =~ m/EXAM/) {
$skipheader = 1;
} else {
$skipheader = 0;
}
2014-09-02 00:14:38 +00:00
2003-05-17 12:33:54 +00:00
# EXAM,file
chomp($_);
2017-12-08 05:49:00 +00:00
my @m = split ',', $_;
open(my $src, '<', "../$m[1]") or die "Error:$srcline:Can't open source file $m[1]";
2014-09-02 00:14:38 +00:00
2003-05-17 12:33:54 +00:00
print "$srcline:Inserting $m[1]:";
2014-09-02 00:14:38 +00:00
2017-12-08 05:49:00 +00:00
my $line = 0;
my $tmp = $m[1];
2003-05-17 12:33:54 +00:00
$tmp =~ s/_/"\\_"/ge;
2017-12-08 05:49:00 +00:00
print {$out} "\\vspace{+3mm}\\begin{small}\n\\hspace{-5.1mm}{\\bf File}: $tmp\n\\vspace{-3mm}\n\\begin{alltt}\n";
2003-05-17 12:33:54 +00:00
$wroteline += 5;
2014-09-02 00:14:38 +00:00
2003-05-17 12:33:54 +00:00
if ($skipheader == 1) {
2014-09-02 00:14:38 +00:00
# scan till next end of comment, e.g. skip license
2017-12-08 05:49:00 +00:00
while (<$src>) {
2017-12-18 22:37:01 +00:00
if ($_ =~ /#ifdef BN/) {
printf {$out} ("%03d ", $line);
for ($x = 0; $x < length($_); $x++) {
print {$out} chr(vec($_, $x, 8));
if ($x == 75) {
print {$out} "\n ";
++$wroteline;
}
}
print {$out} "...\n";
++$wroteline;
}
2003-05-17 12:33:54 +00:00
$text[$line++] = $_;
last if ($_ =~ /libtom\.org/);
2003-05-17 12:33:54 +00:00
}
2017-12-08 05:49:00 +00:00
<$src>;
2017-12-18 22:37:01 +00:00
$text[$line++] = $_;
<$src>;
$text[$line++] = $_;
2003-05-17 12:33:54 +00:00
}
2014-09-02 00:14:38 +00:00
2017-12-08 05:49:00 +00:00
my $inline = 0;
while (<$src>) {
2017-12-18 22:37:01 +00:00
next if ($_ =~ /ref/);
next if ($_ =~ /git commit/);
next if ($_ =~ /commit time/);
2003-05-17 12:33:54 +00:00
$text[$line++] = $_;
++$inline;
chomp($_);
$_ =~ s/\t/" "/ge;
$_ =~ s/{/"^{"/ge;
$_ =~ s/}/"^}"/ge;
$_ =~ s/\\/'\symbol{92}'/ge;
$_ =~ s/\^/"\\"/ge;
2014-09-02 00:14:38 +00:00
2017-12-08 05:49:00 +00:00
printf {$out} ("%03d ", $line);
2003-05-17 12:33:54 +00:00
for ($x = 0; $x < length($_); $x++) {
2017-12-08 05:49:00 +00:00
print {$out} chr(vec($_, $x, 8));
2014-09-02 00:14:38 +00:00
if ($x == 75) {
2017-12-08 05:49:00 +00:00
print {$out} "\n ";
2003-05-17 12:33:54 +00:00
++$wroteline;
}
}
2017-12-08 05:49:00 +00:00
print {$out} "\n";
2003-05-17 12:33:54 +00:00
++$wroteline;
}
$totlines = $line;
2017-12-08 05:49:00 +00:00
print {$out} "\\end{alltt}\n\\end{small}\n";
close $src;
2003-05-17 12:33:54 +00:00
print "$inline lines\n";
$wroteline += 2;
} elsif ($_ =~ m/@\d+,.+@/) {
# line contains [number,text]
# e.g. @14,for (ix = 0)@
2017-12-08 05:49:00 +00:00
my $txt = $_;
2003-05-17 12:33:54 +00:00
while ($txt =~ m/@\d+,.+@/) {
2017-12-08 05:49:00 +00:00
my @m = split '@', $txt; # splits into text, one, two
my @parms = split ',', $m[1]; # splits one,two into two elements
2014-09-02 00:14:38 +00:00
# now search from $parms[0] down for $parms[1]
2017-12-08 05:49:00 +00:00
my $found;
my $found1 = 0;
my $found2 = 0;
my $foundline;
my $foundline1;
my $foundline2;
for (my $i = $parms[0]; $i < $totlines && $found1 == 0; $i++) {
2003-05-17 12:33:54 +00:00
if ($text[$i] =~ m/\Q$parms[1]\E/) {
$foundline1 = $i + 1;
$found1 = 1;
}
}
2014-09-02 00:14:38 +00:00
2003-05-17 12:33:54 +00:00
# now search backwards
2017-12-08 05:49:00 +00:00
for (my $i = $parms[0] - 1; $i >= 0 && $found2 == 0; $i--) {
2003-05-17 12:33:54 +00:00
if ($text[$i] =~ m/\Q$parms[1]\E/) {
$foundline2 = $i + 1;
$found2 = 1;
}
}
2014-09-02 00:14:38 +00:00
2003-05-17 12:33:54 +00:00
# now use the closest match or the first if tied
if ($found1 == 1 && $found2 == 0) {
$found = 1;
$foundline = $foundline1;
} elsif ($found1 == 0 && $found2 == 1) {
$found = 1;
$foundline = $foundline2;
} elsif ($found1 == 1 && $found2 == 1) {
$found = 1;
if (($foundline1 - $parms[0]) <= ($parms[0] - $foundline2)) {
$foundline = $foundline1;
} else {
$foundline = $foundline2;
}
} else {
$found = 0;
}
2014-09-02 00:14:38 +00:00
# if found replace
2003-05-17 12:33:54 +00:00
if ($found == 1) {
2017-12-08 05:49:00 +00:00
my $delta = $parms[0] - $foundline;
2003-05-17 12:33:54 +00:00
print "Found replacement tag for \"$parms[1]\" on line $srcline which refers to line $foundline (delta $delta)\n";
$_ =~ s/@\Q$m[1]\E@/$foundline/;
} else {
print "ERROR: The tag \"$parms[1]\" on line $srcline was not found in the most recently parsed source!\n";
}
2014-09-02 00:14:38 +00:00
# remake the rest of the line
2003-05-17 12:33:54 +00:00
$txt = "";
2017-12-08 05:49:00 +00:00
for (my $i = 2; $i < scalar(@m); $i++) {
2003-05-17 12:33:54 +00:00
$txt = $txt . $m[$i] . "@";
}
}
2017-12-08 05:49:00 +00:00
print {$out} $_;
2003-05-17 12:33:54 +00:00
++$wroteline;
} elsif ($_ =~ /~.+~/) {
# line contains a ~text~ pair used to refer to indexing :-)
2017-12-08 05:49:00 +00:00
my $txt = $_;
2003-05-17 12:33:54 +00:00
while ($txt =~ /~.+~/) {
2017-12-08 05:49:00 +00:00
my @m = split '~', $txt;
2014-09-02 00:14:38 +00:00
2003-05-17 12:33:54 +00:00
# word is the second position
2017-12-08 05:49:00 +00:00
my $word = $m[1];
my $a = $index1{$word};
my $b = $index2{$word};
my $c = $index3{$word};
2014-09-02 00:14:38 +00:00
2003-05-17 12:33:54 +00:00
# if chapter (a) is zero it wasn't found
if ($a == 0) {
print "ERROR: the tag \"$word\" on line $srcline was not found previously marked.\n";
} else {
# format the tag as x, x.y or x.y.z depending on the values
2017-12-08 05:49:00 +00:00
my $str = $a;
2003-05-17 12:33:54 +00:00
$str = $str . ".$b" if ($b != 0);
$str = $str . ".$c" if ($c != 0);
2014-09-02 00:14:38 +00:00
2003-05-17 12:33:54 +00:00
if ($b == 0 && $c == 0) {
# its a chapter
if ($a <= 10) {
if ($a == 1) {
$str = "chapter one";
} elsif ($a == 2) {
$str = "chapter two";
} elsif ($a == 3) {
$str = "chapter three";
} elsif ($a == 4) {
$str = "chapter four";
} elsif ($a == 5) {
$str = "chapter five";
} elsif ($a == 6) {
$str = "chapter six";
} elsif ($a == 7) {
$str = "chapter seven";
} elsif ($a == 8) {
$str = "chapter eight";
} elsif ($a == 9) {
$str = "chapter nine";
2005-11-18 05:16:19 +00:00
} elsif ($a == 10) {
2003-05-17 12:33:54 +00:00
$str = "chapter ten";
}
} else {
$str = "chapter " . $str;
}
} else {
2014-09-02 00:14:38 +00:00
$str = "section " . $str if ($b != 0 && $c == 0);
2003-05-17 12:33:54 +00:00
$str = "sub-section " . $str if ($b != 0 && $c != 0);
}
2014-09-02 00:14:38 +00:00
2003-05-17 12:33:54 +00:00
#substitute
$_ =~ s/~\Q$word\E~/$str/;
2014-09-02 00:14:38 +00:00
2003-05-17 12:33:54 +00:00
print "Found replacement tag for marker \"$word\" on line $srcline which refers to $str\n";
}
2014-09-02 00:14:38 +00:00
2003-05-17 12:33:54 +00:00
# remake rest of the line
$txt = "";
2017-12-08 05:49:00 +00:00
for (my $i = 2; $i < scalar(@m); $i++) {
2003-05-17 12:33:54 +00:00
$txt = $txt . $m[$i] . "~";
}
}
2017-12-08 05:49:00 +00:00
print {$out} $_;
2003-05-17 12:33:54 +00:00
++$wroteline;
} elsif ($_ =~ m/FIGU/) {
# FIGU,file,caption
chomp($_);
2017-12-08 05:49:00 +00:00
my @m = split ',', $_;
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";
2003-05-17 12:33:54 +00:00
$wroteline += 4;
} else {
2017-12-08 05:49:00 +00:00
print {$out} $_;
2003-05-17 12:33:54 +00:00
++$wroteline;
}
}
print "Read $readline lines, wrote $wroteline lines\n";
2017-12-08 05:49:00 +00:00
close $out;
close $in;
system('perl -pli -e "s/\s*$//" tommath.tex');