#!/usr/bin/perl -w # mathinhtml is copyright (C) 2003 Michael J Miller # This program is free software; you can redistribute it and/or modify it # under the terms of the GNU General Public License. This program is # distributed without any warranty (not even the implied warranty of # merchantability or fitness for a particular purpose). use strict; use File::Basename; my $programname="mathinhtml v0.50 (04-Feb-2003)"; ####################################################################### # This section establishes the rules by which mathinhtml translates # the contents of an element into the requested image/text. my %rules; $rules{"TEX"}={ input => "\\nopagenumbers _CONTENTS_ \\bye\n", action => "tex -interaction=batchmode tempfile\n". "dvips -E -q tempfile.dvi -o tempfile.ps", density => 500, attrib => 'ALIGN="CENTER"' }; $rules{"EQ"}={%{$rules{"TEX"}}}; $rules{"EQ"}{"input"}="\\nopagenumbers \$\$ _CONTENTS_ \$\$ \\bye"; my $gnuplot_setup="set terminal postscript eps enhanced 24\n". "set output \"tempfile.ps\" \n"; $rules{"GNUPLOT"}={ input => "$gnuplot_setup; _CONTENTS_ \n", action => "gnuplot tempfile", density => 200, attrib => 'ALIGN="TOP"' }; $rules{"GR"}={%{$rules{"GNUPLOT"}}}; $rules{"GR"}{"input"}="$gnuplot_setup; set zeroaxis; set xtics axis\n". "set ytics axis; set noborder; set nokey; _CONTENTS_ \n"; $rules{"EQN"}={ input => "_CONTENTS_ \n", action => "tex2html tempfile" }; my @tempfiles=qw/ tempfile tempfile.dvi tempfile.log tempfile.ps /; ############################################################### # This section defines the subroutines "translate" (which translates # an element to its corresponding image/text) and "replace" (which # replaces an element by its translation). my %archive; # This hash catalogs those image files that have already been # created, so that existing images will not be recreated. my $tagnames=join '|', keys %rules; sub translate { my $key=$_[0]; my ($tagname, $contents)=split /\t/, $key; my $input=$rules{$tagname}{"input"}; $input =~ s/_CONTENTS_/$contents/g; open INPUTFILE, "> tempfile"; print INPUTFILE $input; close INPUTFILE; print "Translating $key\n"; my $output=qx/ $rules{$tagname}{"action"} \n /; if (-s "tempfile.ps"){ my $num=0; while (-s "IMG_$num.png") { $num++ }; my $imgname="IMG_$num.png"; my $density=$rules{$tagname}{"density"}; system "convert -density $density tempfile.ps $imgname \n"; if (-s "$imgname.0") { #This is a workaround for a dvips bug. rename "$imgname.0", $imgname; unlink "$imgname.1"; }; $_=qx/ identify -ping $imgname /; my ($width, $height) = / ([0-9]+)x([0-9]+)(\+0\+0)? /; $archive{$key}= {filename=>$imgname, width=>$width, height=>$height }; } else { $output =~ s/[\t\r\n]/ /g; $archive{$key}=$output }; unlink @tempfiles; } sub replace { my $qstr=q/(?>".*?"|'.*?')/; # Matches a quoted string my ($tagname, $attributes, $contents)= $_[0] =~ m|^<($tagnames)((?:\s+\w+=$qstr)*)\s*>(.*?)$|is; $contents =~ s/[\n\r\t]/ /g; my $key="$tagname\t$contents"; if (! exists $archive{$key}){ &translate($key) }; if (ref $archive{$key}){ for ($contents) { s/&/&/g; s//>/g; s/"/"/g; s/'/'/g; }; $attributes=" SRC=\"$archive{$key}{'filename'}\"". " WIDTH=\"".($archive{$key}{'width'}/4)."\"". " HEIGHT=\"".($archive{$key}{'height'}/4)."\"". " ALT=\"$contents\" TITLE=\"$contents\"". " $rules{$tagname}{'attrib'} $attributes "; while ($attributes =~ s/\s+(\w+)=$qstr(.*)\s+\1=($qstr)/ $1=$3 $2/is) {}; return ""; } else { return $archive{$key} }; } ############################################################## # This section defines the subroutines readarchive and writearchive, # which synchronize the hash %archive with the file archive.log. # The format of the file archive.log is tab-separated lines of # filename width height tagname contents output sub readarchive { if (open(ARCHIVELOG, "< archive.log")) { while ( ){ chomp; my ($filename, $width, $height, $tagname, $contents, $output)= split /\t/; if ($filename ne "" && -s $filename) { $archive{"$tagname\t$contents"}= { filename => $filename, width => $width, height => $height }; } elsif (defined($output)) { $archive{"$tagname\t$contents"}=$output }; }; }; close(ARCHIVELOG); } sub writearchive { open(ARCHIVELOG, "> archive.log"); foreach my $key (keys %archive){ if (ref $archive{$key}){ print ARCHIVELOG "$archive{$key}{'filename'}\t". "$archive{$key}{'width'}\t". "$archive{$key}{'height'}\t$key\n" } else { print ARCHIVELOG "\t\t\t$key\t$archive{$key}\n"; }; }; close ARCHIVELOG; } ################################################################# # This is the main executable. print "\n$programname\nCopyright (C) 2003 Michael J Miller\n". "This program comes with absolutely no warranty.\n\n"; chdir dirname $ARGV[0]; &readarchive; my $filename=basename $ARGV[0]; open(SOURCEFILE, "< $filename"); undef $/; $_=<>; close SOURCEFILE; s||\n\n|i; my @sections= m/(.*?)(

|
|

  • ||.$)/igs; $filename =~ s/\.mih$//; open(HTMLFILE, "> $filename.html"); foreach (@sections){ s|<($tagnames)[\s>].*?|&replace($&)|iesg; print HTMLFILE "$_"; } close HTMLFILE; &writearchive;