Annotation of loncom/xml/lonxml.pm, revision 1.341

1.2       sakharuk    1: # The LearningOnline Network with CAPA
1.3       sakharuk    2: # XML Parser Module 
1.2       sakharuk    3: #
1.341   ! albertel    4: # $Id: lonxml.pm,v 1.340 2004/10/05 18:54:49 albertel Exp $
1.139     www         5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
                     28: # Copyright for TtHfunc and TtMfunc by Ian Hutchinson. 
                     29: # TtHfunc and TtMfunc (the "Code") may be compiled and linked into 
                     30: # binary executable programs or libraries distributed by the 
                     31: # Michigan State University (the "Licensee"), but any binaries so 
                     32: # distributed are hereby licensed only for use in the context
                     33: # of a program or computational system for which the Licensee is the 
                     34: # primary author or distributor, and which performs substantial 
                     35: # additional tasks beyond the translation of (La)TeX into HTML.
                     36: # The C source of the Code may not be distributed by the Licensee
                     37: # to any other parties under any circumstances.
                     38: #
1.316     albertel   39: 
1.2       sakharuk   40: 
1.4       albertel   41: package Apache::lonxml; 
1.33      www        42: use vars 
1.320     www        43: qw(@pwd @outputstack $redirection $import @extlinks $metamode $evaluate %insertlist @namespace $errorcount $warningcount @htmlareafields);
1.1       sakharuk   44: use strict;
1.167     albertel   45: use HTML::LCParser();
1.161     albertel   46: use HTML::TreeBuilder();
                     47: use HTML::Entities();
                     48: use Safe();
                     49: use Safe::Hole();
                     50: use Math::Cephes();
                     51: use Math::Random();
                     52: use Opcode();
1.271     www        53: use POSIX qw(strftime);
1.339     albertel   54: use Time::HiRes qw( gettimeofday tv_interval );
1.266     bowersj2   55: 
1.72      albertel   56: sub register {
1.141     albertel   57:   my ($space,@taglist) = @_;
                     58:   foreach my $temptag (@taglist) {
                     59:     push(@{ $Apache::lonxml::alltags{$temptag} },$space);
1.72      albertel   60:   }
                     61: }
                     62: 
1.141     albertel   63: sub deregister {
                     64:   my ($space,@taglist) = @_;
                     65:   foreach my $temptag (@taglist) {
                     66:     my $tempspace = $Apache::lonxml::alltags{$temptag}[-1];
                     67:     if ($tempspace eq $space) {
                     68:       pop(@{ $Apache::lonxml::alltags{$temptag} });
                     69:     }
                     70:   }
1.142     albertel   71:   #&printalltags();
1.141     albertel   72: }
                     73: 
1.46      www        74: use Apache::Constants qw(:common);
1.161     albertel   75: use Apache::lontexconvert();
                     76: use Apache::style();
                     77: use Apache::run();
                     78: use Apache::londefdef();
                     79: use Apache::scripttag();
1.285     www        80: use Apache::languagetags();
1.161     albertel   81: use Apache::edit();
1.266     bowersj2   82: use Apache::inputtags();
                     83: use Apache::outputtags();
1.161     albertel   84: use Apache::lonnet();
                     85: use Apache::File();
                     86: use Apache::loncommon();
1.198     www        87: use Apache::lonfeedback();
1.200     www        88: use Apache::lonmsg();
1.217     matthew    89: use Apache::loncacc();
1.280     www        90: use Apache::lonlocal;
1.79      www        91: 
1.72      albertel   92: #==================================================   Main subroutine: xmlparse  
                     93: #debugging control, to turn on debugging modify the correct handler
                     94: $Apache::lonxml::debug=0;
1.206     albertel   95: 
                     96: # keeps count of the number of warnings and errors generated in a parse
                     97: $warningcount=0;
                     98: $errorcount=0;
1.72      albertel   99: 
                    100: #path to the directory containing the file currently being processed
                    101: @pwd=();
                    102: 
                    103: #these two are used for capturing a subset of the output for later processing,
                    104: #don't touch them directly use &startredirection and &endredirection
                    105: @outputstack = ();
                    106: $redirection = 0;
                    107: 
                    108: #controls wheter the <import> tag actually does
                    109: $import = 1;
                    110: @extlinks=();
                    111: 
                    112: # meta mode is a bit weird only some output is to be turned off
                    113: #<output> tag turns metamode off (defined in londefdef.pm)
                    114: $metamode = 0;
                    115: 
                    116: # turns on and of run::evaluate actually derefencing var refs
                    117: $evaluate = 1;
1.7       albertel  118: 
1.74      albertel  119: # data structure for eidt mode, determines what tags can go into what other tags
                    120: %insertlist=();
1.68      www       121: 
1.99      albertel  122: # stores the list of active tag namespaces
1.76      albertel  123: @namespace=();
                    124: 
1.99      albertel  125: # has the dynamic menu been updated to know about this resource
                    126: $Apache::lonxml::registered=0;
                    127: 
1.172     albertel  128: # a pointer the the Apache request object
                    129: $Apache::lonxml::request='';
                    130: 
1.216     sakharuk  131: # a problem number counter, and check on ether it is used
1.237     sakharuk  132: $Apache::lonxml::counter=1;
1.204     albertel  133: $Apache::lonxml::counter_changed=0;
                    134: 
1.212     albertel  135: #internal check on whether to look at style defs
                    136: $Apache::lonxml::usestyle=1;
1.260     albertel  137: 
                    138: #locations used to store the parameter string for style substitutions
                    139: $Apache::lonxml::style_values='';
                    140: $Apache::lonxml::style_end_values='';
1.212     albertel  141: 
1.281     albertel  142: #array of ssi calls that need to occur after we are done parsing
                    143: @Apache::lonxml::ssi_info=();
                    144: 
1.282     albertel  145: #should we do the postag variable interpolation
                    146: $Apache::lonxml::post_evaluate=1;
                    147: 
1.295     albertel  148: #a header message to emit in the case of any generated warning or errors
                    149: $Apache::lonxml::warnings_error_header='';
                    150: 
1.68      www       151: sub xmlbegin {
                    152:   my $output='';
1.325     www       153:   @htmlareafields=();
1.68      www       154:   if ($ENV{'browser.mathml'}) {
                    155:       $output='<?xml version="1.0"?>'
                    156:             .'<?xml-stylesheet type="text/css" href="/adm/MathML/mathml.css"?>'
                    157:             .'<!DOCTYPE html SYSTEM "/adm/MathML/mathml.dtd" '
                    158:             .'[<!ENTITY mathns "http://www.w3.org/1998/Math/MathML">]>'
                    159:             .'<html xmlns:math="http://www.w3.org/1998/Math/MathML" ' 
                    160: 		.'xmlns="http://www.w3.org/TR/REC-html40">';
                    161:   } else {
                    162:       $output='<html>';
                    163:   }
                    164:   return $output;
                    165: }
                    166: 
                    167: sub xmlend {
1.335     sakharuk  168:     my ($target,$parser)=@_;
1.278     www       169:     my $mode='xml';
                    170:     my $status='OPEN';
                    171:     if ($Apache::lonhomework::parsing_a_problem) {
                    172: 	$mode='problem';
                    173: 	$status=$Apache::inputtags::status[-1]; 
                    174:     }
1.335     sakharuk  175:     my $discussion=&Apache::lonfeedback::list_discussion($mode,$status);    
1.334     sakharuk  176:     if ($target eq 'tex') {
1.335     sakharuk  177: 	$discussion.='<tex>\keephidden{ENDOFPROBLEM}\vskip 0.5mm\noindent\makebox[\textwidth/$number_of_columns][b]{\hrulefill}\end{document}</tex>';
1.334     sakharuk  178: 	&Apache::lonxml::newparser($parser,\$discussion,'');
                    179: 	return '';
                    180:     } else {
                    181: 	return $discussion.'</html>';
                    182:     }
1.119     www       183: }
                    184: 
                    185: sub tokeninputfield {
1.120     www       186:     my $defhost=$Apache::lonnet::perlvar{'lonHostID'};
                    187:     $defhost=~tr/a-z/A-Z/;
1.119     www       188:     return (<<ENDINPUTFIELD)
1.226     albertel  189: <script type="text/javascript">
1.120     www       190:     function updatetoken() {
                    191: 	var comp=new Array;
                    192:         var barcode=unescape(document.tokeninput.barcode.value);
                    193:         comp=barcode.split('*');
                    194:         if (typeof(comp[0])!="undefined") {
                    195: 	    document.tokeninput.codeone.value=comp[0];
                    196: 	}
                    197:         if (typeof(comp[1])!="undefined") {
                    198: 	    document.tokeninput.codetwo.value=comp[1];
                    199: 	}
                    200:         if (typeof(comp[2])!="undefined") {
                    201:             comp[2]=comp[2].toUpperCase();
                    202: 	    document.tokeninput.codethree.value=comp[2];
                    203: 	}
                    204:         document.tokeninput.barcode.value='';
                    205:     }  
                    206: </script>
                    207: <form method="post" name="tokeninput">
1.119     www       208: <table border="2" bgcolor="#FFFFBB">
                    209: <tr><th>DocID Checkin</th></tr>
                    210: <tr><td>
                    211: <table>
                    212: <tr>
                    213: <td>Scan in Barcode</td>
1.120     www       214: <td><input type="text" size="22" name="barcode" 
                    215: onChange="updatetoken()"/></td>
1.119     www       216: </tr>
                    217: <tr><td><i>or</i> Type in DocID</td>
                    218: <td>
                    219: <input type="text" size="5" name="codeone" />
1.120     www       220: <b><font size="+2">*</font></b>
1.119     www       221: <input type="text" size="5" name="codetwo" />
1.120     www       222: <b><font size="+2">*</font></b>
                    223: <input type="text" size="10" name="codethree" value="$defhost" 
                    224: onChange="this.value=this.value.toUpperCase()" />
1.119     www       225: </td></tr>
                    226: </table>
                    227: </td></tr>
                    228: <tr><td><input type="submit" value="Check in DocID" /></td></tr>
                    229: </table>
                    230: </form>
                    231: ENDINPUTFIELD
1.112     www       232: }
                    233: 
