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

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

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