summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/latex/stex/bin/msplit
blob: cf178ce9b5fa473b2556007fc6010971ef99a815 (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
#!/usr/bin/perl -w
##############################################################################################
# Program: msplit (a utility for splitting STeX documents along module boundaries)
##############################################################################################
# $URL: svn://kwarc.faculty.iu-bremen.de/kohlhase/kwarc/projects/content/bin/msplit$ 
# $Rev: 3722 $; last modified by $Author: $ 
# $Date: 2005-07-27 03:22:19 +0200 (Wed, 27 Jul 2005) $
# Copyright (c) 2005 Ioan Sucan
##############################################################################################
# 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 [http://www.gnu.org/copyleft/gpl.html]
# for more details.
##############################################################################################
use strict;
use Getopt::Long;
use File::Path;
use Pod::Usage;

my $input='-', my $output='-', my $path='', my $verbose=0, my $stop_at_end=0;
my $flag = "include";
my $header='', my @hdr_data;

sub texf {
    my $fn = shift;
    $fn.='.tex' if $fn !~ /\.tex$/;
    return $fn; }

sub write_file_data{
    my $filename = shift;
    open(FS,$filename) or die "Cannot write $filename\n";
    print FS @_;
    close(FS); }

# remove the last component in a path
sub justpath{
    my $arg = shift;
    return '' if ($arg !~ m!\/!);
    while ($arg !~ m!\/$!) {chop($arg);}
    return $arg; }
# end justpath


sub go {
    my ($input, $output) = @_; 
    my $path = justpath($output);

    my @module_file=();
    my @out_data=();

    print "Splitting $input...\n" if $verbose;
    `mv $output $output~` if (-e $output);
    $input="$input~" if ($input eq $output);
    
    open(FIN,$input) or die "Can't open $input\n";
        my $in_module=0; my $path_name;
    foreach (<FIN>){    
	chomp;
	/([^%]*)((%.*)?)/;
	($_,my $comment)=($1,$2);
	if (/\\begin\s*\{module\}/){
	    $_ =~ s/\s//g;
	    /id=([\w-]*)/;	
	    my $module_name = $1;
	    $path_name = "$path$module_name.tex";	    
	    print "$path_name\n" if $verbose;
	    `mv $path_name $path_name~` if (-e $path_name);
	    push(@module_file,@hdr_data) if $header;
	    push(@module_file,"\n$_$comment\n");
	    push(@out_data,"\\requiremodules[$flag]{$module_name}\n");
	    $in_module=1;
	    next; } 
	if (/\\end\s*\{module\}/){
	    $_ =~ s/\s//g;
	    push(@module_file,"$_$comment\n");
	    mkpath(justpath($path_name));
	    write_file_data(">$path_name",@module_file);
	    $in_module=0;	    
	    next; }
	if ($stop_at_end && /\\end\s*\{document\}/){
	    $_ =~ s/\s//g;
	    push(@out_data,"$_$comment\n");
	    last; }
	$_="$_$comment\n";
	if ($in_module) {push(@module_file,$_);}
	else {push(@out_data,$_); }}
    close(FIN);
    mkpath($path) if $path;
    write_file_data(">$output",@out_data); }

GetOptions("output=s" => \$output,
	   "path=s" => \$path,
	   "header=s" => \$header,
	   "exclude" => sub { $flag = "exclude"; },
	   "verbose" => sub { $verbose=1; },
	   "stop" => sub { $stop_at_end=1; },
	   "help" => sub { pod2usage(2)});

$input = $ARGV[0] if ($#ARGV>=0);
die "Input file does not exist\n" if (! defined $input || ! -e $input);
$output = $input if (! defined $output);

if ($header && -e $header) {
    open(HDR,$header);
    @hdr_data = <HDR>;
    close(HDR); }

$path.='/' if $path && $path !~ m!\/$!;
go($input,$path.$output);
print "Done.\n" if $verbose;

__END__


=head1 SYNOPSIS

msplit <inputfile> [options]

Options:
   
    --output      filename of new main .tex file; default: same as input
    --path        path where to generate the new documents; '.' by default
    --header      file to be copied at beginning of all new module files
    --exclude     modules exluded by default; if not mentioned, modules 
                  are included by default
    --verbose     verbose on
    --stop        stop when \end{document} is found; default is to go on
    --help        this screen

    

    This program splits a .tex file into more files, each containing a module.
    Example usage:
    ./msplit slides.tex -o sl.tex -p ./modules -s