1.116     www       234: sub maketoken {
1.118     www       235:     my ($symb,$tuname,$tudom,$tcrsid)=@_;
1.112     www       236:     unless ($symb) {
                    237: 	$symb=&Apache::lonnet::symbread();
                    238:     }
                    239:     unless ($tuname) {
                    240: 	$tuname=$ENV{'user.name'};
                    241:         $tudom=$ENV{'user.domain'};
                    242:         $tcrsid=$ENV{'request.course.id'};
                    243:     }
1.116     www       244: 
1.118     www       245:     return &Apache::lonnet::checkout($symb,$tuname,$tudom,$tcrsid);
                    246: }
                    247: 
                    248: sub printtokenheader {
1.133     albertel  249:     my ($target,$token,$tsymb,$tcrsid,$tudom,$tuname)=@_;
1.116     www       250:     unless ($token) { return ''; }
1.118     www       251: 
1.133     albertel  252:     my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
                    253:     unless ($tsymb) {
                    254: 	$tsymb=$symb;
1.118     www       255:     }
                    256:     unless ($tuname) {
1.133     albertel  257: 	$tuname=$name;
                    258:         $tudom=$domain;
                    259:         $tcrsid=$courseid;
1.118     www       260:     }
1.114     www       261: 
                    262:     my %reply=&Apache::lonnet::get('environment',
                    263:               ['firstname','middlename','lastname','generation'],
                    264:               $tudom,$tuname);
                    265:     my $plainname=$reply{'firstname'}.' '. 
                    266:                   $reply{'middlename'}.' '.
                    267:                   $reply{'lastname'}.' '.
                    268: 		  $reply{'generation'};
                    269: 
1.112     www       270:     if ($target eq 'web') {
1.145     www       271:         my %idhash=&Apache::lonnet::idrget($tudom,($tuname));
1.115     www       272: 	return 
1.221     albertel  273:  '<img align="right" src="/cgi-bin/barcode.png?encode='.$token.'" />'.
1.284     www       274:                &mt('Checked out for').' '.$plainname.
                    275:                '<br />'.&mt('User').': '.$tuname.' at '.$tudom.
                    276: 	       '<br />'.&mt('ID').': '.$idhash{$tuname}.
                    277: 	       '<br />'.&mt('CourseID').': '.$tcrsid.
                    278: 	       '<br />'.&mt('Course').': '.$ENV{'course.'.$tcrsid.'.description'}.
                    279:                '<br />'.&mt('DocID').': '.$token.
                    280:                '<br />'.&mt('Time').': '.&Apache::lonlocal::locallocaltime().'<hr />';
1.112     www       281:     } else {
1.121     albertel  282:         return $token;
1.112     www       283:     }
1.68      www       284: }
                    285: 
1.70      www       286: sub fontsettings() {
                    287:     my $headerstring='';
                    288:     if (($ENV{'browser.os'} eq 'mac') && (!$ENV{'browser.mathml'})) { 
1.248     albertel  289: 	$headerstring.=
                    290: 	    '<meta Content-Type="text/html; charset=x-mac-roman">';
1.263     albertel  291:     } elsif (!$ENV{'browser.mathml'} && $ENV{'browser.unicode'}) {
1.248     albertel  292: 	$headerstring.=
                    293: 	    '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
1.70      www       294:     }
                    295:     return $headerstring;
1.68      www       296: }
                    297: 
1.48      albertel  298: sub printalltags {
                    299:   my $temp;
                    300:   foreach $temp (sort keys %Apache::lonxml::alltags) {
1.141     albertel  301:     &Apache::lonxml::debug("$temp -- ".
                    302: 		  join(',',@{ $Apache::lonxml::alltags{$temp} }));
1.48      albertel  303:   }
                    304: }
1.31      sakharuk  305: 
1.3       sakharuk  306: sub xmlparse {
1.172     albertel  307:  my ($request,$target,$content_file_string,$safeinit,%style_for_target) = @_;
1.96      albertel  308: 
1.172     albertel  309:  &setup_globals($request,$target);
1.232     albertel  310:  &Apache::inputtags::initialize_inputtags();
                    311:  &Apache::outputtags::initialize_outputtags();
                    312:  &Apache::edit::initialize_edit();
1.287     albertel  313:  &Apache::londefdef::initialize_londefdef();
1.244     albertel  314: 
1.178     www       315: #
                    316: # do we have a course style file?
                    317: #
                    318: 
1.208     albertel  319:  if ($ENV{'request.course.id'} && $ENV{'request.state'} ne 'construct') {
1.178     www       320:      my $bodytext=
                    321: 	 $ENV{'course.'.$ENV{'request.course.id'}.'.default_xml_style'};
                    322:      if ($bodytext) {
1.337     albertel  323: 	 foreach my $file (split(',',$bodytext)) {
                    324: 	     my $location=&Apache::lonnet::filelocation('',$file);
                    325: 	     my $styletext=&Apache::lonnet::getfile($location);
                    326: 	     if ($styletext ne '-1') {
                    327: 		 %style_for_target = (%style_for_target,
                    328: 				      &Apache::style::styleparser($target,$styletext));
                    329: 	     }
                    330: 	 }
                    331:      }
1.292     sakharuk  332:  } elsif ($ENV{'construct.style'} && ($ENV{'request.state'} eq 'construct')) {
                    333:      my $location=&Apache::lonnet::filelocation('',$ENV{'construct.style'});
1.291     sakharuk  334:      my $styletext=&Apache::lonnet::getfile($location);
                    335:        if ($styletext ne '-1') {
                    336:           %style_for_target = (%style_for_target,
                    337:                           &Apache::style::styleparser($target,$styletext));
                    338:       }
1.178     www       339:  }
1.255     sakharuk  340: #&printalltags();
1.16      albertel  341:  my @pars = ();
1.23      albertel  342:  my $pwd=$ENV{'request.filename'};
                    343:  $pwd =~ s:/[^/]*$::;
                    344:  &newparser(\@pars,\$content_file_string,$pwd);
1.24      sakharuk  345: 
1.3       sakharuk  346:  my $safeeval = new Safe;
1.40      albertel  347:  my $safehole = new Safe::Hole;
1.82      ng        348:  &init_safespace($target,$safeeval,$safehole,$safeinit);
1.3       sakharuk  349: #-------------------- Redefinition of the target in the case of compound target
                    350: 
                    351:  ($target, my @tenta) = split('&&',$target);
                    352: 
1.150     albertel  353:  my @stack = ();
1.3       sakharuk  354:  my @parstack = ();
1.17      albertel  355:  &initdepth;
1.67      www       356: 
1.101     albertel  357:  my $finaloutput = &inner_xmlparse($target,\@stack,\@parstack,\@pars,
                    358: 				   $safeeval,\%style_for_target);
1.255     sakharuk  359: 
1.125     www       360:  if ($ENV{'request.uri'}) {
                    361:     &writeallows($ENV{'request.uri'});
                    362:  }
1.281     albertel  363:  &do_registered_ssi();
1.204     albertel  364:  if ($Apache::lonxml::counter_changed) { &store_counter() }
1.3       sakharuk  365:  return $finaloutput;
1.106     www       366: }
                    367: 
                    368: sub htmlclean {
1.107     www       369:     my ($raw,$full)=@_;
1.106     www       370: 
                    371:     my $tree = HTML::TreeBuilder->new;
                    372:     $tree->ignore_unknown(0);
1.140     albertel  373: 
1.106     www       374:     $tree->parse($raw);
                    375: 
1.107     www       376:     my $output= $tree->as_HTML(undef,' ');
1.140     albertel  377: 
1.161     albertel  378:     $output=~s/\<(br|hr|img|meta|allow)(.*?)\>/\<$1$2 \/\>/gis;
1.111     www       379:     $output=~s/\<\/(br|hr|img|meta|allow)\>//gis;
1.107     www       380:     unless ($full) {
                    381:        $output=~s/\<[\/]*(body|head|html)\>//gis;
                    382:     }
1.106     www       383: 
                    384:     $tree = $tree->delete;
                    385: 
                    386:     return $output;
1.15      albertel  387: }
                    388: 
1.191     albertel  389: sub latex_special_symbols {
1.272     albertel  390:     my ($string,$where)=@_;
1.235     sakharuk  391:     if ($where eq 'header') {
1.272     albertel  392: 	$string =~ s/(\\|_|\^)/ /g;
1.311     albertel  393: 	$string =~ s/(\$|%|\{|\})/\\$1/g;
1.273     sakharuk  394: 	$string =~ s/_/ /g;
1.311     albertel  395: 	$string=&Apache::lonprintout::character_chart($string);
                    396: 	# any & or # leftover should be safe to just escape
                    397:         $string=~s/([^\\])\&/$1\\\&/g;
                    398:         $string=~s/([^\\])\#/$1\\\#/g;
1.229     sakharuk  399:     } else {
1.312     albertel  400: 	$string=~s/\\/\\ensuremath{\\backslash}/g;
1.313     albertel  401: 	$string=~s/([^\\]|^)\%/$1\\\%/g;
1.329     sakharuk  402: 	$string=~s/([^\\]|^)\$/$1\\\$/g;
                    403: 	$string=~s/([^\\])\_/$1\\_/g;
1.310     sakharuk  404: 	$string=~s/\$\$/\$\\\$/g;
1.329     sakharuk  405: 	$string=~s/\_\_/\_\\\_/g;
1.310     sakharuk  406: 	$string=~s/\#\#/\#\\\#/g;
1.313     albertel  407:         $string=~s/([^\\]|^)(\~|\^)/$1\\$2\\strut /g;
1.310     sakharuk  408: 	$string=~s/(>|<)/\\ensuremath\{$1\}/g; #more or less
1.311     albertel  409: 	$string=&Apache::lonprintout::character_chart($string);
                    410: 	# any & or # leftover should be safe to just escape
1.313     albertel  411:         $string=~s/([^\\]|^)\&/$1\\\&/g;
                    412:         $string=~s/([^\\]|^)\#/$1\\\#/g;
1.332     sakharuk  413:         $string=~s/\|/\$\\mid\$/g;
1.310     sakharuk  414: #single { or } How to escape?
1.229     sakharuk  415:     }
1.272     albertel  416:     return $string;
1.188     sakharuk  417: }
                    418: 
