| 1 |
####################################################################################### |
| 2 |
# A module for loading .tex files and feeding the data to the various applications. # |
| 3 |
# Copyright (c) 2005, Ioan Sucan, released under the Gnu General Public License (GPL) # |
| 4 |
# see http://www.gnu.org/copyleft/gpl.html # |
| 5 |
# $URL: svn://kwarc.eecs.iu-bremen.de/repos/kwarc/projects/content/bin/Modparse.pm$ # |
| 6 |
# $Date: 2005-09-26 12:44:28 -0500 (Mon, 26 Sep 2005) $ $Rev: 4243 $ # |
| 7 |
####################################################################################### |
| 8 |
|
| 9 |
package Modparse; |
| 10 |
|
| 11 |
use File::Basename; |
| 12 |
use File::Path; |
| 13 |
|
| 14 |
# if both snippath and snippathList are specified, |
| 15 |
# snippathList is ignored ! |
| 16 |
|
| 17 |
# if a default snippath definition file is found |
| 18 |
# (snippath.def) it is used if no snippath is given |
| 19 |
# as argument; |
| 20 |
sub new { |
| 21 |
my ($class, %options) = @_; |
| 22 |
|
| 23 |
$options{recursion} = 1 unless defined $options{recursion}; |
| 24 |
$options{requireStatus} = 'include|exclude' unless defined $options{requireStatus}; |
| 25 |
$options{loadInputs} = 1 unless defined $options{loadInputs}; |
| 26 |
$options{onlyModuleLines} = 0 unless defined $options{onlyModuleLines}; |
| 27 |
|
| 28 |
if ( !(%{$options{snippath}}) && (-e "snippath.def") ) { |
| 29 |
open(SNIPPATHDEF, 'snippath.def'); |
| 30 |
my @snlist = <SNIPPATHDEF>; |
| 31 |
close(SNIPPATHDEF); |
| 32 |
for (my $i = 0 ; $i <= $#snlist; $i++) { chomp($snlist[$i]); } |
| 33 |
%{$options{snippath}} = @snlist; } |
| 34 |
@{$options{snippathList}} = keys(%{$options{snippath}}) |
| 35 |
if ($options{snippath} && %{$options{snippath}}); |
| 36 |
$options{debug} = 0; |
| 37 |
|
| 38 |
bless(\%options, $class); } |
| 39 |
|
| 40 |
sub execute{ |
| 41 |
my ($self, $file) = @_; |
| 42 |
|
| 43 |
local *run_event = sub { |
| 44 |
my ($event, $arg) = @_; |
| 45 |
if ($self->{$event}) { $self->{$event}($arg); } |
| 46 |
}; |
| 47 |
|
| 48 |
local *go = sub { |
| 49 |
my ($location, %param) = @_; |
| 50 |
my ($tex, $path, @tex_data) = $self->readLocation($location); |
| 51 |
return unless @tex_data; |
| 52 |
my $NL = $#tex_data+1; |
| 53 |
|
| 54 |
$param{filename} = $tex; |
| 55 |
$param{depth}++; |
| 56 |
$param{filecount}++; |
| 57 |
my $i = 0; |
| 58 |
|
| 59 |
$param{nextline} = sub { $tex_data[++$i]; }; |
| 60 |
$param{prevline} = sub { $tex_data[--$i]; }; |
| 61 |
$param{rewriteline}= sub { $tex_data[$i] = $_[0]; }; |
| 62 |
|
| 63 |
print "Processing $tex...\n" if $self->{verbose}; |
| 64 |
run_event('onBeginFile', \%param); |
| 65 |
|
| 66 |
while ($i<$NL){ |
| 67 |
$param{original} = $tex_data[$i]; |
| 68 |
$_ = $tex_data[$i]; chomp; /([^%]*)((%.*)?)/; |
| 69 |
($param{tex}, $param{comment}) = ($1, $2); $_ = $1; |
| 70 |
s/\s+//g; $param{simple_tex} = $_; |
| 71 |
|
| 72 |
if (/\\begin\{module\}(\[([^\]]*)\])?/) { |
| 73 |
if ($param{in_module}) { |
| 74 |
push(@{$param{module_stack}}, [$param{module_id}, $param{module_arg}]); } |
| 75 |
$param{in_module} = 1; |
| 76 |
$param{module_args} = $1 ? $2 : undef; |
| 77 |
$param{module_id} = ($param{module_args} && |
| 78 |
$param{module_args} =~ /id=([^\],]+)/) ? $1 : undef; |
| 79 |
$param{module_nesting}++; |
| 80 |
run_event('onBeginModule', \%param); } |
| 81 |
|
| 82 |
if (/\\end\{module\}/) { |
| 83 |
run_event('onEndModule', \%param); |
| 84 |
if ($#{$param{module_stack}} >= 0) { |
| 85 |
($param{module_id}, $param{module_args}) = @{pop(@{$param{module_stack}})}; |
| 86 |
} else { |
| 87 |
$param{in_module} = 0; |
| 88 |
($param{module_id}, $param{module_args}) = (undef, undef); } |
| 89 |
$param{module_nesting}--; |
| 90 |
} |
| 91 |
|
| 92 |
foreach my $srch_snippath(@{$self->{snippathList}}){ |
| 93 |
$self->{snippath}{$srch_snippath} = $1 |
| 94 |
if (/\\def\\$srch_snippath\#1\{([^\#]+)\#1\}$/); } |
| 95 |
|
| 96 |
if ($self->{follow} && (/\\((requiremodules(\[($self->{requireStatus})\])?)|input|include)\{([^\}]+)\}/)){ |
| 97 |
my $req_filename = $5; my $req_spath = ''; my $command = $1; |
| 98 |
my $is_require = ($command =~ /requiremodules/) ? 1 : 0; |
| 99 |
if ($is_require || $self->{loadInputs}) { |
| 100 |
foreach my $srch_snippath(@{$self->{snippathList}}){ |
| 101 |
if ($req_filename =~ /\\$srch_snippath\{(.+)/){ |
| 102 |
($req_spath, $req_filename) = ($self->{snippath}{$srch_snippath}, $1); |
| 103 |
last; }} |
| 104 |
run_event('onRecurse', \%param); |
| 105 |
if ($self->{recursion} || !$is_require) { |
| 106 |
go($self->makeLocation($req_filename, $path, $req_spath), %param); } |
| 107 |
} |
| 108 |
} |
| 109 |
run_event('onEveryLine', \%param) if ($param{in_module} || !$self->{onlyModuleLines}); |
| 110 |
|
| 111 |
last if (/\\end\{document\}/ && $self->{stopAtEnd}); |
| 112 |
$i++; |
| 113 |
} |
| 114 |
run_event('onEndFile', \%param); |
| 115 |
$param{depth}--; return; |
| 116 |
}; |
| 117 |
|
| 118 |
my %param = (pack=>$self, perm=>$self->{_AUX_DATA}, depth=>0, filecount=>0, |
| 119 |
in_module=>0, module_nesting=>0); |
| 120 |
run_event('onBegin', \%param); |
| 121 |
go($self->makeLocation($file), %param); |
| 122 |
run_event('onEnd', \%param); |
| 123 |
return; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
# add a [.tex] extension to the filename |
| 128 |
sub add_ext{ |
| 129 |
my ($self, $fn, $ext) = @_; |
| 130 |
$ext = '.tex' unless $ext; |
| 131 |
$fn.=$ext if $fn !~ /\Q$ext\E$/; |
| 132 |
$fn; } |
| 133 |
|
| 134 |
# as long as the extension is .tex, drop it |
| 135 |
sub drop_ext{ |
| 136 |
my ($self, $filename) = @_; |
| 137 |
while ($filename =~ /\.tex$/){ |
| 138 |
while ($filename !~ /\.$/ ) { chop($filename); } |
| 139 |
chop($filename); } |
| 140 |
$filename; } |
| 141 |
|
| 142 |
sub new_ext{ |
| 143 |
my ($self, $filename, $ext) = @_; |
| 144 |
$self->add_ext($self->drop_ext($filename), $ext); } |
| 145 |
|
| 146 |
# location = pointer to array of strings |
| 147 |
# first element is filename; next are possible paths |
| 148 |
sub makeLocation{ my $self = shift; return \@_; } |
| 149 |
|
| 150 |
# read a location into an array of strings |
| 151 |
# aslo return filename and path to it (just in |
| 152 |
# case some changes were made to it, like adding |
| 153 |
# extension and/or relative paths) |
| 154 |
sub readLocation{ |
| 155 |
my ($self, $location) = @_; |
| 156 |
|
| 157 |
my ($filename, @paths) = @{$location}; |
| 158 |
# try to add proper extension; |
| 159 |
my $filename_ext = $self->new_ext($filename) unless -e $filename; |
| 160 |
$filename = $filename_ext if $filename_ext && -e $filename_ext; |
| 161 |
|
| 162 |
# try search paths |
| 163 |
if (! -e $filename){ |
| 164 |
my $all_p = ''; |
| 165 |
foreach my $p(@paths){ |
| 166 |
$all_p .= $p; |
| 167 |
$filename = $all_p.$filename and last if -e $all_p.$filename; |
| 168 |
$filename = $p.$filename and last if -e $p.$filename; |
| 169 |
if ($filename_ext){ |
| 170 |
$filename = $all_p.$filename_ext and last if -e $all_p.$filename_ext; |
| 171 |
$filename = $p.$filename_ext and last if -e $p.$filename_ext; } |
| 172 |
}} |
| 173 |
# don't do the same work too many times |
| 174 |
foreach my $file(@{$self->{loadedFiles}}) |
| 175 |
{ return undef if $file eq $filename; } |
| 176 |
|
| 177 |
push(@{$self->{loadedFiles}}, $filename); |
| 178 |
open(FIN, $filename) or die "Cannot open '$filename'\n"; |
| 179 |
my @data = <FIN>; |
| 180 |
close(FIN); |
| 181 |
|
| 182 |
# find path of current file |
| 183 |
my $path = dirname($filename); |
| 184 |
$path.='/' if ($path ne ''); |
| 185 |
|
| 186 |
return ($filename, $path, @data); |
| 187 |
} |
| 188 |
|
| 189 |
1; |