--- loncom/xml/londefdef.pm	2007/07/04 14:02:14	1.372
+++ loncom/xml/londefdef.pm	2008/05/28 01:32:59	1.389
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Tags Default Definition Module 
 #
-# $Id: londefdef.pm,v 1.372 2007/07/04 14:02:14 foxr Exp $
+# $Id: londefdef.pm,v 1.389 2008/05/28 01:32:59 www Exp $
 # 
 #
 # Copyright Michigan State University Board of Trustees
@@ -46,6 +46,7 @@ use Apache::File();
 use Image::Magick;
 use Apache::lonmenu();
 use Apache::lonmeta();
+use Apache::lonlocal;
 use Apache::Constants qw(:common);
 use File::Basename;
 use LONCAPA();
@@ -200,6 +201,11 @@ sub start_html {
                           '\usepackage[dvips]{graphicx}'.
 			  '\usepackage{wrapfig}'.
 			  '\usepackage{picins}'.
+			  '\usepackage[T1]{fontenc}'."\n".
+			  '\usepackage[postscript]{ucs}'."\n".
+			  '\usepackage[utf8x]{inputenc}'."\n".
+			  '\usepackage{pifont}' ."\n".
+			  '\usepackage{latexsym}'."\n".
                           '\usepackage{epsfig}'.
                           '\usepackage{calc}'.
                           '\usepackage{amsmath}'.
@@ -448,6 +454,16 @@ sub start_meta {
 	if ((not defined $content) && (not defined $name)) {
 	    &Apache::lonxml::startredirection();
 	}
+    } elsif ($target eq 'edit') {
+	$currentstring .= &Apache::edit::tag_start($target,$token);
+	$currentstring .= &Apache::edit::text_arg('Name:','name',$token,30);
+	$currentstring .= &Apache::edit::text_arg('Content:','content',$token,70);
+	$currentstring .= &Apache::edit::end_row();
+    } elsif ($target eq 'modified') {
+	my $constructtag =
+	    &Apache::edit::get_new_args($token,$parstack,$safeeval,
+					'name','content');
+	if ($constructtag) { $currentstring = &Apache::edit::rebuild_tag($token); }
     }
     return $currentstring;
 }
@@ -471,29 +487,36 @@ sub end_meta {
     return $currentstring;
 }
 
