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