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

1.2       sakharuk    1: # The LearningOnline Network with CAPA
1.3       sakharuk    2: # XML Parser Module 
1.2       sakharuk    3: #
1.3       sakharuk    4: # last modified 06/26/00 by Alexander Sakharuk
1.33      www         5: # 11/6 Gerd Kortemeyer
1.45      www         6: # 6/1/1 Gerd Kortemeyer
1.56      albertel    7: # 2/21,3/13 Guy
1.68      www         8: # 3/29,5/4 Gerd Kortemeyer
1.73      harris41    9: # 5/10 Scott Harrison
1.78      www        10: # 5/26 Gerd Kortemeyer
1.80      harris41   11: # 5/27 H. K. Ng
1.87      www        12: # 6/2,6/3,6/8 Gerd Kortemeyer
1.2       sakharuk   13: 
1.4       albertel   14: package Apache::lonxml; 
1.33      www        15: use vars 
1.76      albertel   16: qw(@pwd @outputstack $redirection $import @extlinks $metamode $evaluate %insertlist @namespace);
1.1       sakharuk   17: use strict;
                     18: use HTML::TokeParser;
1.3       sakharuk   19: use Safe;
1.40      albertel   20: use Safe::Hole;
1.81      ng         21: use Math::Cephes qw(:trigs :hypers :bessels erf erfc);
1.13      albertel   22: use Opcode;
1.72      albertel   23: 
                     24: sub register {
                     25:   my $space;
                     26:   my @taglist;
                     27:   my $temptag;
                     28:   ($space,@taglist) = @_;
                     29:   foreach $temptag (@taglist) {
                     30:     $Apache::lonxml::alltags{$temptag}=$space;
                     31:   }
                     32: }
                     33: 
1.46      www        34: use Apache::Constants qw(:common);
1.71      www        35: use Apache::lontexconvert;
1.72      albertel   36: use Apache::style;
                     37: use Apache::run;
                     38: use Apache::londefdef;
                     39: use Apache::scripttag;
                     40: use Apache::edit;
1.79      www        41: use Apache::lonnet;
                     42: use Apache::File;
                     43: 
1.72      albertel   44: #==================================================   Main subroutine: xmlparse  
                     45: #debugging control, to turn on debugging modify the correct handler
                     46: $Apache::lonxml::debug=0;
                     47: 
                     48: #path to the directory containing the file currently being processed
                     49: @pwd=();
                     50: 
                     51: #these two are used for capturing a subset of the output for later processing,
                     52: #don't touch them directly use &startredirection and &endredirection
                     53: @outputstack = ();
                     54: $redirection = 0;
                     55: 
                     56: #controls wheter the <import> tag actually does
                     57: $import = 1;
                     58: @extlinks=();
                     59: 
                     60: # meta mode is a bit weird only some output is to be turned off
                     61: #<output> tag turns metamode off (defined in londefdef.pm)
                     62: $metamode = 0;
                     63: 
                     64: # turns on and of run::evaluate actually derefencing var refs
                     65: $evaluate = 1;
1.7       albertel   66: 
1.74      albertel   67: # data structure for eidt mode, determines what tags can go into what other tags
                     68: %insertlist=();
1.68      www        69: 
1.76      albertel   70: #stores the list of active tag namespaces
                     71: @namespace=();
                     72: 
1.68      www        73: sub xmlbegin {
                     74:   my $output='';
                     75:   if ($ENV{'browser.mathml'}) {
                     76:       $output='<?xml version="1.0"?>'
                     77:             .'<?xml-stylesheet type="text/css" href="/adm/MathML/mathml.css"?>'
                     78:             .'<!DOCTYPE html SYSTEM "/adm/MathML/mathml.dtd" '
                     79:             .'[<!ENTITY mathns "http://www.w3.org/1998/Math/MathML">]>'
                     80:             .'<html xmlns:math="http://www.w3.org/1998/Math/MathML" ' 
                     81: 		.'xmlns="http://www.w3.org/TR/REC-html40">';
                     82:   } else {
                     83:       $output='<html>';
                     84:   }
                     85:   return $output;
                     86: }
                     87: 
                     88: sub xmlend {
                     89:     return '</html>';
                     90: }
                     91: 
1.70      www        92: sub fontsettings() {
                     93:     my $headerstring='';
                     94:     if (($ENV{'browser.os'} eq 'mac') && (!$ENV{'browser.mathml'})) { 
                     95:          $headerstring.=
                     96:              '<meta Content-Type="text/html; charset=x-mac-roman">';
                     97:     }
                     98:     return $headerstring;
                     99: }
                    100: 
