3 #Used to prepare the book "tommath.src" for LaTeX by pre-processing it into a .tex file
5 #Essentially you write the "tommath.src" as normal LaTex except where you want code snippets you put
9 #This preprocessor will then open "file" and insert it as a verbatim copy.
20 open(IN
,"<tommath.src") or die "Can't open source file";
21 open(OUT
,">tommath.tex") or die "Can't open destination file";
23 print "Scanning for sections\n";
24 $chapter = $section = $subsection = 0;
28 if (!(++$x % 80)) { print "\n"; }
31 if ($_ =~ /\\chapter{.+}/) {
33 $section = $subsection = 0;
34 } elsif ($_ =~ /\\section{.+}/) {
37 } elsif ($_ =~ /\\subsection{.+}/) {
45 $index1{@m[1]} = $chapter;
46 $index2{@m[1]} = $section;
47 $index3{@m[1]} = $subsection;
52 open(IN
,"<tommath.src") or die "Can't open source file";
53 $readline = $wroteline = 0;
61 } elsif ($_ =~ m/EXAM/ || $_ =~ m/LIST/) {
71 open(SRC
,"<$m[1]") or die "Error:$srcline:Can't open source file $m[1]";
73 print "$srcline:Inserting $m[1]:";
78 print OUT
"\\vspace{+3mm}\\begin{small}\n\\hspace{-5.1mm}{\\bf File}: $tmp\n\\vspace{-3mm}\n\\begin{alltt}\n";
81 if ($skipheader == 1) {
82 # scan till next end of comment, e.g. skip license
85 last if ($_ =~ /math\.libtomcrypt\.com/);
92 next if ($_ =~ /\$Source/);
93 next if ($_ =~ /\$Revision/);
94 next if ($_ =~ /\$Date/);
101 $_ =~ s/\\/'\symbol{92}'/ge;
104 printf OUT
("%03d ", $line);
105 for ($x = 0; $x < length($_); $x++) {
106 print OUT
chr(vec($_, $x, 8));
116 print OUT
"\\end{alltt}\n\\end{small}\n";
118 print "$inline lines\n";
120 } elsif ($_ =~ m/@\d+,.+@/) {
121 # line contains [number,text]
122 # e.g. @14,for (ix = 0)@
124 while ($txt =~ m/@\d+,.+@/) {
125 @m = split("@",$txt); # splits into text, one, two
126 @parms = split(",",$m[1]); # splits one,two into two elements
128 # now search from $parms[0] down for $parms[1]
131 for ($i = $parms[0]; $i < $totlines && $found1 == 0; $i++) {
132 if ($text[$i] =~ m/\Q$parms[1]\E/) {
133 $foundline1 = $i + 1;
138 # now search backwards
139 for ($i = $parms[0] - 1; $i >= 0 && $found2 == 0; $i--) {
140 if ($text[$i] =~ m/\Q$parms[1]\E/) {
141 $foundline2 = $i + 1;
146 # now use the closest match or the first if tied
147 if ($found1 == 1 && $found2 == 0) {
149 $foundline = $foundline1;
150 } elsif ($found1 == 0 && $found2 == 1) {
152 $foundline = $foundline2;
153 } elsif ($found1 == 1 && $found2 == 1) {
155 if (($foundline1 - $parms[0]) <= ($parms[0] - $foundline2)) {
156 $foundline = $foundline1;
158 $foundline = $foundline2;
166 $delta = $parms[0] - $foundline;
167 print "Found replacement tag for \"$parms[1]\" on line $srcline which refers to line $foundline (delta $delta)\n";
168 $_ =~ s/@\Q$m[1]\E@/$foundline/;
170 print "ERROR: The tag \"$parms[1]\" on line $srcline was not found in the most recently parsed source!\n";
173 # remake the rest of the line
176 for ($i = 2; $i < $cnt; $i++) {
177 $txt = $txt . $m[$i] . "@";
182 } elsif ($_ =~ /~.+~/) {
183 # line contains a ~text~ pair used to refer to indexing :-)
185 while ($txt =~ /~.+~/) {
186 @m = split("~", $txt);
188 # word is the second position
194 # if chapter (a) is zero it wasn't found
196 print "ERROR: the tag \"$word\" on line $srcline was not found previously marked.\n";
198 # format the tag as x, x.y or x.y.z depending on the values
200 $str = $str . ".$b" if ($b != 0);
201 $str = $str . ".$c" if ($c != 0);
203 if ($b == 0 && $c == 0) {
207 $str = "chapter one";
209 $str = "chapter two";
211 $str = "chapter three";
213 $str = "chapter four";
215 $str = "chapter five";
217 $str = "chapter six";
219 $str = "chapter seven";
221 $str = "chapter eight";
223 $str = "chapter nine";
225 $str = "chapter ten";
228 $str = "chapter " . $str;
231 $str = "section " . $str if ($b != 0 && $c == 0);
232 $str = "sub-section " . $str if ($b != 0 && $c != 0);
236 $_ =~ s/~\Q$word\E~/$str/;
238 print "Found replacement tag for marker \"$word\" on line $srcline which refers to $str\n";
241 # remake rest of the line
244 for ($i = 2; $i < $cnt; $i++) {
245 $txt = $txt . $m[$i] . "~";
250 } elsif ($_ =~ m/FIGU/) {
254 print OUT
"\\begin{center}\n\\begin{figure}[here]\n\\includegraphics{pics/$m[1]$graph}\n";
255 print OUT
"\\caption{$m[2]}\n\\label{pic:$m[1]}\n\\end{figure}\n\\end{center}\n";
262 print "Read $readline lines, wrote $wroteline lines\n";