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

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

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