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

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

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