--- loncom/xml/londefdef.pm	2004/01/28 19:59:13	1.195
+++ loncom/xml/londefdef.pm	2004/03/31 05:24:00	1.204
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Tags Default Definition Module 
 #
-# $Id: londefdef.pm,v 1.195 2004/01/28 19:59:13 sakharuk Exp $
+# $Id: londefdef.pm,v 1.204 2004/03/31 05:24:00 albertel Exp $
 # 
 #
 # Copyright Michigan State University Board of Trustees
@@ -117,11 +117,8 @@ sub start_m {
 sub end_m {
     my ($target,$token) = @_;
     my $currentstring = '';
-    if ($target eq 'web') {
-	$Apache::lonxml::prevent_entity_encode--;
-    } elsif ($target eq 'tex') {
+    if ($target eq 'tex') {
 	$currentstring = "";
-    } elsif ($target eq 'meta') {
     }
     return $currentstring;
 }
@@ -152,7 +149,7 @@ sub start_html {
     my ($target,$token) = @_;
     my $currentstring = '';
     my $options=$ENV{'course.'.$ENV{'request.course.id'}.'.tthoptions'};
-    &init_tth();
+    &Apache::lontexconvert::init_tth();
     if ($target eq 'web' || $target eq 'edit') {
 	$currentstring = &Apache::lonxml::xmlbegin().
 	    &Apache::lonxml::fontsettings();     
@@ -393,7 +390,7 @@ sub start_meta {
             my $display=&Apache::lonxml::get_param
 		('display',$parstack,$safeeval,undef,1);
             if ($display) {
-		$display=&HTML::Entities::encode($display);
+		$display=&HTML::Entities::encode($display,'<>&"');
 		$currentstring.='<'.$name.'.display>'.$display.
                                '</'.$name.'.display>';
             }
@@ -535,12 +532,13 @@ sub start_body {
 	if ($ENV{'request.state'} ne 'published') {
 	    $currentstring.=(<<EDITBUTTON);
 		<form method="post">
-		<input type="submit" name="editmode" value="Edit" />
+		<input type="submit" name="editmode" accesskey="e" value="Edit" />
 		</form>
 EDITBUTTON
 	} else {
 	    $currentstring.=&Apache::lonmenu::menubuttons(undef,$target,1);
 	}
+	$currentstring.=&Apache::lonxml::message_location();
     } elsif ($target eq 'tex') {
 	$currentstring = '\begin{document}';  
     } 
@@ -635,7 +633,7 @@ sub start_h1 {
 	$currentstring .= $token->[4];
     } elsif ($target eq 'tex') {
 	my $pre;
-	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
+	my $align=lc(&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1));
 	if (($align eq 'center') || (not defined $align)) {
 	    $pre='\begin{center}';
 	} elsif ($align eq 'left') {
@@ -1099,24 +1097,45 @@ sub end_q {
 }
 
 #-- <p> tag (end tag optional)
+#optional attribute - align="center|left|right"
 sub start_p {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = '';
     if ($target eq 'web') {
 	$currentstring .= $token->[4];
     } elsif ($target eq 'tex') {
-	my $signal=1;
-	foreach my $tag (@$tagstack) {if (lc($tag) eq 'b') {$signal=0;}}
-	if ($signal) {$currentstring .= '\par ';}
+	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
+	if ($align eq 'center') {
+	    $currentstring='\begin{center}\par';
+	} elsif ($align eq 'right') {
+	    $currentstring='\makebox['.$ENV{'form.textwidth'}.']{\hfill\llap{';
+	} elsif ($align eq 'left') {
+	    $currentstring='\noindent\makebox['.$ENV{'form.textwidth'}.']{\rlap{';
+	}
+	my $signal=1;#<p> does not work inside <b>...</b> 
+	foreach my $tag (@$tagstack) {if (lc($tag) eq 'b') {$signal=0;}
+	if (!$signal) {$currentstring = '';}
+	}
     }
     return $currentstring;
 }
 
 sub end_p {
-    my ($target,$token) = @_;
+    my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = '';
     if ($target eq 'web') {
 	$currentstring .= $token->[2];
+    } elsif ($target eq 'tex') {
+	if ($$tagstack[-1] eq 'p') {
+	    my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
+	    if ($align eq 'center') {		
+		$currentstring .= '\end{center}';
+	    } elsif ($align eq 'right') {
+		$currentstring .= '}}';
+	    } elsif ($align eq 'left') {
+		$currentstring .= '}\hfill}';
+	    }
+	}
     }
     return $currentstring;
 }
@@ -1225,9 +1244,7 @@ sub start_font {
     my $currentstring = '';
     if ($target eq 'web') {
 	my $face=&Apache::lonxml::get_param('face',$parstack,$safeeval);
-	if ($face=~/symbol/i) {
-	    $Apache::lonxml::prevent_entity_encode++;
-	} else {
+	if ($face!~/symbol/i) {
 	    if (($ENV{'browser.fontenhance'} eq 'on') || 
 		($ENV{'browser.blackwhite'} eq 'on')) { return ''; }
 	}
@@ -1245,8 +1262,6 @@ sub end_font {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
     my $currentstring = '';
     if ($target eq 'web') {
-	my $face=&Apache::lonxml::get_param('face',$parstack,$safeeval);
-	if ($face=~/symbol/i) {$Apache::lonxml::prevent_entity_encode--;}
 	$currentstring = $token->[2];    
     }  elsif ($target eq 'tex') {
 	my $fontsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval);
@@ -1316,7 +1331,7 @@ sub start_sub {
     if ($target eq 'web') {
 	$currentstring .= $token->[4];
     } elsif ($target eq 'tex') {
-	$currentstring .= "\$_{";
+	$currentstring .= '\ensuremath{_{';
     } 
     return $currentstring;
 }
@@ -1327,7 +1342,7 @@ sub end_sub {
     if ($target eq 'web') {
 	$currentstring .= $token->[2];
     } elsif ($target eq 'tex') {
-	$currentstring .= "}\$";
+	$currentstring .= '}}';
     }
     return $currentstring;
 }
@@ -1339,7 +1354,7 @@ sub start_sup {
     if ($target eq 'web') {
 	$currentstring .= $token->[4];
     } elsif ($target eq 'tex') {
-	$currentstring .= "\$^{";
+	$currentstring .= '\ensuremath{^{';
     } 
     return $currentstring;
 }
@@ -1350,7 +1365,7 @@ sub end_sup {
     if ($target eq 'web') {
 	$currentstring .= $token->[2];
     } elsif ($target eq 'tex') {
-	$currentstring .= "}\$";
+	$currentstring .= '}}';
     }
     return $currentstring;
 }
@@ -1773,13 +1788,14 @@ sub start_table {
 	if (not defined $TeXwidth) {
 	    my $htmlwidth = &Apache::lonxml::get_param('width',$parstack,$safeeval,undef,1);
 	    if ($htmlwidth=~/%/) {
+                $Apache::londefdef::table[-1]{'percent'}=1;
 		$htmlwidth=~/(\d+)/;
-		my $value=$1*$textwidth/100;
-		$Apache::londefdef::table[-1]{'width'}=$value;
+		$Apache::londefdef::table[-1]{'width'}=$1*$textwidth/100;;
 	    } else {
 		$Apache::londefdef::table[-1]{'width'}=$textwidth;
 	    }
 	} elsif ($TeXwidth=~/%/) {
+	    $Apache::londefdef::table[-1]{'percent'}=1;
 	    $TeXwidth=~/(\d+)/;
 	    my $value=$1*$textwidth/100;
             $Apache::londefdef::table[-1]{'width'}=$value;
@@ -1859,6 +1875,17 @@ sub end_table {
 		$length_row_final[$jn]=0.9*$available_length/$needed;
 	    }
 	}
+        #recalculation for the use of all available width if width is defined in %
+        if ($Apache::londefdef::table[-1]{'percent'}==1) {
+	    my $current=0; 
+	    for (my $i=0;$i<=$#length_row_final;$i++) {  
+		$current+=$length_row_final[$i];
+	    }
+	    my $coef=$Apache::londefdef::table[-1]{'width'}/$current;
+	    for (my $i=0;$i<=$#length_row_final;$i++) {  
+		$length_row_final[$i]*=$coef;
+	    }
+	}
 	#fill the table
 	for (my $in=0;$in<=$Apache::londefdef::table[-1]{'row_number'};$in++) {
 	    for (my $jn=0;$jn<=$#length_row_final;$jn++) {
@@ -2085,8 +2112,6 @@ sub start_img {
     }
     $Apache::lonxml::extlinks[$#Apache::lonxml::extlinks+1]=$src;
     my $currentstring = '';
-    my $width_param = '';
-    my $height_param = '';
     my $scaling = .3;
     if ($target eq 'web') {
 	if ($ENV{'browser.imagesuppress'} ne 'on') {
@@ -2100,42 +2125,13 @@ sub start_img {
 	    }
 	    $currentstring.='[IMAGE: '.$alttag.']';
 	}
-	$src=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$src);
-	&image_replication($src);
     } elsif ($target eq 'tex') {
 	$src=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$src);
-	&image_replication($src);
 	#if original gif/jpg/png file exist do following:
 	if (-e $src) {
-	    #defines the default size of image
-	    my $image = Image::Magick->new;
-	    my $current_figure = $image->Read($src);
-	    $width_param = $image->Get('width') * $scaling;;
-	    $height_param = $image->Get('height') * $scaling;;
-	    undef $image;
-	    #do we have any specified size of the picture?
-	    my $TeXwidth = &Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval);
-	    my $TeXheight = &Apache::lonxml::get_param('TeXheight',$parstack,$safeeval);
-	    my $width = &Apache::lonxml::get_param('width',$parstack,$safeeval,
-						   undef,1);
-	    if ($TeXwidth ne '') {  
-		if ($TeXwidth=~/(\d+)\s*\%/) {
-		    $width_param = $1*$ENV{'form.textwidth'}/100;
-		} else { 
-		    $width_param = $TeXwidth;
-		}
-	    } elsif ($TeXheight ne '') {
-		$width_param = $TeXheight/$height_param*$width_param;
-	    } elsif ($width ne '') {
-		$width_param = $width*$scaling;      
-	    }
-	    if ($width_param > $ENV{'form.textwidth'}) {$width_param =0.95*$ENV{'form.textwidth'}}
-	    my $file;
-	    my $path;	
-	    if ($src =~ m!(.*)/([^/]*)$!) {
-		$file = $2; 
-		$path = $1.'/'; 
-	    } 
+	    #what is the image size?
+	    my $width_param=&image_size($src,$scaling,$parstack,$safeeval);
+            my ($file,$path)=&file_path($src); 
 	    my $newsrc = $src;
 	    $newsrc =~ s/\.(gif|jpg|png)$/.eps/i;
 	    $file=~s/\.(gif|jpg|png)$/.eps/i;
@@ -3340,6 +3336,35 @@ sub image_replication {
     return '';
 }
 
+sub image_size {
+    my ($src,$scaling,$parstack,$safeeval)=@_;
+    #size of image from gif/jpg/jpeg/png 
+    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;;
+    undef $image;
+    #do we have any specified LaTeX size of the picture?
+    my $TeXwidth = &Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval);
+    my $TeXheight = &Apache::lonxml::get_param('TeXheight',$parstack,$safeeval);
+    #do we have any specified web size of the picture?
+    my $width = &Apache::lonxml::get_param('width',$parstack,$safeeval,
+					   undef,1);
+    if ($TeXwidth ne '') {  
+	if ($TeXwidth=~/(\d+)\s*\%/) {
+	    $width_param = $1*$ENV{'form.textwidth'}/100;
+	} else { 
+	    $width_param = $TeXwidth;
+	}
+    } elsif ($TeXheight ne '') {
+	$width_param = $TeXheight/$height_param*$width_param;
+    } elsif ($width ne '') {
+	$width_param = $width*$scaling;      
+    }
+    if ($width_param > $ENV{'form.textwidth'}) {$width_param =0.95*$ENV{'form.textwidth'}}
+    return $width_param;
+}
+
 sub eps_generation {
     my ($src,$file,$width_param) = @_;	     
     my $filename = "/home/httpd/prtspool/$ENV{'user.name'}_$ENV{'user.domain'}_printout.dat";
@@ -3354,6 +3379,16 @@ sub eps_generation {
     return ' \graphicspath{{/home/httpd/prtspool'.$newsrc.'}}\includegraphics[width='.$width_param.' mm]{'.$file.'} ';
 }
 
+sub file_path {     
+    my $src=shift;
+    my ($file,$path); 
+    if ($src =~ m!(.*)/([^/]*)$!) {
+	$file = $2; 
+	$path = $1.'/'; 
+    } 
+    return $file,$path;
+}
+
 sub recalc {
     my $argument = shift;
     if (not $argument=~/(mm|cm|in|pc|pt)/) {return $argument.' mm';}