<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>booker.pl</title>
<style type="text/css">
.enscript-comment { font-style: italic; color: rgb(178,34,34); }
.enscript-function-name { font-weight: bold; color: rgb(0,0,255); }
.enscript-variable-name { font-weight: bold; color: rgb(184,134,11); }
.enscript-keyword { font-weight: bold; color: rgb(160,32,240); }
.enscript-reference { font-weight: bold; color: rgb(95,158,160); }
.enscript-string { font-weight: bold; color: rgb(188,143,143); }
.enscript-builtin { font-weight: bold; color: rgb(218,112,214); }
.enscript-type { font-weight: bold; color: rgb(34,139,34); }
.enscript-highlight { text-decoration: underline; color: 0; }
</style>
</head>
<body id="top">
<h1 style="margin:8px;" id="f1">booker.pl&nbsp;&nbsp;&nbsp;<span style="font-weight: normal; font-size: 0.5em;">[<a href="booker.pl">plain text</a>]</span></h1>
<hr/>
<div></div>
<pre>
<span class="enscript-comment">#!/bin/perl
</span><span class="enscript-comment">#
</span><span class="enscript-comment">#Used to prepare the book &quot;tommath.src&quot; for LaTeX by pre-processing it into a .tex file
</span><span class="enscript-comment">#
</span><span class="enscript-comment">#Essentially you write the &quot;tommath.src&quot; as normal LaTex except where you want code snippets you put
</span><span class="enscript-comment">#
</span><span class="enscript-comment">#EXAM,file
</span><span class="enscript-comment">#
</span><span class="enscript-comment">#This preprocessor will then open &quot;file&quot; and insert it as a verbatim copy.
</span><span class="enscript-comment">#
</span><span class="enscript-comment">#Tom St Denis
</span>
<span class="enscript-comment">#get graphics type
</span><span class="enscript-keyword">if</span> (<span class="enscript-keyword">shift</span> =~ /PDF/) {
   </span>$graph = <span class="enscript-string">&quot;&quot;</span>;
} <span class="enscript-keyword">else</span> {
   </span>$graph = <span class="enscript-string">&quot;.ps&quot;</span>;
}   

<span class="enscript-keyword">open</span>(IN,<span class="enscript-string">&quot;&lt;tommath.src&quot;</span>) <span class="enscript-keyword">or</span> <span class="enscript-keyword">die</span> <span class="enscript-string">&quot;Can't open source file&quot;</span>;
<span class="enscript-keyword">open</span>(OUT,<span class="enscript-string">&quot;&gt;tommath.tex&quot;</span>) <span class="enscript-keyword">or</span> <span class="enscript-keyword">die</span> <span class="enscript-string">&quot;Can't open destination file&quot;</span>;

<span class="enscript-keyword">print</span> <span class="enscript-string">&quot;Scanning for sections\n&quot;</span>;
</span>$chapter = </span>$section = </span>$subsection = 0;
</span>$x = 0;
<span class="enscript-keyword">while</span> (&lt;IN&gt;) {
   <span class="enscript-keyword">print</span> <span class="enscript-string">&quot;.&quot;</span>;
   <span class="enscript-keyword">if</span> (!(++</span>$x % 80)) { <span class="enscript-keyword">print</span> <span class="enscript-string">&quot;\n&quot;</span>; }
   <span class="enscript-comment">#update the headings 
</span>   <span class="enscript-keyword">if</span> (~(</span>$_ =~ /\*/)) {
      <span class="enscript-keyword">if</span> (</span>$_ =~ /\\chapter{.+}/) {
          ++</span>$chapter;
          </span>$section = </span>$subsection = 0;
      } <span class="enscript-keyword">elsif</span> (</span>$_ =~ /\\section{.+}/) {
          ++</span>$section;
          </span>$subsection = 0;
      } <span class="enscript-keyword">elsif</span> (</span>$_ =~ /\\subsection{.+}/) {
          ++</span>$subsection;
      }
   }      

   <span class="enscript-keyword">if</span> (</span>$_ =~ <span class="enscript-keyword">m</span>/MARK/) {
      </span>@m = <span class="enscript-keyword">split</span>(<span class="enscript-string">&quot;,&quot;</span>,</span>$_);
      <span class="enscript-keyword">chomp</span>(</span>@m[1]);
      </span>$index1{</span>@m[1]} = </span>$chapter;
      </span>$index2{</span>@m[1]} = </span>$section;
      </span>$index3{</span>@m[1]} = </span>$subsection;
   }
}
<span class="enscript-keyword">close</span>(IN);

