penn

链接

A perl script for generating eps files from mp codes

2008年11月14日 22:12 | Comments(0) | Category:Metapost | Tags:

I am indebted to instanton and cvgmt at CTeX forum who provided the source codes, you can find the web here

#!/usr/bin/perl

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# mp2eps version 1.0 : produce postscript. files from metapost sources   #
# Copyright (C) 2004 Glad Deschrijver, Wed Jun 16 2004, Wed Oct 06 2004 #
# [email]Glad.Deschrijver@UGent.be[/email]                                             #
#                                                                       #
# 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, #
# USA.                                                                  #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

use File::Spec::Functions;
use File::Basename;
use File::Find;
use File::Copy;

$delete = "yes";
$preview = "no";
$previewcmd = "gsview32";
$rename = "yes";
$standalone = "yes";
$typeset = "latex";

$progname = "$0";
$version = "1.0";
$copyright = "Copyright (C) 2004 Glad Deschrijver";
$file = "";
$extension = 1;
$mp2epstmp = "_mp2eps_";

sub usage
{
       
print <<EOF;
Usage: $progname [OPTIONS] file[.mp]
Options: [defaults in brackets after descriptions]
Configuration:
  --typeset=CMD                        use CMD for typesetting [$typeset]
  --standalone=yes/
no                produce standalone EPS figures [$standalone]
  -
s                                short for --standalone=yes
  --
delete=yes/no                delete auxiliary files produced by mpost
                                [$delete]
  --
rename=yes/no                rename outputfiles to name mentioned in
                                file.mp as e.g.
                                % <figure number="1" name="testfig.eps"/>
  --preview=yes/
no                preview using program defined
                                by --previewcommand [$preview]
  -p                                short for --preview=yes
  --previewcommand=CMD                use CMD to preview the output file [$previewcmd]
  -h, --help                        display this help and
exit
  -v, --version                        output version information and
exit
EOF
}

sub parsefilename
{
        if ($file eq "")
        {
               
print "$progname: At least one file name is required as an argument.\n";
               
exit 1;
        }
        else
        {
                $F = $file;
                $F =~
s/\.[^\.]*$//;
                $ext = $file;
                $ext =~
s/.*\.//g;
                if ($ext eq "mp")
                {
                        $file = $F;
                }
                if (! -r "$file.mp")
                {
                       
print "$progname: $file.mp not found or not readable.";
                       
exit 1;
                }
        }
        $path = dirname("$file.mp");
        $file = basename("$file.mp");
        $file =~
s/\.mp$//;
       
chdir($path);
}

sub createtexfile
{
        $tmpfile2 = "${tmpfile}.tex";
       
open(F, ">$tmpfile2");
       
print F "\\documentclass\{report\}\n";
       
print F "\\usepackage\{amssymb\}\n";
       
print F "\\usepackage\{amsmath\}\n";
       
print F "\\usepackage\{times,mathptmx\}\n";
       
print F "\\usepackage\{graphicx\}\n";
       
print F "\\usepackage\{color\}\n";
       
print F "\\begin\{document\}\n";
       
print F "\\pagestyle\{empty\}\n";
       
print F "\\includegraphics\{$file.$extension\}\n";
       
print F "\\end\{document\}";
       
close(F);
}

sub removewanted
{
        /^$tmpfile/ &&
unlink($File::Find::name);
}

