comparison libtommath/booker.pl @ 1436:60fc6476e044

Update to libtommath v1.0
author Matt Johnston <matt@ucc.asn.au>
date Sat, 24 Jun 2017 22:37:14 +0800
parents 5ff8218bcee9
children
comparison
equal deleted inserted replaced
1435:f849a5ca2efc 1436:60fc6476e044
13 #get graphics type 13 #get graphics type
14 if (shift =~ /PDF/) { 14 if (shift =~ /PDF/) {
15 $graph = ""; 15 $graph = "";
16 } else { 16 } else {
17 $graph = ".ps"; 17 $graph = ".ps";
18 } 18 }
19 19
20 open(IN,"<tommath.src") or die "Can't open source file"; 20 open(IN,"<tommath.src") or die "Can't open source file";
21 open(OUT,">tommath.tex") or die "Can't open destination file"; 21 open(OUT,">tommath.tex") or die "Can't open destination file";
22 22
23 print "Scanning for sections\n"; 23 print "Scanning for sections\n";
24 $chapter = $section = $subsection = 0; 24 $chapter = $section = $subsection = 0;
25 $x = 0; 25 $x = 0;
26 while (<IN>) { 26 while (<IN>) {
27 print "."; 27 print ".";
28 if (!(++$x % 80)) { print "\n"; } 28 if (!(++$x % 80)) { print "\n"; }
29 #update the headings 29 #update the headings
30 if (~($_ =~ /\*/)) { 30 if (~($_ =~ /\*/)) {
31 if ($_ =~ /\\chapter{.+}/) { 31 if ($_ =~ /\\chapter\{.+}/) {
32 ++$chapter; 32 ++$chapter;
33 $section = $subsection = 0; 33 $section = $subsection = 0;
34 } elsif ($_ =~ /\\section{.+}/) { 34 } elsif ($_ =~ /\\section\{.+}/) {
35 ++$section; 35 ++$section;
36 $subsection = 0; 36 $subsection = 0;
37 } elsif ($_ =~ /\\subsection{.+}/) { 37 } elsif ($_ =~ /\\subsection\{.+}/) {
38 ++$subsection; 38 ++$subsection;
39 } 39 }
40 } 40 }
41 41
42 if ($_ =~ m/MARK/) { 42 if ($_ =~ m/MARK/) {
43 @m = split(",",$_); 43 @m = split(",",$_);
44 chomp(@m[1]); 44 chomp(@m[1]);
45 $index1{@m[1]} = $chapter; 45 $index1{@m[1]} = $chapter;
54 $srcline = 0; 54 $srcline = 0;
55 55
56 while (<IN>) { 56 while (<IN>) {
57 ++$readline; 57 ++$readline;
58 ++$srcline; 58 ++$srcline;
59 59
60 if ($_ =~ m/MARK/) { 60 if ($_ =~ m/MARK/) {
61 } elsif ($_ =~ m/EXAM/ || $_ =~ m/LIST/) { 61 } elsif ($_ =~ m/EXAM/ || $_ =~ m/LIST/) {
62 if ($_ =~ m/EXAM/) { 62 if ($_ =~ m/EXAM/) {
63 $skipheader = 1; 63 $skipheader = 1;
64 } else { 64 } else {
65 $skipheader = 0; 65 $skipheader = 0;
66 } 66 }
67 67
68 # EXAM,file 68 # EXAM,file
69 chomp($_); 69 chomp($_);
70 @m = split(",",$_); 70 @m = split(",",$_);
71 open(SRC,"<$m[1]") or die "Error:$srcline:Can't open source file $m[1]"; 71 open(SRC,"<$m[1]") or die "Error:$srcline:Can't open source file $m[1]";
72 72
73 print "$srcline:Inserting $m[1]:"; 73 print "$srcline:Inserting $m[1]:";
74 74
75 $line = 0; 75 $line = 0;
76 $tmp = $m[1]; 76 $tmp = $m[1];
77 $tmp =~ s/_/"\\_"/ge; 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"; 78 print OUT "\\vspace{+3mm}\\begin{small}\n\\hspace{-5.1mm}{\\bf File}: $tmp\n\\vspace{-3mm}\n\\begin{alltt}\n";
79 $wroteline += 5; 79 $wroteline += 5;
80 80
81 if ($skipheader == 1) { 81 if ($skipheader == 1) {
82 # scan till next end of comment, e.g. skip license 82 # scan till next end of comment, e.g. skip license
83 while (<SRC>) { 83 while (<SRC>) {
84 $text[$line++] = $_; 84 $text[$line++] = $_;
85 last if ($_ =~ /math\.libtomcrypt\.com/); 85 last if ($_ =~ /libtom\.org/);
86 } 86 }
87 <SRC>; 87 <SRC>;
88 } 88 }
89 89
90 $inline = 0; 90 $inline = 0;
91 while (<SRC>) { 91 while (<SRC>) {
92 next if ($_ =~ /\$Source/); 92 next if ($_ =~ /\$Source/);
93 next if ($_ =~ /\$Revision/); 93 next if ($_ =~ /\$Revision/);
94 next if ($_ =~ /\$Date/); 94 next if ($_ =~ /\$Date/);
98 $_ =~ s/\t/" "/ge; 98 $_ =~ s/\t/" "/ge;
99 $_ =~ s/{/"^{"/ge; 99 $_ =~ s/{/"^{"/ge;
100 $_ =~ s/}/"^}"/ge; 100 $_ =~ s/}/"^}"/ge;
101 $_ =~ s/\\/'\symbol{92}'/ge; 101 $_ =~ s/\\/'\symbol{92}'/ge;
102 $_ =~ s/\^/"\\"/ge; 102 $_ =~ s/\^/"\\"/ge;
103 103
104 printf OUT ("%03d ", $line); 104 printf OUT ("%03d ", $line);
105 for ($x = 0; $x < length($_); $x++) { 105 for ($x = 0; $x < length($_); $x++) {
106 print OUT chr(vec($_, $x, 8)); 106 print OUT chr(vec($_, $x, 8));
107 if ($x == 75) { 107 if ($x == 75) {
108 print OUT "\n "; 108 print OUT "\n ";
109 ++$wroteline; 109 ++$wroteline;
110 } 110 }
111 } 111 }
112 print OUT "\n"; 112 print OUT "\n";
121 # line contains [number,text] 121 # line contains [number,text]
122 # e.g. @14,for (ix = 0)@ 122 # e.g. @14,for (ix = 0)@
123 $txt = $_; 123 $txt = $_;
124 while ($txt =~ m/@\d+,.+@/) { 124 while ($txt =~ m/@\d+,.+@/) {
125 @m = split("@",$txt); # splits into text, one, two 125 @m = split("@",$txt); # splits into text, one, two
126 @parms = split(",",$m[1]); # splits one,two into two elements 126 @parms = split(",",$m[1]); # splits one,two into two elements
127 127
128 # now search from $parms[0] down for $parms[1] 128 # now search from $parms[0] down for $parms[1]
129 $found1 = 0; 129 $found1 = 0;
130 $found2 = 0; 130 $found2 = 0;
131 for ($i = $parms[0]; $i < $totlines && $found1 == 0; $i++) { 131 for ($i = $parms[0]; $i < $totlines && $found1 == 0; $i++) {
132 if ($text[$i] =~ m/\Q$parms[1]\E/) { 132 if ($text[$i] =~ m/\Q$parms[1]\E/) {
133 $foundline1 = $i + 1; 133 $foundline1 = $i + 1;
134 $found1 = 1; 134 $found1 = 1;
135 } 135 }
136 } 136 }
137 137
138 # now search backwards 138 # now search backwards
139 for ($i = $parms[0] - 1; $i >= 0 && $found2 == 0; $i--) { 139 for ($i = $parms[0] - 1; $i >= 0 && $found2 == 0; $i--) {
140 if ($text[$i] =~ m/\Q$parms[1]\E/) { 140 if ($text[$i] =~ m/\Q$parms[1]\E/) {
141 $foundline2 = $i + 1; 141 $foundline2 = $i + 1;
142 $found2 = 1; 142 $found2 = 1;
143 } 143 }
144 } 144 }
145 145
146 # now use the closest match or the first if tied 146 # now use the closest match or the first if tied
147 if ($found1 == 1 && $found2 == 0) { 147 if ($found1 == 1 && $found2 == 0) {
148 $found = 1; 148 $found = 1;
149 $foundline = $foundline1; 149 $foundline = $foundline1;
150 } elsif ($found1 == 0 && $found2 == 1) { 150 } elsif ($found1 == 0 && $found2 == 1) {
158 $foundline = $foundline2; 158 $foundline = $foundline2;
159 } 159 }
160 } else { 160 } else {
161 $found = 0; 161 $found = 0;
162 } 162 }
163 163
164 # if found replace 164 # if found replace
165 if ($found == 1) { 165 if ($found == 1) {
166 $delta = $parms[0] - $foundline; 166 $delta = $parms[0] - $foundline;
167 print "Found replacement tag for \"$parms[1]\" on line $srcline which refers to line $foundline (delta $delta)\n"; 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/; 168 $_ =~ s/@\Q$m[1]\E@/$foundline/;
169 } else { 169 } else {
170 print "ERROR: The tag \"$parms[1]\" on line $srcline was not found in the most recently parsed source!\n"; 170 print "ERROR: The tag \"$parms[1]\" on line $srcline was not found in the most recently parsed source!\n";
171 } 171 }
172 172
173 # remake the rest of the line 173 # remake the rest of the line
174 $cnt = @m; 174 $cnt = @m;
175 $txt = ""; 175 $txt = "";
176 for ($i = 2; $i < $cnt; $i++) { 176 for ($i = 2; $i < $cnt; $i++) {
177 $txt = $txt . $m[$i] . "@"; 177 $txt = $txt . $m[$i] . "@";
178 } 178 }
182 } elsif ($_ =~ /~.+~/) { 182 } elsif ($_ =~ /~.+~/) {
183 # line contains a ~text~ pair used to refer to indexing :-) 183 # line contains a ~text~ pair used to refer to indexing :-)
184 $txt = $_; 184 $txt = $_;
185 while ($txt =~ /~.+~/) { 185 while ($txt =~ /~.+~/) {
186 @m = split("~", $txt); 186 @m = split("~", $txt);
187 187
188 # word is the second position 188 # word is the second position
189 $word = @m[1]; 189 $word = @m[1];
190 $a = $index1{$word}; 190 $a = $index1{$word};
191 $b = $index2{$word}; 191 $b = $index2{$word};
192 $c = $index3{$word}; 192 $c = $index3{$word};
193 193
194 # if chapter (a) is zero it wasn't found 194 # if chapter (a) is zero it wasn't found
195 if ($a == 0) { 195 if ($a == 0) {
196 print "ERROR: the tag \"$word\" on line $srcline was not found previously marked.\n"; 196 print "ERROR: the tag \"$word\" on line $srcline was not found previously marked.\n";
197 } else { 197 } else {
198 # format the tag as x, x.y or x.y.z depending on the values 198 # format the tag as x, x.y or x.y.z depending on the values
199 $str = $a; 199 $str = $a;
200 $str = $str . ".$b" if ($b != 0); 200 $str = $str . ".$b" if ($b != 0);
201 $str = $str . ".$c" if ($c != 0); 201 $str = $str . ".$c" if ($c != 0);
202 202
203 if ($b == 0 && $c == 0) { 203 if ($b == 0 && $c == 0) {
204 # its a chapter 204 # its a chapter
205 if ($a <= 10) { 205 if ($a <= 10) {
206 if ($a == 1) { 206 if ($a == 1) {
207 $str = "chapter one"; 207 $str = "chapter one";
226 } 226 }
227 } else { 227 } else {
228 $str = "chapter " . $str; 228 $str = "chapter " . $str;
229 } 229 }
230 } else { 230 } else {
231 $str = "section " . $str if ($b != 0 && $c == 0); 231 $str = "section " . $str if ($b != 0 && $c == 0);
232 $str = "sub-section " . $str if ($b != 0 && $c != 0); 232 $str = "sub-section " . $str if ($b != 0 && $c != 0);
233 } 233 }
234 234
235 #substitute 235 #substitute
236 $_ =~ s/~\Q$word\E~/$str/; 236 $_ =~ s/~\Q$word\E~/$str/;
237 237
238 print "Found replacement tag for marker \"$word\" on line $srcline which refers to $str\n"; 238 print "Found replacement tag for marker \"$word\" on line $srcline which refers to $str\n";
239 } 239 }
240 240
241 # remake rest of the line 241 # remake rest of the line
242 $cnt = @m; 242 $cnt = @m;
243 $txt = ""; 243 $txt = "";
244 for ($i = 2; $i < $cnt; $i++) { 244 for ($i = 2; $i < $cnt; $i++) {
245 $txt = $txt . $m[$i] . "~"; 245 $txt = $txt . $m[$i] . "~";
261 } 261 }
262 print "Read $readline lines, wrote $wroteline lines\n"; 262 print "Read $readline lines, wrote $wroteline lines\n";
263 263
264 close (OUT); 264 close (OUT);
265 close (IN); 265 close (IN);
266
267 system('perl -pli -e "s/\s*$//" tommath.tex');