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

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

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