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