- #!/usr/local/bin/perl
- #
- # xtrcode - extract contents of LaTeX environments from a LaTeX file
- # Copyright (C) Thomas Ruedas
- #
- # This program is free software; you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation; either version 2 of the License, or
- # (at your option) any later version.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program; if not, write to the Free Software
- # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- #
- # xtrcode extracts contents of LaTeX environments from a LaTeX file,
- # e.g. program code in verbatim environments from a program documentation.
- # Version: 0.2.0
- # Author: Thomas Ruedas (ruedas@geophysik.uni-frankfurt.de)
- # URL: http://www.geophysik.uni-frankfurt.de/~ruedas/progs.html
- #
- # NOTE: This program requires Perl 5. You may need modify the first line
- # according to the location of Perl 5 on your system.
- $|=1;
- $all=0;
- $ncode=0;
- foreach (@ARGV) {
- OPTION: {
- ($_ eq "-a") && do { $all=1; last OPTION; }; # extract all
- ($_ =~ /^-e/) && do { # choose non-default environment type
- last OPTION;
- };
- ($_ =~ /^-p/) && do { # choose non-default marker pattern
- $mpatt =~ s/\@/\\\@/g;
- $mpatt =~ s/\+/\\\+/g;
- last OPTION;
- };
- ($_ eq "-h") && do { &usage; }; # show help
- $codefile=$_;
- } else { # LaTeX source file
- $texfile=$_;
- };
- };
- }
- # check arguments, set defaults if necessary
- # process LaTeX file
- if ($all == 0) {
- # search with specific codefile or extract all into one file
- # if no codefile name is given, the default name is "xtrcode.out"
- $fpatt="$mpatt $codefile";
- } else {
- $codefile="xtrcode.out";
- $fpatt=$mpatt;
- }
- while ($line = <TEX>) {
- if ($line =~ /^$fpatt/) {
- unless ($fpatt eq "") { $line = <TEX>; } # null pattern is special
- if ($line =~ /\\begin\{$envtype\}/gi) {
- $nested=1;
- while ($line = <TEX>) {
- if ($line =~ /\\end\{$envtype\}/gi) {
- --$nested;
- if ($nested == 0) { last; }
- } elsif ($line =~ /\\begin\{$envtype\}/gi) { ++$nested; }
- ++$ncode;
- }
- }
- }
- }
- } else {
- # extract and put into individual codefiles
- while ($line = <TEX>) {
- if ($line =~ /^$mpatt/) {
- $line = <TEX>;
- if ($line =~ /\\begin\{$envtype\}/gi) {
- $nested{$codefile}=1;
- while ($line = <TEX>) {
- if ($line =~ /\\end\{$envtype\}/gi) {
- --$nested{$codefile};
- if ($nested{$codefile} == 0) { last; }
- } elsif ($line =~ /\\begin\{$envtype\}/gi) {
- ++$nested{$codefile};
- }
- ++$ncode{$codefile};
- }
- }
- }
- }
- format STDOUT_TOP=
- --------------------------------------------------
- .
- @<<<<<<<<<<<<<<<<<<<<<<<<<<@>>>>>>>>>>>>>>>>>>>
- $outfile,$nlines
- .
- $outfile=$_;
- $nlines=$ncode{$_};
- }
- }
- exit;
- sub usage {
- print "Usage: xtrcode <options> texfile <codefile>
- \tOptions:
- \t -a - extract all code; this is for code which is supposed to be
- \t distributed over different target files, so do not use it
- \t together with <codefile>
- \t -e<environment> - extract content of <environment>; default is
- \t [Vv]erbatim
- \t -p<pattern> - marker pattern for environments to extract (see below)
- \t a null pattern is possible
- \t -h - show this help
- texfile is a TeX source file containing code in some kind of LaTeX environment.
- The environment containing the code must be preceded by a line beginning with
- a marker pattern (%%\@ by default), optionally followed by a program source
- name <codefile>.\n";
- exit;
- }
Raw Paste