1.68      www       101: sub registerurl {
1.86      www       102:     if ($ENV{'REQUEST_URI'}!~/^\/(res\/)*adm\//) {
1.87      www       103:         my $hwkadd='';
                    104:         if ($ENV{'REQUEST_URI'}=~/\.(problem|exam|quiz|assess|survey|form)$/) {
                    105: 	    if (&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'})) {
                    106: 		$hwkadd.=(<<ENDSUBM);
                    107:                      menu.switchbutton
                    108:            (7,1,'subm.gif','view sub','missions',
1.88    ! www       109:                 'gocmd("/adm/grades","submission")');
1.87      www       110: ENDSUBM
                    111:             }
                    112: 	    if (&Apache::lonnet::allowed('mgr',$ENV{'request.course.id'})) {
                    113: 		$hwkadd.=(<<ENDGRDS);
                    114:                      menu.switchbutton
                    115:            (7,2,'pgrd.gif','problem','grades',
1.88    ! www       116:                 'gocmd("/adm/grades","viewgrades")');
1.87      www       117: ENDGRDS
                    118:             }
                    119: 	    if (&Apache::lonnet::allowed('opa',$ENV{'request.course.id'})) {
                    120: 		$hwkadd.=(<<ENDPARM);
                    121:                      menu.switchbutton
                    122:            (7,3,'pparm.gif','problem','parms',
1.88    ! www       123:                 'gocmd("/adm/parmset","set")');
1.87      www       124: ENDPARM
                    125:             }
                    126: 	}
1.86      www       127: 	return (<<ENDREGTHIS);
1.87      www       128:      
1.68      www       129: <script language="JavaScript">
1.71      www       130: // BEGIN LON-CAPA Internal
1.86      www       131: 
1.69      www       132:     function LONCAPAreg() {
                    133: 	  menu=window.open("","LONCAPAmenu");
1.86      www       134:           menu.clearTimeout(menu.menucltim);
1.69      www       135: 	  menu.currentURL=window.location.pathname;
                    136:           menu.currentStale=0;
1.85      www       137:           menu.clearbut(3,1);
                    138:           menu.switchbutton
                    139:        (8,1,'eval.gif','evaluate','this','gopost("/adm/evaluate",currentURL)');
                    140:           menu.switchbutton
                    141:     (8,2,'fdbk.gif','feedback','on this','gopost("/adm/feedback",currentURL)');
                    142:           menu.switchbutton
                    143:      (8,3,'prt.gif','prepare','printout','gopost("/adm/printout",currentURL)');
                    144:           menu.switchbutton
                    145:        (2,1,'back.gif','backward','','gopost("/adm/flip","back:"+currentURL)');
                    146:           menu.switchbutton
                    147:      (2,3,'forw.gif','forward','','gopost("/adm/flip","forward:"+currentURL)');
1.87      www       148:           $hwkadd
1.69      www       149:     }
1.86      www       150: 
1.69      www       151:     function LONCAPAstale() {
1.86      www       152: 	  menu=window.open("","LONCAPAmenu");
                    153:           menu.currentStale=1;
                    154:           menu.switchbutton
                    155:             (3,1,'reload.gif','return','location','go(currentURL)');
                    156:           menu.menucltim=menu.setTimeout(
                    157:  'clearbut(2,1);clearbut(2,3);clearbut(8,1);clearbut(8,2);clearbut(8,3);',
                    158: 			  2000);
                    159: 
1.87      www       160:       }
1.86      www       161: 
                    162: // END LON-CAPA Internal
                    163: </script>
                    164: ENDREGTHIS
                    165: 
                    166:     } else {
                    167:         return (<<ENDDONOTREGTHIS);
                    168: 
                    169: <script language="JavaScript">
                    170: // BEGIN LON-CAPA Internal
                    171: 
                    172:     function LONCAPAreg() {
1.69      www       173: 	  menu=window.open("","LONCAPAmenu");
                    174:           menu.currentStale=1;
1.85      www       175:           menu.clearbut(2,1);
                    176:           menu.clearbut(2,3);
                    177:           menu.clearbut(8,1);
                    178:           menu.clearbut(8,2);
                    179:           menu.clearbut(8,3);
1.86      www       180:           if (menu.currentURL) {
                    181:              menu.switchbutton
                    182:               (3,1,'reload.gif','return','location','go(currentURL)');
                    183:  	  } else {
                    184: 	      menu.clearbut(3,1);
                    185:           }
                    186:     }
                    187: 
                    188:     function LONCAPAstale() {
1.68      www       189:     }
1.86      www       190: 
1.71      www       191: // END LON-CAPA Internal
1.68      www       192: </script>
1.86      www       193: ENDDONOTREGTHIS
                    194: 
                    195:     }
1.69      www       196: }
                    197: 
                    198: sub loadevents() {
                    199:     return 'LONCAPAreg();';
                    200: }
                    201: 
                    202: sub unloadevents() {
                    203:     return 'LONCAPAstale();';
1.68      www       204: }
                    205: 
1.48      albertel  206: sub printalltags {
                    207:   my $temp;
                    208:   foreach $temp (sort keys %Apache::lonxml::alltags) {
1.64      albertel  209:     &Apache::lonxml::debug("$temp -- $Apache::lonxml::alltags{$temp}");
1.48      albertel  210:   }
                    211: }
