--- loncom/xml/lonxml.pm	2003/08/07 19:31:16	1.266.2.1
+++ loncom/xml/lonxml.pm	2004/03/12 17:26:29	1.310
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # XML Parser Module 
 #
-# $Id: lonxml.pm,v 1.266.2.1 2003/08/07 19:31:16 albertel Exp $
+# $Id: lonxml.pm,v 1.310 2004/03/12 17:26:29 sakharuk Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -69,6 +69,7 @@ use Safe::Hole();
 use Math::Cephes();
 use Math::Random();
 use Opcode();
+use POSIX qw(strftime);
 
 
 sub register {
@@ -95,6 +96,7 @@ use Apache::style();
 use Apache::run();
 use Apache::londefdef();
 use Apache::scripttag();
+use Apache::languagetags();
 use Apache::edit();
 use Apache::inputtags();
 use Apache::outputtags();
@@ -104,6 +106,7 @@ use Apache::loncommon();
 use Apache::lonfeedback();
 use Apache::lonmsg();
 use Apache::loncacc();
+use Apache::lonlocal;
 
 #==================================================   Main subroutine: xmlparse  
 #debugging control, to turn on debugging modify the correct handler
@@ -158,6 +161,15 @@ $Apache::lonxml::usestyle=1;
 $Apache::lonxml::style_values='';
 $Apache::lonxml::style_end_values='';
 
+#array of ssi calls that need to occur after we are done parsing
+@Apache::lonxml::ssi_info=();
+
+#should we do the postag variable interpolation
+$Apache::lonxml::post_evaluate=1;
+
+#a header message to emit in the case of any generated warning or errors
+$Apache::lonxml::warnings_error_header='';
+
 sub xmlbegin {
   my $output='';
   if ($ENV{'browser.mathml'}) {
@@ -174,113 +186,13 @@ sub xmlbegin {
 }
 
 sub xmlend {
-    my ($discussiononly,$symb)=@_;
-    my $discussion='';
-    if ($ENV{'request.course.id'}) {
-       my $crs='/'.$ENV{'request.course.id'};
-       if ($ENV{'request.course.sec'}) {
-          $crs.='_'.$ENV{'request.course.sec'};
-       }                 
-       $crs=~s/\_/\//g;
-       my $seeid=&Apache::lonnet::allowed('rin',$crs);
-       unless ($symb) {
-           $symb=&Apache::lonnet::symbread();
-       }
-       if ($symb) {
-          my %contrib=&Apache::lonnet::restore($symb,$ENV{'request.course.id'},
-                     $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
-		     $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
-          if ($contrib{'version'}) {
-              unless ($discussiononly) {
-                 $discussion.=
-                  '<address><hr />';
-	     }
-              my $idx;
-              for ($idx=1;$idx<=$contrib{'version'};$idx++) {
-		my $hidden=($contrib{'hidden'}=~/\.$idx\./);
-		my $deleted=($contrib{'deleted'}=~/\.$idx\./);
-		unless ((($hidden) && (!$seeid)) || ($deleted)) {
-                 my $message=$contrib{$idx.':message'};
-                 $message=~s/\n/\<br \/\>/g;
-		 $message=&Apache::lontexconvert::msgtexconverted($message);
-                 if ($contrib{$idx.':attachmenturl'}) {
-                     my ($fname,$ft)
-                        =($contrib{$idx.':attachmenturl'}=~/\/(\w+)\.(\w+)$/);
-		     $message.='<p>Attachment: <a href="'.
-	       &Apache::lonnet::tokenwrapper($contrib{$idx.':attachmenturl'}).
-                     '"><tt>'.$fname.'.'.$ft.'</tt></a>';
-                 }
-                 if ($message) {
-                  if ($hidden) {
-		      $message='<font color="#888888">'.$message.'</font>';
-                  }
-                  my $screenname=&Apache::loncommon::screenname(
-                               $contrib{$idx.':sendername'},
-			       $contrib{$idx.':senderdomain'});
-                  my $plainname=&Apache::loncommon::nickname(
-                               $contrib{$idx.':sendername'},
-			       $contrib{$idx.':senderdomain'});
-
-                  my $sender='Anonymous';
-                  if ((!$contrib{$idx.':anonymous'}) || ($seeid)) {
-                      $sender=&Apache::loncommon::aboutmewrapper(
-                               $plainname,
-                               $contrib{$idx.':sendername'},
-                               $contrib{$idx.':senderdomain'}).' ('.
-                              $contrib{$idx.':sendername'}.' at '.
-		      $contrib{$idx.':senderdomain'}.')';
-                      if ($contrib{$idx.':anonymous'}) {
-			  $sender.=' [anonymous] '.
-                                     $screenname;
-                      }
-                      if ($seeid) {
-			  if ($hidden) {
-                             $sender.=' <a href="/adm/feedback?unhide='.
-				 $symb.':::'.$idx.'">Make Visible</a>';
-                          } else {
-                             $sender.=' <a href="/adm/feedback?hide='.
-				 $symb.':::'.$idx.'">Hide</a>';
-			  }                     
-                          $sender.=' <a href="/adm/feedback?deldisc='.
-				 $symb.':::'.$idx.'">Delete</a>';
-                      }
-                  } else {
-                      if ($screenname) {
-			  $sender='<i>'.$screenname.'</i>';
-                      }
-                  }
-		  $discussion.='<p><b>'.$sender.'</b> ('.
-                      localtime($contrib{$idx.':timestamp'}).
-                      '):<blockquote>'.$message.
-                      '</blockquote></p>';
-	        }
-               } 
-              }
-              unless ($discussiononly) {
-                 $discussion.='</address>';
-	      }
-          }
-          if ($discussiononly) {
-	      $discussion.=(<<ENDDISCUSS);
-<form action="/adm/feedback" method="post" name="mailform" enctype="multipart/form-data">
-<input type="submit" name="discuss" value="Post Discussion" />
-<input type="submit" name="anondiscuss" value="Post Anonymous Discussion" />
-<input type="hidden" name="symb" value="$symb" />
-<input type="hidden" name="sendit" value="true" />
-<br />
-<font size="1">Note: in anonymous discussion, your name is visible only to
-course faculty</font><br />
-<textarea name=comment cols=60 rows=10 wrap=hard></textarea>
-<p>
-Attachment (128 KB max size): <input type="file" name="attachment" />
-</p>
-</form>
-ENDDISCUSS
-             $discussion.=&Apache::lonfeedback::generate_preview_button();
-          }
-       }
+    my $mode='xml';
+    my $status='OPEN';
+    if ($Apache::lonhomework::parsing_a_problem) {
+	$mode='problem';
+	$status=$Apache::inputtags::status[-1]; 
     }
-    return $discussion.($discussiononly?'':'</html>');
+    return &Apache::lonfeedback::list_discussion($mode,$status).'</html>';
 }
 
 sub tokeninputfield {
@@ -372,13 +284,13 @@ sub printtokenheader {
         my %idhash=&Apache::lonnet::idrget($tudom,($tuname));
 	return 
  '<img align="right" src="/cgi-bin/barcode.png?encode='.$token.'" />'.
-               'Checked out for '.$plainname.
-               '<br />User: '.$tuname.' at '.$tudom.
-	       '<br />ID: '.$idhash{$tuname}.
-	       '<br />CourseID: '.$tcrsid.
-	       '<br />Course: '.$ENV{'course.'.$tcrsid.'.description'}.
-               '<br />DocID: '.$token.
-               '<br />Time: '.localtime().'<hr />';
+               &mt('Checked out for').' '.$plainname.
+               '<br />'.&mt('User').': '.$tuname.' at '.$tudom.
+	       '<br />'.&mt('ID').': '.$idhash{$tuname}.
+	       '<br />'.&mt('CourseID').': '.$tcrsid.
+	       '<br />'.&mt('Course').': '.$ENV{'course.'.$tcrsid.'.description'}.
+               '<br />'.&mt('DocID').': '.$token.
+               '<br />'.&mt('Time').': '.&Apache::lonlocal::locallocaltime().'<hr />';
     } else {
         return $token;
     }
@@ -411,6 +323,7 @@ sub xmlparse {
  &Apache::inputtags::initialize_inputtags();
  &Apache::outputtags::initialize_outputtags();
  &Apache::edit::initialize_edit();
+ &Apache::londefdef::initialize_londefdef();
 
 #
 # do we have a course style file?
@@ -427,6 +340,13 @@ sub xmlparse {
                           &Apache::style::styleparser($target,$styletext));
        }
     }
+ } elsif ($ENV{'construct.style'} && ($ENV{'request.state'} eq 'construct')) {
+     my $location=&Apache::lonnet::filelocation('',$ENV{'construct.style'});
+     my $styletext=&Apache::lonnet::getfile($location);
+       if ($styletext ne '-1') {
+          %style_for_target = (%style_for_target,
+                          &Apache::style::styleparser($target,$styletext));
+      }
  }
 #&printalltags();
  my @pars = ();
@@ -451,6 +371,7 @@ sub xmlparse {
  if ($ENV{'request.uri'}) {
     &writeallows($ENV{'request.uri'});
  }
+ &do_registered_ssi();
  if ($Apache::lonxml::counter_changed) { &store_counter() }
  return $finaloutput;
 }
@@ -477,23 +398,23 @@ sub htmlclean {
 }
 
 sub latex_special_symbols {
-    my ($current_token,$stack,$parstack,$where)=@_;
+    my ($string,$where)=@_;
     if ($where eq 'header') {
-	$current_token =~ s/(\\|_|\^)/ /g;
-	$current_token =~ s/(\$|%|\#|&|\{|\})/\\$1/g;
+	$string =~ s/(\\|_|\^)/ /g;
+	$string =~ s/(\$|%|\#|&|\{|\})/\\$1/g;
+	$string =~ s/_/ /g;
     } else {
-	$current_token=~s/\\ /\\char92 /g;
-	$current_token=~s/\^/\\char94 /g;
-	$current_token=~s/\~/\\char126 /g;
-	$current_token=~s/(&[^A-Za-z\#])/\\$1/g;
-	$current_token=~s/([^&])\#/$1\\#/g;
-	$current_token=~s/(\$|_|{|})/\\$1/g;
-	$current_token=~s/\\char92 /\\texttt{\\char92}/g;
-	$current_token=~s/(>|<)/\$$1\$/g; #more or less
-	if ($current_token=~m/\d%/) {$current_token =~ s/(\d)%/$1\\%/g;} #percent after digit
-	if ($current_token=~m/\s%/) {$current_token =~ s/(\s)%/$1\\%/g;} #persent after space
+	$string=~s/([^\\])\%/$1\\\%/g;
+	$string=~s/([^\\])(\$|_)/$1\\$2/g;
+	$string=~s/\$\$/\$\\\$/g;
+        $string=~s/([^\\])\&/$1\\\&/g;
+        $string=~s/([^\\])\#/$1\\\#/g;
+	$string=~s/\#\#/\#\\\#/g;
+        $string=~s/([^\\])(\~|\^)/$1\\$2\\strut /g;
+	$string=~s/(>|<)/\\ensuremath\{$1\}/g; #more or less
+#single { or } How to escape?
     }
-    return $current_token;
+    return $string;
 }
 
 sub inner_xmlparse {
@@ -569,7 +490,7 @@ sub inner_xmlparse {
 	&Apache::lonxml::error("Unknown token event :$token->[0]:$token->[1]:");
       }
       #evaluate variable refs in result
-      if ($result ne "") {
+      if ($Apache::lonxml::post_evaluate &&$result ne "") {
 	  my $extras;
 	  if (!$Apache::lonxml::usestyle) {
 	      $extras=$Apache::lonxml::style_values;
@@ -580,17 +501,19 @@ sub inner_xmlparse {
 	  $result= &Apache::run::evaluate($result,$safeeval,$extras);
 	}
       }
+      $Apache::lonxml::post_evaluate=1;
+
       if (($token->[0] eq 'T') || ($token->[0] eq 'C') || ($token->[0] eq 'D') ) {
 	  #Style file definitions should be correct
 	  if ($target eq 'tex' && ($Apache::lonxml::usestyle)) {
-	      $result=&latex_special_symbols($result,$stack,$parstack);
+	      $result=&latex_special_symbols(&Apache::lonprintout::character_chart($result));
 	  }
       }
 
       # Encode any high ASCII characters
-      if (!$Apache::lonxml::prevent_entity_encode) {
-	$result=&HTML::Entities::encode($result,"\200-\377");
-      }
+#      if (!$Apache::lonxml::prevent_entity_encode) {
+#	$result=&HTML::Entities::encode($result,"\200-\377");
+#      }
       if ($Apache::lonxml::redirection) {
 	$Apache::lonxml::outputstack['-1'] .= $result;
       } else {
@@ -699,6 +622,9 @@ sub setup_globals {
   &init_counter();
   @Apache::lonxml::pwd=();
   @Apache::lonxml::extlinks=();
+  @Apache::lonxml::ssi_info=();
+  $Apache::lonxml::post_evaluate=1;
+  $Apache::lonxml::warnings_error_header='';
   if ($target eq 'meta') {
     $Apache::lonxml::redirection = 0;
     $Apache::lonxml::metamode = 1;
@@ -742,6 +668,7 @@ sub init_safespace {
   $safeeval->permit("entereval");
   $safeeval->permit(":base_math");
   $safeeval->permit("sort");
+  $safeeval->permit("time");
   $safeeval->deny(":base_io");
   $safehole->wrap(\&Apache::scripttag::xmlparse,$safeeval,'&xmlparse');
   $safehole->wrap(\&Apache::outputtags::multipart,$safeeval,'&multipart');
@@ -818,6 +745,7 @@ sub init_safespace {
   $safehole->wrap(\&Math::Random::random_set_seed_from_phrase,$safeeval,'&random_set_seed_from_phrase');
   $safehole->wrap(\&Math::Random::random_get_seed,$safeeval,'&random_get_seed');
   $safehole->wrap(\&Math::Random::random_set_seed,$safeeval,'&random_set_seed');
+  $safehole->wrap(\&Apache::lonxml::error,$safeeval,'&LONCAPA_INTERNAL_ERROR');
 
 #need to inspect this class of ops
 # $safeeval->deny(":base_orig");
@@ -828,6 +756,28 @@ sub init_safespace {
   $safeinit .= ';$external::randomseed='.$rndseed.';';
   &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 {
@@ -889,7 +839,7 @@ sub decreasedepth {
     $Apache::lonxml::olddepth=$Apache::lonxml::depth+1;
   }
   if (  $Apache::lonxml::depth < -1) {
-    &Apache::lonxml::warning("Missing tags, unable to properly run file.");
+    &Apache::lonxml::warning(&mt("Missing tags, unable to properly run file."));
     $Apache::lonxml::depth='-1';
   }
   my $curdepth=join('_',@Apache::lonxml::depthcounter);
@@ -951,83 +901,101 @@ sub store_counter {
 }
 
 sub get_all_text {
- my($tag,$pars)= @_;
- &Apache::lonxml::debug("Got a ".ref($pars));
- my $gotfullstack=1;
- if (ref($pars) ne 'ARRAY') {
-     $gotfullstack=0;
-     $pars=[$pars];
- }
- my $depth=0;
- my $token;
- my $result='';
- if ( $tag =~ m:^/: ) { 
-   my $tag=substr($tag,1); 
-   #&Apache::lonxml::debug("have:$tag:");
-   my $top_empty=0;
-   while (($depth >=0) && ($#$pars > -1) && (!$top_empty)) {
-     while (($depth >=0) && ($token = $$pars[-1]->get_token)) {
-       #&Apache::lonxml::debug("e token:$token->[0]:$depth:$token->[1]:".$#$pars.":".$#Apache::lonxml::pwd);
-       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') {
-	 if ($token->[1] =~ /^$tag$/i) { $depth++; }
-	 $result.=$token->[4];
-       } elsif ($token->[0] eq 'E')  {
-	 if ( $token->[1] =~ /^$tag$/i) { $depth--; }
-	 #skip sending back the last end tag
-	 if ($depth > -1) { $result.=$token->[2]; } else {
-	   $$pars[-1]->unget_token($token);
-	 }
-       }
-     }
-     if (($depth >=0) && ($#$pars == 0) ) { $top_empty=1; }
-     if (($depth >=0) && ($#$pars > 0) ) {
-       pop(@$pars);
-       pop(@Apache::lonxml::pwd);
-     }
-   }
-   if ($top_empty && $depth >= 0) {
-       #never found the end tag ran out of text, throw error send back blank
-       &error('Never found end tag for &lt;'.$tag.'&gt;');
-       if ($gotfullstack) {
-	   my $newstring='</'.$tag.'>'.$result;
-	   &Apache::lonxml::newparser($pars,\$newstring);
-       }
-       $result='';
-   }
- } else {
-     while ($#$pars > -1) {
-	 while ($token = $$pars[-1]->get_token) {
-	     #&Apache::lonxml::debug("s token:$token->[0]:$depth:$token->[1]");
-	     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') {
-		 if ( $token->[1] =~ /^$tag$/i) {
-		     $$pars[-1]->unget_token($token); last;
-		 } else {
-		     $result.=$token->[4];
-		 }
-	     } elsif ($token->[0] eq 'E')  {
-		 $result.=$token->[2];
-	     }
-	 }
-	 if (($#$pars > 0) ) {
-	     pop(@$pars);
-	     pop(@Apache::lonxml::pwd);
-	 } else { last; }
-     }
- }
- if ($result =~ m|<LONCAPA_INTERNAL_TURN_STYLE_ON />|) {
-     $Apache::lonxml::usestyle=1;
- }
- #&Apache::lonxml::debug("Exit:$result:");
- return $result
+    my($tag,$pars,$style)= @_;
+    my $gotfullstack=1;
+    if (ref($pars) ne 'ARRAY') {
+	$gotfullstack=0;
+	$pars=[$pars];
+    }
+    if (ref($style) ne 'HASH') {
+	$style={};
+    }
+    my $depth=0;
+    my $token;
+    my $result='';
+    if ( $tag =~ m:^/: ) { 
+	my $tag=substr($tag,1); 
+	#&Apache::lonxml::debug("have:$tag:");
+	my $top_empty=0;
+	while (($depth >=0) && ($#$pars > -1) && (!$top_empty)) {
+	    while (($depth >=0) && ($token = $$pars[-1]->get_token)) {
+		#&Apache::lonxml::debug("e token:$token->[0]:$depth:$token->[1]:".$#$pars.":".$#Apache::lonxml::pwd);
+		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') {
+		    if ($token->[1] =~ /^$tag$/i) { $depth++; }
+		    if ($token->[1] =~ /^LONCAPA_INTERNAL_TURN_STYLE_ON$/i) { $Apache::lonxml::usestyle=1; }
+		    if ($token->[1] =~ /^LONCAPA_INTERNAL_TURN_STYLE_OFF$/i) { $Apache::lonxml::usestyle=0; }
+		    $result.=$token->[4];
+		} elsif ($token->[0] eq 'E')  {
+		    if ( $token->[1] =~ /^$tag$/i) { $depth--; }
+		    #skip sending back the last end tag
+		    if ($depth == 0 && exists($$style{'/'.$token->[1]}) && $Apache::lonxml::usestyle) {
+			my $string=
+			    '<LONCAPA_INTERNAL_TURN_STYLE_OFF end="yes" />'.
+				$$style{'/'.$token->[1]}.
+				    $token->[2].
+					'<LONCAPA_INTERNAL_TURN_STYLE_ON />';
+			&Apache::lonxml::newparser($pars,\$string);
+			#&Apache::lonxml::debug("reParsing $string");
+			next;
+		    }
+		    if ($depth > -1) {
+			$result.=$token->[2];
+		    } else {
+			$$pars[-1]->unget_token($token);
+		    }
+		}
+	    }
+	    if (($depth >=0) && ($#$pars == 0) ) { $top_empty=1; }
+	    if (($depth >=0) && ($#$pars > 0) ) {
+		pop(@$pars);
+		pop(@Apache::lonxml::pwd);
+	    }
+	}
+	if ($top_empty && $depth >= 0) {
+	    #never found the end tag ran out of text, throw error send back blank
+	    &error('Never found end tag for &lt;'.$tag.
+		   '&gt; current string <pre>'.
+		   &HTML::Entities::encode($result).
+		   '</pre>');
+	    if ($gotfullstack) {
+		my $newstring='</'.$tag.'>'.$result;
+		&Apache::lonxml::newparser($pars,\$newstring);
+	    }
+	    $result='';
+	}
+    } else {
+	while ($#$pars > -1) {
+	    while ($token = $$pars[-1]->get_token) {
+		#&Apache::lonxml::debug("s token:$token->[0]:$depth:$token->[1]");
+		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') {
+		    if ( $token->[1] =~ /^$tag$/i) {
+			$$pars[-1]->unget_token($token); last;
+		    } else {
+			$result.=$token->[4];
+		    }
+		    if ($token->[1] =~ /^LONCAPA_INTERNAL_TURN_STYLE_ON$/i) { $Apache::lonxml::usestyle=1; }
+		    if ($token->[1] =~ /^LONCAPA_INTERNAL_TURN_STYLE_OFF$/i) { $Apache::lonxml::usestyle=0; }
+		} elsif ($token->[0] eq 'E')  {
+		    $result.=$token->[2];
+		}
+	    }
+	    if (($#$pars > 0) ) {
+		pop(@$pars);
+		pop(@Apache::lonxml::pwd);
+	    } else { last; }
+	}
+    }
+    #&Apache::lonxml::debug("Exit:$result:");
+    return $result
 }
 
 sub newparser {
@@ -1049,7 +1017,7 @@ sub parstring {
       my $val=$token->[2]->{$_};
       $val =~ s/([\%\@\\\"\'])/\\$1/g;
       #if ($val =~ m/^[\%\@]/) { $val="\\".$val; }
-      $temp .= "my \$$_=\"$val\";"
+      $temp .= "my \$$_=\"$val\";";
     }
   }
   return $temp;
@@ -1072,6 +1040,19 @@ sub writeallows {
     &Apache::lonnet::appenv(%httpref);
 }
 
+sub register_ssi {
+    my ($url,%form)=@_;
+    push (@Apache::lonxml::ssi_info,{'url'=>$url,'form'=>\%form});
+    return '';
+}
+
+sub do_registered_ssi {
+    foreach my $info (@Apache::lonxml::ssi_info) {
+	my %form=%{ $info->{'form'}};
+	my $url=$info->{'url'};
+	&Apache::lonnet::ssi($url,%form);
+    }
+}
 #
 # Afterburner handles anchors, highlights and links
 #
@@ -1111,11 +1092,14 @@ ENDSCRIPT
 
 sub storefile {
     my ($file,$contents)=@_;
+    &Apache::lonnet::correct_line_ends(\$contents);
     if (my $fh=Apache::File->new('>'.$file)) {
 	print $fh $contents;
         $fh->close();
+        return 1;
     } else {
-      &warning("Unable to save file $file");
+	&warning("Unable to save file $file");
+	return 0;
     }
 }
 
@@ -1137,25 +1121,47 @@ SIMPLECONTENT
   return $filecontents;
 }
 
+sub createnewsty {
+  my $filecontents=(<<SIMPLECONTENT);
+<definetag name="">
+    <render>
+       <web></web>
+       <tex></tex>
+    </render>
+</definetag>
+SIMPLECONTENT
+  return $filecontents;
+}
+
 
 sub inserteditinfo {
-      my ($result,$filecontents)=@_;
+      my ($result,$filecontents,$filetype)=@_;
       $filecontents = &HTML::Entities::encode($filecontents);
 #      my $editheader='<a href="#editsection">Edit below</a><hr />';
-      my $xml_help = Apache::loncommon::helpLatexCheatsheet();
+      my $xml_help = '';
+      if ($filetype eq 'html') {
+	  $xml_help=Apache::loncommon::helpLatexCheatsheet();
+      }
+      my $cleanbut = '';
+      if ($filetype eq 'html') {
+	  $cleanbut='<input type="submit" name="attemptclean" value="'.
+	      &mt('Save and then attempt to clean HTML').'" />';
+      }
       my $titledisplay=&display_title();
+      my %lt=&Apache::lonlocal::texthash('st' => 'Save this',
+					 'vi' => 'View',
+					 'ed' => 'Edit');
       my $buttons=(<<BUTTONS);
-<input type="submit" name="attemptclean" 
-       value="Save and then attempt to clean HTML" />
-<input type="submit" name="savethisfile" value="Save this" />
-<input type="submit" name="viewmode" value="View" />
+$cleanbut
+<input type="submit" name="savethisfile" accesskey="s"  value="$lt{'st'}" />
+<input type="submit" name="viewmode" accesskey="v" value="$lt{'vi'}" />
 BUTTONS
       my $editfooter=(<<ENDFOOTER);
 <hr />
 <a name="editsection" />
 <form method="post">
 $xml_help
-<input type="hidden" name="editmode" value="Edit" />
+<input type="hidden" name="editmode" value="$lt{'ed'}" />
 $buttons<br />
 <textarea cols="80" rows="40" name="filecont">$filecontents</textarea>
 <br />$buttons
@@ -1203,9 +1209,9 @@ sub handler {
     $Apache::lonxml::debug=$ENV{'user.debug'};
     
     if ($ENV{'browser.mathml'}) {
-	$request->content_type('text/xml');
+	&Apache::loncommon::content_type($request,'text/xml');
     } else {
-	$request->content_type('text/html');
+	&Apache::loncommon::content_type($request,'text/html');
     }
     &Apache::loncommon::no_cache($request);
     $request->send_http_header;
@@ -1214,31 +1220,48 @@ sub handler {
 
 
     my $file=&Apache::lonnet::filelocation("",$request->uri);
+    my $filetype;
+    if ($file =~ /\.sty$/) {
+	$filetype='sty';
+    } else {
+	$filetype='html';
+    }
 #
 # Edit action? Save file.
 #
     unless ($ENV{'request.state'} eq 'published') {
 	if (($ENV{'form.savethisfile'}) || ($ENV{'form.attemptclean'})) {
-	    &storefile($file,$ENV{'form.filecont'});
+	    if (&storefile($file,$ENV{'form.filecont'})) {
+		&Apache::lonxml::info("<font COLOR=\"#0000FF\">".
+				      &mt('Updated').": ".
+				      &Apache::lonlocal::locallocaltime(time).
+				      " </font>");
+	    } 
 	}
     }
     my %mystyle;
     my $result = '';
     my $filecontents=&Apache::lonnet::getfile($file);
     if ($filecontents eq -1) {
+	my $bodytag=&Apache::loncommon::bodytag('File Error');
+	my $fnf=&mt('File not found');
 	$result=(<<ENDNOTFOUND);
 <html>
 <head>
-<title>File not found</title>
+<title>$fnf</title>
 </head>
-<body bgcolor="#FFFFFF">
-<b>File not found: $file</b>
+$bodytag
+<b>$fnf: $file</b>
 </body>
 </html>
 ENDNOTFOUND
     $filecontents='';
 	if ($ENV{'request.state'} ne 'published') {
-	    $filecontents=&createnewhtml();
+	    if ($filetype eq 'sty') {
+		$filecontents=&createnewsty();
+	    } else {
+		$filecontents=&createnewhtml();
+	    }
 	    $ENV{'form.editmode'}='Edit'; #force edit mode
 	}
     } else {
@@ -1264,15 +1287,17 @@ ENDNOTFOUND
 	if ($ENV{'form.editmode'} && (!($ENV{'form.viewmode'}))) {
 	    my $displayfile=$request->uri;
 	    $displayfile=~s/^\/[^\/]*//;
-	    $result='<html><body bgcolor="#FFFFFF"><h3>'.$displayfile.
+	    $result='<html><body bgcolor="#FFFFFF">'.
+		&Apache::lonxml::message_location().'<h3>'.
+		$displayfile.
 		'</h3></body></html>';
-	    $result=&inserteditinfo($result,$filecontents);
+	    $result=&inserteditinfo($result,$filecontents,$filetype);
 	}
     }
+    if ($filetype eq 'html') { writeallows($request->uri); }
+	
     
-    writeallows($request->uri);
-    
-
+    &Apache::lonxml::add_messages(\$result);
     $request->print($result);
     
     return OK;
@@ -1292,19 +1317,27 @@ sub display_title {
 }
 
 sub debug {
-  if ($Apache::lonxml::debug eq 1) {
-    $|=1;
-    print('<font size="-2"<pre>DEBUG:'.&HTML::Entities::encode($_[0])."</pre></font>\n");
-  }
+    if ($Apache::lonxml::debug eq "1") {
+	$|=1;
+	my $request=$Apache::lonxml::request;
+	if (!$request) { $request=Apache->request; }
+	$request->print('<font size="-2"><pre>DEBUG:'.&HTML::Entities::encode($_[0])."</pre></font>\n");
+    }
 }
 
 sub error {
   $errorcount++;
+  my $request=$Apache::lonxml::request;
+  if (!$request) { $request=Apache->request; }
   if (($Apache::lonxml::debug eq 1) || ($ENV{'request.state'} eq 'construct') ) {
     # If printing in construction space, put the error inside <pre></pre>
-    print "<b>ERROR:</b>".join("\n",@_)."\n";
+      push(@Apache::lonxml::error_messages,
+	   $Apache::lonxml::warnings_error_header.
+	   "<b>ERROR:</b>".join("<br />\n",@_)."<br />\n");
+      $Apache::lonxml::warnings_error_header='';
   } else {
-    print "<b>An Error occured while processing this resource. The instructor has been notified.</b> <br />";
+      push(@Apache::lonxml::error_messages,
+	   "<b>An Error occured while processing this resource. The instructor has been notified.</b> <br />");
     #notify author
     &Apache::lonmsg::author_res_msg($ENV{'request.filename'},join('<br />',@_));
     #notify course
@@ -1317,21 +1350,46 @@ sub error {
         "Error [$declutter]",join('<br />',@_));
       }
     }
-
-    #FIXME probably shouldn't have me get everything forever.
-    &Apache::lonmsg::user_normal_msg('albertel','msu',"Error in $ENV{'request.filename'}",join('<br />',@_));
-    #&Apache::lonmsg::user_normal_msg('albertel','103',"Error in $ENV{'request.filename'}",$_[0]);
   }
 }
 
 sub warning {
-  $warningcount++;
+    $warningcount++;
   
-  if ($ENV{'form.grade_target'} ne 'tex') {
-      if ($ENV{'request.state'} eq 'construct' || $Apache::lonxml::debug) {
-        print "<b>W</b>ARNING<b>:</b>".join('<br />',@_)."<br />\n";
-      }
-  }
+    if ($ENV{'form.grade_target'} ne 'tex') {
+	if ($ENV{'request.state'} eq 'construct' || $Apache::lonxml::debug) {
+	    my $request=$Apache::lonxml::request;
+	    if (!$request) { $request=Apache->request; }
+	    push(@Apache::lonxml::warning_messages,
+		 $Apache::lonxml::warnings_error_header.
+		 "<b>W</b>ARNING<b>:</b>".join('<br />',@_)."<br />\n");
+	    $Apache::lonxml::warnings_error_header='';
+	}
+    }
+}
+
+sub info {
+    if ($ENV{'form.grade_target'} ne 'tex' 
+	&& $ENV{'request.state'} eq 'construct') {
+	push(@Apache::lonxml::info_messages,join('<br />',@_)."<br />\n");
+    }
+}
+
+sub message_location {
+    return '__LONCAPA_INTERNAL_MESSAGE_LOCATION__';
+}
+
+sub add_messages {
+    my ($msg)=@_;
+    my $result=join(' ',
+		    @Apache::lonxml::info_messages,
+		    @Apache::lonxml::error_messages,
+		    @Apache::lonxml::warning_messages);
+    undef(@Apache::lonxml::info_messages);
+    undef(@Apache::lonxml::error_messages);
+    undef(@Apache::lonxml::warning_messages);
+    $$msg=~s/__LONCAPA_INTERNAL_MESSAGE_LOCATION__/$result/;
+    $$msg=~s/__LONCAPA_INTERNAL_MESSAGE_LOCATION__//g;
 }
 
 sub get_param {
@@ -1339,6 +1397,9 @@ sub get_param {
     if ( ! $context ) { $context = -1; }
     my $args ='';
     if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; }
+    if ( ! $Apache::lonxml::usestyle ) {
+	$args=$Apache::lonxml::style_values.$args;
+    }
     if ( ! $args ) { return undef; }
     if ( $case_insensitive ) {
 	if ($args =~ s/(my \$)(\Q$param\E)(=\")/$1.lc($2).$3/ei) {
@@ -1362,6 +1423,9 @@ sub get_param_var {
   if ( ! $context ) { $context = -1; }
   my $args ='';
   if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; }
+  if ( ! $Apache::lonxml::usestyle ) {
+      $args=$Apache::lonxml::style_values.$args;
+  }
   &Apache::lonxml::debug("Args are $args param is $param");
   if ($case_insensitive) {
       if (! ($args=~s/(my \$)(\Q$param\E)(=\")/$1.lc($2).$3/ei)) {
@@ -1392,7 +1456,7 @@ sub register_insert {
     my $line = $data[$i];
     if ( $line =~ /^\#/ || $line =~ /^\s*\n/) { next; }
     if ( $line =~ /TABLE/ ) { last; }
-    my ($tag,$descrip,$color,$function,$show) = split(/,/, $line);
+    my ($tag,$descrip,$color,$function,$show,$helpfile,$helpdesc) = split(/,/, $line);
     if ($tag) {
       $insertlist{"$tagnum.tag"} = $tag;
       $insertlist{"$tagnum.description"} = $descrip;
@@ -1400,6 +1464,8 @@ sub register_insert {
       $insertlist{"$tagnum.function"} = $function;
       if (!defined($show)) { $show='yes'; }
       $insertlist{"$tagnum.show"}= $show;
+      $insertlist{"$tagnum.helpfile"} = $helpfile;
+      $insertlist{"$tagnum.helpdesc"} = $helpdesc;
       $insertlist{"$tag.num"}=$tagnum;
       $tagnum++;
     }
@@ -1434,6 +1500,20 @@ sub description {
   return $insertlist{$tagnum.'.description'};
 }
 
+# Returns a list containing the help file, and the description
+sub helpinfo {
+  my ($token)=@_;
+  my $tagnum;
+  my $tag=$token->[1];
+  foreach my $namespace (reverse @Apache::lonxml::namespace) {
+    my $testtag=$namespace.'::'.$tag;
+    $tagnum=$insertlist{"$testtag.num"};
+    if (defined($tagnum)) { last; }
+  }
+  if (!defined ($tagnum)) { $tagnum=$Apache::lonxml::insertlist{"$tag.num"}; }
+  return ($insertlist{$tagnum.'.helpfile'}, $insertlist{$tagnum.'.helpdesc'});
+}
+
 # ----------------------------------------------------------------- whichuser
 # returns a list of $symb, $courseid, $domain, $name that is correct for
 # calls to lonnet functions for this setup.