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