--- loncom/xml/lonxml.pm	2004/06/04 14:03:57	1.321
+++ loncom/xml/lonxml.pm	2004/07/13 19:12:46	1.330
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # XML Parser Module 
 #
-# $Id: lonxml.pm,v 1.321 2004/06/04 14:03:57 www Exp $
+# $Id: lonxml.pm,v 1.330 2004/07/13 19:12:46 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -150,6 +150,7 @@ $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"?>'
@@ -388,8 +389,10 @@ sub latex_special_symbols {
     } else {
 	$string=~s/\\/\\ensuremath{\\backslash}/g;
 	$string=~s/([^\\]|^)\%/$1\\\%/g;
-	$string=~s/([^\\]|^)(\$|_)/$1\\$2/g;
+	$string=~s/([^\\]|^)\$/$1\\\$/g;
+	$string=~s/([^\\])\_/$1\\_/g;
 	$string=~s/\$\$/\$\\\$/g;
+	$string=~s/\_\_/\_\\\_/g;
 	$string=~s/\#\#/\#\\\#/g;
         $string=~s/([^\\]|^)(\~|\^)/$1\\$2\\strut /g;
 	$string=~s/(>|<)/\\ensuremath\{$1\}/g; #more or less
@@ -604,6 +607,7 @@ sub setup_globals {
   my ($request,$target)=@_;
   $Apache::lonxml::request=$request;
   $Apache::lonxml::registered = 0;
+  @Apache::lonxml::htmlareafields=();
   $errorcount=0;
   $warningcount=0;
   $Apache::lonxml::default_homework_loaded=0;
@@ -662,7 +666,9 @@ sub init_safespace {
   $safehole->wrap(\&Apache::scripttag::xmlparse,$safeeval,'&xmlparse');
   $safehole->wrap(\&Apache::outputtags::multipart,$safeeval,'&multipart');
   $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
-  
+  $safehole->wrap(\&Apache::chemresponse::chem_standard_order,$safeeval,
+		  '&chem_standard_order');
+
   $safehole->wrap(\&Math::Cephes::asin,$safeeval,'&asin');
   $safehole->wrap(\&Math::Cephes::acos,$safeeval,'&acos');
   $safehole->wrap(\&Math::Cephes::atan,$safeeval,'&atan');
@@ -736,6 +742,7 @@ sub init_safespace {
   $safehole->wrap(\&Math::Random::random_set_seed,$safeeval,'&random_set_seed');
   $safehole->wrap(\&Apache::lonxml::error,$safeeval,'&LONCAPA_INTERNAL_ERROR');
   $safehole->wrap(\&Apache::lonxml::debug,$safeeval,'&LONCAPA_INTERNAL_DEBUG');
+  $safehole->wrap(\&Apache::caparesponse::get_sigrange,$safeeval,'&LONCAPA_INTERNAL_get_sigrange');
 
 #need to inspect this class of ops
 # $safeeval->deny(":base_orig");
@@ -747,27 +754,6 @@ sub init_safespace {
   &Apache::lonxml::debug("Setting rndseed to $rndseed");
   &Apache::run::run($safeinit,$safeeval);
 
-  my $subroutine=<<'EVALUATESUB';
-sub __LC_INTERNAL_EVALUATE__ {
-    my ($__LC__a,$__LC__b,$__LC__c)=@_;
-    my $__LC__prefix;
-    while(1){
-	{ 
-	    use strict;
-	    no strict "vars";
-	    if (eval(defined(eval($__LC__a.$__LC__b)))) {
-		return $__LC__prefix.eval($__LC__a.$__LC__b.$__LC__c);
-	    }
-	}
-	$__LC__prefix.=substr($__LC__a,0,1,"");
-	if ($__LC__a!~/^(\$|&|\#)/) { last; }
-    }
-    return $__LC__prefix.$__LC__a.$__LC__b.$__LC__c;
-}
-EVALUATESUB
-    $safeeval->permit("require");
-    $safeeval->reval($subroutine);
-    $safeeval->deny("require");
 }
 
 sub default_homework_load {
@@ -782,18 +768,26 @@ sub default_homework_load {
     }
 }
 
+my $metamode_was;
 sub startredirection {
-  $Apache::lonxml::redirection++;
-  push (@Apache::lonxml::outputstack, '');
+    if (!$Apache::lonxml::redirection) {
+	$metamode_was=$Apache::lonxml::metamode;
+    }
+    $Apache::lonxml::metamode=0;
+    $Apache::lonxml::redirection++;
+    push (@Apache::lonxml::outputstack, '');
 }
 
 sub endredirection {
-  if (!$Apache::lonxml::redirection) {
-    &Apache::lonxml::error("Endredirection was called, before a startredirection, perhaps you have unbalanced tags. Some debuging information:".join ":",caller);
-    return '';
-  }
-  $Apache::lonxml::redirection--;
-  pop @Apache::lonxml::outputstack;
+    if (!$Apache::lonxml::redirection) {
+	&Apache::lonxml::error("Endredirection was called, before a startredirection, perhaps you have unbalanced tags. Some debuging information:".join ":",caller);
+	return '';
+    }
+    $Apache::lonxml::redirection--;
+    if (!$Apache::lonxml::redirection) {
+	$Apache::lonxml::metamode=$metamode_was;
+    }
+    pop @Apache::lonxml::outputstack;
 }
 
 sub end_tag {
@@ -839,30 +833,30 @@ sub decreasedepth {
 
 sub get_all_text_unbalanced {
 #there is a copy of this in lonpublisher.pm
- my($tag,$pars)= @_;
- my $token;
- my $result='';
- $tag='<'.$tag.'>';
- while ($token = $$pars[-1]->get_token) {
-   if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
-     $result.=$token->[1];
-   } elsif ($token->[0] eq 'PI') {
-     $result.=$token->[2];
-   } elsif ($token->[0] eq 'S') {
-     $result.=$token->[4];
-   } elsif ($token->[0] eq 'E')  {
-     $result.=$token->[2];
-   }
-   if ($result =~ /(.*)\Q$tag\E(.*)/is) {
-     &Apache::lonxml::debug('Got a winner with leftovers ::'.$2);
-     &Apache::lonxml::debug('Result is :'.$1);
-     $result=$1;
-     my $redo=$tag.$2;
-     &Apache::lonxml::newparser($pars,\$redo);
-     last;
-   }
- }
- return $result
+    my($tag,$pars)= @_;
+    my $token;
+    my $result='';
+    $tag='<'.$tag.'>';
+    while ($token = $$pars[-1]->get_token) {
+	if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
+	    $result.=$token->[1];
+	} elsif ($token->[0] eq 'PI') {
+	    $result.=$token->[2];
+	} elsif ($token->[0] eq 'S') {
+	    $result.=$token->[4];
+	} elsif ($token->[0] eq 'E')  {
+	    $result.=$token->[2];
+	}
+	if ($result =~ /\Q$tag\E/is) {
+	    ($result,my $redo)=$result =~ /(.*)\Q$tag\E(.*)/is;
+	    #&Apache::lonxml::debug('Got a winner with leftovers ::'.$2);
+	    #&Apache::lonxml::debug('Result is :'.$1);
+	    $redo=$tag.$redo;
+	    &Apache::lonxml::newparser($pars,\$redo);
+	    last;
+	}
+    }
+    return $result
 }
 
 sub increment_counter {
@@ -1128,15 +1122,17 @@ sub inserteditinfo {
       $filecontents = &HTML::Entities::encode($filecontents,'<>&"');
 #      my $editheader='<a href="#editsection">Edit below</a><hr />';
       my $xml_help = '';
-      my $activate='';
       my $initialize='';
       if ($filetype eq 'html') {
+	  my $addbuttons=&Apache::lonhtmlcommon::htmlareaaddbuttons();
 	  $initialize=&Apache::lonhtmlcommon::htmlareaheaders().(<<FULLPAGE);
 <script type="text/javascript">
+$addbuttons
+
     HTMLArea.loadPlugin("FullPage");
 
     function initDocument() {
-	var editor=new HTMLArea("editor");
+	var editor=new HTMLArea("filecont",config);
 	editor.registerPlugin(FullPage);
 	editor.generate();
     }
@@ -1144,8 +1140,6 @@ sub inserteditinfo {
 FULLPAGE
           $result=~s/\<body([^\>]*)\>/\<body onload="initDocument()" $1\>/i;
 	  $xml_help=&Apache::loncommon::helpLatexCheatsheet();
-#FIXME: need to install FullPage plugin during install
-# 	  $activate=&Apache::lonhtmlcommon::htmlareaactive();
       }
       my $cleanbut = '';
       if ($filetype eq 'html') {
@@ -1174,7 +1168,6 @@ $buttons<br />
 <br />
 </form>
 $titledisplay
-$activate
 </body>
 ENDFOOTER
 #      $result=~s/(\<body[^\>]*\>)/$1$editheader/is;
@@ -1330,6 +1323,7 @@ sub debug {
 	my $request=$Apache::lonxml::request;
 	if (!$request) { $request=Apache->request; }
 	$request->print('<font size="-2"><pre>DEBUG:'.&HTML::Entities::encode($_[0],'<>&"')."</pre></font>\n");
+#	&Apache::lonnet::logthis($_[0]);
     }
 }