Mercurial > dropbear
comparison libtommath/booker.pl @ 284:eed26cff980b
propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
to branch 'au.asn.ucc.matt.dropbear' (head fff0894a0399405a9410ea1c6d118f342cf2aa64)
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Wed, 08 Mar 2006 13:23:49 +0000 |
parents | |
children | 5ff8218bcee9 |
comparison
equal
deleted
inserted
replaced
283:bd240aa12ba7 | 284:eed26cff980b |
---|---|
1 #!/bin/perl | |
2 # | |
3 #Used to prepare the book "tommath.src" for LaTeX by pre-processing it into a .tex file | |
4 # | |
5 #Essentially you write the "tommath.src" as normal LaTex except where you want code snippets you put | |
6 # | |
7 #EXAM,file | |
8 # | |
9 #This preprocessor will then open "file" and insert it as a verbatim copy. | |
10 # | |
11 #Tom St Denis | |
12 | |
13 #get graphics type | |
14 if (shift =~ /PDF/) { | |
15 $graph = ""; | |
16 } else { | |
17 $graph = ".ps"; | |
18 } | |
19 | |
20 open(IN,"<tommath.src") or die "Can't open source file"; | |
21 open(OUT,">tommath.tex") or die "Can't open destination file"; | |
22 | |
23 print "Scanning for sections\n"; | |
24 $chapter = $section = $subsection = 0; | |
25 $x = 0; | |
26 while (<IN>) { | |
27 print "."; | |
28 if (!(++$x % 80)) { print "\n"; } | |
29 #update the headings | |
30 if (~($_ =~ /\*/)) { | |
31 if ($_ =~ /\\chapter{.+}/) { | |
32 ++$chapter; | |
33 $section = $subsection = 0; | |
34 } elsif ($_ =~ /\\section{.+}/) { | |
35 ++$section; | |
36 $subsection = 0; | |
37 } elsif ($_ =~ /\\subsection{.+}/) { | |
38 ++$subsection; | |
39 } | |
40 } | |
41 | |
42 if ($_ =~ m/MARK/) { | |
43 @m = split(",",$_); | |
44 chomp(@m[1]); | |
45 $index1{@m[1]} = $chapter; | |
46 $index2{@m[1]} = $section; | |
47 $index3{@m[1]} = $subsection; | |
48 } | |
49 } | |
50 close(IN); | |
51 | |
52 open(IN,"<tommath.src") or die "Can't open source file"; | |
53 $readline = $wroteline = 0; | |
54 $srcline = 0; | |
55 | |
56 while (<IN>) { | |
57 ++$readline; | |
58 ++$srcline; | |
59 | |
60 if ($_ =~ m/MARK/) { | |
61 } elsif ($_ =~ m/EXAM/ || $_ =~ m/LIST/) { | |
62 if ($_ =~ m/EXAM/) { | |
63 $skipheader = 1; | |
64 } else { | |
65 $skipheader = 0; | |
66 } | |
67 | |
68 # EXAM,file | |
69 chomp($_); | |
70 @m = split(",",$_); | |
71 open(SRC,"<$m[1]") or die "Error:$srcline:Can't open source file $m[1]"; | |
72 | |
73 print "$srcline:Inserting $m[1]:"; | |
74 | |
75 $line = 0; | |
76 $tmp = $m[1]; | |
77 $tmp =~ s/_/"\\_"/ge; | |
78 print OUT "\\vspace{+3mm}\\begin{small}\n\\hspace{-5.1mm}{\\bf File}: $tmp\n\\vspace{-3mm}\n\\begin{alltt}\n"; | |
79 $wroteline += 5; | |
80 | |
81 if ($skipheader == 1) { | |
82 # scan till next end of comment, e.g. skip license | |
83 while (<SRC>) { | |
84 $text[$line++] = $_; | |
85 last if ($_ =~ /math\.libtomcrypt\.org/); | |
86 } | |
87 <SRC>; | |
88 } | |
89 | |
90 $inline = 0; | |
91 while (<SRC>) { | |
92 $text[$line++] = $_; | |
93 ++$inline; | |
94 chomp($_); | |
95 $_ =~ s/\t/" "/ge; | |
96 $_ =~ s/{/"^{"/ge; | |
97 $_ =~ s/}/"^}"/ge; | |
98 $_ =~ s/\\/'\symbol{92}'/ge; | |
99 $_ =~ s/\^/"\\"/ge; | |
100 | |
101 printf OUT ("%03d ", $line); | |
102 for ($x = 0; $x < length($_); $x++) { | |
103 print OUT chr(vec($_, $x, 8)); | |
104 if ($x == 75) { | |
105 print OUT "\n "; | |
106 ++$wroteline; | |
107 } | |
108 } | |
109 print OUT "\n"; | |
110 ++$wroteline; | |
111 } | |
112 $totlines = $line; | |
113 print OUT "\\end{alltt}\n\\end{small}\n"; | |
114 close(SRC); | |
115 print "$inline lines\n"; | |
116 $wroteline += 2; | |
117 } elsif ($_ =~ m/@\d+,.+@/) { | |
118 # line contains [number,text] | |
119 # e.g. @14,for (ix = 0)@ | |
120 $txt = $_; | |
121 while ($txt =~ m/@\d+,.+@/) { | |
122 @m = split("@",$txt); # splits into text, one, two | |
123 @parms = split(",",$m[1]); # splits one,two into two elements | |
124 | |
125 # now search from $parms[0] down for $parms[1] | |
126 $found1 = 0; | |
127 $found2 = 0; | |
128 for ($i = $parms[0]; $i < $totlines && $found1 == 0; $i++) { | |
129 if ($text[$i] =~ m/\Q$parms[1]\E/) { | |
130 $foundline1 = $i + 1; | |
131 $found1 = 1; | |
132 } | |
133 } | |
134 | |
135 # now search backwards | |
136 for ($i = $parms[0] - 1; $i >= 0 && $found2 == 0; $i--) { | |
137 if ($text[$i] =~ m/\Q$parms[1]\E/) { | |
138 $foundline2 = $i + 1; | |
139 $found2 = 1; | |
140 } | |
141 } | |
142 | |
143 # now use the closest match or the first if tied | |
144 if ($found1 == 1 && $found2 == 0) { | |
145 $found = 1; | |
146 $foundline = $foundline1; | |
147 } elsif ($found1 == 0 && $found2 == 1) { | |
148 $found = 1; | |
149 $foundline = $foundline2; | |
150 } elsif ($found1 == 1 && $found2 == 1) { | |
151 $found = 1; | |
152 if (($foundline1 - $parms[0]) <= ($parms[0] - $foundline2)) { | |
153 $foundline = $foundline1; | |
154 } else { | |
155 $foundline = $foundline2; | |
156 } | |
157 } else { | |
158 $found = 0; | |
159 } | |
160 | |
161 # if found replace | |
162 if ($found == 1) { | |
163 $delta = $parms[0] - $foundline; | |
164 print "Found replacement tag for \"$parms[1]\" on line $srcline which refers to line $foundline (delta $delta)\n"; | |
165 $_ =~ s/@\Q$m[1]\E@/$foundline/; | |
166 } else { | |
167 print "ERROR: The tag \"$parms[1]\" on line $srcline was not found in the most recently parsed source!\n"; | |
168 } | |
169 | |
170 # remake the rest of the line | |
171 $cnt = @m; | |
172 $txt = ""; | |
173 for ($i = 2; $i < $cnt; $i++) { | |
174 $txt = $txt . $m[$i] . "@"; | |
175 } | |
176 } | |
177 print OUT $_; | |
178 ++$wroteline; | |
179 } elsif ($_ =~ /~.+~/) { | |
180 # line contains a ~text~ pair used to refer to indexing :-) | |
181 $txt = $_; | |
182 while ($txt =~ /~.+~/) { | |
183 @m = split("~", $txt); | |
184 | |
185 # word is the second position | |
186 $word = @m[1]; | |
187 $a = $index1{$word}; | |
188 $b = $index2{$word}; | |
189 $c = $index3{$word}; | |
190 | |
191 # if chapter (a) is zero it wasn't found | |
192 if ($a == 0) { | |
193 print "ERROR: the tag \"$word\" on line $srcline was not found previously marked.\n"; | |
194 } else { | |
195 # format the tag as x, x.y or x.y.z depending on the values | |
196 $str = $a; | |
197 $str = $str . ".$b" if ($b != 0); | |
198 $str = $str . ".$c" if ($c != 0); | |
199 | |
200 if ($b == 0 && $c == 0) { | |
201 # its a chapter | |
202 if ($a <= 10) { | |
203 if ($a == 1) { | |
204 $str = "chapter one"; | |
205 } elsif ($a == 2) { | |
206 $str = "chapter two"; | |
207 } elsif ($a == 3) { | |
208 $str = "chapter three"; | |
209 } elsif ($a == 4) { | |
210 $str = "chapter four"; | |
211 } elsif ($a == 5) { | |
212 $str = "chapter five"; | |
213 } elsif ($a == 6) { | |
214 $str = "chapter six"; | |
215 } elsif ($a == 7) { | |
216 $str = "chapter seven"; | |
217 } elsif ($a == 8) { | |
218 $str = "chapter eight"; | |
219 } elsif ($a == 9) { | |
220 $str = "chapter nine"; | |
221 } elsif ($a == 2) { | |
222 $str = "chapter ten"; | |
223 } | |
224 } else { | |
225 $str = "chapter " . $str; | |
226 } | |
227 } else { | |
228 $str = "section " . $str if ($b != 0 && $c == 0); | |
229 $str = "sub-section " . $str if ($b != 0 && $c != 0); | |
230 } | |
231 | |
232 #substitute | |
233 $_ =~ s/~\Q$word\E~/$str/; | |
234 | |
235 print "Found replacement tag for marker \"$word\" on line $srcline which refers to $str\n"; | |
236 } | |
237 | |
238 # remake rest of the line | |
239 $cnt = @m; | |
240 $txt = ""; | |
241 for ($i = 2; $i < $cnt; $i++) { | |
242 $txt = $txt . $m[$i] . "~"; | |
243 } | |
244 } | |
245 print OUT $_; | |
246 ++$wroteline; | |
247 } elsif ($_ =~ m/FIGU/) { | |
248 # FIGU,file,caption | |
249 chomp($_); | |
250 @m = split(",", $_); | |
251 print OUT "\\begin{center}\n\\begin{figure}[here]\n\\includegraphics{pics/$m[1]$graph}\n"; | |
252 print OUT "\\caption{$m[2]}\n\\label{pic:$m[1]}\n\\end{figure}\n\\end{center}\n"; | |
253 $wroteline += 4; | |
254 } else { | |
255 print OUT $_; | |
256 ++$wroteline; | |
257 } | |
258 } | |
259 print "Read $readline lines, wrote $wroteline lines\n"; | |
260 | |
261 close (OUT); | |
262 close (IN); |