--- loncom/xml/londefdef.pm	2005/02/18 23:22:43	1.257
+++ loncom/xml/londefdef.pm	2005/03/16 21:35:17	1.264
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Tags Default Definition Module 
 #
-# $Id: londefdef.pm,v 1.257 2005/02/18 23:22:43 albertel Exp $
+# $Id: londefdef.pm,v 1.264 2005/03/16 21:35:17 raeburn Exp $
 # 
 #
 # Copyright Michigan State University Board of Trustees
@@ -84,7 +84,6 @@ sub start_m {
     my $currentstring = '';
     my $inside = &Apache::lonxml::get_all_text_unbalanced("/m",$parser);
     if ($target eq 'web' || $target eq 'analyze') {
-	$inside ='\\documentstyle{article}'.$inside;
 	&Apache::lonxml::debug("M is starting with:$inside:");
 	my $eval=&Apache::lonxml::get_param('eval',$parstack,$safeeval);
 	if ($eval eq 'on') {
@@ -185,7 +184,7 @@ sub end_html {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = '';
     if ($target eq 'web') {
-	$currentstring = &Apache::lonxml::xmlend($target,$parser);
+	$currentstring = '</html>';
     }
     return $currentstring;
 }
@@ -565,10 +564,10 @@ EDITBUTTON
 }
 
 sub end_body {
-    my ($target,$token) = @_;
+    my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = '';
     if ($target eq 'web') {
-	$currentstring = $token->[2];     
+	$currentstring = &Apache::lonxml::xmlend($target,$parser);
     } elsif ($target eq 'tex') {
 	$currentstring = '\strut\newline\noindent\makebox[\textwidth/$number_of_columns][b]{\hrulefill}\newline\noindent \end{document}';  
     } 
@@ -2496,16 +2495,22 @@ sub start_img {
 	if(!$latex_rendering) {
 	    $latex_rendering = "parbox";
 	}
-	&Apache::lonxml::debug("LaTeX rendering = $latex_rendering");
+	&Apache::lonxml::debug("LaTeX rendering = $latex_rendering image file: $src");
 
 	#if original gif/jpg/png file exist do following:
 	my ($path,$file) = &get_eps_image($src);
 	$src=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$src);
+	&Apache::lonxml::debug("path = $path file = $file src = $src");
 	if (-e $src) {
+	    &Apache::lonxml::debug("$src exists");
 	    my ($height_param,$width_param)=
 		&image_size($src,0.3,$parstack,$safeeval);
-	    $currentstring .= '\graphicspath{{'.$path.'}}'
-		.'\includegraphics[width='.$width_param.' mm,height='.$height_param.'mm]{'.$file.'} ';
+	    my $destpath = $path;
+	    $destpath    =~ s/ /\_/g; # Spaces in path cause LaTex to vomit.
+	    my $destfile = $file;
+	    $destfile    =~ s/ /\_/g;
+	    $currentstring .= '\graphicspath{{'.$destpath.'}}'
+		.'\includegraphics[width='.$width_param.' mm,height='.$height_param.'mm]{'.$destfile.'} ';
 
 	    #    If there's an alignment specification we need to honor it here.
 	    #    For the horizontal alignments, we will also honor the
@@ -2538,6 +2543,7 @@ sub start_img {
 		# $currentstring = '\raisebox{'.$height_param.'mm}{'.$currentstring.'}';
 	    }
 	} else {
+	    &Apache::lonxml::debug("$src does not exist");
 	    #original image file doesn't exist so check the alt attribute
 	    my $alt = 
 		&Apache::lonxml::get_param('alt',$parstack,$safeeval,undef,1);
@@ -2616,6 +2622,7 @@ sub start_img {
 	}
 	if ($ctag) {$currentstring=&Apache::edit::rebuild_tag($token);}
     }
+
     return $currentstring;
 }
 
@@ -3750,22 +3757,30 @@ sub image_replication {
     $pssrc  =~ s/\.(gif|jpg|jpeg|png)$/.ps/i;
     if (not -e $epssrc && not -e $pssrc) {
 	my $result=&Apache::lonnet::repcopy($epssrc);
-	if ($result ne OK) { &Apache::lonnet::repcopy($pssrc); }
+	if ($result ne 'ok') { &Apache::lonnet::repcopy($pssrc); }
     }
     return '';
 }
 
 sub image_size {
     my ($src,$scaling,$parstack,$safeeval,$depth,$cis)=@_;
+
     #size of image from gif/jpg/jpeg/png 
-    $src=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$src);
+    my $ressrc=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$src);
+    if (-e $ressrc) {
+	$src = $ressrc;
+    }
     my $image = Image::Magick->new;
     my $current_figure = $image->Read($src);
     my $width_param = $image->Get('width') * $scaling;;
-    my $height_param = $image->Get('height') * $scaling;;
+    my $height_param = $image->Get('height') * $scaling;
+    &Apache::lonxml::debug("Image magick says: $src :  Height = $height_param width = $width_param");
     undef($image);
     #do we have any specified LaTeX size of the picture?
-    my $toget='TeXwidth'; if ($cis) { $toget=lc($toget); }
+    my $toget='TeXwidth'; 
+    if ($cis) { 
+	$toget=lc($toget); 
+    }
     my $TeXwidth = &Apache::lonxml::get_param($toget,$parstack,
 					      $safeeval,$depth,$cis);
     $toget='TeXheight'; if ($cis) { $toget=lc($toget); }
@@ -3812,29 +3827,39 @@ sub image_height {
 
 sub get_eps_image {
     my ($src)=@_;
-    my $orig_src=$src;
+    my $orig_src=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1], $src);
+    &Apache::lonxml::debug("get_eps_image: Original image: $orig_src<BR />");
     $src=~s/\.(gif|png|jpg|jpeg)$/\.eps/i;
     $src=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$src);
+    &Apache::lonxml::debug("Filelocation gives; $src <BR />");
     if (! -e $src) {
-	if (&Apache::lonnet::repcopy($src) ne OK ) {
+	&Apache::lonxml::debug("$src does not exist");
+	if (&Apache::lonnet::repcopy($src) ne 'ok' ) {
+	    &Apache::lonxml::debug("Repcopy of $src failed (1)");
 	    #if replication failed try to find ps file
 	    $src=~s/\.eps$/\.ps/;
+	    &Apache::lonxml::debug("Now looking for $src");
 	    #if no ps file try to replicate it
 	    if (not -e $src &&
-		&Apache::lonnet::repcopy($src) ne OK) {
+		&Apache::lonnet::repcopy($src) ne 'ok') {
+		&Apache::lonxml::debug("Failed to find or replicate $src");
+
 		#if replication failed try to produce eps file dynamically
 		$src=~s/\.ps$/\.eps/;
 		my $temp_file;
 		open(FILE,">>/home/httpd/prtspool/$ENV{'user.name'}_$ENV{'user.domain'}_printout.dat");
 		my $newsrc=$orig_src;
 		$newsrc =~ s|(.*)/res/|/home/httpd/html/res/|;
+		&Apache::lonxml::debug("queueing $newsrc for dynamic eps production. <BR/>");
 		print FILE "$newsrc\n";
+		close FILE;
 		$src=~s|/home/httpd/html/res|/home/httpd/prtspool|;
 		$src=~s|/home/([^/]*)/public_html/|/home/httpd/prtspool/$1/|;
 	    }
 	}
     }
     my ($path,$file)=($src=~m|(.*)/([^/]*)$|);
+    &Apache::lonxml::debug("get_eps_image returning: $path / $file<BR />");
     return ($path.'/',$file);
 }