Annotation of loncom/xml/lonxml.pm, revision 1.257
1.2 sakharuk 1: # The LearningOnline Network with CAPA
1.3 sakharuk 2: # XML Parser Module
1.2 sakharuk 3: #
1.257 ! albertel 4: # $Id: lonxml.pm,v 1.256 2003/05/22 20:19:18 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: }
1.255 sakharuk 424: #&printalltags();
1.16 albertel 425: my @pars = ();
1.23 albertel 426: my $pwd=$ENV{'request.filename'};
427: $pwd =~ s:/[^/]*$::;
428: &newparser(\@pars,\$content_file_string,$pwd);
1.24 sakharuk 429:
1.3 sakharuk 430: my $safeeval = new Safe;
1.40 albertel 431: my $safehole = new Safe::Hole;
1.82 ng 432: &init_safespace($target,$safeeval,$safehole,$safeinit);
1.3 sakharuk 433: #-------------------- Redefinition of the target in the case of compound target
434:
435: ($target, my @tenta) = split('&&',$target);
436:
1.150 albertel 437: my @stack = ();
1.3 sakharuk 438: my @parstack = ();
1.17 albertel 439: &initdepth;
1.67 www 440:
1.101 albertel 441: my $finaloutput = &inner_xmlparse($target,\@stack,\@parstack,\@pars,
442: $safeeval,\%style_for_target);
1.255 sakharuk 443:
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.257 ! albertel 524: $Apache::lonxml::style_values=$$parstack[-1];
1.101 albertel 525: } else {
526: $result = &callsub("start_$token->[1]", $target, $token, $stack,
527: $parstack, $pars, $safeeval, $style_for_target);
1.140 albertel 528: }
1.101 albertel 529: } elsif ($token->[0] eq 'E') {
530: #clear out any tags that didn't end
531: while ($token->[1] ne $$stack['-1'] && ($#$stack > -1)) {
1.150 albertel 532: my $lasttag=$$stack[-1];
533: if ($token->[1] =~ /^$lasttag$/i) {
1.244 albertel 534: &Apache::lonxml::warning('Using tag </'.$token->[1].'> on line '.$token->[3].' as end tag to <'.$$stack[-1].'>');
1.150 albertel 535: last;
536: } else {
1.244 albertel 537: &Apache::lonxml::warning('Found tag </'.$token->[1].'> on line '.$token->[3].' when looking for </'.$$stack[-1].'> in file');
1.150 albertel 538: &end_tag($stack,$parstack,$token);
539: }
1.101 albertel 540: }
1.140 albertel 541:
1.212 albertel 542: if ($Apache::lonxml::usestyle &&
543: exists($$style_for_target{'/'."$token->[1]"})) {
544: $Apache::lonxml::usestyle=0;
545: my $string=$$style_for_target{'/'.$token->[1]}.
546: '<LONCAPA_INTERNAL_TURN_STYLE_ON />';
547: &Apache::lonxml::newparser($pars,\$string);
1.257 ! albertel 548: $Apache::lonxml::style_values=$$parstack[-1];
1.101 albertel 549: } else {
550: $result = &callsub("end_$token->[1]", $target, $token, $stack,
551: $parstack, $pars,$safeeval, $style_for_target);
552: }
553: } else {
554: &Apache::lonxml::error("Unknown token event :$token->[0]:$token->[1]:");
555: }
556: #evaluate variable refs in result
557: if ($result ne "") {
1.257 ! albertel 558: my $extras;
! 559: if (!$Apache::lonxml::usestyle) {
! 560: $extras=$Apache::lonxml::style_values;
! 561: }
1.101 albertel 562: if ( $#$parstack > -1 ) {
1.257 ! albertel 563: $result=&Apache::run::evaluate($result,$safeeval,$extras.$$parstack[-1]);
1.101 albertel 564: } else {
1.257 ! albertel 565: $result= &Apache::run::evaluate($result,$safeeval,$extras);
1.101 albertel 566: }
1.163 albertel 567: }
1.190 albertel 568: if (($token->[0] eq 'T') || ($token->[0] eq 'C') || ($token->[0] eq 'D') ) {
1.249 albertel 569: #Style file definitions should be correct
1.250 albertel 570: if ($target eq 'tex' && ($Apache::lonxml::usestyle)) {
1.249 albertel 571: $result=&latex_special_symbols($result,$stack,$parstack);
572: }
1.190 albertel 573: }
574:
1.169 albertel 575: # Encode any high ASCII characters
576: if (!$Apache::lonxml::prevent_entity_encode) {
577: $result=&HTML::Entities::encode($result,"\200-\377");
578: }
579: if ($Apache::lonxml::redirection) {
580: $Apache::lonxml::outputstack['-1'] .= $result;
581: } else {
582: $finaloutput.=$result;
583: }
584: $result = '';
585:
1.101 albertel 586: if ($token->[0] eq 'E') {
587: &end_tag($stack,$parstack,$token);
588: }
1.224 albertel 589: }
1.212 albertel 590: if ($#$pars > -1) {
591: pop @$pars;
592: pop @Apache::lonxml::pwd;
593: }
1.101 albertel 594: }
595:
596: # if ($target eq 'meta') {
597: # $finaloutput.=&endredirection;
598: # }
599:
1.169 albertel 600:
1.101 albertel 601: if (($ENV{'QUERY_STRING'}) && ($target eq 'web')) {
602: $finaloutput=&afterburn($finaloutput);
1.216 sakharuk 603: }
1.101 albertel 604: return $finaloutput;
605: }
1.67 www 606:
1.7 albertel 607: sub callsub {
1.84 albertel 608: my ($sub,$target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.7 albertel 609: my $currentstring='';
1.72 albertel 610: my $nodefault;
1.7 albertel 611: {
1.59 albertel 612: my $sub1;
1.7 albertel 613: no strict 'refs';
1.68 www 614: my $tag=$token->[1];
1.236 www 615: # get utterly rid of extended html tags
616: if ($tag=~/^x\-/i) { return ''; }
1.141 albertel 617: my $space=$Apache::lonxml::alltags{$tag}[-1];
1.68 www 618: if (!$space) {
1.141 albertel 619: $tag=~tr/A-Z/a-z/;
1.68 www 620: $sub=~tr/A-Z/a-z/;
1.141 albertel 621: $space=$Apache::lonxml::alltags{$tag}[-1]
1.68 www 622: }
1.97 albertel 623:
624: my $deleted=0;
625: $Apache::lonxml::curdepth=join('_',@Apache::lonxml::depthcounter);
626: if (($token->[0] eq 'S') && ($target eq 'modified')) {
627: $deleted=&Apache::edit::handle_delete($space,$target,$token,$tagstack,
628: $parstack,$parser,$safeeval,
629: $style);
630: }
631: if (!$deleted) {
632: if ($space) {
1.220 albertel 633: #&Apache::lonxml::debug("Calling sub $sub in $space $metamode");
1.97 albertel 634: $sub1="$space\:\:$sub";
635: ($currentstring,$nodefault) = &$sub1($target,$token,$tagstack,
636: $parstack,$parser,$safeeval,
637: $style);
638: } else {
1.220 albertel 639: #&Apache::lonxml::debug("NOT Calling sub $sub in $space $metamode");
1.97 albertel 640: if ($metamode <1) {
641: if (defined($token->[4]) && ($metamode < 1)) {
642: $currentstring = $token->[4];
643: } else {
644: $currentstring = $token->[2];
645: }
1.62 sakharuk 646: }
1.7 albertel 647: }
1.97 albertel 648: # &Apache::lonxml::debug("nodefalt:$nodefault:");
649: if ($currentstring eq '' && $nodefault eq '') {
650: if ($target eq 'edit') {
1.220 albertel 651: #&Apache::lonxml::debug("doing default edit for $token->[1]");
1.97 albertel 652: if ($token->[0] eq 'S') {
653: $currentstring = &Apache::edit::tag_start($target,$token);
654: } elsif ($token->[0] eq 'E') {
655: $currentstring = &Apache::edit::tag_end($target,$token);
656: }
657: } elsif ($target eq 'modified') {
658: if ($token->[0] eq 'S') {
659: $currentstring = $token->[4];
660: $currentstring.=&Apache::edit::handle_insert();
1.210 www 661: } elsif ($token->[0] eq 'E') {
662: $currentstring = $token->[2];
663: $currentstring.=&Apache::edit::handle_insertafter($token->[1]);
1.97 albertel 664: } else {
665: $currentstring = $token->[2];
666: }
1.72 albertel 667: }
668: }
1.7 albertel 669: }
670: use strict 'refs';
671: }
672: return $currentstring;
1.82 ng 673: }
674:
1.96 albertel 675: sub setup_globals {
1.172 albertel 676: my ($request,$target)=@_;
677: $Apache::lonxml::request=$request;
1.99 albertel 678: $Apache::lonxml::registered = 0;
1.205 www 679: $errorcount=0;
680: $warningcount=0;
1.207 albertel 681: $Apache::lonxml::default_homework_loaded=0;
1.212 albertel 682: $Apache::lonxml::usestyle=1;
1.204 albertel 683: &init_counter();
1.101 albertel 684: @Apache::lonxml::pwd=();
1.124 albertel 685: @Apache::lonxml::extlinks=();
1.96 albertel 686: if ($target eq 'meta') {
687: $Apache::lonxml::redirection = 0;
688: $Apache::lonxml::metamode = 1;
689: $Apache::lonxml::evaluate = 1;
690: $Apache::lonxml::import = 0;
1.129 albertel 691: } elsif ($target eq 'answer') {
692: $Apache::lonxml::redirection = 0;
693: $Apache::lonxml::metamode = 1;
694: $Apache::lonxml::evaluate = 1;
695: $Apache::lonxml::import = 1;
1.96 albertel 696: } elsif ($target eq 'grade') {
697: &startredirection;
698: $Apache::lonxml::metamode = 0;
699: $Apache::lonxml::evaluate = 1;
700: $Apache::lonxml::import = 1;
701: } elsif ($target eq 'modified') {
702: $Apache::lonxml::redirection = 0;
703: $Apache::lonxml::metamode = 0;
704: $Apache::lonxml::evaluate = 0;
705: $Apache::lonxml::import = 0;
706: } elsif ($target eq 'edit') {
707: $Apache::lonxml::redirection = 0;
708: $Apache::lonxml::metamode = 0;
709: $Apache::lonxml::evaluate = 0;
710: $Apache::lonxml::import = 0;
1.163 albertel 711: } elsif ($target eq 'analyze') {
712: $Apache::lonxml::redirection = 0;
713: $Apache::lonxml::metamode = 0;
714: $Apache::lonxml::evaluate = 1;
715: $Apache::lonxml::import = 1;
1.96 albertel 716: } else {
717: $Apache::lonxml::redirection = 0;
718: $Apache::lonxml::metamode = 0;
719: $Apache::lonxml::evaluate = 1;
720: $Apache::lonxml::import = 1;
721: }
722: }
723:
1.82 ng 724: sub init_safespace {
725: my ($target,$safeeval,$safehole,$safeinit) = @_;
726: $safeeval->permit("entereval");
727: $safeeval->permit(":base_math");
728: $safeeval->permit("sort");
729: $safeeval->deny(":base_io");
1.102 albertel 730: $safehole->wrap(\&Apache::scripttag::xmlparse,$safeeval,'&xmlparse');
1.251 albertel 731: $safehole->wrap(\&Apache::outputtags::multipart,$safeeval,'&multipart');
1.82 ng 732: $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
733:
734: $safehole->wrap(\&Math::Cephes::asin,$safeeval,'&asin');
735: $safehole->wrap(\&Math::Cephes::acos,$safeeval,'&acos');
736: $safehole->wrap(\&Math::Cephes::atan,$safeeval,'&atan');
737: $safehole->wrap(\&Math::Cephes::sinh,$safeeval,'&sinh');
738: $safehole->wrap(\&Math::Cephes::cosh,$safeeval,'&cosh');
739: $safehole->wrap(\&Math::Cephes::tanh,$safeeval,'&tanh');
740: $safehole->wrap(\&Math::Cephes::asinh,$safeeval,'&asinh');
741: $safehole->wrap(\&Math::Cephes::acosh,$safeeval,'&acosh');
742: $safehole->wrap(\&Math::Cephes::atanh,$safeeval,'&atanh');
743: $safehole->wrap(\&Math::Cephes::erf,$safeeval,'&erf');
744: $safehole->wrap(\&Math::Cephes::erfc,$safeeval,'&erfc');
745: $safehole->wrap(\&Math::Cephes::j0,$safeeval,'&j0');
746: $safehole->wrap(\&Math::Cephes::j1,$safeeval,'&j1');
747: $safehole->wrap(\&Math::Cephes::jn,$safeeval,'&jn');
748: $safehole->wrap(\&Math::Cephes::jv,$safeeval,'&jv');
749: $safehole->wrap(\&Math::Cephes::y0,$safeeval,'&y0');
750: $safehole->wrap(\&Math::Cephes::y1,$safeeval,'&y1');
751: $safehole->wrap(\&Math::Cephes::yn,$safeeval,'&yn');
752: $safehole->wrap(\&Math::Cephes::yv,$safeeval,'&yv');
1.215 albertel 753:
754: $safehole->wrap(\&Math::Cephes::bdtr ,$safeeval,'&bdtr' );
755: $safehole->wrap(\&Math::Cephes::bdtrc ,$safeeval,'&bdtrc' );
756: $safehole->wrap(\&Math::Cephes::bdtri ,$safeeval,'&bdtri' );
757: $safehole->wrap(\&Math::Cephes::btdtr ,$safeeval,'&btdtr' );
758: $safehole->wrap(\&Math::Cephes::chdtr ,$safeeval,'&chdtr' );
759: $safehole->wrap(\&Math::Cephes::chdtrc,$safeeval,'&chdtrc');
760: $safehole->wrap(\&Math::Cephes::chdtri,$safeeval,'&chdtri');
761: $safehole->wrap(\&Math::Cephes::fdtr ,$safeeval,'&fdtr' );
762: $safehole->wrap(\&Math::Cephes::fdtrc ,$safeeval,'&fdtrc' );
763: $safehole->wrap(\&Math::Cephes::fdtri ,$safeeval,'&fdtri' );
764: $safehole->wrap(\&Math::Cephes::gdtr ,$safeeval,'&gdtr' );
765: $safehole->wrap(\&Math::Cephes::gdtrc ,$safeeval,'&gdtrc' );
766: $safehole->wrap(\&Math::Cephes::nbdtr ,$safeeval,'&nbdtr' );
767: $safehole->wrap(\&Math::Cephes::nbdtrc,$safeeval,'&nbdtrc');
768: $safehole->wrap(\&Math::Cephes::nbdtri,$safeeval,'&nbdtri');
769: $safehole->wrap(\&Math::Cephes::ndtr ,$safeeval,'&ndtr' );
770: $safehole->wrap(\&Math::Cephes::ndtri ,$safeeval,'&ndtri' );
771: $safehole->wrap(\&Math::Cephes::pdtr ,$safeeval,'&pdtr' );
772: $safehole->wrap(\&Math::Cephes::pdtrc ,$safeeval,'&pdtrc' );
773: $safehole->wrap(\&Math::Cephes::pdtri ,$safeeval,'&pdtri' );
774: $safehole->wrap(\&Math::Cephes::stdtr ,$safeeval,'&stdtr' );
775: $safehole->wrap(\&Math::Cephes::stdtri,$safeeval,'&stdtri');
776:
777: # $safehole->wrap(\&Math::Cephes::new_fract,$safeeval,'&new_fract');
778: # $safehole->wrap(\&Math::Cephes::radd,$safeeval,'&radd');
779: # $safehole->wrap(\&Math::Cephes::rsub,$safeeval,'&rsub');
780: # $safehole->wrap(\&Math::Cephes::rmul,$safeeval,'&rmul');
781: # $safehole->wrap(\&Math::Cephes::rdiv,$safeeval,'&rdiv');
782: # $safehole->wrap(\&Math::Cephes::euclid,$safeeval,'&euclid');
783:
1.91 ng 784: $safehole->wrap(\&Math::Random::random_beta,$safeeval,'&math_random_beta');
785: $safehole->wrap(\&Math::Random::random_chi_square,$safeeval,'&math_random_chi_square');
786: $safehole->wrap(\&Math::Random::random_exponential,$safeeval,'&math_random_exponential');
787: $safehole->wrap(\&Math::Random::random_f,$safeeval,'&math_random_f');
788: $safehole->wrap(\&Math::Random::random_gamma,$safeeval,'&math_random_gamma');
789: $safehole->wrap(\&Math::Random::random_multivariate_normal,$safeeval,'&math_random_multivariate_normal');
790: $safehole->wrap(\&Math::Random::random_multinomial,$safeeval,'&math_random_multinomial');
791: $safehole->wrap(\&Math::Random::random_noncentral_chi_square,$safeeval,'&math_random_noncentral_chi_square');
792: $safehole->wrap(\&Math::Random::random_noncentral_f,$safeeval,'&math_random_noncentral_f');
793: $safehole->wrap(\&Math::Random::random_normal,$safeeval,'&math_random_normal');
794: $safehole->wrap(\&Math::Random::random_permutation,$safeeval,'&math_random_permutation');
1.93 ng 795: $safehole->wrap(\&Math::Random::random_permuted_index,$safeeval,'&math_random_permuted_index');
1.91 ng 796: $safehole->wrap(\&Math::Random::random_uniform,$safeeval,'&math_random_uniform');
797: $safehole->wrap(\&Math::Random::random_poisson,$safeeval,'&math_random_poisson');
798: $safehole->wrap(\&Math::Random::random_uniform_integer,$safeeval,'&math_random_uniform_integer');
799: $safehole->wrap(\&Math::Random::random_negative_binomial,$safeeval,'&math_random_negative_binomial');
800: $safehole->wrap(\&Math::Random::random_binomial,$safeeval,'&math_random_binomial');
801: $safehole->wrap(\&Math::Random::random_seed_from_phrase,$safeeval,'&random_seed_from_phrase');
802: $safehole->wrap(\&Math::Random::random_set_seed_from_phrase,$safeeval,'&random_set_seed_from_phrase');
803: $safehole->wrap(\&Math::Random::random_get_seed,$safeeval,'&random_get_seed');
804: $safehole->wrap(\&Math::Random::random_set_seed,$safeeval,'&random_set_seed');
805:
1.82 ng 806: #need to inspect this class of ops
807: # $safeeval->deny(":base_orig");
1.91 ng 808: $safeinit .= ';$external::target="'.$target.'";';
1.121 albertel 809: my $rndseed;
1.123 albertel 810: my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
811: $rndseed=&Apache::lonnet::rndseed($symb,$courseid,$domain,$name);
1.121 albertel 812: $safeinit .= ';$external::randomseed='.$rndseed.';';
1.249 albertel 813: &Apache::lonxml::debug("Setting rndseed to $rndseed");
1.82 ng 814: &Apache::run::run($safeinit,$safeeval);
1.207 albertel 815: }
816:
817: sub default_homework_load {
818: my ($safeeval)=@_;
819: &Apache::lonxml::debug('Loading default_homework');
820: my $default=&Apache::lonnet::getfile('/home/httpd/html/res/adm/includes/default_homework.lcpm');
1.241 albertel 821: if ($default eq -1) {
1.207 albertel 822: &Apache::lonxml::error("<b>Unable to find <i>default_homework.lcpm</i></b>");
823: } else {
824: &Apache::run::run($default,$safeeval);
825: $Apache::lonxml::default_homework_loaded=1;
826: }
1.17 albertel 827: }
828:
1.55 albertel 829: sub startredirection {
830: $Apache::lonxml::redirection++;
831: push (@Apache::lonxml::outputstack, '');
832: }
833:
834: sub endredirection {
835: if (!$Apache::lonxml::redirection) {
1.72 albertel 836: &Apache::lonxml::error("Endredirection was called, before a startredirection, perhaps you have unbalanced tags. Some debuging information:".join ":",caller);
1.55 albertel 837: return '';
838: }
839: $Apache::lonxml::redirection--;
840: pop @Apache::lonxml::outputstack;
1.97 albertel 841: }
842:
843: sub end_tag {
844: my ($tagstack,$parstack,$token)=@_;
845: pop(@$tagstack);
846: pop(@$parstack);
847: &decreasedepth($token);
1.55 albertel 848: }
849:
1.17 albertel 850: sub initdepth {
851: @Apache::lonxml::depthcounter=();
852: $Apache::lonxml::depth=-1;
853: $Apache::lonxml::olddepth=-1;
854: }
855:
856: sub increasedepth {
1.19 albertel 857: my ($token) = @_;
1.17 albertel 858: $Apache::lonxml::depth++;
859: $Apache::lonxml::depthcounter[$Apache::lonxml::depth]++;
860: if ($Apache::lonxml::depthcounter[$Apache::lonxml::depth]==1) {
861: $Apache::lonxml::olddepth=$Apache::lonxml::depth;
862: }
1.42 albertel 863: my $curdepth=join('_',@Apache::lonxml::depthcounter);
1.64 albertel 864: &Apache::lonxml::debug("s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n");
1.54 albertel 865: #print "<br />s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n";
1.17 albertel 866: }
867:
868: sub decreasedepth {
1.19 albertel 869: my ($token) = @_;
1.17 albertel 870: $Apache::lonxml::depth--;
1.36 albertel 871: if ($Apache::lonxml::depth<$Apache::lonxml::olddepth-1) {
872: $#Apache::lonxml::depthcounter--;
873: $Apache::lonxml::olddepth=$Apache::lonxml::depth+1;
874: }
1.43 albertel 875: if ( $Apache::lonxml::depth < -1) {
1.140 albertel 876: &Apache::lonxml::warning("Missing tags, unable to properly run file.");
1.43 albertel 877: $Apache::lonxml::depth='-1';
878: }
1.42 albertel 879: my $curdepth=join('_',@Apache::lonxml::depthcounter);
1.64 albertel 880: &Apache::lonxml::debug("e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n");
1.54 albertel 881: #print "<br />e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n";
1.1 sakharuk 882: }
1.19 albertel 883:
1.180 albertel 884: sub get_all_text_unbalanced {
1.190 albertel 885: #there is a copy of this in lonpublisher.pm
1.180 albertel 886: my($tag,$pars)= @_;
887: my $token;
888: my $result='';
889: $tag='<'.$tag.'>';
890: while ($token = $$pars[-1]->get_token) {
891: if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
892: $result.=$token->[1];
893: } elsif ($token->[0] eq 'PI') {
894: $result.=$token->[2];
895: } elsif ($token->[0] eq 'S') {
896: $result.=$token->[4];
897: } elsif ($token->[0] eq 'E') {
898: $result.=$token->[2];
899: }
1.211 albertel 900: if ($result =~ /(.*)\Q$tag\E(.*)/s) {
1.180 albertel 901: &Apache::lonxml::debug('Got a winner with leftovers ::'.$2);
902: &Apache::lonxml::debug('Result is :'.$1);
903: $result=$1;
904: my $redo=$tag.$2;
905: &Apache::lonxml::newparser($pars,\$redo);
906: last;
907: }
908: }
909: return $result
1.204 albertel 910: }
911:
912: sub increment_counter {
1.247 albertel 913: my ($increment) = @_;
914: if (defined($increment) && $increment gt 0) {
915: $Apache::lonxml::counter+=$increment;
916: } else {
917: $Apache::lonxml::counter++;
918: }
1.204 albertel 919: $Apache::lonxml::counter_changed=1;
920: }
921:
922: sub init_counter {
923: if (defined($ENV{'form.counter'})) {
924: $Apache::lonxml::counter=$ENV{'form.counter'};
1.247 albertel 925: $Apache::lonxml::counter_changed=0;
1.237 sakharuk 926: } else {
1.204 albertel 927: $Apache::lonxml::counter=1;
1.247 albertel 928: $Apache::lonxml::counter_changed=1;
1.204 albertel 929: }
930: }
931:
932: sub store_counter {
933: &Apache::lonnet::appenv(('form.counter' => $Apache::lonxml::counter));
934: return '';
1.180 albertel 935: }
936:
1.19 albertel 937: sub get_all_text {
938: my($tag,$pars)= @_;
1.212 albertel 939: &Apache::lonxml::debug("Got a ".ref($pars));
1.224 albertel 940: my $gotfullstack=1;
1.212 albertel 941: if (ref($pars) ne 'ARRAY') {
1.224 albertel 942: $gotfullstack=0;
1.212 albertel 943: $pars=[$pars];
944: }
1.19 albertel 945: my $depth=0;
946: my $token;
947: my $result='';
1.57 albertel 948: if ( $tag =~ m:^/: ) {
949: my $tag=substr($tag,1);
1.212 albertel 950: #&Apache::lonxml::debug("have:$tag:");
1.223 albertel 951: my $top_empty=0;
952: while (($depth >=0) && ($#$pars > -1) && (!$top_empty)) {
1.212 albertel 953: while (($depth >=0) && ($token = $$pars[-1]->get_token)) {
954: #&Apache::lonxml::debug("e token:$token->[0]:$depth:$token->[1]:".$#$pars.":".$#Apache::lonxml::pwd);
955: if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
956: $result.=$token->[1];
957: } elsif ($token->[0] eq 'PI') {
958: $result.=$token->[2];
959: } elsif ($token->[0] eq 'S') {
960: if ($token->[1] =~ /^$tag$/i) { $depth++; }
961: $result.=$token->[4];
962: } elsif ($token->[0] eq 'E') {
963: if ( $token->[1] =~ /^$tag$/i) { $depth--; }
964: #skip sending back the last end tag
965: if ($depth > -1) { $result.=$token->[2]; } else {
966: $$pars[-1]->unget_token($token);
967: }
1.57 albertel 968: }
969: }
1.228 albertel 970: if (($depth >=0) && ($#$pars == 0) ) { $top_empty=1; }
1.212 albertel 971: if (($depth >=0) && ($#$pars > 0) ) {
972: pop(@$pars);
973: pop(@Apache::lonxml::pwd);
974: }
1.224 albertel 975: }
976: if ($top_empty && $depth >= 0) {
977: #never found the end tag ran out of text, throw error send back blank
978: &error('Never found end tag for <'.$tag.'>');
979: if ($gotfullstack) {
980: my $newstring='</'.$tag.'>'.$result;
981: &Apache::lonxml::newparser($pars,\$newstring);
982: }
983: $result='';
1.57 albertel 984: }
985: } else {
1.212 albertel 986: while ($#$pars > -1) {
987: while ($token = $$pars[-1]->get_token) {
988: #&Apache::lonxml::debug("s token:$token->[0]:$depth:$token->[1]");
989: if (($token->[0] eq 'T')||($token->[0] eq 'C')||
990: ($token->[0] eq 'D')) {
991: $result.=$token->[1];
992: } elsif ($token->[0] eq 'PI') {
993: $result.=$token->[2];
994: } elsif ($token->[0] eq 'S') {
995: if ( $token->[1] =~ /^$tag$/i) {
996: $$pars[-1]->unget_token($token); last;
997: } else {
998: $result.=$token->[4];
999: }
1000: } elsif ($token->[0] eq 'E') {
1001: $result.=$token->[2];
1002: }
1003: }
1004: if (($#$pars > 0) ) {
1005: pop(@$pars);
1006: pop(@Apache::lonxml::pwd);
1007: } else { last; }
1.36 albertel 1008: }
1.19 albertel 1009: }
1.212 albertel 1010: if ($result =~ m|<LONCAPA_INTERNAL_TURN_STYLE_ON />|) {
1011: $Apache::lonxml::usestyle=1;
1012: }
1013: #&Apache::lonxml::debug("Exit:$result:");
1.19 albertel 1014: return $result
1015: }
1016:
1.23 albertel 1017: sub newparser {
1018: my ($parser,$contentref,$dir) = @_;
1.167 albertel 1019: push (@$parser,HTML::LCParser->new($contentref));
1.56 albertel 1020: $$parser['-1']->xml_mode('1');
1.23 albertel 1021: if ( $dir eq '' ) {
1022: push (@Apache::lonxml::pwd, $Apache::lonxml::pwd[$#Apache::lonxml::pwd]);
1023: } else {
1024: push (@Apache::lonxml::pwd, $dir);
1025: }
1026: # &Apache::lonxml::debug("pwd:$#Apache::lonxml::pwd");
1027: # &Apache::lonxml::debug("pwd:$Apache::lonxml::pwd[$#Apache::lonxml::pwd]");
1028: }
1.1 sakharuk 1029:
1.8 albertel 1030: sub parstring {
1031: my ($token) = @_;
1032: my $temp='';
1.142 albertel 1033: foreach (@{$token->[3]}) {
1.35 www 1034: unless ($_=~/\W/) {
1.42 albertel 1035: my $val=$token->[2]->{$_};
1.231 albertel 1036: $val =~ s/([\%\@\\\"\'])/\\$1/g;
1.51 albertel 1037: #if ($val =~ m/^[\%\@]/) { $val="\\".$val; }
1.42 albertel 1038: $temp .= "my \$$_=\"$val\";"
1.20 albertel 1039: }
1.142 albertel 1040: }
1.8 albertel 1041: return $temp;
1042: }
1.22 albertel 1043:
1.34 www 1044: sub writeallows {
1.126 www 1045: unless ($#extlinks>=0) { return; }
1.34 www 1046: my $thisurl='/res/'.&Apache::lonnet::declutter(shift);
1.111 www 1047: if ($ENV{'httpref.'.$thisurl}) {
1048: $thisurl=$ENV{'httpref.'.$thisurl};
1049: }
1.34 www 1050: my $thisdir=$thisurl;
1051: $thisdir=~s/\/[^\/]+$//;
1052: my %httpref=();
1.142 albertel 1053: foreach (@extlinks) {
1.34 www 1054: $httpref{'httpref.'.
1.125 www 1055: &Apache::lonnet::hreflocation($thisdir,$_)}=$thisurl;
1.142 albertel 1056: }
1.126 www 1057: @extlinks=();
1.34 www 1058: &Apache::lonnet::appenv(%httpref);
1059: }
1060:
1.66 www 1061: #
1062: # Afterburner handles anchors, highlights and links
1063: #
1064: sub afterburn {
1065: my $result=shift;
1.154 albertel 1066: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1067: ['highlight','anchor','link']);
1.66 www 1068: if ($ENV{'form.highlight'}) {
1.142 albertel 1069: foreach (split(/\,/,$ENV{'form.highlight'})) {
1.66 www 1070: my $anchorname=$_;
1071: my $matchthis=$anchorname;
1072: $matchthis=~s/\_+/\\s\+/g;
1073: $result=~s/($matchthis)/\<font color=\"red\"\>$1\<\/font\>/gs;
1.142 albertel 1074: }
1.66 www 1075: }
1076: if ($ENV{'form.link'}) {
1.142 albertel 1077: foreach (split(/\,/,$ENV{'form.link'})) {
1.66 www 1078: my ($anchorname,$linkurl)=split(/\>/,$_);
1079: my $matchthis=$anchorname;
1080: $matchthis=~s/\_+/\\s\+/g;
1081: $result=~s/($matchthis)/\<a href=\"$linkurl\"\>$1\<\/a\>/gs;
1.142 albertel 1082: }
1.66 www 1083: }
1084: if ($ENV{'form.anchor'}) {
1085: my $anchorname=$ENV{'form.anchor'};
1086: my $matchthis=$anchorname;
1087: $matchthis=~s/\_+/\\s\+/g;
1088: $result=~s/($matchthis)/\<a name=\"$anchorname\"\>$1\<\/a\>/s;
1089: $result.=(<<"ENDSCRIPT");
1.226 albertel 1090: <script type="text/javascript">
1.66 www 1091: document.location.hash='$anchorname';
1092: </script>
1093: ENDSCRIPT
1094: }
1095: return $result;
1096: }
1097:
1.79 www 1098: sub storefile {
1099: my ($file,$contents)=@_;
1100: if (my $fh=Apache::File->new('>'.$file)) {
1101: print $fh $contents;
1102: $fh->close();
1.147 albertel 1103: } else {
1104: &warning("Unable to save file $file");
1.79 www 1105: }
1106: }
1107:
1.151 albertel 1108: sub createnewhtml {
1109: my $filecontents=(<<SIMPLECONTENT);
1.78 www 1110: <html>
1111: <head>
1112: <title>
1113: Title of Document Goes Here
1114: </title>
1115: </head>
1116: <body bgcolor="#FFFFFF">
1117:
1118: Body of Document Goes Here
1119:
1120: </body>
1121: </html>
1122: SIMPLECONTENT
1.151 albertel 1123: return $filecontents;
1124: }
1125:
1.147 albertel 1126:
1.151 albertel 1127: sub inserteditinfo {
1128: my ($result,$filecontents)=@_;
1.157 albertel 1129: $filecontents = &HTML::Entities::encode($filecontents);
1.147 albertel 1130: # my $editheader='<a href="#editsection">Edit below</a><hr />';
1.240 albertel 1131: my $xml_help = '<table><tr><td>'.
1132: &Apache::loncommon::help_open_topic("Greek_Symbols",'Greek Symbols',
1133: undef,undef,600)
1134: .'</td><td>'.
1135: &Apache::loncommon::help_open_topic("Other_Symbols",'Other Symbols',
1136: undef,undef,600)
1137: .'</td></tr></table>';
1.254 albertel 1138: my $titledisplay=&display_title();
1.161 albertel 1139: my $buttons=(<<BUTTONS);
1140: <input type="submit" name="attemptclean"
1141: value="Save and then attempt to clean HTML" />
1142: <input type="submit" name="savethisfile" value="Save this" />
1143: <input type="submit" name="viewmode" value="View" />
1144: BUTTONS
1.78 www 1145: my $editfooter=(<<ENDFOOTER);
1146: <hr />
1147: <a name="editsection" />
1148: <form method="post">
1.240 albertel 1149: $xml_help
1.161 albertel 1150: <input type="hidden" name="editmode" value="Edit" />
1.170 www 1151: $buttons<br />
1.78 www 1152: <textarea cols="80" rows="40" name="filecont">$filecontents</textarea>
1.170 www 1153: <br />$buttons
1.78 www 1154: <br />
1155: </form>
1.254 albertel 1156: $titledisplay
1.78 www 1157: ENDFOOTER
1.147 albertel 1158: # $result=~s/(\<body[^\>]*\>)/$1$editheader/is;
1.78 www 1159: $result=~s/(\<\/body\>)/$editfooter/is;
1160: return $result;
1161: }
1162:
1.152 albertel 1163: sub get_target {
1164: my $viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
1165: if ( $ENV{'request.state'} eq 'published') {
1166: if ( defined($ENV{'form.grade_target'})
1167: && ($viewgrades == 'F' )) {
1168: return ($ENV{'form.grade_target'});
1.153 albertel 1169: } elsif (defined($ENV{'form.grade_target'})) {
1170: if (($ENV{'form.grade_target'} eq 'web') ||
1171: ($ENV{'form.grade_target'} eq 'tex') ) {
1172: return $ENV{'form.grade_target'}
1173: } else {
1174: return 'web';
1175: }
1.152 albertel 1176: } else {
1177: return 'web';
1178: }
1179: } elsif ($ENV{'request.state'} eq 'construct') {
1180: if ( defined($ENV{'form.grade_target'})) {
1181: return ($ENV{'form.grade_target'});
1182: } else {
1183: return 'web';
1184: }
1185: } else {
1186: return 'web';
1187: }
1188: }
1189:
1.24 sakharuk 1190: sub handler {
1.255 sakharuk 1191: my $request=shift;
1192:
1193: my $target=&get_target();
1194:
1195: $Apache::lonxml::debug=0;
1196:
1197: if ($ENV{'browser.mathml'}) {
1198: $request->content_type('text/xml');
1199: } else {
1200: $request->content_type('text/html');
1201: }
1202: &Apache::loncommon::no_cache($request);
1203: $request->send_http_header;
1204:
1205: return OK if $request->header_only;
1.68 www 1206:
1207:
1.255 sakharuk 1208: my $file=&Apache::lonnet::filelocation("",$request->uri);
1.78 www 1209: #
1210: # Edit action? Save file.
1211: #
1.255 sakharuk 1212: unless ($ENV{'request.state'} eq 'published') {
1213: if (($ENV{'form.savethisfile'}) || ($ENV{'form.attemptclean'})) {
1214: &storefile($file,$ENV{'form.filecont'});
1215: }
1216: }
1217: my %mystyle;
1218: my $result = '';
1219: my $filecontents=&Apache::lonnet::getfile($file);
1220: if ($filecontents eq -1) {
1221: $result=(<<ENDNOTFOUND);
1.78 www 1222: <html>
1223: <head>
1224: <title>File not found</title>
1225: </head>
1226: <body bgcolor="#FFFFFF">
1227: <b>File not found: $file</b>
1228: </body>
1229: </html>
1230: ENDNOTFOUND
1.50 albertel 1231: $filecontents='';
1.255 sakharuk 1232: if ($ENV{'request.state'} ne 'published') {
1233: $filecontents=&createnewhtml();
1234: $ENV{'form.editmode'}='Edit'; #force edit mode
1235: }
1236: } else {
1237: unless ($ENV{'request.state'} eq 'published') {
1238: if ($ENV{'form.attemptclean'}) {
1239: $filecontents=&htmlclean($filecontents,1);
1240: }
1241: }
1242: if (!$ENV{'form.editmode'} || $ENV{'form.viewmode'}) {
1243: $result = &Apache::lonxml::xmlparse($request,$target,$filecontents,
1244: '',%mystyle);
1245: }
1.147 albertel 1246: }
1.255 sakharuk 1247:
1.78 www 1248: #
1249: # Edit action? Insert editing commands
1250: #
1.255 sakharuk 1251: unless ($ENV{'request.state'} eq 'published') {
1252: if ($ENV{'form.editmode'} && (!($ENV{'form.viewmode'}))) {
1253: my $displayfile=$request->uri;
1254: $displayfile=~s/^\/[^\/]*//;
1255: $result='<html><body bgcolor="#FFFFFF"><h3>'.$displayfile.
1256: '</h3></body></html>';
1257: $result=&inserteditinfo($result,$filecontents);
1258: }
1.147 albertel 1259: }
1.255 sakharuk 1260:
1261: writeallows($request->uri);
1262:
1263:
1264: $request->print($result);
1265:
1266: return OK;
1.253 albertel 1267: }
1268:
1269: sub display_title {
1270: my $result;
1271: if ($ENV{'request.state'} eq 'construct') {
1272: my $title=&Apache::lonnet::gettitle();
1273: if (!defined($title) || $title eq '') {
1274: $title = $ENV{'request.filename'};
1275: $title = substr($title, rindex($title, '/') + 1);
1276: }
1277: $result = "<script type='text/javascript'>top.document.title = '$title - LON-CAPA Construction Space';</script>";
1278: }
1279: return $result;
1.24 sakharuk 1280: }
1.147 albertel 1281:
1.22 albertel 1282: sub debug {
1283: if ($Apache::lonxml::debug eq 1) {
1.146 albertel 1284: $|=1;
1.202 albertel 1285: print('<font size="-2"<pre>DEBUG:'.&HTML::Entities::encode($_[0])."</pre></font>\n");
1.22 albertel 1286: }
1287: }
1.49 albertel 1288:
1.22 albertel 1289: sub error {
1.205 www 1290: $errorcount++;
1.74 albertel 1291: if (($Apache::lonxml::debug eq 1) || ($ENV{'request.state'} eq 'construct') ) {
1.166 matthew 1292: # If printing in construction space, put the error inside <pre></pre>
1.167 albertel 1293: print "<b>ERROR:</b>".join("\n",@_)."\n";
1.52 albertel 1294: } else {
1295: print "<b>An Error occured while processing this resource. The instructor has been notified.</b> <br />";
1296: #notify author
1.146 albertel 1297: &Apache::lonmsg::author_res_msg($ENV{'request.filename'},join('<br />',@_));
1.52 albertel 1298: #notify course
1299: if ( $ENV{'request.course.id'} ) {
1.209 www 1300: my (undef,%users)=&Apache::lonfeedback::decide_receiver(undef,0,1,1,1);
1.143 www 1301: my $declutter=&Apache::lonnet::declutter($ENV{'request.filename'});
1.209 www 1302: foreach (keys %users) {
1303: my ($user,$domain) = split(/:/, $_);
1.143 www 1304: &Apache::lonmsg::user_normal_msg($user,$domain,
1.146 albertel 1305: "Error [$declutter]",join('<br />',@_));
1.52 albertel 1306: }
1307: }
1.74 albertel 1308:
1.52 albertel 1309: #FIXME probably shouldn't have me get everything forever.
1.146 albertel 1310: &Apache::lonmsg::user_normal_msg('albertel','msu',"Error in $ENV{'request.filename'}",join('<br />',@_));
1.74 albertel 1311: #&Apache::lonmsg::user_normal_msg('albertel','103',"Error in $ENV{'request.filename'}",$_[0]);
1.52 albertel 1312: }
1.22 albertel 1313: }
1.49 albertel 1314:
1.22 albertel 1315: sub warning {
1.205 www 1316: $warningcount++;
1.257 ! albertel 1317: if ($ENV{'request.state'} eq 'construct' || $Apache::lonxml::debug) {
1.146 albertel 1318: print "<b>W</b>ARNING<b>:</b>".join('<br />',@_)."<br />\n";
1.73 harris41 1319: }
1.83 albertel 1320: }
1321:
1322: sub get_param {
1.213 albertel 1323: my ($param,$parstack,$safeeval,$context,$case_insensitive) = @_;
1324: if ( ! $context ) { $context = -1; }
1325: my $args ='';
1326: if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; }
1327: if ( ! $args ) { return undef; }
1328: if ( $case_insensitive ) {
1329: if ($args =~ s/(my \$)(\Q$param\E)(=\")/$1.lc($2).$3/ei) {
1330: return &Apache::run::run("{$args;".'return $'.$param.'}',
1331: $safeeval); #'
1332: } else {
1333: return undef;
1334: }
1335: } else {
1336: if ( $args =~ /my \$\Q$param\E=\"/ ) {
1337: return &Apache::run::run("{$args;".'return $'.$param.'}',
1338: $safeeval); #'
1339: } else {
1340: return undef;
1341: }
1342: }
1.22 albertel 1343: }
1344:
1.132 albertel 1345: sub get_param_var {
1.213 albertel 1346: my ($param,$parstack,$safeeval,$context,$case_insensitive) = @_;
1.132 albertel 1347: if ( ! $context ) { $context = -1; }
1348: my $args ='';
1349: if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; }
1.230 albertel 1350: &Apache::lonxml::debug("Args are $args param is $param");
1.213 albertel 1351: if ($case_insensitive) {
1352: if (! ($args=~s/(my \$)(\Q$param\E)(=\")/$1.lc($2).$3/ei)) {
1353: return undef;
1354: }
1355: } elsif ( $args !~ /my \$\Q$param\E=\"/ ) { return undef; }
1.132 albertel 1356: my $value=&Apache::run::run("{$args;".'return $'.$param.'}',$safeeval); #'
1.230 albertel 1357: &Apache::lonxml::debug("first run is $value");
1358: if ($value =~ /^[\$\@\%]\w+$/) {
1359: &Apache::lonxml::debug("doing second");
1360: my @result=&Apache::run::run("return $value",$safeeval,1);
1361: if (!defined($result[0])) {
1362: return $value
1363: } else {
1364: if (wantarray) { return @result; } else { return $result[0]; }
1365: }
1.132 albertel 1366: } else {
1367: return $value;
1368: }
1369: }
1370:
1.74 albertel 1371: sub register_insert {
1.75 albertel 1372: my @data = split /\n/, &Apache::lonnet::getfile('/home/httpd/lonTabs/insertlist.tab');
1.74 albertel 1373: my $i;
1.76 albertel 1374: my $tagnum=0;
1.74 albertel 1375: my @order;
1376: for ($i=0;$i < $#data; $i++) {
1377: my $line = $data[$i];
1378: if ( $line =~ /^\#/ || $line =~ /^\s*\n/) { next; }
1379: if ( $line =~ /TABLE/ ) { last; }
1.92 albertel 1380: my ($tag,$descrip,$color,$function,$show) = split(/,/, $line);
1.135 albertel 1381: if ($tag) {
1382: $insertlist{"$tagnum.tag"} = $tag;
1383: $insertlist{"$tagnum.description"} = $descrip;
1384: $insertlist{"$tagnum.color"} = $color;
1385: $insertlist{"$tagnum.function"} = $function;
1386: if (!defined($show)) { $show='yes'; }
1387: $insertlist{"$tagnum.show"}= $show;
1388: $insertlist{"$tag.num"}=$tagnum;
1389: $tagnum++;
1390: }
1.74 albertel 1391: }
1.76 albertel 1392: $i++; #skipping TABLE line
1393: $tagnum = 0;
1.74 albertel 1394: for (;$i < $#data;$i++) {
1395: my $line = $data[$i];
1.76 albertel 1396: my ($mnemonic,@which) = split(/ +/,$line);
1397: my $tag = $insertlist{"$tagnum.tag"};
1.144 matthew 1398: for (my $j=0;$j <=$#which;$j++) {
1.74 albertel 1399: if ( $which[$j] eq 'Y' ) {
1.76 albertel 1400: if ($insertlist{"$j.show"} ne 'no') {
1401: push(@{ $insertlist{"$tag.which"} },$j);
1402: }
1.74 albertel 1403: }
1404: }
1.76 albertel 1405: $tagnum++;
1.74 albertel 1406: }
1407: }
1.98 albertel 1408:
1409: sub description {
1410: my ($token)=@_;
1.138 albertel 1411: my $tagnum;
1412: my $tag=$token->[1];
1413: foreach my $namespace (reverse @Apache::lonxml::namespace) {
1414: my $testtag=$namespace.'::'.$tag;
1415: $tagnum=$insertlist{"$testtag.num"};
1416: if (defined($tagnum)) { last; }
1417: }
1418: if (!defined ($tagnum)) { $tagnum=$Apache::lonxml::insertlist{"$tag.num"}; }
1419: return $insertlist{$tagnum.'.description'};
1.98 albertel 1420: }
1.123 albertel 1421:
1422: # ----------------------------------------------------------------- whichuser
1423: # returns a list of $symb, $courseid, $domain, $name that is correct for
1424: # calls to lonnet functions for this setup.
1425: # - looks for form.grade_ parameters
1426: sub whichuser {
1.245 albertel 1427: my ($symb,$courseid,$domain,$name,$publicuser);
1.123 albertel 1428: if (defined($ENV{'form.grade_symb'})) {
1429: my $tmp_courseid=$ENV{'form.grade_courseid'};
1430: my $allowed=&Apache::lonnet::allowed('mgr',$tmp_courseid);
1431: if ($allowed) {
1432: $symb=$ENV{'form.grade_symb'};
1433: $courseid=$ENV{'form.grade_courseid'};
1434: $domain=$ENV{'form.grade_domain'};
1435: $name=$ENV{'form.grade_username'};
1436: }
1.134 albertel 1437: } else {
1.244 albertel 1438: $symb=&Apache::lonnet::symbread();
1439: $courseid=$ENV{'request.course.id'};
1440: $domain=$ENV{'user.domain'};
1441: $name=$ENV{'user.name'};
1442: if ($name eq 'public' && $domain eq 'public') {
1443: if (!defined($ENV{'form.username'})) {
1444: $ENV{'form.username'}.=time.rand(10000000);
1445: }
1446: $name.=$ENV{'form.username'};
1447: }
1.123 albertel 1448: }
1.245 albertel 1449: return ($symb,$courseid,$domain,$name,$publicuser);
1.123 albertel 1450: }
1451:
1.1 sakharuk 1452: 1;
1453: __END__
1.68 www 1454:
1455:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>