+sub insert_meta {
+    return '
+    <meta />';
+}
+
 # accessrule
 sub start_accessrule {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
     my $currentstring = '';
-    my $eff=&Apache::lonxml::get_param
-	('effect',$parstack,$safeeval,undef,1);
-    my $realm=&Apache::lonxml::get_param
-	('realm',$parstack,$safeeval,undef,1);
-    my $role=&Apache::lonxml::get_param
-	('role',$parstack,$safeeval,undef,1);
-    my ($dom,$crs,$sec)=split(/\_/,$realm);
-    $dom = &LONCAPA::clean_domain($dom);
-    my $type=&Apache::lonxml::get_param
-	('type',$parstack,$safeeval,undef,1);
+    my $eff  =&Apache::lonxml::get_param('effect',$parstack,$safeeval,undef,1);
+    my $realm=&Apache::lonxml::get_param('realm', $parstack,$safeeval,undef,1);
+    my $role =&Apache::lonxml::get_param('role',  $parstack,$safeeval,undef,1);
+    my $type =&Apache::lonxml::get_param('type',  $parstack,$safeeval,undef,1);
+
+    my ($dom,$crs,$sec,$separator);
     if ($type eq 'user') {
+	($dom,$crs,$sec)=split(m{/},$realm);
 	$crs = &LONCAPA::clean_username($crs);
+	$separator = '/';
     } else {
+	($dom,$crs,$sec)=split(/\_/,$realm);
 	$crs = &LONCAPA::clean_courseid($crs);
+	$separator = '_';
     }
+    $dom = &LONCAPA::clean_domain($dom);
+
     $sec =~s/\W//;
     $realm = $dom;
-    if ($crs =~ /\S/) { $realm .= '_'.$crs; }
-    if ($sec =~ /\S/) { $realm .= '_'.$sec; }
+    if ($crs =~ /\S/) { $realm .= $separator.$crs; }
+    if ($sec =~ /\S/) { $realm .= $separator.$sec; }
     $role=~s/\W//g;
 
     if ($target eq 'web') {
@@ -562,12 +585,7 @@ sub start_body {
 
 	if ($env{'request.state'} ne 'published') {
 	    $currentstring.=&Apache::lonmenu::constspaceform();
-	    $currentstring.=(<<EDITBUTTON);
-<form method="post">
-<input type="submit" name="editmode" accesskey="e" value="Edit" />
-</form>
-<br />
-EDITBUTTON
+	    $currentstring.=&Apache::londefdef::edit_controls();
 	}
 	$currentstring.=&Apache::lonxml::message_location();
     } elsif ($target eq 'tex') {
@@ -576,6 +594,20 @@ EDITBUTTON
     return $currentstring;
 }
 
+sub edit_controls {
+    my $result .= '
+<form method="post">
+<div class="LC_edit_problem_header">
+<div class="LC_edit_problem_header_row1">'.
+&Apache::lonxml::renderingoptions().'
+<input type="submit" name="changeproblemmode" value="'.&mt('Change View').'" />
+</div>
+<div class="LC_edit_problem_header_edit_row"><input type="submit" name="editmode" accesskey="e" value="Edit" /></div></div>
+</form>
+<br />';
+    return $result;
+}
+
 sub end_body {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = &end_p();	# Close off unclosed <p>
@@ -1251,9 +1283,7 @@ sub start_br {
 		$signal = 1;
 	    }
 	}
-	if ($signal eq 1) {
-	    $currentstring .= ' \vskip 0 mm ';
-	} else {
+	if ($signal != 1) {
 	    $currentstring .= '\strut \\\\ \strut ';
 	}
     
@@ -1565,6 +1595,9 @@ sub start_div {
     } 
     if ($target eq 'tex') {
 	# 4 possible alignments: left, right, center, and -missing-.
+        # If inside a table row, we must let the table logic
+	# do the alignment, however.
+	# 
 
 	my $endstring = '';
 
@@ -1575,6 +1608,7 @@ sub start_div {
 	    $endstring      = '\end{center}';
 	    if (&is_inside_of($tagstack, "table")) {
 		$currentstring = &center_correction().$currentstring;
+		$endstring    .= &center_end_correction(); 
 	    }
 	}
 	elsif ($align eq 'right') {
@@ -1630,14 +1664,18 @@ sub end_a {
 	    &Apache::lonxml::get_param('href',$parstack,$safeeval,undef,1);
 	my $name =
 	    &Apache::lonxml::get_param('name',$parstack,$safeeval,undef,1);
-	if ($href =~ /\S/) {
+        my $uriprint =
+            &Apache::lonxml::get_param('uriprint',$parstack,$safeeval,undef,1);
+        my $anchorprint =
+            &Apache::lonxml::get_param('anchorprint',$parstack,$safeeval,undef,1);
+	if (($href =~ /\S/) && ($uriprint=~/^on|uriprint|yes|1$/i)) {
 	    $href =~ s/([^\\])%/$1\\\%/g;
 	    # Substitute special symbols... and allow line breaks at each /
 	    #
 	    $href = &Apache::lonxml::latex_special_symbols($href);
 	    $href =~ s/\//\/\\-/g;              # Map / to /\- to allow hyphenation.
 	    $currentstring .= ' ({\tt URI:'.$href.'})';
-	} elsif ($name =~ /\S/) {
+	} elsif (($name =~ /\S/) && ($anchorprint=~/^on|anchorprint|yes|1$/i)) {
 	    $currentstring .= ' ({\tt Anchor:'.&Apache::lonxml::latex_special_symbols($name).'})';
 	} else {
 	    $currentstring.='';
@@ -2072,6 +2110,10 @@ sub end_table {
 	my $WARNING='';
         #width of columns from TeXwidth attributes
 
+	# Protect against unbalanced </table> tag.
+
+	if (scalar(@Apache::londefdef::table) > 0) {
+
 	for (my $in=0;$in<=$Apache::londefdef::table[-1]{'row_number'};$in++) {
 	    for (my $jn=0;$jn<=$Apache::londefdef::table[-1]{'counter_columns'};$jn++) {
 		if ($Apache::londefdef::table[-1]{'TeXlen'}[0][$jn]<$Apache::londefdef::table[-1]{'TeXlen'}[$in][$jn]) {
@@ -2475,6 +2517,7 @@ sub end_table {
 	    undef @Apache::londefdef::table;
 	}
     }
+    }
     return $currentstring;
 }
 
@@ -3039,11 +3082,14 @@ sub start_img {
 	my $width=  &Apache::lonxml::get_param('width',$parstack,$safeeval);
 	my $height= &Apache::lonxml::get_param('height',$parstack,$safeeval);
 
-
-	$currentstring .= '<img src="'.$src.'" alt="'.$alt.'" ';
-	if ($width) { $currentstring.=' width="'.$width.'" '; }
-	if ($height) { $currentstring.=' height="'.$height.'" '; }
-	$currentstring .= ' />';
+        if ($token->[2]{'src'}=~/\$/) {
+           $currentstring.='Variable image source';
+        } else {
+	   $currentstring .= '<img src="'.$src.'" alt="'.$alt.'" ';
+	   if ($width) { $currentstring.=' width="'.$width.'" '; }
+	   if ($height) { $currentstring.=' height="'.$height.'" '; }
+	   $currentstring .= ' />';
+        }
     } elsif ($target eq 'modified') {
 	my ($osrc,$owidth,$oheight)=
 	    ($token->[2]{'src'},$token->[2]{'width'},$token->[2]{'height'});
@@ -4523,6 +4569,12 @@ sub align_latex_image {
     my ($align, $latex_rendering, $image, $width, $height) = @_;
     my $currentstring;        # The 1/2 wrapped image.
     my $closure;              # The closure of the wrappage.
+
+    # if it's none just return it back
+    if ($latex_rendering eq 'none') {
+	return ($image,'');
+    }
+
     #    If there's an alignment specification we need to honor it here.
     #    For the horizontal alignments, we will also honor the
     #    value of the latex specfication.  The default is parbox,