--- loncom/xml/lonxml.pm	2005/01/30 12:56:45	1.354
+++ loncom/xml/lonxml.pm	2005/04/01 18:08:14	1.369
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # XML Parser Module 
 #
-# $Id: lonxml.pm,v 1.354 2005/01/30 12:56:45 www Exp $
+# $Id: lonxml.pm,v 1.369 2005/04/01 18:08:14 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -149,37 +149,49 @@ $Apache::lonxml::post_evaluate=1;
 $Apache::lonxml::warnings_error_header='';
 
 sub xmlbegin {
-  my $output='';
-  @htmlareafields=();
-  if ($ENV{'browser.mathml'}) {
-      $output='<?xml version="1.0"?>'
-            .'<?xml-stylesheet type="text/css" href="/adm/MathML/mathml.css"?>'
-            .'<!DOCTYPE html SYSTEM "/adm/MathML/mathml.dtd" '
-            .'[<!ENTITY mathns "http://www.w3.org/1998/Math/MathML">]>'
+    my ($style)=@_;
+    my $output='';
+    @htmlareafields=();
+    if ($ENV{'browser.mathml'}) {
+	$output='<?xml version="1.0"?>'
+            #.'<?xml-stylesheet type="text/css" href="/adm/MathML/mathml.css"?>'."\n"
+#            .'<!DOCTYPE html SYSTEM "/adm/MathML/mathml.dtd" '
+            
+#	    .'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" [<!ENTITY mathns "http://www.w3.org/1998/Math/MathML">] >'
+	    .'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN" "http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg.dtd">'
             .'<html xmlns:math="http://www.w3.org/1998/Math/MathML" ' 
-		.'xmlns="http://www.w3.org/TR/REC-html40">';
-  } else {
-      $output='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-<html>';
-  }
-  return $output;
+	    .'xmlns="http://www.w3.org/1999/xhtml">';
+    } else {
+	$output='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>';
+    }
+    if ($style eq 'encode') {
+	$output=&HTML::Entities::encode($output,'<>&"');
+    }
+    return $output;
 }
 
 sub xmlend {
     my ($target,$parser)=@_;
     my $mode='xml';
     my $status='OPEN';
-    if ($Apache::lonhomework::parsing_a_problem) {
+    if ($Apache::lonhomework::parsing_a_problem ||
+	$Apache::lonhomework::parsing_a_task ) {
 	$mode='problem';
 	$status=$Apache::inputtags::status[-1]; 
     }
-    my $discussion=&Apache::lonfeedback::list_discussion($mode,$status);    
+    my $discussion;
+    &Apache::loncommon::get_unprocessed_cgi
+        ($ENV{'query_string'},['LONCAPA_INTERNAL_no_discussion']);
+    if (! exists($ENV{'form.LONCAPA_INTERNAL_no_discussion'}) ||
+        $ENV{'form.LONCAPA_INTERNAL_no_discussion'} ne 'true') {
+        $discussion=&Apache::lonfeedback::list_discussion($mode,$status);
+    }
     if ($target eq 'tex') {
 	$discussion.='<tex>\keephidden{ENDOFPROBLEM}\vskip 0.5mm\noindent\makebox[\textwidth/$number_of_columns][b]{\hrulefill}\end{document}</tex>';
 	&Apache::lonxml::newparser($parser,\$discussion,'');
 	return '';
     } else {
-	return $discussion.'</html>';
+	return $discussion.&Apache::loncommon::endbodytag();
     }
 }
 
@@ -284,7 +296,7 @@ sub printtokenheader {
     }
 }
 