1.101     albertel  419: sub inner_xmlparse {
                    420:   my ($target,$stack,$parstack,$pars,$safeeval,$style_for_target)=@_;
                    421:   my $finaloutput = '';
                    422:   my $result;
                    423:   my $token;
1.258     albertel  424:   my $dontpop=0;
1.101     albertel  425:   while ( $#$pars > -1 ) {
                    426:     while ($token = $$pars['-1']->get_token) {
1.261     albertel  427:       if (($token->[0] eq 'T') || ($token->[0] eq 'C') ) {
1.101     albertel  428: 	if ($metamode<1) {
1.190     albertel  429: 	    my $text=$token->[1];
1.193     albertel  430: 	    if ($token->[0] eq 'C' && $target eq 'tex') {
1.239     sakharuk  431: 		$text = '';
                    432: #		$text = '%'.$text."\n";
1.182     sakharuk  433: 	    }
1.190     albertel  434: 	    $result.=$text;
1.101     albertel  435: 	}
1.261     albertel  436:       } elsif (($token->[0] eq 'D')) {
                    437: 	if ($metamode<1 && $target eq 'web') {
                    438: 	    my $text=$token->[1];
                    439: 	    $result.=$text;
                    440: 	}
1.101     albertel  441:       } elsif ($token->[0] eq 'PI') {
1.261     albertel  442: 	if ($metamode<1 && $target eq 'web') {
1.101     albertel  443: 	  $result=$token->[2];
                    444: 	}
                    445:       } elsif ($token->[0] eq 'S') {
1.140     albertel  446: 	# add tag to stack
1.101     albertel  447: 	push (@$stack,$token->[1]);
                    448: 	# add parameters list to another stack
                    449: 	push (@$parstack,&parstring($token));
1.140     albertel  450: 	&increasedepth($token);
1.212     albertel  451: 	if ($Apache::lonxml::usestyle &&
                    452: 	    exists($$style_for_target{$token->[1]})) {
                    453: 	    $Apache::lonxml::usestyle=0;
                    454: 	    my $string=$$style_for_target{$token->[1]}.
                    455: 	      '<LONCAPA_INTERNAL_TURN_STYLE_ON />';
                    456: 	    &Apache::lonxml::newparser($pars,\$string);
1.257     albertel  457: 	    $Apache::lonxml::style_values=$$parstack[-1];
1.259     albertel  458: 	    $Apache::lonxml::style_end_values=$$parstack[-1];
1.101     albertel  459: 	} else {
                    460: 	  $result = &callsub("start_$token->[1]", $target, $token, $stack,
                    461: 			     $parstack, $pars, $safeeval, $style_for_target);
1.140     albertel  462: 	}
1.101     albertel  463:       } elsif ($token->[0] eq 'E') {
1.212     albertel  464: 	if ($Apache::lonxml::usestyle &&
                    465: 	    exists($$style_for_target{'/'."$token->[1]"})) {
                    466: 	    $Apache::lonxml::usestyle=0;
                    467: 	    my $string=$$style_for_target{'/'.$token->[1]}.
1.258     albertel  468: 	      '<LONCAPA_INTERNAL_TURN_STYLE_ON end="'.$token->[1].'" />';
1.212     albertel  469: 	    &Apache::lonxml::newparser($pars,\$string);
1.259     albertel  470: 	    $Apache::lonxml::style_values=$Apache::lonxml::style_end_values;
                    471: 	    $Apache::lonxml::style_end_values='';
1.258     albertel  472: 	    $dontpop=1;
1.101     albertel  473: 	} else {
1.258     albertel  474: 	    #clear out any tags that didn't end
                    475: 	    while ($token->[1] ne $$stack['-1'] && ($#$stack > -1)) {
                    476: 		my $lasttag=$$stack[-1];
1.317     albertel  477: 		if ($token->[1] =~ /^\Q$lasttag\E$/i) {
1.258     albertel  478: 		    &Apache::lonxml::warning('Using tag &lt;/'.$token->[1].'&gt; on line '.$token->[3].' as end tag to &lt;'.$$stack[-1].'&gt;');
                    479: 		    last;
                    480: 		} else {
                    481: 		    &Apache::lonxml::warning('Found tag &lt;/'.$token->[1].'&gt; on line '.$token->[3].' when looking for &lt;/'.$$stack[-1].'&gt; in file');
                    482: 		    &end_tag($stack,$parstack,$token);
                    483: 		}
                    484: 	    }
                    485: 	    $result = &callsub("end_$token->[1]", $target, $token, $stack,
                    486: 			       $parstack, $pars,$safeeval, $style_for_target);
1.101     albertel  487: 	}
                    488:       } else {
                    489: 	&Apache::lonxml::error("Unknown token event :$token->[0]:$token->[1]:");
                    490:       }
                    491:       #evaluate variable refs in result
1.282     albertel  492:       if ($Apache::lonxml::post_evaluate &&$result ne "") {
1.257     albertel  493: 	  my $extras;
                    494: 	  if (!$Apache::lonxml::usestyle) {
                    495: 	      $extras=$Apache::lonxml::style_values;
                    496: 	  }
1.101     albertel  497: 	if ( $#$parstack > -1 ) {
1.257     albertel  498: 	  $result=&Apache::run::evaluate($result,$safeeval,$extras.$$parstack[-1]);
1.101     albertel  499: 	} else {
1.257     albertel  500: 	  $result= &Apache::run::evaluate($result,$safeeval,$extras);
1.101     albertel  501: 	}
1.163     albertel  502:       }
1.282     albertel  503:       $Apache::lonxml::post_evaluate=1;
                    504: 
1.190     albertel  505:       if (($token->[0] eq 'T') || ($token->[0] eq 'C') || ($token->[0] eq 'D') ) {
1.249     albertel  506: 	  #Style file definitions should be correct
1.250     albertel  507: 	  if ($target eq 'tex' && ($Apache::lonxml::usestyle)) {
1.311     albertel  508: 	      $result=&latex_special_symbols($result);
1.249     albertel  509: 	  }
1.190     albertel  510:       }
                    511: 
1.169     albertel  512:       if ($Apache::lonxml::redirection) {
                    513: 	$Apache::lonxml::outputstack['-1'] .= $result;
                    514:       } else {
                    515: 	$finaloutput.=$result;
                    516:       }
                    517:       $result = '';
                    518: 
1.258     albertel  519:       if ($token->[0] eq 'E' && !$dontpop) {
1.101     albertel  520: 	&end_tag($stack,$parstack,$token);
                    521:       }
1.258     albertel  522:       $dontpop=0;
1.224     albertel  523:     }	
1.212     albertel  524:     if ($#$pars > -1) {
                    525: 	pop @$pars;
                    526: 	pop @Apache::lonxml::pwd;
                    527:     }
1.101     albertel  528:   }
                    529: 
                    530:   # if ($target eq 'meta') {
                    531:   #   $finaloutput.=&endredirection;
                    532:   # }
                    533: 
1.169     albertel  534: 
1.101     albertel  535:   if (($ENV{'QUERY_STRING'}) && ($target eq 'web')) {
                    536:     $finaloutput=&afterburn($finaloutput);
1.216     sakharuk  537:   }	    
1.101     albertel  538:   return $finaloutput;
                    539: }
1.67      www       540: 
1.318     matthew   541: ## 
                    542: ## Looks to see if there is a subroutine defined for this tag.  If so, call it,
                    543: ## otherwise do not call it as we do not know what it is.
                    544: ##
1.7       albertel  545: sub callsub {
1.84      albertel  546:   my ($sub,$target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.7       albertel  547:   my $currentstring='';
1.72      albertel  548:   my $nodefault;
1.7       albertel  549:   {
1.59      albertel  550:     my $sub1;
1.7       albertel  551:     no strict 'refs';
1.68      www       552:     my $tag=$token->[1];
1.236     www       553: # get utterly rid of extended html tags
                    554:     if ($tag=~/^x\-/i) { return ''; }
1.141     albertel  555:     my $space=$Apache::lonxml::alltags{$tag}[-1];
1.68      www       556:     if (!$space) {
1.141     albertel  557:      	$tag=~tr/A-Z/a-z/;
1.68      www       558: 	$sub=~tr/A-Z/a-z/;
1.141     albertel  559: 	$space=$Apache::lonxml::alltags{$tag}[-1]
1.68      www       560:     }
1.97      albertel  561: 
                    562:     my $deleted=0;
                    563:     $Apache::lonxml::curdepth=join('_',@Apache::lonxml::depthcounter);
                    564:     if (($token->[0] eq 'S') && ($target eq 'modified')) {
                    565:       $deleted=&Apache::edit::handle_delete($space,$target,$token,$tagstack,
                    566: 					     $parstack,$parser,$safeeval,
                    567: 					     $style);
                    568:     }
                    569:     if (!$deleted) {
                    570:       if ($space) {
1.220     albertel  571: 	#&Apache::lonxml::debug("Calling sub $sub in $space $metamode");
1.97      albertel  572: 	$sub1="$space\:\:$sub";
                    573: 	($currentstring,$nodefault) = &$sub1($target,$token,$tagstack,
                    574: 					     $parstack,$parser,$safeeval,
                    575: 					     $style);
                    576:       } else {
1.318     matthew   577:           if ($target eq 'tex') {
                    578:               # throw away tag name
                    579:               return '';
                    580:           }
1.220     albertel  581: 	#&Apache::lonxml::debug("NOT Calling sub $sub in $space $metamode");
1.97      albertel  582: 	if ($metamode <1) {
                    583: 	  if (defined($token->[4]) && ($metamode < 1)) {
                    584: 	    $currentstring = $token->[4];
                    585: 	  } else {
                    586: 	    $currentstring = $token->[2];
                    587: 	  }
1.62      sakharuk  588: 	}
1.7       albertel  589:       }
1.97      albertel  590:       #    &Apache::lonxml::debug("nodefalt:$nodefault:");
                    591:       if ($currentstring eq '' && $nodefault eq '') {
                    592: 	if ($target eq 'edit') {
1.220     albertel  593: 	  #&Apache::lonxml::debug("doing default edit for $token->[1]");
1.97      albertel  594: 	  if ($token->[0] eq 'S') {
                    595: 	    $currentstring = &Apache::edit::tag_start($target,$token);
                    596: 	  } elsif ($token->[0] eq 'E') {
                    597: 	    $currentstring = &Apache::edit::tag_end($target,$token);
                    598: 	  }
                    599: 	} elsif ($target eq 'modified') {
                    600: 	  if ($token->[0] eq 'S') {
                    601: 	    $currentstring = $token->[4];
                    602: 	    $currentstring.=&Apache::edit::handle_insert();
1.210     www       603: 	  } elsif ($token->[0] eq 'E') {
                    604: 	    $currentstring = $token->[2];
                    605:             $currentstring.=&Apache::edit::handle_insertafter($token->[1]);
1.97      albertel  606: 	  } else {
                    607: 	    $currentstring = $token->[2];
                    608: 	  }
1.72      albertel  609: 	}
                    610:       }
1.7       albertel  611:     }
                    612:     use strict 'refs';
                    613:   }
                    614:   return $currentstring;
1.82      ng        615: }
                    616: 
1.96      albertel  617: sub setup_globals {
1.172     albertel  618:   my ($request,$target)=@_;
                    619:   $Apache::lonxml::request=$request;
1.99      albertel  620:   $Apache::lonxml::registered = 0;
1.325     www       621:   @Apache::lonxml::htmlareafields=();
1.205     www       622:   $errorcount=0;
                    623:   $warningcount=0;
1.207     albertel  624:   $Apache::lonxml::default_homework_loaded=0;
1.212     albertel  625:   $Apache::lonxml::usestyle=1;
1.204     albertel  626:   &init_counter();
1.101     albertel  627:   @Apache::lonxml::pwd=();
1.124     albertel  628:   @Apache::lonxml::extlinks=();
1.281     albertel  629:   @Apache::lonxml::ssi_info=();
1.282     albertel  630:   $Apache::lonxml::post_evaluate=1;
1.295     albertel  631:   $Apache::lonxml::warnings_error_header='';
1.96      albertel  632:   if ($target eq 'meta') {
                    633:     $Apache::lonxml::redirection = 0;
                    634:     $Apache::lonxml::metamode = 1;
                    635:     $Apache::lonxml::evaluate = 1;
                    636:     $Apache::lonxml::import = 0;
1.129     albertel  637:   } elsif ($target eq 'answer') {
                    638:     $Apache::lonxml::redirection = 0;
                    639:     $Apache::lonxml::metamode = 1;
                    640:     $Apache::lonxml::evaluate = 1;
                    641:     $Apache::lonxml::import = 1;
1.96      albertel  642:   } elsif ($target eq 'grade') {
                    643:     &startredirection;
                    644:     $Apache::lonxml::metamode = 0;
                    645:     $Apache::lonxml::evaluate = 1;
                    646:     $Apache::lonxml::import = 1;
                    647:   } elsif ($target eq 'modified') {
                    648:     $Apache::lonxml::redirection = 0;
                    649:     $Apache::lonxml::metamode = 0;
                    650:     $Apache::lonxml::evaluate = 0;
                    651:     $Apache::lonxml::import = 0;
                    652:   } elsif ($target eq 'edit') {
                    653:     $Apache::lonxml::redirection = 0;
                    654:     $Apache::lonxml::metamode = 0;
                    655:     $Apache::lonxml::evaluate = 0;
                    656:     $Apache::lonxml::import = 0;
1.163     albertel  657:   } elsif ($target eq 'analyze') {
                    658:     $Apache::lonxml::redirection = 0;
                    659:     $Apache::lonxml::metamode = 0;
                    660:     $Apache::lonxml::evaluate = 1;
                    661:     $Apache::lonxml::import = 1;
1.96      albertel  662:   } else {
                    663:     $Apache::lonxml::redirection = 0;
                    664:     $Apache::lonxml::metamode = 0;
                    665:     $Apache::lonxml::evaluate = 1;
                    666:     $Apache::lonxml::import = 1;
                    667:   }
                    668: }
                    669: 
1.82      ng        670: sub init_safespace {
                    671:   my ($target,$safeeval,$safehole,$safeinit) = @_;
                    672:   $safeeval->permit("entereval");
                    673:   $safeeval->permit(":base_math");
                    674:   $safeeval->permit("sort");
1.286     albertel  675:   $safeeval->permit("time");
1.82      ng        676:   $safeeval->deny(":base_io");
1.102     albertel  677:   $safehole->wrap(\&Apache::scripttag::xmlparse,$safeeval,'&xmlparse');
1.251     albertel  678:   $safehole->wrap(\&Apache::outputtags::multipart,$safeeval,'&multipart');
1.82      ng        679:   $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
1.324     albertel  680:   $safehole->wrap(\&Apache::chemresponse::chem_standard_order,$safeeval,
                    681: 		  '&chem_standard_order');
                    682: 
1.82      ng        683:   $safehole->wrap(\&Math::Cephes::asin,$safeeval,'&asin');
                    684:   $safehole->wrap(\&Math::Cephes::acos,$safeeval,'&acos');
                    685:   $safehole->wrap(\&Math::Cephes::atan,$safeeval,'&atan');
                    686:   $safehole->wrap(\&Math::Cephes::sinh,$safeeval,'&sinh');
                    687:   $safehole->wrap(\&Math::Cephes::cosh,$safeeval,'&cosh');
                    688:   $safehole->wrap(\&Math::Cephes::tanh,$safeeval,'&tanh');
                    689:   $safehole->wrap(\&Math::Cephes::asinh,$safeeval,'&asinh');
                    690:   $safehole->wrap(\&Math::Cephes::acosh,$safeeval,'&acosh');
                    691:   $safehole->wrap(\&Math::Cephes::atanh,$safeeval,'&atanh');
                    692:   $safehole->wrap(\&Math::Cephes::erf,$safeeval,'&erf');
                    693:   $safehole->wrap(\&Math::Cephes::erfc,$safeeval,'&erfc');
                    694:   $safehole->wrap(\&Math::Cephes::j0,$safeeval,'&j0');
                    695:   $safehole->wrap(\&Math::Cephes::j1,$safeeval,'&j1');
                    696:   $safehole->wrap(\&Math::Cephes::jn,$safeeval,'&jn');
                    697:   $safehole->wrap(\&Math::Cephes::jv,$safeeval,'&jv');
                    698:   $safehole->wrap(\&Math::Cephes::y0,$safeeval,'&y0');
                    699:   $safehole->wrap(\&Math::Cephes::y1,$safeeval,'&y1');
                    700:   $safehole->wrap(\&Math::Cephes::yn,$safeeval,'&yn');
                    701:   $safehole->wrap(\&Math::Cephes::yv,$safeeval,'&yv');
1.215     albertel  702:   
                    703:   $safehole->wrap(\&Math::Cephes::bdtr  ,$safeeval,'&bdtr'  );
                    704:   $safehole->wrap(\&Math::Cephes::bdtrc ,$safeeval,'&bdtrc' );
                    705:   $safehole->wrap(\&Math::Cephes::bdtri ,$safeeval,'&bdtri' );
                    706:   $safehole->wrap(\&Math::Cephes::btdtr ,$safeeval,'&btdtr' );
                    707:   $safehole->wrap(\&Math::Cephes::chdtr ,$safeeval,'&chdtr' );
                    708:   $safehole->wrap(\&Math::Cephes::chdtrc,$safeeval,'&chdtrc');
                    709:   $safehole->wrap(\&Math::Cephes::chdtri,$safeeval,'&chdtri');
                    710:   $safehole->wrap(\&Math::Cephes::fdtr  ,$safeeval,'&fdtr'  );
                    711:   $safehole->wrap(\&Math::Cephes::fdtrc ,$safeeval,'&fdtrc' );
                    712:   $safehole->wrap(\&Math::Cephes::fdtri ,$safeeval,'&fdtri' );
                    713:   $safehole->wrap(\&Math::Cephes::gdtr  ,$safeeval,'&gdtr'  );
                    714:   $safehole->wrap(\&Math::Cephes::gdtrc ,$safeeval,'&gdtrc' );
                    715:   $safehole->wrap(\&Math::Cephes::nbdtr ,$safeeval,'&nbdtr' );
                    716:   $safehole->wrap(\&Math::Cephes::nbdtrc,$safeeval,'&nbdtrc');
                    717:   $safehole->wrap(\&Math::Cephes::nbdtri,$safeeval,'&nbdtri');
                    718:   $safehole->wrap(\&Math::Cephes::ndtr  ,$safeeval,'&ndtr'  );
                    719:   $safehole->wrap(\&Math::Cephes::ndtri ,$safeeval,'&ndtri' );
                    720:   $safehole->wrap(\&Math::Cephes::pdtr  ,$safeeval,'&pdtr'  );
                    721:   $safehole->wrap(\&Math::Cephes::pdtrc ,$safeeval,'&pdtrc' );
                    722:   $safehole->wrap(\&Math::Cephes::pdtri ,$safeeval,'&pdtri' );
                    723:   $safehole->wrap(\&Math::Cephes::stdtr ,$safeeval,'&stdtr' );
                    724:   $safehole->wrap(\&Math::Cephes::stdtri,$safeeval,'&stdtri');
                    725: 
                    726: #  $safehole->wrap(\&Math::Cephes::new_fract,$safeeval,'&new_fract');
                    727: #  $safehole->wrap(\&Math::Cephes::radd,$safeeval,'&radd');
                    728: #  $safehole->wrap(\&Math::Cephes::rsub,$safeeval,'&rsub');
                    729: #  $safehole->wrap(\&Math::Cephes::rmul,$safeeval,'&rmul');
                    730: #  $safehole->wrap(\&Math::Cephes::rdiv,$safeeval,'&rdiv');
                    731: #  $safehole->wrap(\&Math::Cephes::euclid,$safeeval,'&euclid');
                    732: 
1.91      ng        733:   $safehole->wrap(\&Math::Random::random_beta,$safeeval,'&math_random_beta');
                    734:   $safehole->wrap(\&Math::Random::random_chi_square,$safeeval,'&math_random_chi_square');
                    735:   $safehole->wrap(\&Math::Random::random_exponential,$safeeval,'&math_random_exponential');
                    736:   $safehole->wrap(\&Math::Random::random_f,$safeeval,'&math_random_f');
                    737:   $safehole->wrap(\&Math::Random::random_gamma,$safeeval,'&math_random_gamma');
                    738:   $safehole->wrap(\&Math::Random::random_multivariate_normal,$safeeval,'&math_random_multivariate_normal');
                    739:   $safehole->wrap(\&Math::Random::random_multinomial,$safeeval,'&math_random_multinomial');
                    740:   $safehole->wrap(\&Math::Random::random_noncentral_chi_square,$safeeval,'&math_random_noncentral_chi_square');
                    741:   $safehole->wrap(\&Math::Random::random_noncentral_f,$safeeval,'&math_random_noncentral_f');
                    742:   $safehole->wrap(\&Math::Random::random_normal,$safeeval,'&math_random_normal');
                    743:   $safehole->wrap(\&Math::Random::random_permutation,$safeeval,'&math_random_permutation');
1.93      ng        744:   $safehole->wrap(\&Math::Random::random_permuted_index,$safeeval,'&math_random_permuted_index');
1.91      ng        745:   $safehole->wrap(\&Math::Random::random_uniform,$safeeval,'&math_random_uniform');
                    746:   $safehole->wrap(\&Math::Random::random_poisson,$safeeval,'&math_random_poisson');
                    747:   $safehole->wrap(\&Math::Random::random_uniform_integer,$safeeval,'&math_random_uniform_integer');
                    748:   $safehole->wrap(\&Math::Random::random_negative_binomial,$safeeval,'&math_random_negative_binomial');
                    749:   $safehole->wrap(\&Math::Random::random_binomial,$safeeval,'&math_random_binomial');
                    750:   $safehole->wrap(\&Math::Random::random_seed_from_phrase,$safeeval,'&random_seed_from_phrase');
                    751:   $safehole->wrap(\&Math::Random::random_set_seed_from_phrase,$safeeval,'&random_set_seed_from_phrase');
                    752:   $safehole->wrap(\&Math::Random::random_get_seed,$safeeval,'&random_get_seed');
                    753:   $safehole->wrap(\&Math::Random::random_set_seed,$safeeval,'&random_set_seed');
1.305     albertel  754:   $safehole->wrap(\&Apache::lonxml::error,$safeeval,'&LONCAPA_INTERNAL_ERROR');
1.311     albertel  755:   $safehole->wrap(\&Apache::lonxml::debug,$safeeval,'&LONCAPA_INTERNAL_DEBUG');
1.322     albertel  756:   $safehole->wrap(\&Apache::caparesponse::get_sigrange,$safeeval,'&LONCAPA_INTERNAL_get_sigrange');
1.91      ng        757: 
1.82      ng        758: #need to inspect this class of ops
                    759: # $safeeval->deny(":base_orig");
1.331     albertel  760:   $safeeval->permit("require");
1.91      ng        761:   $safeinit .= ';$external::target="'.$target.'";';
1.121     albertel  762:   my $rndseed;
1.123     albertel  763:   my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
                    764:   $rndseed=&Apache::lonnet::rndseed($symb,$courseid,$domain,$name);
1.319     albertel  765:   $safeinit .= ';$external::randomseed="'.$rndseed.'";';
1.249     albertel  766:   &Apache::lonxml::debug("Setting rndseed to $rndseed");
1.82      ng        767:   &Apache::run::run($safeinit,$safeeval);
1.303     albertel  768: 
1.207     albertel  769: }
                    770: 
                    771: sub default_homework_load {
                    772:     my ($safeeval)=@_;
                    773:     &Apache::lonxml::debug('Loading default_homework');
                    774:     my $default=&Apache::lonnet::getfile('/home/httpd/html/res/adm/includes/default_homework.lcpm');
1.241     albertel  775:     if ($default eq -1) {
1.207     albertel  776: 	&Apache::lonxml::error("<b>Unable to find <i>default_homework.lcpm</i></b>");
                    777:     } else {
                    778: 	&Apache::run::run($default,$safeeval);
                    779: 	$Apache::lonxml::default_homework_loaded=1;
                    780:     }
1.17      albertel  781: }
                    782: 
1.328     albertel  783: my $metamode_was;
1.55      albertel  784: sub startredirection {
1.328     albertel  785:     if (!$Apache::lonxml::redirection) {
                    786: 	$metamode_was=$Apache::lonxml::metamode;
                    787:     }
                    788:     $Apache::lonxml::metamode=0;
                    789:     $Apache::lonxml::redirection++;
                    790:     push (@Apache::lonxml::outputstack, '');
1.55      albertel  791: }
                    792: 
                    793: sub endredirection {
1.328     albertel  794:     if (!$Apache::lonxml::redirection) {
                    795: 	&Apache::lonxml::error("Endredirection was called, before a startredirection, perhaps you have unbalanced tags. Some debuging information:".join ":",caller);
                    796: 	return '';
                    797:     }
                    798:     $Apache::lonxml::redirection--;
                    799:     if (!$Apache::lonxml::redirection) {
                    800: 	$Apache::lonxml::metamode=$metamode_was;
                    801:     }
                    802:     pop @Apache::lonxml::outputstack;
1.97      albertel  803: }
                    804: 
                    805: sub end_tag {
                    806:   my ($tagstack,$parstack,$token)=@_;
                    807:   pop(@$tagstack);
                    808:   pop(@$parstack);
                    809:   &decreasedepth($token);
1.55      albertel  810: }
                    811: 
1.17      albertel  812: sub initdepth {
                    813:   @Apache::lonxml::depthcounter=();
                    814:   $Apache::lonxml::depth=-1;
                    815:   $Apache::lonxml::olddepth=-1;
                    816: }
                    817: 
1.339     albertel  818: my @timers;
                    819: my $lasttime;
1.17      albertel  820: sub increasedepth {
1.19      albertel  821:   my ($token) = @_;
1.17      albertel  822:   $Apache::lonxml::depth++;
                    823:   $Apache::lonxml::depthcounter[$Apache::lonxml::depth]++;
                    824:   if ($Apache::lonxml::depthcounter[$Apache::lonxml::depth]==1) {
                    825:     $Apache::lonxml::olddepth=$Apache::lonxml::depth;
                    826:   }
1.340     albertel  827:   my $time;
                    828:   if ($Apache::lonxml::debug eq "1") {
                    829:       push(@timers,[&gettimeofday()]);
                    830:       $time=&tv_interval($lasttime);
                    831:       $lasttime=[&gettimeofday()];
                    832:   }
1.339     albertel  833:   my $spacing='  'x($Apache::lonxml::depth-1);
1.42      albertel  834:   my $curdepth=join('_',@Apache::lonxml::depthcounter);
1.339     albertel  835:   &Apache::lonxml::debug("s$spacing$Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1] : $time : \n");
1.54      albertel  836: #print "<br />s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n";
1.17      albertel  837: }
                    838: 
                    839: sub decreasedepth {
1.19      albertel  840:   my ($token) = @_;
1.17      albertel  841:   $Apache::lonxml::depth--;
1.36      albertel  842:   if ($Apache::lonxml::depth<$Apache::lonxml::olddepth-1) {
                    843:     $#Apache::lonxml::depthcounter--;
                    844:     $Apache::lonxml::olddepth=$Apache::lonxml::depth+1;
                    845:   }
1.43      albertel  846:   if (  $Apache::lonxml::depth < -1) {
1.280     www       847:     &Apache::lonxml::warning(&mt("Missing tags, unable to properly run file."));
1.43      albertel  848:     $Apache::lonxml::depth='-1';
                    849:   }
1.340     albertel  850:   my ($timer,$time);
                    851:   if ($Apache::lonxml::debug eq "1") {
                    852:       $timer=pop(@timers);
                    853:       $time=&tv_interval($lasttime);
                    854:       $lasttime=[&gettimeofday()];
                    855:   }
1.339     albertel  856:   my $spacing='  'x$Apache::lonxml::depth;
1.42      albertel  857:   my $curdepth=join('_',@Apache::lonxml::depthcounter);
1.339     albertel  858:   &Apache::lonxml::debug("e$spacing$Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1] : $time : ".&tv_interval($timer)."\n");
1.54      albertel  859: #print "<br />e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n";
1.1       sakharuk  860: }
1.19      albertel  861: 
1.180     albertel  862: sub get_all_text_unbalanced {
1.190     albertel  863: #there is a copy of this in lonpublisher.pm
1.326     albertel  864:     my($tag,$pars)= @_;
                    865:     my $token;
                    866:     my $result='';
                    867:     $tag='<'.$tag.'>';
                    868:     while ($token = $$pars[-1]->get_token) {
                    869: 	if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
                    870: 	    $result.=$token->[1];
                    871: 	} elsif ($token->[0] eq 'PI') {
                    872: 	    $result.=$token->[2];
                    873: 	} elsif ($token->[0] eq 'S') {
                    874: 	    $result.=$token->[4];
                    875: 	} elsif ($token->[0] eq 'E')  {
                    876: 	    $result.=$token->[2];
                    877: 	}
                    878: 	if ($result =~ /\Q$tag\E/is) {
                    879: 	    ($result,my $redo)=$result =~ /(.*)\Q$tag\E(.*)/is;
                    880: 	    #&Apache::lonxml::debug('Got a winner with leftovers ::'.$2);
                    881: 	    #&Apache::lonxml::debug('Result is :'.$1);
                    882: 	    $redo=$tag.$redo;
                    883: 	    &Apache::lonxml::newparser($pars,\$redo);
                    884: 	    last;
                    885: 	}
                    886:     }
                    887:     return $result
1.204     albertel  888: }
                    889: 
                    890: sub increment_counter {
1.247     albertel  891:     my ($increment) = @_;
1.289     sakharuk  892:     if (defined($increment) && $increment gt 0) {
                    893: 	$Apache::lonxml::counter+=$increment;
                    894:     } else {
                    895: 	$Apache::lonxml::counter++;
1.247     albertel  896:     }
1.289     sakharuk  897:     $Apache::lonxml::counter_changed=1;
1.204     albertel  898: }
                    899: 
                    900: sub init_counter {
                    901:     if (defined($ENV{'form.counter'})) {
                    902: 	$Apache::lonxml::counter=$ENV{'form.counter'};
1.247     albertel  903: 	$Apache::lonxml::counter_changed=0;
1.237     sakharuk  904:     } else {
1.204     albertel  905: 	$Apache::lonxml::counter=1;
1.247     albertel  906: 	$Apache::lonxml::counter_changed=1;
1.204     albertel  907:     }
                    908: }
                    909: 
                    910: sub store_counter {
                    911:     &Apache::lonnet::appenv(('form.counter' => $Apache::lonxml::counter));
                    912:     return '';
1.180     albertel  913: }
                    914: 
1.19      albertel  915: sub get_all_text {
1.270     albertel  916:     my($tag,$pars,$style)= @_;
                    917:     my $gotfullstack=1;
                    918:     if (ref($pars) ne 'ARRAY') {
                    919: 	$gotfullstack=0;
                    920: 	$pars=[$pars];
                    921:     }
                    922:     if (ref($style) ne 'HASH') {
                    923: 	$style={};
                    924:     }
                    925:     my $depth=0;
                    926:     my $token;
                    927:     my $result='';
                    928:     if ( $tag =~ m:^/: ) { 
                    929: 	my $tag=substr($tag,1); 
                    930: 	#&Apache::lonxml::debug("have:$tag:");
                    931: 	my $top_empty=0;
                    932: 	while (($depth >=0) && ($#$pars > -1) && (!$top_empty)) {
                    933: 	    while (($depth >=0) && ($token = $$pars[-1]->get_token)) {
                    934: 		#&Apache::lonxml::debug("e token:$token->[0]:$depth:$token->[1]:".$#$pars.":".$#Apache::lonxml::pwd);
                    935: 		if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
                    936: 		    $result.=$token->[1];
                    937: 		} elsif ($token->[0] eq 'PI') {
                    938: 		    $result.=$token->[2];
                    939: 		} elsif ($token->[0] eq 'S') {
1.316     albertel  940: 		    if ($token->[1] =~ /^\Q$tag\E$/i) { $depth++; }
                    941: 		    if ($token->[1] =~ /^LONCAPA_INTERNAL_TURN_STYLE_ON$/) { $Apache::lonxml::usestyle=1; }
                    942: 		    if ($token->[1] =~ /^LONCAPA_INTERNAL_TURN_STYLE_OFF$/) { $Apache::lonxml::usestyle=0; }
1.270     albertel  943: 		    $result.=$token->[4];
                    944: 		} elsif ($token->[0] eq 'E')  {
1.316     albertel  945: 		    if ( $token->[1] =~ /^\Q$tag\E$/i) { $depth--; }
1.270     albertel  946: 		    #skip sending back the last end tag
1.283     albertel  947: 		    if ($depth == 0 && exists($$style{'/'.$token->[1]}) && $Apache::lonxml::usestyle) {
1.270     albertel  948: 			my $string=
                    949: 			    '<LONCAPA_INTERNAL_TURN_STYLE_OFF end="yes" />'.
                    950: 				$$style{'/'.$token->[1]}.
                    951: 				    $token->[2].
                    952: 					'<LONCAPA_INTERNAL_TURN_STYLE_ON />';
                    953: 			&Apache::lonxml::newparser($pars,\$string);
                    954: 			#&Apache::lonxml::debug("reParsing $string");
                    955: 			next;
                    956: 		    }
                    957: 		    if ($depth > -1) {
                    958: 			$result.=$token->[2];
                    959: 		    } else {
                    960: 			$$pars[-1]->unget_token($token);
                    961: 		    }
                    962: 		}
                    963: 	    }
                    964: 	    if (($depth >=0) && ($#$pars == 0) ) { $top_empty=1; }
                    965: 	    if (($depth >=0) && ($#$pars > 0) ) {
                    966: 		pop(@$pars);
                    967: 		pop(@Apache::lonxml::pwd);
                    968: 	    }
                    969: 	}
                    970: 	if ($top_empty && $depth >= 0) {
                    971: 	    #never found the end tag ran out of text, throw error send back blank
                    972: 	    &error('Never found end tag for &lt;'.$tag.
                    973: 		   '&gt; current string <pre>'.
1.314     albertel  974: 		   &HTML::Entities::encode($result,'<>&"').
1.270     albertel  975: 		   '</pre>');
                    976: 	    if ($gotfullstack) {
                    977: 		my $newstring='</'.$tag.'>'.$result;
                    978: 		&Apache::lonxml::newparser($pars,\$newstring);
                    979: 	    }
                    980: 	    $result='';
                    981: 	}
                    982:     } else {
                    983: 	while ($#$pars > -1) {
                    984: 	    while ($token = $$pars[-1]->get_token) {
                    985: 		#&Apache::lonxml::debug("s token:$token->[0]:$depth:$token->[1]");
                    986: 		if (($token->[0] eq 'T')||($token->[0] eq 'C')||
                    987: 		    ($token->[0] eq 'D')) {
                    988: 		    $result.=$token->[1];
                    989: 		} elsif ($token->[0] eq 'PI') {
                    990: 		    $result.=$token->[2];
                    991: 		} elsif ($token->[0] eq 'S') {
1.316     albertel  992: 		    if ( $token->[1] =~ /^\Q$tag\E$/i) {
1.270     albertel  993: 			$$pars[-1]->unget_token($token); last;
                    994: 		    } else {
                    995: 			$result.=$token->[4];
                    996: 		    }
1.316     albertel  997: 		    if ($token->[1] =~ /^LONCAPA_INTERNAL_TURN_STYLE_ON$/) { $Apache::lonxml::usestyle=1; }
                    998: 		    if ($token->[1] =~ /^LONCAPA_INTERNAL_TURN_STYLE_OFF$/) { $Apache::lonxml::usestyle=0; }
1.270     albertel  999: 		} elsif ($token->[0] eq 'E')  {
                   1000: 		    $result.=$token->[2];
                   1001: 		}
                   1002: 	    }
                   1003: 	    if (($#$pars > 0) ) {
                   1004: 		pop(@$pars);
                   1005: 		pop(@Apache::lonxml::pwd);
                   1006: 	    } else { last; }
                   1007: 	}
                   1008:     }
                   1009:     #&Apache::lonxml::debug("Exit:$result:");
                   1010:     return $result
1.19      albertel 1011: }
                   1012: 
1.23      albertel 1013: sub newparser {
                   1014:   my ($parser,$contentref,$dir) = @_;
1.167     albertel 1015:   push (@$parser,HTML::LCParser->new($contentref));
1.56      albertel 1016:   $$parser['-1']->xml_mode('1');
1.23      albertel 1017:   if ( $dir eq '' ) {
                   1018:     push (@Apache::lonxml::pwd, $Apache::lonxml::pwd[$#Apache::lonxml::pwd]);
                   1019:   } else {
                   1020:     push (@Apache::lonxml::pwd, $dir);
                   1021:   } 
                   1022: }
1.1       sakharuk 1023: 
1.8       albertel 1024: sub parstring {
                   1025:   my ($token) = @_;
                   1026:   my $temp='';
1.142     albertel 1027:   foreach (@{$token->[3]}) {
1.35      www      1028:     unless ($_=~/\W/) {
1.42      albertel 1029:       my $val=$token->[2]->{$_};
1.231     albertel 1030:       $val =~ s/([\%\@\\\"\'])/\\$1/g;
1.341   ! albertel 1031:       $val =~ s/^(\$\d)/\\$1/;
1.51      albertel 1032:       #if ($val =~ m/^[\%\@]/) { $val="\\".$val; }
1.267     sakharuk 1033:       $temp .= "my \$$_=\"$val\";";
1.20      albertel 1034:     }
1.142     albertel 1035:   }
1.8       albertel 1036:   return $temp;
                   1037: }
1.22      albertel 1038: 
1.34      www      1039: sub writeallows {
1.126     www      1040:     unless ($#extlinks>=0) { return; }
1.34      www      1041:     my $thisurl='/res/'.&Apache::lonnet::declutter(shift);
1.111     www      1042:     if ($ENV{'httpref.'.$thisurl}) {
                   1043: 	$thisurl=$ENV{'httpref.'.$thisurl};
                   1044:     }
1.34      www      1045:     my $thisdir=$thisurl;
                   1046:     $thisdir=~s/\/[^\/]+$//;
                   1047:     my %httpref=();
1.142     albertel 1048:     foreach (@extlinks) {
1.34      www      1049:        $httpref{'httpref.'.
1.125     www      1050:  	        &Apache::lonnet::hreflocation($thisdir,$_)}=$thisurl;
1.142     albertel 1051:     }
1.126     www      1052:     @extlinks=();
1.34      www      1053:     &Apache::lonnet::appenv(%httpref);
                   1054: }
                   1055: 
1.281     albertel 1056: sub register_ssi {
                   1057:     my ($url,%form)=@_;
                   1058:     push (@Apache::lonxml::ssi_info,{'url'=>$url,'form'=>\%form});
                   1059:     return '';
                   1060: }
                   1061: 
                   1062: sub do_registered_ssi {
                   1063:     foreach my $info (@Apache::lonxml::ssi_info) {
                   1064: 	my %form=%{ $info->{'form'}};
                   1065: 	my $url=$info->{'url'};
                   1066: 	&Apache::lonnet::ssi($url,%form);
                   1067:     }
                   1068: }
1.66      www      1069: #
                   1070: # Afterburner handles anchors, highlights and links
                   1071: #
                   1072: sub afterburn {
                   1073:     my $result=shift;
1.154     albertel 1074:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                   1075: 					    ['highlight','anchor','link']);
1.66      www      1076:     if ($ENV{'form.highlight'}) {
1.142     albertel 1077:        foreach (split(/\,/,$ENV{'form.highlight'})) {
1.66      www      1078:            my $anchorname=$_;
                   1079: 	   my $matchthis=$anchorname;
                   1080:            $matchthis=~s/\_+/\\s\+/g;
1.317     albertel 1081:            $result=~s/(\Q$matchthis\E)/\<font color=\"red\"\>$1\<\/font\>/gs;
1.142     albertel 1082:        }
1.66      www      1083:     }
                   1084:     if ($ENV{'form.link'}) {
1.142     albertel 1085:        foreach (split(/\,/,$ENV{'form.link'})) {
1.66      www      1086:            my ($anchorname,$linkurl)=split(/\>/,$_);
                   1087: 	   my $matchthis=$anchorname;
                   1088:            $matchthis=~s/\_+/\\s\+/g;
1.317     albertel 1089:            $result=~s/(\Q$matchthis\E)/\<a href=\"$linkurl\"\>$1\<\/a\>/gs;
1.142     albertel 1090:        }
1.66      www      1091:     }
                   1092:     if ($ENV{'form.anchor'}) {
                   1093:         my $anchorname=$ENV{'form.anchor'};
                   1094: 	my $matchthis=$anchorname;
                   1095:         $matchthis=~s/\_+/\\s\+/g;
1.317     albertel 1096:         $result=~s/(\Q$matchthis\E)/\<a name=\"$anchorname\"\>$1\<\/a\>/s;
1.66      www      1097:         $result.=(<<"ENDSCRIPT");
1.226     albertel 1098: <script type="text/javascript">
1.66      www      1099:     document.location.hash='$anchorname';
                   1100: </script>
                   1101: ENDSCRIPT
                   1102:     }
                   1103:     return $result;
                   1104: }
                   1105: 
1.79      www      1106: sub storefile {
                   1107:     my ($file,$contents)=@_;
1.290     albertel 1108:     &Apache::lonnet::correct_line_ends(\$contents);
1.79      www      1109:     if (my $fh=Apache::File->new('>'.$file)) {
                   1110: 	print $fh $contents;
                   1111:         $fh->close();
1.271     www      1112:         return 1;
1.147     albertel 1113:     } else {
1.271     www      1114: 	&warning("Unable to save file $file");
                   1115: 	return 0;
1.79      www      1116:     }
                   1117: }
                   1118: 
1.151     albertel 1119: sub createnewhtml {
1.321     www      1120:     my $title=&mt('Title of document goes here');
                   1121:     my $body=&mt('Body of document goes here');
                   1122:     my $filecontents=(<<SIMPLECONTENT);
                   1123: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml/11/DTD/xhtml11.dtd">
1.78      www      1124: <html>
                   1125: <head>
1.321     www      1126: <title>$title</title>
1.78      www      1127: </head>
                   1128: <body bgcolor="#FFFFFF">
1.321     www      1129: $body
1.78      www      1130: </body>
                   1131: </html>
                   1132: SIMPLECONTENT
1.321     www      1133:     return $filecontents;
1.151     albertel 1134: }
                   1135: 
1.274     albertel 1136: sub createnewsty {
                   1137:   my $filecontents=(<<SIMPLECONTENT);
                   1138: <definetag name="">
                   1139:     <render>
                   1140:        <web></web>
                   1141:        <tex></tex>
                   1142:     </render>
                   1143: </definetag>
                   1144: SIMPLECONTENT
                   1145:   return $filecontents;
                   1146: }
                   1147: 
1.147     albertel 1148: 
1.151     albertel 1149: sub inserteditinfo {
1.274     albertel 1150:       my ($result,$filecontents,$filetype)=@_;
1.314     albertel 1151:       $filecontents = &HTML::Entities::encode($filecontents,'<>&"');
1.147     albertel 1152: #      my $editheader='<a href="#editsection">Edit below</a><hr />';
1.274     albertel 1153:       my $xml_help = '';
1.321     www      1154:       my $initialize='';
1.274     albertel 1155:       if ($filetype eq 'html') {
1.323     www      1156: 	  my $addbuttons=&Apache::lonhtmlcommon::htmlareaaddbuttons();
1.333     www      1157: 	  $initialize=&Apache::lonhtmlcommon::htmlareaheaders().
                   1158: 	      &Apache::lonhtmlcommon::spellheader().(<<FULLPAGE);
1.321     www      1159: <script type="text/javascript">
1.323     www      1160: $addbuttons
                   1161: 
1.321     www      1162:     HTMLArea.loadPlugin("FullPage");
                   1163: 
                   1164:     function initDocument() {
1.323     www      1165: 	var editor=new HTMLArea("filecont",config);
1.321     www      1166: 	editor.registerPlugin(FullPage);
                   1167: 	editor.generate();
                   1168:     }
                   1169: </script>
                   1170: FULLPAGE
                   1171:           $result=~s/\<body([^\>]*)\>/\<body onload="initDocument()" $1\>/i;
                   1172: 	  $xml_help=&Apache::loncommon::helpLatexCheatsheet();
1.274     albertel 1173:       }
                   1174:       my $cleanbut = '';
                   1175:       if ($filetype eq 'html') {
1.280     www      1176: 	  $cleanbut='<input type="submit" name="attemptclean" value="'.
                   1177: 	      &mt('Save and then attempt to clean HTML').'" />';
1.274     albertel 1178:       }
1.254     albertel 1179:       my $titledisplay=&display_title();
1.280     www      1180:       my %lt=&Apache::lonlocal::texthash('st' => 'Save this',
                   1181: 					 'vi' => 'View',
                   1182: 					 'ed' => 'Edit');
1.161     albertel 1183:       my $buttons=(<<BUTTONS);
1.274     albertel 1184: $cleanbut
1.304     matthew  1185: <input type="submit" name="savethisfile" accesskey="s"  value="$lt{'st'}" />
                   1186: <input type="submit" name="viewmode" accesskey="v" value="$lt{'vi'}" />
1.161     albertel 1187: BUTTONS
1.333     www      1188:       $buttons.=&Apache::lonhtmlcommon::spelllink('xmledit','filecont');
1.338     albertel 1189:       $buttons.=&Apache::lonhtmlcommon::htmlareaselectactive('filecont');
1.78      www      1190:       my $editfooter=(<<ENDFOOTER);
1.321     www      1191: $initialize
1.78      www      1192: <hr />
                   1193: <a name="editsection" />
1.333     www      1194: <form method="post" name="xmledit">
1.240     albertel 1195: $xml_help
1.280     www      1196: <input type="hidden" name="editmode" value="$lt{'ed'}" />
1.170     www      1197: $buttons<br />
1.321     www      1198: <textarea cols="80" rows="44" name="filecont" id="filecont">$filecontents</textarea>
1.170     www      1199: <br />$buttons
1.78      www      1200: <br />
                   1201: </form>
1.254     albertel 1202: $titledisplay
1.321     www      1203: </body>
1.78      www      1204: ENDFOOTER
1.147     albertel 1205: #      $result=~s/(\<body[^\>]*\>)/$1$editheader/is;
1.78      www      1206:       $result=~s/(\<\/body\>)/$editfooter/is;
                   1207:       return $result;
                   1208: }
                   1209: 
1.152     albertel 1210: sub get_target {
                   1211:   my $viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
                   1212:   if ( $ENV{'request.state'} eq 'published') {
                   1213:     if ( defined($ENV{'form.grade_target'})
                   1214: 	 && ($viewgrades == 'F' )) {
                   1215:       return ($ENV{'form.grade_target'});
1.153     albertel 1216:     } elsif (defined($ENV{'form.grade_target'})) {
                   1217:       if (($ENV{'form.grade_target'} eq 'web') ||
                   1218: 	  ($ENV{'form.grade_target'} eq 'tex') ) {
                   1219: 	return $ENV{'form.grade_target'}
                   1220:       } else {
                   1221: 	return 'web';
                   1222:       }
1.152     albertel 1223:     } else {
                   1224:       return 'web';
                   1225:     }
                   1226:   } elsif ($ENV{'request.state'} eq 'construct') {
                   1227:     if ( defined($ENV{'form.grade_target'})) {
                   1228:       return ($ENV{'form.grade_target'});
                   1229:     } else {
                   1230:       return 'web';
                   1231:     }
                   1232:   } else {
                   1233:     return 'web';
                   1234:   }
                   1235: }
                   1236: 
1.24      sakharuk 1237: sub handler {
1.255     sakharuk 1238:     my $request=shift;
                   1239:     
                   1240:     my $target=&get_target();
                   1241:     
1.258     albertel 1242:     $Apache::lonxml::debug=$ENV{'user.debug'};
1.255     sakharuk 1243:     
                   1244:     if ($ENV{'browser.mathml'}) {
1.280     www      1245: 	&Apache::loncommon::content_type($request,'text/xml');
1.255     sakharuk 1246:     } else {
1.280     www      1247: 	&Apache::loncommon::content_type($request,'text/html');
1.255     sakharuk 1248:     }
                   1249:     &Apache::loncommon::no_cache($request);
                   1250:     $request->send_http_header;
                   1251:     
                   1252:     return OK if $request->header_only;
1.68      www      1253: 
                   1254: 
1.255     sakharuk 1255:     my $file=&Apache::lonnet::filelocation("",$request->uri);
1.274     albertel 1256:     my $filetype;
                   1257:     if ($file =~ /\.sty$/) {
                   1258: 	$filetype='sty';
                   1259:     } else {
                   1260: 	$filetype='html';
                   1261:     }
1.78      www      1262: #
                   1263: # Edit action? Save file.
                   1264: #
1.255     sakharuk 1265:     unless ($ENV{'request.state'} eq 'published') {
                   1266: 	if (($ENV{'form.savethisfile'}) || ($ENV{'form.attemptclean'})) {
1.271     www      1267: 	    if (&storefile($file,$ENV{'form.filecont'})) {
1.309     albertel 1268: 		&Apache::lonxml::info("<font COLOR=\"#0000FF\">".
                   1269: 				      &mt('Updated').": ".
                   1270: 				      &Apache::lonlocal::locallocaltime(time).
                   1271: 				      " </font>");
1.271     www      1272: 	    } 
1.255     sakharuk 1273: 	}
                   1274:     }
                   1275:     my %mystyle;
                   1276:     my $result = '';
                   1277:     my $filecontents=&Apache::lonnet::getfile($file);
                   1278:     if ($filecontents eq -1) {
1.284     www      1279: 	my $bodytag=&Apache::loncommon::bodytag('File Error');
                   1280: 	my $fnf=&mt('File not found');
1.255     sakharuk 1281: 	$result=(<<ENDNOTFOUND);
1.78      www      1282: <html>
                   1283: <head>
1.284     www      1284: <title>$fnf</title>
1.78      www      1285: </head>
1.284     www      1286: $bodytag
                   1287: <b>$fnf: $file</b>
1.78      www      1288: </body>
                   1289: </html>
                   1290: ENDNOTFOUND
1.50      albertel 1291:     $filecontents='';
1.255     sakharuk 1292: 	if ($ENV{'request.state'} ne 'published') {
1.274     albertel 1293: 	    if ($filetype eq 'sty') {
                   1294: 		$filecontents=&createnewsty();
                   1295: 	    } else {
                   1296: 		$filecontents=&createnewhtml();
                   1297: 	    }
1.255     sakharuk 1298: 	    $ENV{'form.editmode'}='Edit'; #force edit mode
                   1299: 	}
                   1300:     } else {
                   1301: 	unless ($ENV{'request.state'} eq 'published') {
                   1302: 	    if ($ENV{'form.attemptclean'}) {
                   1303: 		$filecontents=&htmlclean($filecontents,1);
                   1304: 	    }
1.264     www      1305: #
                   1306: # we are in construction space, see if edit mode forced
                   1307:             &Apache::loncommon::get_unprocessed_cgi
                   1308:                           ($ENV{'QUERY_STRING'},['editmode']);
1.255     sakharuk 1309: 	}
                   1310: 	if (!$ENV{'form.editmode'} || $ENV{'form.viewmode'}) {
                   1311: 	    $result = &Apache::lonxml::xmlparse($request,$target,$filecontents,
                   1312: 						'',%mystyle);
                   1313: 	}
1.147     albertel 1314:     }
1.255     sakharuk 1315:     
1.78      www      1316: #
                   1317: # Edit action? Insert editing commands
                   1318: #
1.255     sakharuk 1319:     unless ($ENV{'request.state'} eq 'published') {
                   1320: 	if ($ENV{'form.editmode'} && (!($ENV{'form.viewmode'}))) {
                   1321: 	    my $displayfile=$request->uri;
                   1322: 	    $displayfile=~s/^\/[^\/]*//;
1.309     albertel 1323: 	    $result='<html><body bgcolor="#FFFFFF">'.
                   1324: 		&Apache::lonxml::message_location().'<h3>'.
                   1325: 		$displayfile.
1.255     sakharuk 1326: 		'</h3></body></html>';
1.274     albertel 1327: 	    $result=&inserteditinfo($result,$filecontents,$filetype);
1.255     sakharuk 1328: 	}
1.147     albertel 1329:     }
1.274     albertel 1330:     if ($filetype eq 'html') { writeallows($request->uri); }
                   1331: 	
1.255     sakharuk 1332:     
1.309     albertel 1333:     &Apache::lonxml::add_messages(\$result);
1.255     sakharuk 1334:     $request->print($result);
                   1335:     
                   1336:     return OK;
1.253     albertel 1337: }
                   1338: 
                   1339: sub display_title {
                   1340:     my $result;
                   1341:     if ($ENV{'request.state'} eq 'construct') {
                   1342: 	my $title=&Apache::lonnet::gettitle();
                   1343: 	if (!defined($title) || $title eq '') {
                   1344: 	    $title = $ENV{'request.filename'};
                   1345: 	    $title = substr($title, rindex($title, '/') + 1);
                   1346: 	}
                   1347: 	$result = "<script type='text/javascript'>top.document.title = '$title - LON-CAPA Construction Space';</script>";
                   1348:     }
                   1349:     return $result;
1.24      sakharuk 1350: }
1.147     albertel 1351: 
1.22      albertel 1352: sub debug {
1.298     albertel 1353:     if ($Apache::lonxml::debug eq "1") {
                   1354: 	$|=1;
1.300     albertel 1355: 	my $request=$Apache::lonxml::request;
                   1356: 	if (!$request) { $request=Apache->request; }
1.314     albertel 1357: 	$request->print('<font size="-2"><pre>DEBUG:'.&HTML::Entities::encode($_[0],'<>&"')."</pre></font>\n");
1.327     sakharuk 1358: #	&Apache::lonnet::logthis($_[0]);
1.298     albertel 1359:     }
1.22      albertel 1360: }
1.49      albertel 1361: 
1.22      albertel 1362: sub error {
1.336     albertel 1363:     $errorcount++;
                   1364:     if (($Apache::lonxml::debug eq 1) || ($ENV{'request.state'} eq 'construct') ) {
                   1365: 	# If printing in construction space, put the error inside <pre></pre>
                   1366: 	push(@Apache::lonxml::error_messages,
                   1367: 	     $Apache::lonxml::warnings_error_header.
                   1368: 	     "<b>ERROR:</b>".join("<br />\n",@_)."<br />\n");
                   1369: 	$Apache::lonxml::warnings_error_header='';
                   1370:     } else {
                   1371: 	my $errormsg;
                   1372: 	my ($symb)=&Apache::lonnet::symbread();
                   1373: 	if ( !$symb ) {
                   1374: 	    #public or browsers
                   1375: 	    $errormsg=&mt("An error occured while processing this resource. The author has been notified.");
                   1376: 	} 
                   1377: 	#notify author
                   1378: 	&Apache::lonmsg::author_res_msg($ENV{'request.filename'},join('<br />',@_));
                   1379: 	#notify course
                   1380: 	if ( $symb && $ENV{'request.course.id'} ) {
                   1381: 	    my (undef,%users)=&Apache::lonfeedback::decide_receiver(undef,0,1,1,1);
                   1382: 	    my $declutter=&Apache::lonnet::declutter($ENV{'request.filename'});
                   1383: 	    my @userlist;
                   1384: 	    foreach (keys %users) {
                   1385: 		my ($user,$domain) = split(/:/, $_);
                   1386: 		push(@userlist,"$user\@$domain");
                   1387: 		&Apache::lonmsg::user_normal_msg($user,$domain,
                   1388: 						 "Error [$declutter]",join('<br />',@_));
                   1389: 	    }
                   1390: 	    if ($ENV{'request.role.adv'}) {
                   1391: 		$errormsg=&mt("An error occured while processing this resource. The course personnel ([_1]) and the author have been notified.",join(', ',@userlist));
                   1392: 	    } else {
                   1393: 		$errormsg=&mt("An error occured while processing this resource. The instructor has been notified.");
                   1394: 	    }
                   1395: 	}
                   1396: 	push(@Apache::lonxml::error_messages,"<b>$errormsg</b> <br />");
1.52      albertel 1397:     }
1.22      albertel 1398: }
1.49      albertel 1399: 
1.22      albertel 1400: sub warning {
1.295     albertel 1401:     $warningcount++;
1.261     albertel 1402:   
1.295     albertel 1403:     if ($ENV{'form.grade_target'} ne 'tex') {
                   1404: 	if ($ENV{'request.state'} eq 'construct' || $Apache::lonxml::debug) {
1.300     albertel 1405: 	    my $request=$Apache::lonxml::request;
                   1406: 	    if (!$request) { $request=Apache->request; }
1.309     albertel 1407: 	    push(@Apache::lonxml::warning_messages,
                   1408: 		 $Apache::lonxml::warnings_error_header.
                   1409: 		 "<b>W</b>ARNING<b>:</b>".join('<br />',@_)."<br />\n");
1.295     albertel 1410: 	    $Apache::lonxml::warnings_error_header='';
                   1411: 	}
                   1412:     }
1.309     albertel 1413: }
                   1414: 
                   1415: sub info {
                   1416:     if ($ENV{'form.grade_target'} ne 'tex' 
                   1417: 	&& $ENV{'request.state'} eq 'construct') {
                   1418: 	push(@Apache::lonxml::info_messages,join('<br />',@_)."<br />\n");
                   1419:     }
                   1420: }
                   1421: 
                   1422: sub message_location {
                   1423:     return '__LONCAPA_INTERNAL_MESSAGE_LOCATION__';
                   1424: }
                   1425: 
                   1426: sub add_messages {
                   1427:     my ($msg)=@_;
                   1428:     my $result=join(' ',
                   1429: 		    @Apache::lonxml::info_messages,
                   1430: 		    @Apache::lonxml::error_messages,
                   1431: 		    @Apache::lonxml::warning_messages);
                   1432:     undef(@Apache::lonxml::info_messages);
                   1433:     undef(@Apache::lonxml::error_messages);
                   1434:     undef(@Apache::lonxml::warning_messages);
                   1435:     $$msg=~s/__LONCAPA_INTERNAL_MESSAGE_LOCATION__/$result/;
                   1436:     $$msg=~s/__LONCAPA_INTERNAL_MESSAGE_LOCATION__//g;
1.83      albertel 1437: }
                   1438: 
                   1439: sub get_param {
1.213     albertel 1440:     my ($param,$parstack,$safeeval,$context,$case_insensitive) = @_;
                   1441:     if ( ! $context ) { $context = -1; }
                   1442:     my $args ='';
                   1443:     if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; }
1.297     sakharuk 1444:     if ( ! $Apache::lonxml::usestyle ) {
                   1445: 	$args=$Apache::lonxml::style_values.$args;
                   1446:     }
1.213     albertel 1447:     if ( ! $args ) { return undef; }
                   1448:     if ( $case_insensitive ) {
                   1449: 	if ($args =~ s/(my \$)(\Q$param\E)(=\")/$1.lc($2).$3/ei) {
                   1450: 	    return &Apache::run::run("{$args;".'return $'.$param.'}',
                   1451:                                      $safeeval); #'
                   1452: 	} else {
                   1453: 	    return undef;
                   1454: 	}
                   1455:     } else {
                   1456: 	if ( $args =~ /my \$\Q$param\E=\"/ ) {
                   1457: 	    return &Apache::run::run("{$args;".'return $'.$param.'}',
                   1458:                                      $safeeval); #'
                   1459: 	} else {
                   1460: 	    return undef;
                   1461: 	}
                   1462:     }
1.22      albertel 1463: }
                   1464: 
1.132     albertel 1465: sub get_param_var {
1.213     albertel 1466:   my ($param,$parstack,$safeeval,$context,$case_insensitive) = @_;
1.132     albertel 1467:   if ( ! $context ) { $context = -1; }
                   1468:   my $args ='';
                   1469:   if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; }
1.297     sakharuk 1470:   if ( ! $Apache::lonxml::usestyle ) {
                   1471:       $args=$Apache::lonxml::style_values.$args;
                   1472:   }
1.230     albertel 1473:   &Apache::lonxml::debug("Args are $args param is $param");
1.213     albertel 1474:   if ($case_insensitive) {
                   1475:       if (! ($args=~s/(my \$)(\Q$param\E)(=\")/$1.lc($2).$3/ei)) {
                   1476: 	  return undef;
                   1477:       }
                   1478:   } elsif ( $args !~ /my \$\Q$param\E=\"/ ) { return undef; }
1.132     albertel 1479:   my $value=&Apache::run::run("{$args;".'return $'.$param.'}',$safeeval); #'
1.230     albertel 1480:   &Apache::lonxml::debug("first run is $value");
1.341   ! albertel 1481:   if ($value =~ /^[\$\@\%][a-zA-Z_]\w*$/) {
1.230     albertel 1482:       &Apache::lonxml::debug("doing second");
                   1483:       my @result=&Apache::run::run("return $value",$safeeval,1);
                   1484:       if (!defined($result[0])) {
                   1485: 	  return $value
                   1486:       } else {
                   1487: 	  if (wantarray) { return @result; } else { return $result[0]; }
                   1488:       }
1.132     albertel 1489:   } else {
                   1490:     return $value;
                   1491:   }
                   1492: }
                   1493: 
1.74      albertel 1494: sub register_insert {
1.75      albertel 1495:   my @data = split /\n/, &Apache::lonnet::getfile('/home/httpd/lonTabs/insertlist.tab');
1.74      albertel 1496:   my $i;
1.76      albertel 1497:   my $tagnum=0;
1.74      albertel 1498:   my @order;
                   1499:   for ($i=0;$i < $#data; $i++) {
                   1500:     my $line = $data[$i];
                   1501:     if ( $line =~ /^\#/ || $line =~ /^\s*\n/) { next; }
                   1502:     if ( $line =~ /TABLE/ ) { last; }
1.268     bowersj2 1503:     my ($tag,$descrip,$color,$function,$show,$helpfile,$helpdesc) = split(/,/, $line);
1.135     albertel 1504:     if ($tag) {
                   1505:       $insertlist{"$tagnum.tag"} = $tag;
                   1506:       $insertlist{"$tagnum.description"} = $descrip;
                   1507:       $insertlist{"$tagnum.color"} = $color;
                   1508:       $insertlist{"$tagnum.function"} = $function;
                   1509:       if (!defined($show)) { $show='yes'; }
                   1510:       $insertlist{"$tagnum.show"}= $show;
1.268     bowersj2 1511:       $insertlist{"$tagnum.helpfile"} = $helpfile;
                   1512:       $insertlist{"$tagnum.helpdesc"} = $helpdesc;
1.135     albertel 1513:       $insertlist{"$tag.num"}=$tagnum;
                   1514:       $tagnum++;
                   1515:     }
1.74      albertel 1516:   }
1.76      albertel 1517:   $i++; #skipping TABLE line
                   1518:   $tagnum = 0;
1.74      albertel 1519:   for (;$i < $#data;$i++) {
                   1520:     my $line = $data[$i];
1.76      albertel 1521:     my ($mnemonic,@which) = split(/ +/,$line);
                   1522:     my $tag = $insertlist{"$tagnum.tag"};
1.144     matthew  1523:     for (my $j=0;$j <=$#which;$j++) {
1.74      albertel 1524:       if ( $which[$j] eq 'Y' ) {
1.76      albertel 1525: 	if ($insertlist{"$j.show"} ne 'no') {
                   1526: 	  push(@{ $insertlist{"$tag.which"} },$j);
                   1527: 	}
1.74      albertel 1528:       }
                   1529:     }
1.76      albertel 1530:     $tagnum++;
1.74      albertel 1531:   }
                   1532: }
1.98      albertel 1533: 
                   1534: sub description {
                   1535:   my ($token)=@_;
1.138     albertel 1536:   my $tagnum;
                   1537:   my $tag=$token->[1];
                   1538:   foreach my $namespace (reverse @Apache::lonxml::namespace) {
                   1539:     my $testtag=$namespace.'::'.$tag;
                   1540:     $tagnum=$insertlist{"$testtag.num"};
                   1541:     if (defined($tagnum)) { last; }
                   1542:   }
                   1543:   if (!defined ($tagnum)) { $tagnum=$Apache::lonxml::insertlist{"$tag.num"}; }
                   1544:   return $insertlist{$tagnum.'.description'};
1.268     bowersj2 1545: }
                   1546: 
                   1547: # Returns a list containing the help file, and the description
                   1548: sub helpinfo {
                   1549:   my ($token)=@_;
                   1550:   my $tagnum;
                   1551:   my $tag=$token->[1];
                   1552:   foreach my $namespace (reverse @Apache::lonxml::namespace) {
                   1553:     my $testtag=$namespace.'::'.$tag;
                   1554:     $tagnum=$insertlist{"$testtag.num"};
                   1555:     if (defined($tagnum)) { last; }
                   1556:   }
                   1557:   if (!defined ($tagnum)) { $tagnum=$Apache::lonxml::insertlist{"$tag.num"}; }
                   1558:   return ($insertlist{$tagnum.'.helpfile'}, $insertlist{$tagnum.'.helpdesc'});
1.98      albertel 1559: }
1.123     albertel 1560: 
                   1561: # ----------------------------------------------------------------- whichuser
                   1562: # returns a list of $symb, $courseid, $domain, $name that is correct for
                   1563: # calls to lonnet functions for this setup.
                   1564: # - looks for form.grade_ parameters
                   1565: sub whichuser {
1.262     matthew  1566:   my ($passedsymb)=@_;
1.245     albertel 1567:   my ($symb,$courseid,$domain,$name,$publicuser);
1.123     albertel 1568:   if (defined($ENV{'form.grade_symb'})) {
                   1569:     my $tmp_courseid=$ENV{'form.grade_courseid'};
1.269     albertel 1570:     my $allowed=&Apache::lonnet::allowed('vgr',$tmp_courseid);
1.123     albertel 1571:     if ($allowed) {
                   1572:       $symb=$ENV{'form.grade_symb'};
                   1573:       $courseid=$ENV{'form.grade_courseid'};
                   1574:       $domain=$ENV{'form.grade_domain'};
                   1575:       $name=$ENV{'form.grade_username'};
                   1576:     }
1.134     albertel 1577:   } else {
1.262     matthew  1578:       if (!$passedsymb) {
                   1579:           $symb=&Apache::lonnet::symbread();
                   1580:       } else {
                   1581:           $symb=$passedsymb;
                   1582:       }
1.244     albertel 1583:       $courseid=$ENV{'request.course.id'};
                   1584:       $domain=$ENV{'user.domain'};
                   1585:       $name=$ENV{'user.name'};
                   1586:       if ($name eq 'public' && $domain eq 'public') {
                   1587: 	  if (!defined($ENV{'form.username'})) {
                   1588: 	      $ENV{'form.username'}.=time.rand(10000000);
                   1589: 	  }
                   1590: 	  $name.=$ENV{'form.username'};
                   1591:       }
1.123     albertel 1592:   }
1.245     albertel 1593:   return ($symb,$courseid,$domain,$name,$publicuser);
1.123     albertel 1594: }
                   1595: 
1.1       sakharuk 1596: 1;
                   1597: __END__
1.68      www      1598: 
                   1599: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>