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