PERL   40
xtrcode
Guest on 12th February 2023 03:21:12 AM


  1. #!/usr/local/bin/perl
  2. #
  3. #    xtrcode - extract contents of LaTeX environments from a LaTeX file
  4. #    Copyright (C)  Thomas Ruedas
  5. #
  6. #    This program is free software; you can redistribute it and/or modify
  7. #    it under the terms of the GNU General Public License as published by
  8. #    the Free Software Foundation; either version 2 of the License, or
  9. #    (at your option) any later version.
  10. #
  11. #    This program is distributed in the hope that it will be useful,
  12. #    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. #    GNU General Public License for more details.
  15. #
  16. #    You should have received a copy of the GNU General Public License
  17. #    along with this program; if not, write to the Free Software
  18. #    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. #
  20. # xtrcode extracts contents of LaTeX environments from a LaTeX file,
  21. # e.g. program code in verbatim environments from a program documentation.
  22. # Version: 0.2.0
  23. # Author: Thomas Ruedas (ruedas@geophysik.uni-frankfurt.de)
  24. # URL: http://www.geophysik.uni-frankfurt.de/~ruedas/progs.html
  25. #
  26. # NOTE: This program requires Perl 5. You may need modify the first line
  27. # according to the location of Perl 5 on your system.
  28. $|=1;
  29. $all=0;
  30. $ncode=0;
  31. unless (defined @ARGV) { &usage; }
  32. foreach (@ARGV) {
  33.   OPTION: {
  34.       ($_ eq "-a") && do { $all=1; last OPTION; }; # extract all
  35.       ($_ =~ /^-e/) && do { # choose non-default environment type
  36.         $envtype=substr($_,2);
  37.         last OPTION;
  38.       };
  39.       ($_ =~ /^-p/) && do { # choose non-default marker pattern
  40.         $mpatt=substr($_,2);
  41.         $mpatt =~ s/\@/\\\@/g;
  42.         $mpatt =~ s/\+/\\\+/g;
  43.         last OPTION;
  44.       };
  45.       ($_ eq "-h") && do { &usage; }; # show help
  46.       if (defined $texfile) { # specify a codefile name (together w/ marker)
  47.           if ($all == 1) { print "<codefile> $_ ignored.\n"; last OPTION; }
  48.           $codefile=$_;
  49.       } else { # LaTeX source file
  50.           $texfile=$_;
  51.       };
  52.   };
  53. }
  54. # check arguments, set defaults if necessary
  55. unless (defined $texfile) { die "Error: No <texfile> specified.\n"; }
  56. unless (defined $envtype) { $envtype="verbatim"; }
  57. unless (defined $mpatt) { $mpatt="%%\@"; }
  58. # process LaTeX file
  59. open(TEX,"$texfile");
  60. if ($all == 0) {
  61. # search with specific codefile or extract all into one file
  62. #   if no codefile name is given, the default name is "xtrcode.out"
  63.     if (defined $codefile) {
  64.       $fpatt="$mpatt $codefile";
  65.     } else {
  66.       $codefile="xtrcode.out";
  67.       $fpatt=$mpatt;
  68.     }
  69.     open(CODE,">$codefile");
  70.     while ($line = <TEX>) {
  71.         if ($line =~ /^$fpatt/) {
  72.             unless ($fpatt eq "") { $line = <TEX>; } # null pattern is special
  73.             if ($line =~ /\\begin\{$envtype\}/gi) {
  74.                 $nested=1;
  75.                 while ($line = <TEX>) {
  76.                     if ($line =~ /\\end\{$envtype\}/gi) {
  77.                         --$nested;
  78.                         if ($nested == 0) { last; }
  79.                     } elsif ($line =~ /\\begin\{$envtype\}/gi) { ++$nested; }
  80.                     ++$ncode;
  81.                     print CODE $line;
  82.                 }
  83.             }
  84.         }
  85.     }
  86.     close(CODE);
  87.     print "$ncode lines of extracted code written to $codefile.\n";
  88. } else {
  89. # extract and put into individual codefiles
  90.   while ($line = <TEX>) {
  91.     if ($line =~ /^$mpatt/) {
  92.       $codefile=substr($line,(length $mpatt));
  93.       chomp $codefile;
  94.       unless (defined $ncode{$codefile}) { ++$nfiles; }
  95.       open(CODE,"+>>$codefile");
  96.       $line = <TEX>;
  97.       if ($line =~ /\\begin\{$envtype\}/gi) {
  98.           $nested{$codefile}=1;
  99.           while ($line = <TEX>) {
  100.               if ($line =~ /\\end\{$envtype\}/gi) {
  101.                   --$nested{$codefile};
  102.                   if ($nested{$codefile} == 0) { last; }
  103.               } elsif ($line =~ /\\begin\{$envtype\}/gi) {
  104.                   ++$nested{$codefile};
  105.               }
  106.               ++$ncode{$codefile};
  107.               print CODE $line;
  108.           }
  109.       }
  110.       close(CODE);
  111.     }
  112.   }
  113.   print "$nfiles files have been extracted from $texfile:\n\n";
  114.   format STDOUT_TOP=
  115. @<<<file@>>>>>>>>>>>>>>>>>>>>>>>>>>>> no. of lines
  116.  
  117. --------------------------------------------------
  118. .
  119.   write;
  120.   format STDOUT=
  121.    @<<<<<<<<<<<<<<<<<<<<<<<<<<@>>>>>>>>>>>>>>>>>>>
  122. $outfile,$nlines
  123. .
  124.   foreach (keys %ncode) {
  125.     $outfile=$_;
  126.     $nlines=$ncode{$_};
  127.     write;
  128.   }
  129. }
  130. close(TEX);
  131.  
  132. sub usage {
  133.     print "Usage: xtrcode <options> texfile <codefile>
  134. \tOptions:
  135. \t  -a - extract all code; this is for code which is supposed to be
  136. \t       distributed over different target files, so do not use it
  137. \t       together with <codefile>
  138. \t  -e<environment> - extract content of <environment>; default is
  139. \t                    [Vv]erbatim
  140. \t  -p<pattern> - marker pattern for environments to extract (see below)
  141. \t                a null pattern is possible
  142. \t  -h - show this help
  143.  
  144. texfile is a TeX source file containing code in some kind of LaTeX environment.
  145. The environment containing the code must be preceded by a line beginning with
  146. a marker pattern (%%\@ by default), optionally followed by a program source
  147. name <codefile>.\n";
  148.     exit;
  149. }

Raw Paste

Login or Register to edit or fork this paste. It's free.