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

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

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