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

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

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