-sub fontsettings() {
+sub fontsettings {
     my $headerstring='';
     if (($ENV{'browser.os'} eq 'mac') && (!$ENV{'browser.mathml'})) { 
 	$headerstring.=
@@ -353,8 +365,8 @@ sub xmlparse {
 
  my @stack = ();
  my @parstack = ();
- &initdepth;
-
+ &initdepth();
+ &init_alarm();
  my $finaloutput = &inner_xmlparse($target,\@stack,\@parstack,\@pars,
 				   $safeeval,\%style_for_target);
 
@@ -363,6 +375,9 @@ sub xmlparse {
  }
  &do_registered_ssi();
  if ($Apache::lonxml::counter_changed) { &store_counter() }
+ if ($ENV{'form.return_only_error_and_warning_counts'}) {
+     return "$errorcount:$warningcount";
+ }
  return $finaloutput;
 }
 
@@ -384,7 +399,8 @@ sub htmlclean {
 # Make standard tags lowercase
     foreach ('html','body','head','meta','h1','h2','h3','h4','b','i','m',
              'table','tr','td','th','p','br','hr','img','embed','font',
-             'a','strong','center','title','basefont') {
+             'a','strong','center','title','basefont','li','ol','ul',
+             'input','select','form','option','script','pre') {
 	$raw=~s/\<$_\s*\>/\<$_\>/gis;
         $raw=~s/\<\/$_\s*\>/<\/$_\>/gis;
         $raw=~s/\<$_\s([^\>]*)\>/<$_ $1\>/gis;
@@ -404,18 +420,17 @@ sub latex_special_symbols {
         $string=~s/([^\\])\#/$1\\\#/g;
     } else {
 	$string=~s/\\/\\ensuremath{\\backslash}/g;
-	$string=~s/([^\\]|^)\%/$1\\\%/g;
-	$string=~s/([^\\]|^)\$/$1\\\$/g;
-	$string=~s/([^\\])\_/$1\\_/g;
-	$string=~s/\$\$/\$\\\$/g;
-	$string=~s/\_\_/\_\\\_/g;
-	$string=~s/\#\#/\#\\\#/g;
+	$string=~s/\\\%|\%/\\\%/g;
+	$string=~s/\\{|{/\\{/g;
+	$string=~s/\\}|}/\\}/g;
+	$string=~s/\\\$|\$/\\\$/g;
+	$string=~s/\\\_|\_/\\\_/g;
         $string=~s/([^\\]|^)(\~|\^)/$1\\$2\\strut /g;
 	$string=~s/(>|<)/\\ensuremath\{$1\}/g; #more or less
 	$string=&Apache::lonprintout::character_chart($string);
 	# any & or # leftover should be safe to just escape
-        $string=~s/([^\\]|^)\&/$1\\\&/g;
-        $string=~s/([^\\]|^)\#/$1\\\#/g;
+	$string=~s/\\\&|\&/\\\&/g;
+	$string=~s/\\\#|\#/\\\#/g;
         $string=~s/\|/\$\\mid\$/g;
 #single { or } How to escape?
     }
@@ -685,6 +700,7 @@ sub init_safespace {
   $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
   $safehole->wrap(\&Apache::chemresponse::chem_standard_order,$safeeval,
 		  '&chem_standard_order');
+  $safehole->wrap(\&Apache::response::check_status,$safeeval,'&check_status');
 
   $safehole->wrap(\&Math::Cephes::asin,$safeeval,'&asin');
   $safehole->wrap(\&Math::Cephes::acos,$safeeval,'&acos');
@@ -786,6 +802,28 @@ sub default_homework_load {
     }
 }
 
+{
+    my $alarm_depth;
+    sub init_alarm {
+	alarm(0);
+	$alarm_depth=0;
+    }
+
+    sub start_alarm {
+	if ($alarm_depth<1) {
+	    my $old=alarm($Apache::lonnet::perlvar{'lonScriptTimeout'});
+	    if ($old) {
+		&Apache::lonxml::error("Cancelled an alarm of $old, this shouldn't occur.");
+	    }
+	}
+	$alarm_depth++;
+    }
+
+    sub end_alarm {
+	$alarm_depth--;
+	if ($alarm_depth<1) { alarm(0); }
+    }
+}
 my $metamode_was;
 sub startredirection {
     if (!$Apache::lonxml::redirection) {
@@ -1019,7 +1057,8 @@ sub get_all_text {
 sub newparser {
   my ($parser,$contentref,$dir) = @_;
   push (@$parser,HTML::LCParser->new($contentref));
-  $$parser['-1']->xml_mode('1');
+  $$parser[-1]->xml_mode(1);
+  $$parser[-1]->marked_sections(1);
   if ( $dir eq '' ) {
     push (@Apache::lonxml::pwd, $Apache::lonxml::pwd[$#Apache::lonxml::pwd]);
   } else {
@@ -1214,7 +1253,7 @@ $initialize
 $xml_help
 <input type="hidden" name="editmode" value="$lt{'ed'}" />
 $buttons<br />
-<textarea cols="80" rows="44" name="filecont" id="filecont">$filecontents</textarea>
+<textarea style="width:100%" cols="80" rows="44" name="filecont" id="filecont">$filecontents</textarea>
 <br />$buttons
 <br />
 </form>
@@ -1260,14 +1299,12 @@ sub handler {
     
     $Apache::lonxml::debug=$ENV{'user.debug'};
     
-    if ($ENV{'browser.mathml'}) {
-	&Apache::loncommon::content_type($request,'text/xml');
-    } else {
-	&Apache::loncommon::content_type($request,'text/html');
-    }
+    &Apache::loncommon::content_type($request,'text/html');
     &Apache::loncommon::no_cache($request);
-    $request->set_last_modified(&Apache::lonnet::metadata($request->uri,
-							  'lastrevisiondate'));
+    if ($ENV{'request.state'} eq 'published') {
+	$request->set_last_modified(&Apache::lonnet::metadata($request->uri,
+							      'lastrevisiondate'));
+    }
     $request->send_http_header;
     
     return OK if $request->header_only;
@@ -1335,6 +1372,7 @@ ENDNOTFOUND
 	if (!$ENV{'form.editmode'} || $ENV{'form.viewmode'}) {
 	    $result = &Apache::lonxml::xmlparse($request,$target,$filecontents,
 						'',%mystyle);
+	    undef($Apache::lonhomework::parsing_a_task);
 	}
     }