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

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

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