1.31      sakharuk  212: 
1.3       sakharuk  213: sub xmlparse {
                    214: 
1.18      albertel  215:  my ($target,$content_file_string,$safeinit,%style_for_target) = @_;
1.41      albertel  216:  if ($target eq 'meta') {
1.59      albertel  217:    $Apache::lonxml::redirection = 0;
                    218:    $Apache::lonxml::metamode = 1;
1.72      albertel  219:    $Apache::lonxml::evaluate = 1;
1.55      albertel  220:    $Apache::lonxml::import = 0;
1.47      albertel  221:  } elsif ($target eq 'grade') {
1.55      albertel  222:    &startredirection;
1.59      albertel  223:    $Apache::lonxml::metamode = 0;
1.72      albertel  224:    $Apache::lonxml::evaluate = 1;
1.55      albertel  225:    $Apache::lonxml::import = 1;
1.72      albertel  226:  } elsif ($target eq 'modified') {
                    227:    $Apache::lonxml::redirection = 0;
                    228:    $Apache::lonxml::metamode = 0;
                    229:    $Apache::lonxml::evaluate = 0;
                    230:    $Apache::lonxml::import = 0;
1.44      albertel  231:  } else {
1.72      albertel  232:    $Apache::lonxml::redirection = 0;
1.59      albertel  233:    $Apache::lonxml::metamode = 0;
1.72      albertel  234:    $Apache::lonxml::evaluate = 1;
1.55      albertel  235:    $Apache::lonxml::import = 1;
1.32      sakharuk  236:  }
1.48      albertel  237:  #&printalltags();
1.16      albertel  238:  my @pars = ();
1.23      albertel  239:  @Apache::lonxml::pwd=();
                    240:  my $pwd=$ENV{'request.filename'};
                    241:  $pwd =~ s:/[^/]*$::;
                    242:  &newparser(\@pars,\$content_file_string,$pwd);
1.3       sakharuk  243:  my $currentstring = '';
                    244:  my $finaloutput = ''; 
                    245:  my $newarg = '';
1.16      albertel  246:  my $result;
1.24      sakharuk  247: 
1.3       sakharuk  248:  my $safeeval = new Safe;
1.40      albertel  249:  my $safehole = new Safe::Hole;
1.82      ng        250:  &init_safespace($target,$safeeval,$safehole,$safeinit);
1.3       sakharuk  251: #-------------------- Redefinition of the target in the case of compound target
                    252: 
                    253:  ($target, my @tenta) = split('&&',$target);
                    254: 
                    255:  my @stack = (); 
                    256:  my @parstack = ();
1.17      albertel  257:  &initdepth;
1.3       sakharuk  258:  my $token;
1.16      albertel  259:  while ( $#pars > -1 ) {
                    260:    while ($token = $pars[$#pars]->get_token) {
1.57      albertel  261:      if (($token->[0] eq 'T') || ($token->[0] eq 'C') || ($token->[0] eq 'D') ) {
1.61      albertel  262:        if ($metamode<1) { $result=$token->[1]; }
1.57      albertel  263:      } elsif ($token->[0] eq 'PI') {
1.61      albertel  264:        if ($metamode<1) { $result=$token->[2]; }
1.16      albertel  265:      } elsif ($token->[0] eq 'S') {
                    266:        # add tag to stack 	    
                    267:        push (@stack,$token->[1]);
                    268:        # add parameters list to another stack
                    269:        push (@parstack,&parstring($token));
1.19      albertel  270:        &increasedepth($token);       
1.16      albertel  271:        if (exists $style_for_target{$token->[1]}) {
1.61      albertel  272: 	 if ($Apache::lonxml::redirection) {
1.55      albertel  273: 	   $Apache::lonxml::outputstack['-1'] .=  
                    274: 	     &recurse($style_for_target{$token->[1]},$target,$safeeval,
                    275: 		      \%style_for_target,@parstack);
1.41      albertel  276: 	 } else {
1.55      albertel  277: 	   $finaloutput .= &recurse($style_for_target{$token->[1]},$target,
                    278: 				    $safeeval,\%style_for_target,@parstack);
1.41      albertel  279: 	 }
1.16      albertel  280:        } else {
1.84      albertel  281: 	 $result = &callsub("start_$token->[1]", $target, $token, \@stack,
                    282: 			    \@parstack, \@pars, $safeeval, \%style_for_target);
1.16      albertel  283:        }              
                    284:      } elsif ($token->[0] eq 'E')  {
                    285:        #clear out any tags that didn't end
1.55      albertel  286:        while ($token->[1] ne $stack[$#stack] && ($#stack > -1)) {
                    287: 	 &Apache::lonxml::warning("Unbalanced tags in resource $stack['-1']");
1.43      albertel  288: 	 pop @stack;pop @parstack;&decreasedepth($token);
                    289:        }
1.16      albertel  290:        
                    291:        if (exists $style_for_target{'/'."$token->[1]"}) {
1.61      albertel  292: 	 if ($Apache::lonxml::redirection) {
1.55      albertel  293: 	   $Apache::lonxml::outputstack['-1'] .=  
                    294: 	     &recurse($style_for_target{'/'."$token->[1]"},
                    295: 		      $target,$safeeval,\%style_for_target,@parstack);
                    296: 	 } else {
                    297: 	   $finaloutput .= &recurse($style_for_target{'/'."$token->[1]"},
                    298: 				    $target,$safeeval,\%style_for_target,
                    299: 				    @parstack);
                    300: 	 }
1.59      albertel  301: 
1.16      albertel  302:        } else {
1.84      albertel  303: 	 $result = &callsub("end_$token->[1]", $target, $token, \@stack, 
                    304: 			    \@parstack, \@pars,$safeeval, \%style_for_target);
1.13      albertel  305:        }
1.57      albertel  306:      } else {
                    307:        &Apache::lonxml::error("Unknown token event :$token->[0]:$token->[1]:");
1.16      albertel  308:      }
1.55      albertel  309:      #evaluate variable refs in result
1.25      sakharuk  310:      if ($result ne "") {
1.24      sakharuk  311:        if ( $#parstack > -1 ) {
1.55      albertel  312: 	 if ($Apache::lonxml::redirection) {
                    313: 	   $Apache::lonxml::outputstack['-1'] .= 
                    314: 	     &Apache::run::evaluate($result,$safeeval,$parstack[$#parstack]);
                    315: 	 } else {
                    316: 	   $finaloutput .= &Apache::run::evaluate($result,$safeeval,
                    317: 						  $parstack[$#parstack]);
                    318: 	 }
1.16      albertel  319:        } else {
                    320: 	 $finaloutput .= &Apache::run::evaluate($result,$safeeval,'');
1.13      albertel  321:        }
1.16      albertel  322:        $result = '';
1.55      albertel  323:      } 
                    324:      if ($token->[0] eq 'E') { 
                    325:        pop @stack;pop @parstack;&decreasedepth($token);
1.2       sakharuk  326:      }
1.5       albertel  327:    }
1.16      albertel  328:    pop @pars;
1.23      albertel  329:    pop @Apache::lonxml::pwd;
1.3       sakharuk  330:  }
1.24      sakharuk  331: 
1.59      albertel  332: # if ($target eq 'meta') {
                    333: #   $finaloutput.=&endredirection;
                    334: # }
1.67      www       335: 
                    336:   if (($ENV{'QUERY_STRING'}) && ($target eq 'web')) {
                    337:       $finaloutput=&afterburn($finaloutput);
                    338:   }
                    339: 
1.3       sakharuk  340:  return $finaloutput;
1.15      albertel  341: }
                    342: 
1.67      www       343: 
1.15      albertel  344: sub recurse {
                    345:   
                    346:   my @innerstack = (); 
                    347:   my @innerparstack = ();
                    348:   my ($newarg,$target,$safeeval,$style_for_target,@parstack) = @_;
1.16      albertel  349:   my @pat = ();
1.23      albertel  350:   &newparser(\@pat,\$newarg);
1.15      albertel  351:   my $tokenpat;
                    352:   my $partstring = '';
                    353:   my $output='';
1.16      albertel  354:   my $decls='';
                    355:   while ( $#pat > -1 ) {
                    356:     while  ($tokenpat = $pat[$#pat]->get_token) {
1.57      albertel  357:       if (($tokenpat->[0] eq 'T') || ($tokenpat->[0] eq 'C') || ($tokenpat->[0] eq 'D') ) {
1.61      albertel  358: 	if ($metamode<1) { $partstring=$tokenpat->[1]; }
1.57      albertel  359:       } elsif ($tokenpat->[0] eq 'PI') {
1.61      albertel  360: 	if ($metamode<1) { $partstring=$tokenpat->[2]; }
1.16      albertel  361:       } elsif ($tokenpat->[0] eq 'S') {
                    362: 	push (@innerstack,$tokenpat->[1]);
                    363: 	push (@innerparstack,&parstring($tokenpat));
1.19      albertel  364: 	&increasedepth($tokenpat);
1.84      albertel  365: 	$partstring = &callsub("start_$tokenpat->[1]", $target, $tokenpat,
                    366: 			       \@innerstack, \@innerparstack, \@pat,
                    367: 			       $safeeval, $style_for_target);
1.16      albertel  368:       } elsif ($tokenpat->[0] eq 'E') {
                    369: 	#clear out any tags that didn't end
                    370: 	while ($tokenpat->[1] ne $innerstack[$#innerstack] 
1.43      albertel  371: 	       && ($#innerstack > -1)) {
1.49      albertel  372: 	  &Apache::lonxml::warning("Unbalanced tags in resource $innerstack['-1']");
1.43      albertel  373: 	  pop @innerstack;pop @innerparstack;&decreasedepth($tokenpat);
                    374: 	}
1.84      albertel  375: 	$partstring = &callsub("end_$tokenpat->[1]", $target, $tokenpat,
                    376: 			       \@innerstack, \@innerparstack, \@pat,
                    377: 			       $safeeval, $style_for_target);
1.57      albertel  378:       } else {
                    379: 	&Apache::lonxml::error("Unknown token event :$tokenpat->[0]:$tokenpat->[1]:");
1.16      albertel  380:       }
                    381:       #pass both the variable to the style tag, and the tag we 
                    382:       #are processing inside the <definedtag>
                    383:       if ( $partstring ne "" ) {
                    384: 	if ( $#parstack > -1 ) { 
                    385: 	  if ( $#innerparstack > -1 ) { 
                    386: 	    $decls= $parstack[$#parstack].$innerparstack[$#innerparstack];
                    387: 	  } else {
                    388: 	    $decls= $parstack[$#parstack];
                    389: 	  }
                    390: 	} else {
                    391: 	  if ( $#innerparstack > -1 ) { 
                    392: 	    $decls=$innerparstack[$#innerparstack];
                    393: 	  } else {
                    394: 	    $decls='';
                    395: 	  }
                    396: 	}
                    397: 	$output .= &Apache::run::evaluate($partstring,$safeeval,$decls);
                    398: 	$partstring = '';
                    399:       }
1.17      albertel  400:       if ($tokenpat->[0] eq 'E') { pop @innerstack;pop @innerparstack;
1.19      albertel  401: 				 &decreasedepth($tokenpat);}
1.15      albertel  402:     }
1.16      albertel  403:     pop @pat;
1.23      albertel  404:     pop @Apache::lonxml::pwd;
1.15      albertel  405:   }
                    406:   return $output;
1.7       albertel  407: }
                    408: 
                    409: sub callsub {
1.84      albertel  410:   my ($sub,$target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.7       albertel  411:   my $currentstring='';
1.72      albertel  412:   my $nodefault;
1.7       albertel  413:   {
1.59      albertel  414:     my $sub1;
1.7       albertel  415:     no strict 'refs';
1.59      albertel  416:     if ($target eq 'edit' && $token->[0] eq 'S') {
1.84      albertel  417:       $currentstring = &Apache::edit::tag_start($target,$token,$tagstack,
                    418: 						$parstack,$parser,
1.59      albertel  419: 						$safeeval,$style);
                    420:     }
1.68      www       421:     my $tag=$token->[1];
                    422:     my $space=$Apache::lonxml::alltags{$tag};
                    423:     if (!$space) {
                    424: 	$tag=~tr/A-Z/a-z/;
                    425: 	$sub=~tr/A-Z/a-z/;
                    426: 	$space=$Apache::lonxml::alltags{$tag}
                    427:     }
                    428:     if ($space) {
1.72      albertel  429:       #&Apache::lonxml::debug("Calling sub $sub in $space $metamode<br />\n");
1.24      sakharuk  430:       $sub1="$space\:\:$sub";
1.17      albertel  431:       $Apache::lonxml::curdepth=join('_',@Apache::lonxml::depthcounter);
1.84      albertel  432:       ($currentstring,$nodefault) = &$sub1($target,$token,$tagstack,
                    433: 					   $parstack,$parser,$safeeval,
                    434: 					   $style);
1.7       albertel  435:     } else {
1.72      albertel  436:       #&Apache::lonxml::debug("NOT Calling sub $sub in $space $metamode<br />\n");
1.62      sakharuk  437:       if ($metamode <1) {
                    438: 	if (defined($token->[4]) && ($metamode < 1)) {
1.72      albertel  439: 	  $currentstring = $token->[4];
1.62      sakharuk  440: 	} else {
1.72      albertel  441: 	  $currentstring = $token->[2];
1.62      sakharuk  442: 	}
1.7       albertel  443:       }
1.59      albertel  444:     }
1.83      albertel  445: #    &Apache::lonxml::debug("nodefalt:$nodefault:");
1.72      albertel  446:     if ($currentstring eq '' && $nodefault eq '') {
                    447:       if ($target eq 'edit') {
1.74      albertel  448: 	&Apache::lonxml::debug("doing default edit for $token->[1]");
1.72      albertel  449: 	if ($token->[0] eq 'S') {
1.74      albertel  450: 	  $currentstring = &Apache::edit::tag_start($target,$token);
1.72      albertel  451: 	} elsif ($token->[0] eq 'E') {
1.74      albertel  452: 	  $currentstring = &Apache::edit::tag_end($target,$token);
1.72      albertel  453: 	}
                    454:       } elsif ($target eq 'modified') {
                    455: 	if ($token->[0] eq 'S') {
                    456: 	  $currentstring = $token->[4];
1.76      albertel  457: 	  $currentstring.=&Apache::edit::handle_insert();
1.72      albertel  458: 	} else {
                    459: 	  $currentstring = $token->[2];
                    460: 	}
                    461:       }
1.7       albertel  462:     }
                    463:     use strict 'refs';
                    464:   }
                    465:   return $currentstring;
1.82      ng        466: }
                    467: 
                    468: sub init_safespace {
                    469:   my ($target,$safeeval,$safehole,$safeinit) = @_;
                    470:   $safeeval->permit("entereval");
                    471:   $safeeval->permit(":base_math");
                    472:   $safeeval->permit("sort");
                    473:   $safeeval->deny(":base_io");
                    474:   $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
                    475:   
                    476:   $safehole->wrap(\&Math::Cephes::asin,$safeeval,'&asin');
                    477:   $safehole->wrap(\&Math::Cephes::acos,$safeeval,'&acos');
                    478:   $safehole->wrap(\&Math::Cephes::atan,$safeeval,'&atan');
                    479:   $safehole->wrap(\&Math::Cephes::sinh,$safeeval,'&sinh');
                    480:   $safehole->wrap(\&Math::Cephes::cosh,$safeeval,'&cosh');
                    481:   $safehole->wrap(\&Math::Cephes::tanh,$safeeval,'&tanh');
                    482:   $safehole->wrap(\&Math::Cephes::asinh,$safeeval,'&asinh');
                    483:   $safehole->wrap(\&Math::Cephes::acosh,$safeeval,'&acosh');
                    484:   $safehole->wrap(\&Math::Cephes::atanh,$safeeval,'&atanh');
                    485:   $safehole->wrap(\&Math::Cephes::erf,$safeeval,'&erf');
                    486:   $safehole->wrap(\&Math::Cephes::erfc,$safeeval,'&erfc');
                    487:   $safehole->wrap(\&Math::Cephes::j0,$safeeval,'&j0');
                    488:   $safehole->wrap(\&Math::Cephes::j1,$safeeval,'&j1');
                    489:   $safehole->wrap(\&Math::Cephes::jn,$safeeval,'&jn');
                    490:   $safehole->wrap(\&Math::Cephes::jv,$safeeval,'&jv');
                    491:   $safehole->wrap(\&Math::Cephes::y0,$safeeval,'&y0');
                    492:   $safehole->wrap(\&Math::Cephes::y1,$safeeval,'&y1');
                    493:   $safehole->wrap(\&Math::Cephes::yn,$safeeval,'&yn');
                    494:   $safehole->wrap(\&Math::Cephes::yv,$safeeval,'&yv');
                    495:   
                    496: #need to inspect this class of ops
                    497: # $safeeval->deny(":base_orig");
                    498:   $safeinit .= ';$external::target='.$target.';';
                    499:   $safeinit .= ';$external::randomseed='.&Apache::lonnet::rndseed().';';
                    500:   &Apache::run::run($safeinit,$safeeval);
1.17      albertel  501: }
                    502: 
1.55      albertel  503: sub startredirection {
                    504:   $Apache::lonxml::redirection++;
                    505:   push (@Apache::lonxml::outputstack, '');
                    506: }
                    507: 
                    508: sub endredirection {
                    509:   if (!$Apache::lonxml::redirection) {
1.72      albertel  510:     &Apache::lonxml::error("Endredirection was called, before a startredirection, perhaps you have unbalanced tags. Some debuging information:".join ":",caller);
1.55      albertel  511:     return '';
                    512:   }
                    513:   $Apache::lonxml::redirection--;
                    514:   pop @Apache::lonxml::outputstack;
                    515: }
                    516: 
1.17      albertel  517: sub initdepth {
                    518:   @Apache::lonxml::depthcounter=();
                    519:   $Apache::lonxml::depth=-1;
                    520:   $Apache::lonxml::olddepth=-1;
                    521: }
                    522: 
                    523: sub increasedepth {
1.19      albertel  524:   my ($token) = @_;
1.17      albertel  525:   $Apache::lonxml::depth++;
                    526:   $Apache::lonxml::depthcounter[$Apache::lonxml::depth]++;
                    527:   if ($Apache::lonxml::depthcounter[$Apache::lonxml::depth]==1) {
                    528:     $Apache::lonxml::olddepth=$Apache::lonxml::depth;
                    529:   }
1.42      albertel  530:   my $curdepth=join('_',@Apache::lonxml::depthcounter);
1.64      albertel  531:   &Apache::lonxml::debug("s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n");
1.54      albertel  532: #print "<br />s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n";
1.17      albertel  533: }
                    534: 
                    535: sub decreasedepth {
1.19      albertel  536:   my ($token) = @_;
1.17      albertel  537:   $Apache::lonxml::depth--;
1.36      albertel  538:   if ($Apache::lonxml::depth<$Apache::lonxml::olddepth-1) {
                    539:     $#Apache::lonxml::depthcounter--;
                    540:     $Apache::lonxml::olddepth=$Apache::lonxml::depth+1;
                    541:   }
1.43      albertel  542:   if (  $Apache::lonxml::depth < -1) {
1.49      albertel  543:     &Apache::lonxml::warning("Unbalanced tags in resource");   
1.43      albertel  544:     $Apache::lonxml::depth='-1';
                    545:   }
1.42      albertel  546:   my $curdepth=join('_',@Apache::lonxml::depthcounter);
1.64      albertel  547:   &Apache::lonxml::debug("e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n");
1.54      albertel  548: #print "<br />e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n";
1.1       sakharuk  549: }
1.19      albertel  550: 
                    551: sub get_all_text {
                    552: 
                    553:  my($tag,$pars)= @_;
                    554:  my $depth=0;
                    555:  my $token;
                    556:  my $result='';
1.57      albertel  557:  if ( $tag =~ m:^/: ) { 
                    558:    my $tag=substr($tag,1); 
                    559: #   &Apache::lonxml::debug("have:$tag:");
                    560:    while (($depth >=0) && ($token = $pars->get_token)) {
                    561: #     &Apache::lonxml::debug("e token:$token->[0]:$depth:$token->[1]");
                    562:      if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
                    563:        $result.=$token->[1];
                    564:      } elsif ($token->[0] eq 'PI') {
                    565:        $result.=$token->[2];
                    566:      } elsif ($token->[0] eq 'S') {
                    567:        if ($token->[1] eq $tag) { $depth++; }
                    568:        $result.=$token->[4];
                    569:      } elsif ($token->[0] eq 'E')  {
                    570:        if ( $token->[1] eq $tag) { $depth--; }
                    571:        #skip sending back the last end tag
                    572:        if ($depth > -1) { $result.=$token->[2]; } else {
                    573: 	 $pars->unget_token($token);
                    574:        }
                    575:      }
                    576:    }
                    577:  } else {
                    578:    while ($token = $pars->get_token) {
                    579: #     &Apache::lonxml::debug("s token:$token->[0]:$depth:$token->[1]");
                    580:      if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
                    581:        $result.=$token->[1];
                    582:      } elsif ($token->[0] eq 'PI') {
                    583:        $result.=$token->[2];
                    584:      } elsif ($token->[0] eq 'S') {
                    585:        if ( $token->[1] eq $tag) { 
                    586: 	 $pars->unget_token($token); last;
                    587:        } else {
                    588: 	 $result.=$token->[4];
                    589:        }
                    590:      } elsif ($token->[0] eq 'E')  {
                    591:        $result.=$token->[2];
1.36      albertel  592:      }
1.19      albertel  593:    }
                    594:  }
1.49      albertel  595: # &Apache::lonxml::debug("Exit:$result:");
1.19      albertel  596:  return $result
                    597: }
                    598: 
1.23      albertel  599: sub newparser {
                    600:   my ($parser,$contentref,$dir) = @_;
                    601:   push (@$parser,HTML::TokeParser->new($contentref));
1.56      albertel  602:   $$parser['-1']->xml_mode('1');
1.23      albertel  603:   if ( $dir eq '' ) {
                    604:     push (@Apache::lonxml::pwd, $Apache::lonxml::pwd[$#Apache::lonxml::pwd]);
                    605:   } else {
                    606:     push (@Apache::lonxml::pwd, $dir);
                    607:   } 
                    608: #  &Apache::lonxml::debug("pwd:$#Apache::lonxml::pwd");
                    609: #  &Apache::lonxml::debug("pwd:$Apache::lonxml::pwd[$#Apache::lonxml::pwd]");
                    610: }
1.1       sakharuk  611: 
1.8       albertel  612: sub parstring {
                    613:   my ($token) = @_;
                    614:   my $temp='';
1.20      albertel  615:   map {
1.35      www       616:     unless ($_=~/\W/) {
1.42      albertel  617:       my $val=$token->[2]->{$_};
1.53      albertel  618:       $val =~ s/([\%\@\\])/\\$1/g;
1.51      albertel  619:       #if ($val =~ m/^[\%\@]/) { $val="\\".$val; }
1.42      albertel  620:       $temp .= "my \$$_=\"$val\";"
1.20      albertel  621:     }
                    622:   } @{$token->[3]};
1.8       albertel  623:   return $temp;
                    624: }
1.22      albertel  625: 
1.34      www       626: sub writeallows {
                    627:     my $thisurl='/res/'.&Apache::lonnet::declutter(shift);
                    628:     my $thisdir=$thisurl;
                    629:     $thisdir=~s/\/[^\/]+$//;
                    630:     my %httpref=();
                    631:     map {
                    632:        $httpref{'httpref.'.
                    633:  	        &Apache::lonnet::hreflocation($thisdir,$_)}=$thisurl;              } @extlinks;
                    634:     &Apache::lonnet::appenv(%httpref);
                    635: }
                    636: 
1.66      www       637: #
                    638: # Afterburner handles anchors, highlights and links
                    639: #
                    640: sub afterburn {
                    641:     my $result=shift;
                    642:     map {
                    643:        my ($name, $value) = split(/=/,$_);
                    644:        $value =~ tr/+/ /;
                    645:        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
                    646:        if (($name eq 'highlight')||($name eq 'anchor')||($name eq 'link')) {
                    647:            unless ($ENV{'form.'.$name}) {
                    648:               $ENV{'form.'.$name}=$value;
                    649: 	   }
                    650:        }
                    651:     } (split(/&/,$ENV{'QUERY_STRING'}));
                    652:     if ($ENV{'form.highlight'}) {
                    653:         map {
                    654:            my $anchorname=$_;
                    655: 	   my $matchthis=$anchorname;
                    656:            $matchthis=~s/\_+/\\s\+/g;
                    657:            $result=~s/($matchthis)/\<font color=\"red\"\>$1\<\/font\>/gs;
                    658:        } split(/\,/,$ENV{'form.highlight'});
                    659:     }
                    660:     if ($ENV{'form.link'}) {
                    661:         map {
                    662:            my ($anchorname,$linkurl)=split(/\>/,$_);
                    663: 	   my $matchthis=$anchorname;
                    664:            $matchthis=~s/\_+/\\s\+/g;
                    665:            $result=~s/($matchthis)/\<a href=\"$linkurl\"\>$1\<\/a\>/gs;
                    666:        } split(/\,/,$ENV{'form.link'});
                    667:     }
                    668:     if ($ENV{'form.anchor'}) {
                    669:         my $anchorname=$ENV{'form.anchor'};
                    670: 	my $matchthis=$anchorname;
                    671:         $matchthis=~s/\_+/\\s\+/g;
                    672:         $result=~s/($matchthis)/\<a name=\"$anchorname\"\>$1\<\/a\>/s;
                    673:         $result.=(<<"ENDSCRIPT");
                    674: <script>
                    675:     document.location.hash='$anchorname';
                    676: </script>
                    677: ENDSCRIPT
                    678:     }
                    679:     return $result;
                    680: }
                    681: 
1.79      www       682: sub storefile {
                    683:     my ($file,$contents)=@_;
                    684:     if (my $fh=Apache::File->new('>'.$file)) {
                    685: 	print $fh $contents;
                    686:         $fh->close();
                    687:     }
                    688: }
                    689: 
1.78      www       690: sub inserteditinfo {
                    691:       my ($result,$filecontents)=@_;
                    692:       unless ($filecontents) {
                    693: 	  $filecontents=(<<SIMPLECONTENT);
                    694: <html>
                    695: <head>
                    696: <title>
                    697:                            Title of Document Goes Here
                    698: </title>
                    699: </head>
                    700: <body bgcolor="#FFFFFF">
                    701: 
                    702:                            Body of Document Goes Here
                    703: 
                    704: </body>
                    705: </html>
                    706: SIMPLECONTENT
                    707:       }
                    708:       my $editheader='<a href="#editsection">Edit below</a><hr />';
                    709:       my $editfooter=(<<ENDFOOTER);
                    710: <hr />
                    711: <a name="editsection" />
                    712: <form method="post">
                    713: <textarea cols="80" rows="40" name="filecont">$filecontents</textarea>
                    714: <br />
                    715: <input type="submit" name="savethisfile" value="Save this file" />
                    716: </form>
                    717: ENDFOOTER
                    718:       $result=~s/(\<body[^\>]*\>)/$1$editheader/is;
                    719:       $result=~s/(\<\/body\>)/$editfooter/is;
                    720:       return $result;
                    721: }
                    722: 
1.24      sakharuk  723: sub handler {
                    724:   my $request=shift;
1.68      www       725: 
1.64      albertel  726:   my $target='web';
1.68      www       727: 
1.65      albertel  728:   $Apache::lonxml::debug=0;
1.68      www       729: 
1.25      sakharuk  730:   if ($ENV{'browser.mathml'}) {
1.27      albertel  731:     $request->content_type('text/xml');
                    732:   } else {
                    733:     $request->content_type('text/html');
1.25      sakharuk  734:   }
1.64      albertel  735:   
1.27      albertel  736:   $request->send_http_header;
1.64      albertel  737:   
1.45      www       738:   return OK if $request->header_only;
1.27      albertel  739: 
1.79      www       740: 
                    741:   my $file=&Apache::lonnet::filelocation("",$request->uri);
1.78      www       742: #
                    743: # Edit action? Save file.
                    744: #
                    745:   unless ($ENV{'request.state'} eq 'published') {
                    746:       if ($ENV{'form.savethisfile'}) {
1.79      www       747: 	  &storefile($file,$ENV{'form.filecont'});
1.78      www       748:       }
                    749:   }
1.24      sakharuk  750:   my %mystyle;
1.50      albertel  751:   my $result = ''; 
                    752:   my $filecontents=&Apache::lonnet::getfile($file);
                    753:   if ($filecontents == -1) {
1.78      www       754:     $result=(<<ENDNOTFOUND);
                    755: <html>
                    756: <head>
                    757: <title>File not found</title>
                    758: </head>
                    759: <body bgcolor="#FFFFFF">
                    760: <b>File not found: $file</b>
                    761: </body>
                    762: </html>
                    763: ENDNOTFOUND
1.50      albertel  764:     $filecontents='';
                    765:   } else {
                    766:     $result = &Apache::lonxml::xmlparse($target,$filecontents,'',%mystyle);
1.78      www       767:   }
                    768: 
                    769: #
                    770: # Edit action? Insert editing commands
                    771: #
                    772:   unless ($ENV{'request.state'} eq 'published') {
                    773:       $result=&inserteditinfo($result,$filecontents);
1.66      www       774:   }
1.50      albertel  775: 
1.67      www       776:   $request->print($result);
1.64      albertel  777: 
1.34      www       778:   writeallows($request->uri);
1.45      www       779:   return OK;
1.24      sakharuk  780: }
                    781:  
1.22      albertel  782: sub debug {
                    783:   if ($Apache::lonxml::debug eq 1) {
1.54      albertel  784:     print "DEBUG:".$_[0]."<br />\n";
1.22      albertel  785:   }
                    786: }
1.49      albertel  787: 
1.22      albertel  788: sub error {
1.74      albertel  789:   if (($Apache::lonxml::debug eq 1) || ($ENV{'request.state'} eq 'construct') ) {
1.55      albertel  790:     print "<b>ERROR:</b>".$_[0]."<br />\n";
1.52      albertel  791:   } else {
                    792:     print "<b>An Error occured while processing this resource. The instructor has been notified.</b> <br />";
                    793:     #notify author
                    794:     &Apache::lonmsg::author_res_msg($ENV{'request.filename'},$_[0]);
                    795:     #notify course
                    796:     if ( $ENV{'request.course.id'} ) {
                    797:       my $users=$ENV{'course.'.$ENV{'request.course.id'}.'.comment.email'};
                    798:       foreach my $user (split /\,/, $users) {
                    799: 	($user,my $domain) = split /:/, $user;
1.54      albertel  800: 	&Apache::lonmsg::user_normal_msg($user,$domain,"Error in $ENV{'request.filename'}",$_[0]);
1.52      albertel  801:       }
                    802:     }
1.74      albertel  803: 
1.52      albertel  804:     #FIXME probably shouldn't have me get everything forever.
1.54      albertel  805:     &Apache::lonmsg::user_normal_msg('albertel','msu',"Error in $ENV{'request.filename'}",$_[0]);
1.74      albertel  806:     #&Apache::lonmsg::user_normal_msg('albertel','103',"Error in $ENV{'request.filename'}",$_[0]);
1.52      albertel  807:   }
1.22      albertel  808: }
1.49      albertel  809: 
1.22      albertel  810: sub warning {
1.73      harris41  811:   if ($ENV{'request.state'} eq 'construct') {
1.55      albertel  812:     print "<b>W</b>ARNING<b>:</b>".$_[0]."<br />\n";
1.73      harris41  813:   }
1.83      albertel  814: }
                    815: 
                    816: sub get_param {
                    817:   my ($param,$parstack,$safeeval,$context) = @_;
                    818:   if ( ! $context ) { $context = -1; }
                    819:   my $args ='';
                    820:   if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; }
                    821:   return &Apache::run::run("{$args;".'return $'.$param.'}',$safeeval); #'
1.22      albertel  822: }
                    823: 
1.74      albertel  824: sub register_insert {
1.75      albertel  825:   my @data = split /\n/, &Apache::lonnet::getfile('/home/httpd/lonTabs/insertlist.tab');
1.74      albertel  826:   my $i;
1.76      albertel  827:   my $tagnum=0;
1.74      albertel  828:   my @order;
                    829:   for ($i=0;$i < $#data; $i++) {
                    830:     my $line = $data[$i];
                    831:     if ( $line =~ /^\#/ || $line =~ /^\s*\n/) { next; }
                    832:     if ( $line =~ /TABLE/ ) { last; }
                    833:     my ($tag,$descrip,$function,$show) = split(/,/, $line);
1.76      albertel  834:     $insertlist{"$tagnum.tag"} = $tag;
                    835:     $insertlist{"$tagnum.description"} = $descrip;
                    836:     $insertlist{"$tagnum.function"} = $function;
                    837:     $insertlist{"$tagnum.show"}= $show;
                    838:     $tagnum++;
1.74      albertel  839:   }
1.76      albertel  840:   $i++; #skipping TABLE line
                    841:   $tagnum = 0;
1.74      albertel  842:   for (;$i < $#data;$i++) {
                    843:     my $line = $data[$i];
1.76      albertel  844:     my ($mnemonic,@which) = split(/ +/,$line);
                    845:     my $tag = $insertlist{"$tagnum.tag"};
1.74      albertel  846:     for (my $j=0;$j <$#which;$j++) {
                    847:       if ( $which[$j] eq 'Y' ) {
1.76      albertel  848: 	if ($insertlist{"$j.show"} ne 'no') {
                    849: 	  push(@{ $insertlist{"$tag.which"} },$j);
                    850: 	}
1.74      albertel  851:       }
                    852:     }
1.76      albertel  853:     $tagnum++;
1.74      albertel  854:   }
                    855: }
1.1       sakharuk  856: 1;
                    857: __END__
1.68      www       858: 
                    859: 

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