sub typesetfile
{
        $tmpfile = "${file}${mp2epstmp}";
       
system("mpto -tex \"$file.mp\" > \"${tmpfile}.tex\"");
        find(\&removewanted, curdir());
       
system("mpost \"$file.mp\"");

       
open(F, "<$file.mp");
        @filecontents = <F>;
       
close(F);
        $i = 0;
        foreach $fileline (@filecontents)
        {
                if ($fileline =~ /^beginfig/)
                {
                        $fileline =~
s/.*\(//;
                        $fileline =~
s/\).*//;
                       
chop($fileline);
                        $fignums[$i++] = $fileline;
                }
        }
       
undef @filecontents;
        for ($i = 0; $i < @fignums; $i++)
        {
                $extension = $fignums[$i];
                $file2 = "${file}_${extension}";
                if ($standalone eq "yes")
                {
                        createtexfile();
                       
system("latex $tmpfile");
                       
system("dvips -E ${tmpfile}.dvi -o ${tmpfile}.eps");
                       
open(F, "<${tmpfile}.eps");
                        @tmpfilecontents = <F>;
                       
close(F);
                       
open(F, ">${file2}.eps");
                        foreach $tmpfileline (@tmpfilecontents)
                        {
                                $tmpfileline =~
s/$tmpfile\.eps/\"${file2}.eps\"/g;
                                $tmpfileline =~ s/$tmpfile\.dvi/\"${file2}.dvi\"/g;
                                print F $tmpfileline;
                        }
                        close(F);
                        undef @tmpfilecontents;
                        find(\&removewanted, curdir());
                }
                else
                {
                        move("
${file}.${extension}", "${file2}.eps");
                }
                if ($preview eq "
yes")
                {
                        system("
$previewcmd $file2.eps");
                }
        }
}

sub deleteauxfiles
{
        unlink("
${file}.mpx");
        unlink("
${file}.
log");
        for ($i = 0; $i < @fignums; $i++)
        {
                unlink("
${file}.$fignums[$i]");
        }
        if ($rename eq "
yes")
        {
                for ($i = 0; $i < @fignums2; $i++)
                {
                        unlink("
${file}_$fignums2[$i].eps");
                }
        }
        unlink("
mptextmp.mp");
        unlink("
mptextmp.mpx");
}

sub renameoutputfiles
{
        open(F, "
<${file}.mp");
        @filecontents = <F>;
        close(F);
        $i = 0;
        foreach $fileline (@filecontents)
        {
                if ($fileline =~ /\%.* <figure\ .*>/)
                {
                        $filenum = $fileline;
                        $filename = $fileline;
                        $filenum =~ s/.*number=\"([^\"]*)\".*/\1/;
                        $filename =~ s/.*name=\"([^\"]*)\".*/\1/;
                        chop($filenum);
                        chop($filename);
                        $fignums2[$i++] = $filenum;
                        $filename =~ s/\.(eps|ps)$//;
                        $tmpfile = "
${file}_$i";
                        open(F, "
<${tmpfile}.eps");
                        @tmpfilecontents = <F>;
                        close(F);
                        open(F, "
>${filename}.eps");
                        foreach $tmpfileline (@tmpfilecontents)
                        {
                                $tmpfileline =~ s/$tmpfile\.eps/${filename}.eps/g;
                                $tmpfileline =~ s/$tmpfile\.dvi/${filename}.dvi/g;
                                print F $tmpfileline;
                        }
                        close(F);
                        undef @tmpfilecontents;
                }
        }
        undef @filecontents;
}

foreach $arg (@ARGV)
{
        if ($arg eq "
-h" || $arg eq "--help")
        {
                usage();
                exit 0;
        }
        elsif ($arg =~ /^--preview=/)
        {
                $preview = $arg;
                $preview =~ s/^--preview=//;
        }
        elsif ($arg eq "
-p")
        {
                $preview = "
yes";
        }
        elsif ($arg =~ /^--standalone=/)
        {
                $standalone = $arg;
                $standalone =~ s/^--standalone=//;
        }
        elsif ($arg eq "
-s")
        {
                $standalone = "
yes";
        }
        elsif ($arg =~ /^--rename=/)
        {
                $rename = $arg;
                $rename =~ s/^--rename=//;
        }
        elsif ($arg =~ /^--typeset=/)
        {
                $typeset = $arg;
                $typeset =~ s/^--typeset=//;
        }
        elsif ($arg eq "
-v" || $arg eq "--version")
        {
                print "
$progname $version\n";
                print "
$copyright\n";
                print "
This program comes with ABSOLUTELY NO WARRANTY.  This is free\n";
                print "
software, and you are welcome to redistribute it under\n";
                print "
the terms of the GNU General Public License.\n";
                exit 0;
        }
        elsif ($arg =~ /^-/)
        {
                usage();
                exit 1;
        }
        else
        {
                $file = $arg;
        }
}

parsefilename;
typesetfile;
if ($rename eq "
yes")
{
        renameoutputfiles;
}
if ($delete eq "
yes")
{
        deleteauxfiles;
}

After saved the file, you should change the attribute of the file as an executive program.

$ chmod +x mp.pl
and then, you can move the file to /usr/local/bin or $HOME/bin for using.