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