home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / tex / lametex_.z / lametex_ / lametex / src / plaintext.pl < prev    next >
Encoding:
Perl Script  |  1992-09-19  |  4.5 KB  |  198 lines

  1. #!/usr/local/bin/perl -P
  2. # This program converts a PostScript file written by lametex -t and converts it
  3. # into a plain text file.
  4. #
  5. # Copyright 1992 Jonathan Monsarrat. Permission given to freely distribute,
  6. # edit and use as long as this copyright statement remains intact.
  7.  
  8. # Cycle through all the input, breaking everything apart into word. Interpret
  9. # each word, building together a line of word and accumulating information
  10. # about the margins, line spacing, indentation, and justification.
  11. $justify = 102;
  12. $firstpage=1;
  13. while(<>)
  14. {
  15.   split;    # Breaks this line up into tokens by whitespace.
  16.   foreach (@_) {
  17.      if(/^def$/) { &FLUSHLINE; &DEFINE; }
  18.      elsif(/^NW$/) { &NEXTWORD; }
  19.      elsif(/^NEWPARA$/) { &NEWPARA; }
  20.      elsif(/^BASELINESKIP$/) { &FLUSHLINE; $baselineskip = $last; }
  21.      elsif(/^NewFont$/) { if ($lastlast eq "true") { $glue = 1;} }
  22.      elsif(/^ENDPAGE/) { &FLUSHLINE;}
  23.      elsif(/^STARTPAGE/) {
  24.             if($firstpage==1) { $firstpage=0; }
  25.             else { printf("\n\014\n");} }
  26.      elsif(/^BULLET/) { $bullet=1; }
  27.      elsif(/^VERBATIM/) { &VERBATIM; }
  28.      elsif(/^ENUMERATE/) { 
  29.            $_ = substr($last,1,length($last)-2);  # Take off parenthesis.
  30.            s/\\\\/"/;
  31.            s/\\\(/\(/;
  32.            s/\\\)/\)/;
  33.            $enum = $_; }
  34.  
  35.     
  36.      $lastlast=$last;
  37.      $last=$_;
  38.   }
  39. }
  40.  
  41. &FLUSHLINE;   # Don't forget to print the last line!
  42.  
  43. ###############################################
  44. # Handle the "def" keyword.
  45. sub DEFINE
  46. {
  47.   $lastlast = substr($lastlast,1);
  48.   eval("\$$lastlast = $last");
  49. }
  50.  
  51. ###############################################
  52. # Handle the next word to be printed.
  53. sub NEXTWORD
  54. {
  55.   $_ = $last;
  56.   if($last =~ /^\((\\\\)\)$/) {
  57.      $_ = "\\";
  58.   } else {
  59.      $_ = substr($_,1,length($_)-2);  # Take off parenthesis
  60.   }
  61.   s/\\\\/\"/;
  62.   $word = $_;
  63.   if(length($word) + $linelength + 1 >
  64.       80 - int($leftmargin/9) - int($rightmargin/9)) {
  65.     &PRINTLINE($justify);
  66.   }
  67.   if($glue) {
  68.     $glue = pop(@line);
  69.     push(@line, $glue.$word);
  70.     $glue = 0;
  71.   } else {
  72.     push(@line, $word);
  73.   }
  74.   $linelength += length($word)+1;
  75. }
  76.  
  77. ###############################################
  78. # Handle the next verbatim line to be printed.
  79. sub VERBATIM
  80. {
  81.   $_ = $last;
  82.   $_ = substr($_,1,length($_)-2);  # Take off parenthesis
  83.  
  84.   while(/\\\\/) {
  85.      s/\\\\/\"/;
  86.   }
  87.   while(/\\_/) {
  88.      s/\\_/ /;
  89.   }
  90.   while(/\\\(/) {
  91.      s/\\\(/\(/;
  92.   }
  93.   while(/\\\)/) {
  94.      s/\\\)/\)/;
  95.   }
  96.   $word = $_;
  97.   print "$word\n";
  98. }
  99.  
  100. ###############################################
  101. # Forces a print of the world list before margin space filled
  102. sub FLUSHLINE
  103. {
  104.   if($linelength > 0) {
  105.      if($justify == 102) {
  106.         &PRINTLINE(108);     # Don't fully justify this line.
  107.      } else {
  108.        &PRINTLINE($justify);
  109.      }
  110.   }
  111. }
  112.  
  113. ###############################################
  114. # Make a new paragraph
  115. sub NEWPARA
  116. {
  117.   &FLUSHLINE;
  118.   if($vspace > 0) {
  119.      for($x=0; $x < $vspace/14; $x++) {
  120.         print "\n";
  121.       }
  122.      $vspace = 0;
  123.      if($para > 0) {
  124.         $leftmargin -= $para;
  125.         $para = 0;
  126.      }
  127.   } else {
  128.      for($x=0; $x < $parskip/14; $x++) {
  129.         print "\n";
  130.      }
  131.     if($justify != 99) {
  132.        $para=$parindent;
  133.        $leftmargin += $para;
  134.     }
  135.   }
  136. }
  137.  
  138. ###############################################
  139. # Print the current line to stdout with the given justification.
  140. sub PRINTLINE {
  141.    $justification = shift;
  142.  
  143.    for($x=0; $x < int($leftmargin/9) - length($enum) - 1;
  144.        $x++) {
  145.      if($bullet && $x == int($leftmargin/9) - 2) {
  146.         print "*";
  147.         $bullet=0;
  148.      } else {
  149.         print " ";
  150.      }
  151.    }
  152.    if($enum) {
  153.      print $enum;
  154.      $enum="";
  155.    }
  156.    print " ";
  157.    if($justification == 114) {     # flushright
  158.       for($x=0; $x <
  159.            80 - int($rightmargin/9) - int($leftmargin/9) - $linelength; $x++) {
  160.          print " ";
  161.       }
  162.    } elsif($justification == 99) { # centered
  163.       for($x=0;$x <
  164.            (80 - int($rightmargin/9) - int($leftmargin/9) - $linelength)/2;
  165.             $x++) {
  166.          print " ";
  167.       }
  168.    }
  169.  
  170.    $slack = 80 - int($leftmargin/9) - int($rightmargin/9) - $linelength;
  171.    $spaces = 1;
  172.    $count = 0;
  173.    foreach (@line) {
  174.       $count++;                      # fully justified?
  175.       if($justification == 102 && $slack > 0 && $count > 1) {
  176.           while($count > $spaces / ($slack+1) * ($#line + 1)) {
  177.              print " ";
  178.              $spaces++;
  179.           }
  180.       }
  181.       print;
  182.       print " ";
  183.   }
  184.  
  185.   for($x=0; $x < $baselineskip/14; $x++) {
  186.      print "\n";
  187.   }
  188.  
  189.   if($para > 0) {
  190.     $leftmargin -= $para;
  191.     $para = 0;
  192.   }
  193.  
  194.   $linelength = 0;   # Empty the current line variable.
  195.   $#line = -1;
  196. }
  197.  
  198.