--- loncom/xml/londefdef.pm	2006/02/04 22:11:42	1.318
+++ loncom/xml/londefdef.pm	2006/03/23 23:47:12	1.324
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Tags Default Definition Module 
 #
-# $Id: londefdef.pm,v 1.318 2006/02/04 22:11:42 foxr Exp $
+# $Id: londefdef.pm,v 1.324 2006/03/23 23:47:12 albertel Exp $
 # 
 #
 # Copyright Michigan State University Board of Trustees
@@ -211,7 +211,7 @@ sub start_html {
 sub end_html {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = '';
-    if ($target eq 'web') {
+    if ($target eq 'web' || $target eq 'webgrade') {
 	$currentstring = '</html>';
     }
     return $currentstring;
@@ -221,7 +221,7 @@ sub end_html {
 sub start_head {
     my ($target,$token) = @_;
     my $currentstring = '';
-    if ($target eq 'web') {
+    if ($target eq 'web' || $target eq 'webgrade') {
 	$currentstring = $token->[4].&Apache::lonxml::fontsettings();
     } 
     return $currentstring;
@@ -230,7 +230,8 @@ sub start_head {
 sub end_head {
     my ($target,$token) = @_;
     my $currentstring = '';
-    if ($target eq 'web' && $env{'request.state'} eq 'published') {
+    if (($target eq 'web'      && $env{'request.state'} eq 'published') ||
+	($target eq 'webgrade' && $env{'request.state'} eq 'published')) {
 	$currentstring = &Apache::lonmenu::registerurl(undef,$target).
 	    $token->[2];    
     } 
@@ -366,7 +367,7 @@ sub end_form {
 sub start_title {
     my ($target,$token) = @_;
     my $currentstring = '';
-    if ($target eq 'web') {
+    if ($target eq 'web' || $target eq 'webgrade') {
 	$currentstring = $token->[4];     
     } elsif ($target eq 'tex') {
 	$currentstring .= '\keephidden{Title of the document:  ' 
@@ -381,7 +382,7 @@ sub start_title {
 sub end_title {
     my ($target,$token) = @_;
     my $currentstring = '';
-    if ($target eq 'web') {
+    if ($target eq 'web' || $target eq 'webgrade') {
 	$currentstring = $token->[2];    
     } elsif ($target eq 'tex') {
 	$currentstring .= '}';
@@ -512,7 +513,7 @@ sub start_body {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = '';
 
-    if ($target eq 'web') {
+    if ($target eq 'web' || $target eq 'webgrade') {
 	if ($Apache::lonhomework::parsing_a_problem) {
 	    &Apache::lonxml::warning("<body> tag found inside of <problem> tag this can cause problems.");
 	    return '';
@@ -596,7 +597,7 @@ EDITBUTTON
 sub end_body {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = &end_p();	# Close off unclosed <p>
-    if ($target eq 'web') {
+    if ($target eq 'web' || $target eq 'webgrade') {
 	$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}';  
@@ -1204,11 +1205,13 @@ sub start_p {
 		$currentstring = &center_correction().$currentstring;
 	    }
 	} elsif ($align eq 'right') {
-	    $currentstring.='\makebox['.$env{'form.textwidth'}.']{\hfill\llap{';
-	    $closing_string= '}}';
+	    $currentstring.="\n".'{\flushright ';
+#	    $currentstring.='\makebox['.$env{'form.textwidth'}.']{\hfill\llap{';
+	    $closing_string= "}\n";
 	} elsif ($align eq 'left') {
-	    $currentstring.='\noindent\makebox['.$env{'form.textwidth'}.']{\rlap{';
-	    $closing_string = '}\hfill}';
+	    $currentstring.= "\n".'{\flushleft ';
+#	    $currentstring.='\noindent\makebox['.$env{'form.textwidth'}.']{{';
+	    $closing_string = "}\n";
 	} else {
             $currentstring.='\par ';
 	    if (&is_inside_of($tagstack, 'table')) {
@@ -2039,7 +2042,6 @@ sub end_table {
 	my $inmemory = '';
 	my $output = '';
 	my $WARNING='';
-	# &debug_dump_table($Apache::londefdef::table[-1]);
         #width of columns from TeXwidth attributes
 
 	for (my $in=0;$in<=$Apache::londefdef::table[-1]{'row_number'};$in++) {
@@ -2226,18 +2228,26 @@ sub end_table {
 	for (my $in=0;$in<=$Apache::londefdef::table[-1]{'row_number'};$in++) {
 	    my $have_rowspan = 0;
 	    for (my $jn=0;$jn<=$#fwidth;$jn++) {
+
+		#-----------------------------------------------------------
+                #   I think this order of doing things will ensure that
+		#   single rowspan, columspan and combined row/colspans will
+                #   work correctly.  LaTeX is delicate here.
+		#    RF.
+
+		# Start a rowspan if necessary:
+
+		my $rowspan = $Apache::londefdef::table[-1]{'rowspan'}[$in][$jn];
+		my $colspan = $Apache::londefdef::table[-1]{'colspan'}[$in][$jn];
 		#
 		#  Do the appropriate magic if this has a colspan
 		# 
-		my $colspan = $Apache::londefdef::table[-1]{'colspan'}[$in][$jn];
+
 		if ($colspan > 1) {
 		    $output .= '\multicolumn{'.
 			$colspan
 			.'}{|l|}{';
 		}
-		my $rowspan = $Apache::londefdef::table[-1]{'rowspan'}[$in][$jn];
-
-		# Start a rowspan if necessary:
 
 		if ($rowspan > 1) {
 		    $have_rowspan++;
@@ -2246,6 +2256,7 @@ sub end_table {
 		if (($rowspan eq '^') || ($rowspan eq '_')) {
 		    $have_rowspan++;
 		}
+		#--------------------------------------------------------------
 
 		if ($Apache::londefdef::table[-1]{'align'}[$in][$jn] eq 'c') {
 		    $output.=&center_correction().'\begin{center}';
@@ -2280,9 +2291,11 @@ sub end_table {
 		$output .= ' \\\\ ';
 		for (my $jn=0; $jn<=$#fwidth;$jn++) {
 		    my $rowspan = $Apache::londefdef::table[-1]{'rowspan'}[$in][$jn];
-		    if (($rowspan <= 1) || ($rowspan eq '_')) {
-			my $column = $jn+1;
-			$output .= '\cline{'.$column.'-'.$column.'} ';
+		    if ($rowspan ne "^") {
+			if (($rowspan <= 1) || ($rowspan eq '_')) {
+			    my $column = $jn+1;
+			    $output .= '\cline{'.$column.'-'.$column.'} ';
+			}
 		    }
 		}
 
@@ -2452,6 +2465,13 @@ sub end_td_tex {
     $Apache::londefdef::table[-1]{'counter_columns'} = $current_column;
    
 
+    # Get the column and row spans.
+    # Colspan can be done via \multicolumn if I can figure out the data structs.
+
+    my $colspan = &Apache::lonxml::get_param('colspan', $parstack, $safeeval, undef, 0);
+    if (!$colspan) {
+	$colspan = 1;
+    }
 
     my $rowspan = &Apache::lonxml::get_param('rowspan', $parstack, $safeeval, undef, 0);
     if (!$rowspan) {
@@ -2460,11 +2480,13 @@ sub end_td_tex {
 
 
 
-    $Apache::londefdef::table[-1]{'rowspan'}[$current_row][$current_column] = $rowspan;
-    for (my $i = 1; $i < $rowspan; $i++) {
-	$Apache::londefdef::table[-1]{'rowspan'}[$current_row+$i][$current_column] = '^';
-	if ($i == ($rowspan-1)) {
-	    $Apache::londefdef::table[-1]{'rowspan'}[$current_row+$i][$current_column] = '_';
+    for (my $c = 0; $c < $colspan; $c++) {
+	$Apache::londefdef::table[-1]{'rowspan'}[$current_row][$current_column+$c] = $rowspan;
+	for (my $i = 1; $i < $rowspan; $i++) {
+	    $Apache::londefdef::table[-1]{'rowspan'}[$current_row+$i][$current_column+$c] = '^';
+	    if ($i == ($rowspan-1)) {
+		$Apache::londefdef::table[-1]{'rowspan'}[$current_row+$i][$current_column+$c] = '_';
+	    }
 	}
     }
 
@@ -2559,13 +2581,7 @@ sub end_td_tex {
 
     push @ {$Apache::londefdef::table[-1]{'content'}[-1] },$data;
 
-    # Get the column and row spans.
-    # Colspan can be done via \multicolumn if I can figure out the data structs.
 
-    my $colspan = &Apache::lonxml::get_param('colspan', $parstack, $safeeval, undef, 0);
-    if (!$colspan) {
-	$colspan = 1;
-    }
 
 
     #  the colspan array will indicate how many columns will be spanned by this
@@ -2574,14 +2590,16 @@ sub end_td_tex {
     #  counter_columns is incremented in the start_td_tex, we adjust by colspan-1.
     #
 
-    $Apache::londefdef::table[-1]{'colspan'}[$current_row][$current_column] = $colspan;
     $Apache::londefdef::table[-1]{'counter_columns'} += $colspan -1;
-
-    # Put empty text in spanned cols.
-
     for (my $i = 0; $i < ($colspan -1); $i++) {
 	push @ {$Apache::londefdef::table[-1]{'content'}[-1] },'';
     }
+    for (my $r = 0; $r < $rowspan; $r++) {
+	$Apache::londefdef::table[-1]{'colspan'}[$current_row+$r][$current_column] = $colspan;
+	# Put empty text in spanned cols.
+	
+    }
+
 
 
     return '';
@@ -2755,13 +2773,17 @@ sub start_img {
 
     if ($target eq 'web') {
 	if ($env{'browser.imagesuppress'} ne 'on') {
-	    $currentstring.=&Apache::lonenc::encrypt_ref($token,{'src'=>$src});
-	} else {
-	    my $alttag= &Apache::lonxml::get_param
-		('alt',$parstack,$safeeval,undef,1);
-	    unless ($alttag) {
-		$alttag=&Apache::lonmeta::alttag
-		    ($Apache::lonxml::pwd[-1],$src);
+	    my $enc = ('yes' eq 
+		       lc(&Apache::lonxml::get_param('encrypturl',$parstack,
+						     $safeeval)));
+	    $currentstring.=&Apache::lonenc::encrypt_ref($token,{'src'=>$src},
+							 $enc);
+	} else {
+	    my $alttag = &Apache::lonxml::get_param('alt',$parstack,$safeeval,
+						    undef,1);
+	    if (!$alttag) {
+		$alttag = &Apache::lonmeta::alttag($Apache::lonxml::pwd[-1],
+						   $src);
 	    }
 	    $currentstring.='[IMAGE: '.$alttag.']';
 	}
@@ -2805,6 +2827,7 @@ sub start_img {
 	if (($latex_rendering eq 'texwrap') && &is_inside_of($tagstack, "table")) {
 	    $latex_rendering = 'parpic';
 	}
+
 	# &Apache::lonxml::debug("LaTeX rendering = $latex_rendering image file: $src");
 
 	#if original gif/jpg/png file exist do following:
@@ -2887,6 +2910,8 @@ sub start_img {
 						   ['','bottom','middle','top','left','right'],$token,5);
 	$currentstring .=&Apache::edit::select_arg('TeXwrap:', 'TeXwrap',
 						   ['', 'parbox', 'parpic'], $token, 2);
+	$currentstring .=&Apache::edit::select_arg('Encyrpt URL:','encrypturl',
+						   ['no','yes'], $token, 2);
 	$currentstring .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
 	my $src=    &Apache::lonxml::get_param('src',$parstack,$safeeval);
 	my $alt=    &Apache::lonxml::get_param('alt',$parstack,$safeeval);
@@ -2904,7 +2929,7 @@ sub start_img {
 	my $ctag=&Apache::edit::get_new_args($token,$parstack,
 					     $safeeval,'src','alt','align',
 					     'TeXwidth','TeXheight', 'TeXwrap',
-					     'width','height');
+					     'width','height','encrypturl');
 	my ($nsrc,$nwidth,$nheight)=
 	    ($token->[2]{'src'},$token->[2]{'width'},$token->[2]{'height'});
 	my $loc=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$nsrc);
@@ -3179,7 +3204,7 @@ sub start_pre {
 	$currentstring .= $token->[4];
     } elsif ($target eq 'tex') {
 	$currentstring .= '\begin{verbatim}';
-	&Apache::lonxml::disable_LaTeX_substitutions;
+	&Apache::lonxml::disable_LaTeX_substitutions();
     } 
     return $currentstring;
 }
@@ -3191,7 +3216,7 @@ sub end_pre {
 	$currentstring .= $token->[2];
     } elsif ($target eq 'tex') {
 	$currentstring .= '\end{verbatim}';
-	&Apache::lonxml::enable_LaTeX_substitutions;
+	&Apache::lonxml::enable_LaTeX_substitutions();
     }
     return $currentstring;
 }
@@ -3678,7 +3703,7 @@ sub end_legend {
 sub start_link {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = '';
-    if ($target eq 'web') {
+    if ($target eq 'web' || $target eq 'webgrade') {
 	my $href=&Apache::lonxml::get_param('href',$parstack,$safeeval,
 					    undef,1);
 	&Apache::lonxml::extlink($href);