# -*-perl-*-

#/*@@
#  @file      grdoc_texsupport
#  @date      Sun Apr 21 11:33:40 1996
#  @author    Paul Walker
#  @desc 
#  grdoc support for tex regions.  Requires that everything is setup
#  properly at the top of @seefile grdoc in @seeroutine main
#  @enddesc 
#  @comment
#  <code>$Id: grdoc_texsupport,v 1.5 1996/04/21 17:45:34 pwalker Exp $</code>
#  @endcomment
#@@*/



#/*@@
#  @routine    tex_init
#  @date       Sun Apr 21 11:34:29 1996
#  @author     Paul Walker
#  @desc 
#  Announces TeX mode and creates a dir to take the images if needed.
#  @enddesc 
#  @calledby   main
#@@*/

# Some globals
$TeXImgCtr = 1;
$TeXImgDir = "TeXImages";

sub
tex_init
{
    if (!( -d "$opt_o/$TeXImgDir")) {
	mkdir ("$opt_o/$TeXImgDir", 0755);
    }
    print "==> Converting TeX Equations <==\n";
}


#/*@@
#  @routine    tex_to_html
#  @date       Sun Apr 21 11:35:06 1996
#  @author     Paul Walker
#  @desc 
#  Takes an equation and returns an bit of html which points to that equation.
#  Note this routine <b>DOES</b> assume you have the $ or $$ and changes the 
#  returns html accordingly.
#  <p>
#  This also creates the appropriate gif image, using @seeroutine 
#  texeqn_to_gif
#  @enddesc 
#  @calls     texeqn_to_gif
#  @var     eqn
#  @vdesc   The thing stripped from the comments, including $ or $$, 
#           to be converted
#  @vtype   string
#  @vio     in
#  @endvar 

#  @returntype string
#  @returndesc
#  A bit of html which you can insert in your out file which contains
#  the appropriate link to am image.
#  @endreturndesc

#@@*/

sub
tex_to_html
{
    local ($eqn) = @_[0];
    local ($texhtml, $div, $enddiv);

    print "           TeX: $eqn\n" if $opt_V;
    if ($eqn =~ m:\$\$:) {
	$div = "<div align=center>";
	$enddiv = "</div>";	# Its kind of like bitter lettuce.
    } else {
	$div = "";
	$enddiv = "";
    }
    $texhtml = "$div<img src=\"$opt_t/$TeXImgDir/Img_$TeXImgCtr.gif\">$enddiv";
    $eqn =~ s:\$::g;
    &texeqn_to_gif ($eqn, "$opt_o/$TeXImgDir/Img_$TeXImgCtr.gif");
    $TeXImgCtr++;

    return $texhtml;
}

#/*@@
#  @routine    texeqn_to_gif
#  @date       Sun Apr 21 11:38:23 1996
#  @author     Paul Walker
#  @desc 
#  Given a tex eqn and a file name, makes a gif of it!
#  @enddesc 
#  @calledby   tex_to_html

#  @var     eqn
#  @vdesc   string
#  @vtype   the TeX equation without the #
#  @vio     in
#  @endvar 

#  @var     outg
#  @vdesc   string
#  @vtype   the filename for the resulting gif
#  @vio     in
#  @endvar 

#  @comment
#  Requires that @seeroutine main have the TeX and imageMagick 
#  vars set up properly!
#  @endcomment
#@@*/

sub
texeqn_to_gif
# arguments are equation_string and out_gif.  I assume the eqn
# does NOT have $ around it.
{
    local ($eqn, $outg) = @_;
    open (TTEX, "> /tmp/grdoc_$$.tex") || die "TS: /tmp/grdoc_$$.tex";
    print TTEX <<EOT;
\\magnification \\magstep 5
\\nopagenumbers
\\overfullrule 0pt
\$\$$eqn\$\$
\\bye
EOT
    close TTEX;
    system "cd /tmp; $TeX grdoc_$$.tex";
    system "$Dvips -f < /tmp/grdoc_$$.dvi | $Convert -geometry 60% -colors 8 PS:- $outg";
    system "/bin/rm /tmp/grdoc_$$.*";
}

1;

