summaryrefslogtreecommitdiff
path: root/support/xtrcode/xtrcode
blob: c21875eba43757a2bc043f34f98476c1f1bf71a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/local/bin/perl
#
#    xtrcode - extract contents of LaTeX environments from a LaTeX file
#    Copyright (C) 2000  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 (Jan 12, 2000)
# 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;
unless (defined @ARGV) { &usage; }
foreach (@ARGV) {
  OPTION: {
      ($_ eq "-a") && do { $all=1; last OPTION; }; # extract all
      ($_ =~ /^-e/) && do { # choose non-default environment type
	$envtype=substr($_,2);
	last OPTION;
      };
      ($_ =~ /^-p/) && do { # choose non-default marker pattern
	$mpatt=substr($_,2);
	$mpatt =~ s/\@/\\\@/g;
	$mpatt =~ s/\+/\\\+/g;
	last OPTION;
      };
      ($_ eq "-h") && do { &usage; }; # show help
      if (defined $texfile) { # specify a codefile name (together w/ marker)
          if ($all == 1) { print "<codefile> $_ ignored.\n"; last OPTION; }
          $codefile=$_;
      } else { # LaTeX source file
          $texfile=$_;
      };
  };
}
# check arguments, set defaults if necessary
unless (defined $texfile) { die "Error: No <texfile> specified.\n"; }
unless (defined $envtype) { $envtype="verbatim"; }
unless (defined $mpatt) { $mpatt="%%\@"; }
# process LaTeX file
open(TEX,"$texfile");
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"
    if (defined $codefile) {
      $fpatt="$mpatt $codefile";
    } else {
      $codefile="xtrcode.out";
      $fpatt=$mpatt;
    }
    open(CODE,">$codefile");
    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;
                    print CODE $line;
                }
            }
        }
    }
    close(CODE);
    print "$ncode lines of extracted code written to $codefile.\n";
} else {
# extract and put into individual codefiles
  while ($line = <TEX>) {
    if ($line =~ /^$mpatt/) {
      $codefile=substr($line,(length $mpatt));
      chomp $codefile;
      unless (defined $ncode{$codefile}) { ++$nfiles; }
      open(CODE,"+>>$codefile");
      $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};
	      print CODE $line;
	  }
      }
      close(CODE);
    }
  }
  print "$nfiles files have been extracted from $texfile:\n\n";
  format STDOUT_TOP=
@<<<file@>>>>>>>>>>>>>>>>>>>>>>>>>>>> no. of lines

--------------------------------------------------
.
  write;
  format STDOUT=
   @<<<<<<<<<<<<<<<<<<<<<<<<<<@>>>>>>>>>>>>>>>>>>>
$outfile,$nlines
.
  foreach (keys %ncode) {
    $outfile=$_;
    $nlines=$ncode{$_};
    write;
  }
}
close(TEX);
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;
}