<span class="enscript-keyword">open</span>(IN,<span class="enscript-string">&quot;&lt;tommath.src&quot;</span>) <span class="enscript-keyword">or</span> <span class="enscript-keyword">die</span> <span class="enscript-string">&quot;Can't open source file&quot;</span>;
</span>$readline = </span>$wroteline = 0;
</span>$srcline = 0;

<span class="enscript-keyword">while</span> (&lt;IN&gt;) {
   ++</span>$readline;
   ++</span>$srcline;
   
   <span class="enscript-keyword">if</span> (</span>$_ =~ <span class="enscript-keyword">m</span>/MARK/) {
   } <span class="enscript-keyword">elsif</span> (</span>$_ =~ <span class="enscript-keyword">m</span>/EXAM/ || </span>$_ =~ <span class="enscript-keyword">m</span>/LIST/) {
      <span class="enscript-keyword">if</span> (</span>$_ =~ <span class="enscript-keyword">m</span>/EXAM/) {
         </span>$skipheader = 1;
      } <span class="enscript-keyword">else</span> {
         </span>$skipheader = 0;
      }
      
      <span class="enscript-comment"># EXAM,file
</span>      <span class="enscript-keyword">chomp</span>(</span>$_);
      </span>@m = <span class="enscript-keyword">split</span>(<span class="enscript-string">&quot;,&quot;</span>,</span>$_);
      <span class="enscript-keyword">open</span>(SRC,<span class="enscript-string">&quot;&lt;$m[1]&quot;</span>) <span class="enscript-keyword">or</span> <span class="enscript-keyword">die</span> <span class="enscript-string">&quot;Error:$srcline:Can't open source file $m[1]&quot;</span>;
      
      <span class="enscript-keyword">print</span> <span class="enscript-string">&quot;$srcline:Inserting $m[1]:&quot;</span>;
      
      </span>$line = 0;
      </span>$tmp = </span>$m[1];
      </span>$tmp =~ <span class="enscript-keyword">s</span>/_/<span class="enscript-string">&quot;\\_&quot;</span>/<span class="enscript-keyword">ge</span>;
      <span class="enscript-keyword">print</span> OUT <span class="enscript-string">&quot;\\vspace{+3mm}\\begin{small}\n\\hspace{-5.1mm}{\\bf File}: $tmp\n\\vspace{-3mm}\n\\begin{alltt}\n&quot;</span>;
      </span>$wroteline += 5;
      
      <span class="enscript-keyword">if</span> (</span>$skipheader == 1) {
         <span class="enscript-comment"># scan till next end of comment, e.g. skip license 
</span>         <span class="enscript-keyword">while</span> (&lt;SRC&gt;) {
            </span>$text[</span>$line++] = </span>$_;
            <span class="enscript-keyword">last</span> <span class="enscript-keyword">if</span> (</span>$_ =~ /math\.libtomcrypt\.com/);
         }
         &lt;SRC&gt;;   
      }
      
      </span>$inline = 0;
      <span class="enscript-keyword">while</span> (&lt;SRC&gt;) {
      <span class="enscript-keyword">next</span> <span class="enscript-keyword">if</span> (</span>$_ =~ /\</span>$Source/);
      <span class="enscript-keyword">next</span> <span class="enscript-keyword">if</span> (</span>$_ =~ /\</span>$Revision/);
      <span class="enscript-keyword">next</span> <span class="enscript-keyword">if</span> (</span>$_ =~ /\</span>$Date/);
         </span>$text[</span>$line++] = </span>$_;
         ++</span>$inline;
         <span class="enscript-keyword">chomp</span>(</span>$_);
         </span>$_ =~ <span class="enscript-keyword">s</span>/\t/<span class="enscript-string">&quot;    &quot;</span>/<span class="enscript-keyword">ge</span>;
         </span>$_ =~ <span class="enscript-keyword">s</span>/{/<span class="enscript-string">&quot;^{&quot;</span>/<span class="enscript-keyword">ge</span>;
         </span>$_ =~ <span class="enscript-keyword">s</span>/}/<span class="enscript-string">&quot;^}&quot;</span>/<span class="enscript-keyword">ge</span>;
         </span>$_ =~ <span class="enscript-keyword">s</span>/\\/<span class="enscript-string">'\symbol{92}'</span>/<span class="enscript-keyword">ge</span>;
         </span>$_ =~ <span class="enscript-keyword">s</span>/\^/<span class="enscript-string">&quot;\\&quot;</span>/<span class="enscript-keyword">ge</span>;
           
         <span class="enscript-keyword">printf</span> OUT (<span class="enscript-string">&quot;%03d   &quot;</span>, </span>$line);
         <span class="enscript-keyword">for</span> (</span>$x = 0; </span>$x &lt; <span class="enscript-keyword">length</span>(</span>$_); </span>$x++) {
             <span class="enscript-keyword">print</span> OUT <span class="enscript-keyword">chr</span>(<span class="enscript-keyword">vec</span>(</span>$_, </span>$x, 8));
             <span class="enscript-keyword">if</span> (</span>$x == 75) { 
                 <span class="enscript-keyword">print</span> OUT <span class="enscript-string">&quot;\n      &quot;</span>;
                 ++</span>$wroteline;
             }
         }
         <span class="enscript-keyword">print</span> OUT <span class="enscript-string">&quot;\n&quot;</span>;
         ++</span>$wroteline;
      }
      </span>$totlines = </span>$line;
      <span class="enscript-keyword">print</span> OUT <span class="enscript-string">&quot;\\end{alltt}\n\\end{small}\n&quot;</span>;
      <span class="enscript-keyword">close</span>(SRC);
      <span class="enscript-keyword">print</span> <span class="enscript-string">&quot;$inline lines\n&quot;</span>;
      </span>$wroteline += 2;
   } <span class="enscript-keyword">elsif</span> (</span>$_ =~ <span class="enscript-keyword">m</span>/@\d+,.+@/) {
     <span class="enscript-comment"># line contains [number,text]
</span>     <span class="enscript-comment"># e.g. @14,for (ix = 0)@
</span>     </span>$txt = </span>$_;
     <span class="enscript-keyword">while</span> (</span>$txt =~ <span class="enscript-keyword">m</span>/@\d+,.+@/) {
        </span>@m = <span class="enscript-keyword">split</span>(<span class="enscript-string">&quot;@&quot;</span>,</span>$txt);      <span class="enscript-comment"># splits into text, one, two
</span>        </span>@parms = <span class="enscript-keyword">split</span>(<span class="enscript-string">&quot;,&quot;</span>,</span>$m[1]);  <span class="enscript-comment"># splits one,two into two elements 
</span>                
        <span class="enscript-comment"># now search from $parms[0] down for $parms[1] 
</span>        </span>$found1 = 0;
        </span>$found2 = 0;
        <span class="enscript-keyword">for</span> (</span>$i = </span>$parms[0]; </span>$i &lt; </span>$totlines &amp;&amp; </span>$found1 == 0; </span>$i++) {
           <span class="enscript-keyword">if</span> (</span>$text[</span>$i] =~ <span class="enscript-keyword">m</span>/\Q</span>$parms[1]\E/) {
              </span>$foundline1 = </span>$i + 1;
              </span>$found1 = 1;
           }
        }
        
        <span class="enscript-comment"># now search backwards
</span>        <span class="enscript-keyword">for</span> (</span>$i = </span>$parms[0] - 1; </span>$i &gt;= 0 &amp;&amp; </span>$found2 == 0; </span>$i--) {
           <span class="enscript-keyword">if</span> (</span>$text[</span>$i] =~ <span class="enscript-keyword">m</span>/\Q</span>$parms[1]\E/) {
              </span>$foundline2 = </span>$i + 1;
              </span>$found2 = 1;
           }
        }
        
        <span class="enscript-comment"># now use the closest match or the first if tied
</span>        <span class="enscript-keyword">if</span> (</span>$found1 == 1 &amp;&amp; </span>$found2 == 0) {
           </span>$found = 1;
           </span>$foundline = </span>$foundline1;
        } <span class="enscript-keyword">elsif</span> (</span>$found1 == 0 &amp;&amp; </span>$found2 == 1) {
           </span>$found = 1;
           </span>$foundline = </span>$foundline2;
        } <span class="enscript-keyword">elsif</span> (</span>$found1 == 1 &amp;&amp; </span>$found2 == 1) {
           </span>$found = 1;
           <span class="enscript-keyword">if</span> ((</span>$foundline1 - </span>$parms[0]) &lt;= (</span>$parms[0] - </span>$foundline2)) {
              </span>$foundline = </span>$foundline1;
           } <span class="enscript-keyword">else</span> {
              </span>$foundline = </span>$foundline2;
           }
        } <span class="enscript-keyword">else</span> {
           </span>$found = 0;
        }
                      
        <span class="enscript-comment"># if found replace 
</span>        <span class="enscript-keyword">if</span> (</span>$found == 1) {
           </span>$delta = </span>$parms[0] - </span>$foundline;
           <span class="enscript-keyword">print</span> <span class="enscript-string">&quot;Found replacement tag for \&quot;$parms[1]\&quot; on line $srcline which refers to line $foundline (delta $delta)\n&quot;</span>;
           </span>$_ =~ <span class="enscript-keyword">s</span>/@\Q</span>$m[1]\E@/</span>$foundline/;
        } <span class="enscript-keyword">else</span> {
           <span class="enscript-keyword">print</span> <span class="enscript-string">&quot;ERROR:  The tag \&quot;$parms[1]\&quot; on line $srcline was not found in the most recently parsed source!\n&quot;</span>;
        }
        
        <span class="enscript-comment"># remake the rest of the line 
</span>        </span>$cnt = </span>@m;
        </span>$txt = <span class="enscript-string">&quot;&quot;</span>;
        <span class="enscript-keyword">for</span> (</span>$i = 2; </span>$i &lt; </span>$cnt; </span>$i++) {
            </span>$txt = </span>$txt . </span>$m[</span>$i] . <span class="enscript-string">&quot;@&quot;</span>;
        }
     }
     <span class="enscript-keyword">print</span> OUT </span>$_;
     ++</span>$wroteline;
   } <span class="enscript-keyword">elsif</span> (</span>$_ =~ /~.+~/) {
      <span class="enscript-comment"># line contains a ~text~ pair used to refer to indexing :-)
</span>      </span>$txt = </span>$_;
      <span class="enscript-keyword">while</span> (</span>$txt =~ /~.+~/) {
         </span>@m = <span class="enscript-keyword">split</span>(<span class="enscript-string">&quot;~&quot;</span>, </span>$txt);
         
         <span class="enscript-comment"># word is the second position
</span>         </span>$word = </span>@m[1];
         </span>$a = </span>$index1{</span>$word};
         </span>$b = </span>$index2{</span>$word};
         </span>$c = </span>$index3{</span>$word};
         
         <span class="enscript-comment"># if chapter (a) is zero it wasn't found
</span>         <span class="enscript-keyword">if</span> (</span>$a == 0) {
            <span class="enscript-keyword">print</span> <span class="enscript-string">&quot;ERROR: the tag \&quot;$word\&quot; on line $srcline was not found previously marked.\n&quot;</span>;
         } <span class="enscript-keyword">else</span> {
            <span class="enscript-comment"># format the tag as x, x.y or x.y.z depending on the values
</span>            </span>$str = </span>$a;
            </span>$str = </span>$str . <span class="enscript-string">&quot;.$b&quot;</span> <span class="enscript-keyword">if</span> (</span>$b != 0);
            </span>$str = </span>$str . <span class="enscript-string">&quot;.$c&quot;</span> <span class="enscript-keyword">if</span> (</span>$c != 0);
            
            <span class="enscript-keyword">if</span> (</span>$b == 0 &amp;&amp; </span>$c == 0) {
               <span class="enscript-comment"># its a chapter
</span>               <span class="enscript-keyword">if</span> (</span>$a &lt;= 10) {
                  <span class="enscript-keyword">if</span> (</span>$a == 1) {
                     </span>$str = <span class="enscript-string">&quot;chapter one&quot;</span>;
                  } <span class="enscript-keyword">elsif</span> (</span>$a == 2) {
                     </span>$str = <span class="enscript-string">&quot;chapter two&quot;</span>;
                  } <span class="enscript-keyword">elsif</span> (</span>$a == 3) {
                     </span>$str = <span class="enscript-string">&quot;chapter three&quot;</span>;
                  } <span class="enscript-keyword">elsif</span> (</span>$a == 4) {
                     </span>$str = <span class="enscript-string">&quot;chapter four&quot;</span>;
                  } <span class="enscript-keyword">elsif</span> (</span>$a == 5) {
                     </span>$str = <span class="enscript-string">&quot;chapter five&quot;</span>;
                  } <span class="enscript-keyword">elsif</span> (</span>$a == 6) {
                     </span>$str = <span class="enscript-string">&quot;chapter six&quot;</span>;
                  } <span class="enscript-keyword">elsif</span> (</span>$a == 7) {
                     </span>$str = <span class="enscript-string">&quot;chapter seven&quot;</span>;
                  } <span class="enscript-keyword">elsif</span> (</span>$a == 8) {
                     </span>$str = <span class="enscript-string">&quot;chapter eight&quot;</span>;
                  } <span class="enscript-keyword">elsif</span> (</span>$a == 9) {
                     </span>$str = <span class="enscript-string">&quot;chapter nine&quot;</span>;
                  } <span class="enscript-keyword">elsif</span> (</span>$a == 10) {
                     </span>$str = <span class="enscript-string">&quot;chapter ten&quot;</span>;
                  }
               } <span class="enscript-keyword">else</span> {
                  </span>$str = <span class="enscript-string">&quot;chapter &quot;</span> . </span>$str;
               }
            } <span class="enscript-keyword">else</span> {
               </span>$str = <span class="enscript-string">&quot;section &quot;</span> . </span>$str     <span class="enscript-keyword">if</span> (</span>$b != 0 &amp;&amp; </span>$c == 0);            
               </span>$str = <span class="enscript-string">&quot;sub-section &quot;</span> . </span>$str <span class="enscript-keyword">if</span> (</span>$b != 0 &amp;&amp; </span>$c != 0);
            }
            
            <span class="enscript-comment">#substitute
</span>            </span>$_ =~ <span class="enscript-keyword">s</span>/~\Q</span>$word\E~/</span>$str/;
            
            <span class="enscript-keyword">print</span> <span class="enscript-string">&quot;Found replacement tag for marker \&quot;$word\&quot; on line $srcline which refers to $str\n&quot;</span>;
         }
         
         <span class="enscript-comment"># remake rest of the line
</span>         </span>$cnt = </span>@m;
         </span>$txt = <span class="enscript-string">&quot;&quot;</span>;
         <span class="enscript-keyword">for</span> (</span>$i = 2; </span>$i &lt; </span>$cnt; </span>$i++) {
             </span>$txt = </span>$txt . </span>$m[</span>$i] . <span class="enscript-string">&quot;~&quot;</span>;
         }
      }
      <span class="enscript-keyword">print</span> OUT </span>$_;
      ++</span>$wroteline;
   } <span class="enscript-keyword">elsif</span> (</span>$_ =~ <span class="enscript-keyword">m</span>/FIGU/) {
      <span class="enscript-comment"># FIGU,file,caption
</span>      <span class="enscript-keyword">chomp</span>(</span>$_);
      </span>@m = <span class="enscript-keyword">split</span>(<span class="enscript-string">&quot;,&quot;</span>, </span>$_);
      <span class="enscript-keyword">print</span> OUT <span class="enscript-string">&quot;\\begin{center}\n\\begin{figure}[here]\n\\includegraphics{pics/$m[1]$graph}\n&quot;</span>;
      <span class="enscript-keyword">print</span> OUT <span class="enscript-string">&quot;\\caption{$m[2]}\n\\label{pic:$m[1]}\n\\end{figure}\n\\end{center}\n&quot;</span>;
      </span>$wroteline += 4;
   } <span class="enscript-keyword">else</span> {
      <span class="enscript-keyword">print</span> OUT </span>$_;
      ++</span>$wroteline;
   }
}
<span class="enscript-keyword">print</span> <span class="enscript-string">&quot;Read $readline lines, wrote $wroteline lines\n&quot;</span>;

<span class="enscript-keyword">close</span> (OUT);
<span class="enscript-keyword">close</span> (IN);
</pre>
<hr />
</body></html>