--- loncom/xml/lonxml.pm	2002/01/09 09:50:59	1.148
+++ loncom/xml/lonxml.pm	2002/02/21 04:18:59	1.153
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # XML Parser Module 
 #
-# $Id: lonxml.pm,v 1.148 2002/01/09 09:50:59 albertel Exp $
+# $Id: lonxml.pm,v 1.153 2002/02/21 04:18:59 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -463,7 +463,7 @@ sub xmlparse {
 
  ($target, my @tenta) = split('&&',$target);
 
- my @stack = (); 
+ my @stack = ();
  my @parstack = ();
  &initdepth;
 
@@ -533,8 +533,14 @@ sub inner_xmlparse {
       } elsif ($token->[0] eq 'E') {
 	#clear out any tags that didn't end
 	while ($token->[1] ne $$stack['-1'] && ($#$stack > -1)) {
-	  &Apache::lonxml::warning('Missing tag </'.$$stack['-1'].'> in file');
-	  &end_tag($stack,$parstack,$token);
+	  my $lasttag=$$stack[-1];
+	  if ($token->[1] =~ /^$lasttag$/i) {
+	    &Apache::lonxml::warning('Using tag </'.$token->[1].'> as end tag to <'.$$stack[-1].'>');
+	    last;
+	  } else {
+	    &Apache::lonxml::warning('Found tag </'.$token->[1].'> when looking for </'.$$stack[-1].'> in file');
+	    &end_tag($stack,$parstack,$token);
+	  }
 	}
 
 	if (exists($$style_for_target{'/'."$token->[1]"})) {
@@ -613,10 +619,16 @@ sub recurse {
 			       $safeeval, $style_for_target);
       } elsif ($tokenpat->[0] eq 'E') {
 	#clear out any tags that didn't end
-	while ($tokenpat->[1] ne $innerstack[$#innerstack] 
+	while ($tokenpat->[1] ne $innerstack[$#innerstack]
 	       && ($#innerstack > -1)) {
-	  &Apache::lonxml::warning('Missing tag </'.$innerstack['-1'].'> in style');
-	  &end_tag(\@innerstack,\@innerparstack,$tokenpat);
+	  my $lasttag=$innerstack[-1];
+	  if ($tokenpat->[1] =~ /^$lasttag$/i) {
+	    &Apache::lonxml::warning('Using tag </'.$tokenpat->[1].'> as end tag to <'.$innerstack[-1].'>');
+	    last;
+	  } else {
+	    &Apache::lonxml::warning('Found tag </'.$tokenpat->[1].'> when looking for </'.$innerstack[-1].'> in file');
+	    &end_tag(\@innerstack,\@innerparstack,$tokenpat);
+	  }
 	}
 	$partstring = &callsub("end_$tokenpat->[1]", $target, $tokenpat,
 			       \@innerstack, \@innerparstack, \@pat,
@@ -677,13 +689,13 @@ sub callsub {
     }
     if (!$deleted) {
       if ($space) {
-	#&Apache::lonxml::debug("Calling sub $sub in $space $metamode<br />\n");
+	#&Apache::lonxml::debug("Calling sub $sub in $space $metamode");
 	$sub1="$space\:\:$sub";
 	($currentstring,$nodefault) = &$sub1($target,$token,$tagstack,
 					     $parstack,$parser,$safeeval,
 					     $style);
       } else {
-	#&Apache::lonxml::debug("NOT Calling sub $sub in $space $metamode<br />\n");
+	#&Apache::lonxml::debug("NOT Calling sub $sub in $space $metamode");
 	if ($metamode <1) {
 	  if (defined($token->[4]) && ($metamode < 1)) {
 	    $currentstring = $token->[4];
@@ -885,10 +897,10 @@ sub get_all_text {
      } elsif ($token->[0] eq 'PI') {
        $result.=$token->[2];
      } elsif ($token->[0] eq 'S') {
-       if ($token->[1] eq $tag) { $depth++; }
+       if ($token->[1] =~ /^$tag$/i) { $depth++; }
        $result.=$token->[4];
      } elsif ($token->[0] eq 'E')  {
-       if ( $token->[1] eq $tag) { $depth--; }
+       if ( $token->[1] =~ /^$tag$/i) { $depth--; }
        #skip sending back the last end tag
        if ($depth > -1) { $result.=$token->[2]; } else {
 	 $pars->unget_token($token);
@@ -903,7 +915,7 @@ sub get_all_text {
      } elsif ($token->[0] eq 'PI') {
        $result.=$token->[2];
      } elsif ($token->[0] eq 'S') {
-       if ( $token->[1] eq $tag) { 
+       if ( $token->[1] =~ /^$tag$/i) {
 	 $pars->unget_token($token); last;
        } else {
 	 $result.=$token->[4];
@@ -936,7 +948,7 @@ sub parstring {
   foreach (@{$token->[3]}) {
     unless ($_=~/\W/) {
       my $val=$token->[2]->{$_};
-      $val =~ s/([\%\@\\])/\\$1/g;
+      $val =~ s/([\%\@\\\"])/\\$1/g;
       #if ($val =~ m/^[\%\@]/) { $val="\\".$val; }
       $temp .= "my \$$_=\"$val\";"
     }
@@ -1016,10 +1028,8 @@ sub storefile {
     }
 }
 
-sub inserteditinfo {
-      my ($result,$filecontents)=@_;
-      unless ($filecontents) {
-	  $filecontents=(<<SIMPLECONTENT);
+sub createnewhtml {
+  my $filecontents=(<<SIMPLECONTENT);
 <html>
 <head>
 <title>
@@ -1033,8 +1043,12 @@ sub inserteditinfo {
 </body>
 </html>
 SIMPLECONTENT
-      }
+  return $filecontents;
+}
+
 
+sub inserteditinfo {
+      my ($result,$filecontents)=@_;
       $filecontents =~ s:</textarea>:&lt;/textarea&gt;:ig;
 #      my $editheader='<a href="#editsection">Edit below</a><hr />';
       my $editfooter=(<<ENDFOOTER);
@@ -1055,21 +1069,37 @@ ENDFOOTER
       return $result;
 }
 
-sub editbutton {
-  my ($result) = @_;
-  my $button=(<<EDITBUTTON);
-<form method="post">
-<input type="submit" name="showmode" value="Edit" />
-</form>
-EDITBUTTON
-  $result=~s/(\<\/body\>)/$button/is;
-  return $result;
+sub get_target {
+  my $viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
+  if ( $ENV{'request.state'} eq 'published') {
+    if ( defined($ENV{'form.grade_target'})
+	 && ($viewgrades == 'F' )) {
+      return ($ENV{'form.grade_target'});
+    } elsif (defined($ENV{'form.grade_target'})) {
+      if (($ENV{'form.grade_target'} eq 'web') ||
+	  ($ENV{'form.grade_target'} eq 'tex') ) {
+	return $ENV{'form.grade_target'}
+      } else {
+	return 'web';
+      }
+    } else {
+      return 'web';
+    }
+  } elsif ($ENV{'request.state'} eq 'construct') {
+    if ( defined($ENV{'form.grade_target'})) {
+      return ($ENV{'form.grade_target'});
+    } else {
+      return 'web';
+    }
+  } else {
+    return 'web';
+  }
 }
 
 sub handler {
   my $request=shift;
 
-  my $target='web';
+  my $target=&get_target();
 
   $Apache::lonxml::debug=0;
 
@@ -1108,6 +1138,10 @@ sub handler {
 </html>
 ENDNOTFOUND
     $filecontents='';
+    if ($ENV{'request.state'} ne 'published') {
+      $filecontents=&createnewhtml();
+      $ENV{'form.showmode'}='Edit'; #force edit mode
+    }
   } else {
     unless ($ENV{'request.state'} eq 'published') {
       if ($ENV{'form.attemptclean'}) {
@@ -1126,8 +1160,6 @@ ENDNOTFOUND
     if ($ENV{'form.showmode'} eq 'Edit') {
       $result='<html><body bgcolor="#FFFFFF"></body></html>';
       $result=&inserteditinfo($result,$filecontents);
-    } else {
-      $result = &editbutton($result);
     }
   }