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

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

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