Annotation of loncom/xml/lonxml.pm, revision 1.84
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.84 ! albertel 206: $result = &callsub("start_$token->[1]", $target, $token, \@stack,
! 207: \@parstack, \@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.84 ! albertel 228: $result = &callsub("end_$token->[1]", $target, $token, \@stack,
! 229: \@parstack, \@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.84 ! albertel 290: $partstring = &callsub("start_$tokenpat->[1]", $target, $tokenpat,
! 291: \@innerstack, \@innerparstack, \@pat,
! 292: $safeeval, $style_for_target);
1.16 albertel 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.84 ! albertel 300: $partstring = &callsub("end_$tokenpat->[1]", $target, $tokenpat,
! 301: \@innerstack, \@innerparstack, \@pat,
! 302: $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.84 ! albertel 335: my ($sub,$target,$token,$tagstack,$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') {
1.84 ! albertel 342: $currentstring = &Apache::edit::tag_start($target,$token,$tagstack,
! 343: $parstack,$parser,
1.59 albertel 344: $safeeval,$style);
345: }
1.68 www 346: my $tag=$token->[1];
347: my $space=$Apache::lonxml::alltags{$tag};
348: if (!$space) {
349: $tag=~tr/A-Z/a-z/;
350: $sub=~tr/A-Z/a-z/;
351: $space=$Apache::lonxml::alltags{$tag}
352: }
353: if ($space) {
1.72 albertel 354: #&Apache::lonxml::debug("Calling sub $sub in $space $metamode<br />\n");
1.24 sakharuk 355: $sub1="$space\:\:$sub";
1.17 albertel 356: $Apache::lonxml::curdepth=join('_',@Apache::lonxml::depthcounter);
1.84 ! albertel 357: ($currentstring,$nodefault) = &$sub1($target,$token,$tagstack,
! 358: $parstack,$parser,$safeeval,
! 359: $style);
1.7 albertel 360: } else {
1.72 albertel 361: #&Apache::lonxml::debug("NOT Calling sub $sub in $space $metamode<br />\n");
1.62 sakharuk 362: if ($metamode <1) {
363: if (defined($token->[4]) && ($metamode < 1)) {
1.72 albertel 364: $currentstring = $token->[4];
1.62 sakharuk 365: } else {
1.72 albertel 366: $currentstring = $token->[2];
1.62 sakharuk 367: }
1.7 albertel 368: }
1.59 albertel 369: }
1.83 albertel 370: # &Apache::lonxml::debug("nodefalt:$nodefault:");
1.72 albertel 371: if ($currentstring eq '' && $nodefault eq '') {
372: if ($target eq 'edit') {
1.74 albertel 373: &Apache::lonxml::debug("doing default edit for $token->[1]");
1.72 albertel 374: if ($token->[0] eq 'S') {
1.74 albertel 375: $currentstring = &Apache::edit::tag_start($target,$token);
1.72 albertel 376: } elsif ($token->[0] eq 'E') {
1.74 albertel 377: $currentstring = &Apache::edit::tag_end($target,$token);
1.72 albertel 378: }
379: } elsif ($target eq 'modified') {
380: if ($token->[0] eq 'S') {
381: $currentstring = $token->[4];
1.76 albertel 382: $currentstring.=&Apache::edit::handle_insert();
1.72 albertel 383: } else {
384: $currentstring = $token->[2];
385: }
386: }
1.7 albertel 387: }
388: use strict 'refs';
389: }
390: return $currentstring;
1.82 ng 391: }
392:
393: sub init_safespace {
394: my ($target,$safeeval,$safehole,$safeinit) = @_;
395: $safeeval->permit("entereval");
396: $safeeval->permit(":base_math");
397: $safeeval->permit("sort");
398: $safeeval->deny(":base_io");
399: $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
400:
401: $safehole->wrap(\&Math::Cephes::asin,$safeeval,'&asin');
402: $safehole->wrap(\&Math::Cephes::acos,$safeeval,'&acos');
403: $safehole->wrap(\&Math::Cephes::atan,$safeeval,'&atan');
404: $safehole->wrap(\&Math::Cephes::sinh,$safeeval,'&sinh');
405: $safehole->wrap(\&Math::Cephes::cosh,$safeeval,'&cosh');
406: $safehole->wrap(\&Math::Cephes::tanh,$safeeval,'&tanh');
407: $safehole->wrap(\&Math::Cephes::asinh,$safeeval,'&asinh');
408: $safehole->wrap(\&Math::Cephes::acosh,$safeeval,'&acosh');
409: $safehole->wrap(\&Math::Cephes::atanh,$safeeval,'&atanh');
410: $safehole->wrap(\&Math::Cephes::erf,$safeeval,'&erf');
411: $safehole->wrap(\&Math::Cephes::erfc,$safeeval,'&erfc');
412: $safehole->wrap(\&Math::Cephes::j0,$safeeval,'&j0');
413: $safehole->wrap(\&Math::Cephes::j1,$safeeval,'&j1');
414: $safehole->wrap(\&Math::Cephes::jn,$safeeval,'&jn');
415: $safehole->wrap(\&Math::Cephes::jv,$safeeval,'&jv');
416: $safehole->wrap(\&Math::Cephes::y0,$safeeval,'&y0');
417: $safehole->wrap(\&Math::Cephes::y1,$safeeval,'&y1');
418: $safehole->wrap(\&Math::Cephes::yn,$safeeval,'&yn');
419: $safehole->wrap(\&Math::Cephes::yv,$safeeval,'&yv');
420:
421: #need to inspect this class of ops
422: # $safeeval->deny(":base_orig");
423: $safeinit .= ';$external::target='.$target.';';
424: $safeinit .= ';$external::randomseed='.&Apache::lonnet::rndseed().';';
425: &Apache::run::run($safeinit,$safeeval);
1.17 albertel 426: }
427:
1.55 albertel 428: sub startredirection {
429: $Apache::lonxml::redirection++;
430: push (@Apache::lonxml::outputstack, '');
431: }
432:
433: sub endredirection {
434: if (!$Apache::lonxml::redirection) {
1.72 albertel 435: &Apache::lonxml::error("Endredirection was called, before a startredirection, perhaps you have unbalanced tags. Some debuging information:".join ":",caller);
1.55 albertel 436: return '';
437: }
438: $Apache::lonxml::redirection--;
439: pop @Apache::lonxml::outputstack;
440: }
441:
1.17 albertel 442: sub initdepth {
443: @Apache::lonxml::depthcounter=();
444: $Apache::lonxml::depth=-1;
445: $Apache::lonxml::olddepth=-1;
446: }
447:
448: sub increasedepth {
1.19 albertel 449: my ($token) = @_;
1.17 albertel 450: $Apache::lonxml::depth++;
451: $Apache::lonxml::depthcounter[$Apache::lonxml::depth]++;
452: if ($Apache::lonxml::depthcounter[$Apache::lonxml::depth]==1) {
453: $Apache::lonxml::olddepth=$Apache::lonxml::depth;
454: }
1.42 albertel 455: my $curdepth=join('_',@Apache::lonxml::depthcounter);
1.64 albertel 456: &Apache::lonxml::debug("s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n");
1.54 albertel 457: #print "<br />s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n";
1.17 albertel 458: }
459:
460: sub decreasedepth {
1.19 albertel 461: my ($token) = @_;
1.17 albertel 462: $Apache::lonxml::depth--;
1.36 albertel 463: if ($Apache::lonxml::depth<$Apache::lonxml::olddepth-1) {
464: $#Apache::lonxml::depthcounter--;
465: $Apache::lonxml::olddepth=$Apache::lonxml::depth+1;
466: }
1.43 albertel 467: if ( $Apache::lonxml::depth < -1) {
1.49 albertel 468: &Apache::lonxml::warning("Unbalanced tags in resource");
1.43 albertel 469: $Apache::lonxml::depth='-1';
470: }
1.42 albertel 471: my $curdepth=join('_',@Apache::lonxml::depthcounter);
1.64 albertel 472: &Apache::lonxml::debug("e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n");
1.54 albertel 473: #print "<br />e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n";
1.1 sakharuk 474: }
1.19 albertel 475:
476: sub get_all_text {
477:
478: my($tag,$pars)= @_;
479: my $depth=0;
480: my $token;
481: my $result='';
1.57 albertel 482: if ( $tag =~ m:^/: ) {
483: my $tag=substr($tag,1);
484: # &Apache::lonxml::debug("have:$tag:");
485: while (($depth >=0) && ($token = $pars->get_token)) {
486: # &Apache::lonxml::debug("e token:$token->[0]:$depth:$token->[1]");
487: if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
488: $result.=$token->[1];
489: } elsif ($token->[0] eq 'PI') {
490: $result.=$token->[2];
491: } elsif ($token->[0] eq 'S') {
492: if ($token->[1] eq $tag) { $depth++; }
493: $result.=$token->[4];
494: } elsif ($token->[0] eq 'E') {
495: if ( $token->[1] eq $tag) { $depth--; }
496: #skip sending back the last end tag
497: if ($depth > -1) { $result.=$token->[2]; } else {
498: $pars->unget_token($token);
499: }
500: }
501: }
502: } else {
503: while ($token = $pars->get_token) {
504: # &Apache::lonxml::debug("s token:$token->[0]:$depth:$token->[1]");
505: if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
506: $result.=$token->[1];
507: } elsif ($token->[0] eq 'PI') {
508: $result.=$token->[2];
509: } elsif ($token->[0] eq 'S') {
510: if ( $token->[1] eq $tag) {
511: $pars->unget_token($token); last;
512: } else {
513: $result.=$token->[4];
514: }
515: } elsif ($token->[0] eq 'E') {
516: $result.=$token->[2];
1.36 albertel 517: }
1.19 albertel 518: }
519: }
1.49 albertel 520: # &Apache::lonxml::debug("Exit:$result:");
1.19 albertel 521: return $result
522: }
523:
1.23 albertel 524: sub newparser {
525: my ($parser,$contentref,$dir) = @_;
526: push (@$parser,HTML::TokeParser->new($contentref));
1.56 albertel 527: $$parser['-1']->xml_mode('1');
1.23 albertel 528: if ( $dir eq '' ) {
529: push (@Apache::lonxml::pwd, $Apache::lonxml::pwd[$#Apache::lonxml::pwd]);
530: } else {
531: push (@Apache::lonxml::pwd, $dir);
532: }
533: # &Apache::lonxml::debug("pwd:$#Apache::lonxml::pwd");
534: # &Apache::lonxml::debug("pwd:$Apache::lonxml::pwd[$#Apache::lonxml::pwd]");
535: }
1.1 sakharuk 536:
1.8 albertel 537: sub parstring {
538: my ($token) = @_;
539: my $temp='';
1.20 albertel 540: map {
1.35 www 541: unless ($_=~/\W/) {
1.42 albertel 542: my $val=$token->[2]->{$_};
1.53 albertel 543: $val =~ s/([\%\@\\])/\\$1/g;
1.51 albertel 544: #if ($val =~ m/^[\%\@]/) { $val="\\".$val; }
1.42 albertel 545: $temp .= "my \$$_=\"$val\";"
1.20 albertel 546: }
547: } @{$token->[3]};
1.8 albertel 548: return $temp;
549: }
1.22 albertel 550:
1.34 www 551: sub writeallows {
552: my $thisurl='/res/'.&Apache::lonnet::declutter(shift);
553: my $thisdir=$thisurl;
554: $thisdir=~s/\/[^\/]+$//;
555: my %httpref=();
556: map {
557: $httpref{'httpref.'.
558: &Apache::lonnet::hreflocation($thisdir,$_)}=$thisurl; } @extlinks;
559: &Apache::lonnet::appenv(%httpref);
560: }
561:
1.66 www 562: #
563: # Afterburner handles anchors, highlights and links
564: #
565: sub afterburn {
566: my $result=shift;
567: map {
568: my ($name, $value) = split(/=/,$_);
569: $value =~ tr/+/ /;
570: $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
571: if (($name eq 'highlight')||($name eq 'anchor')||($name eq 'link')) {
572: unless ($ENV{'form.'.$name}) {
573: $ENV{'form.'.$name}=$value;
574: }
575: }
576: } (split(/&/,$ENV{'QUERY_STRING'}));
577: if ($ENV{'form.highlight'}) {
578: map {
579: my $anchorname=$_;
580: my $matchthis=$anchorname;
581: $matchthis=~s/\_+/\\s\+/g;
582: $result=~s/($matchthis)/\<font color=\"red\"\>$1\<\/font\>/gs;
583: } split(/\,/,$ENV{'form.highlight'});
584: }
585: if ($ENV{'form.link'}) {
586: map {
587: my ($anchorname,$linkurl)=split(/\>/,$_);
588: my $matchthis=$anchorname;
589: $matchthis=~s/\_+/\\s\+/g;
590: $result=~s/($matchthis)/\<a href=\"$linkurl\"\>$1\<\/a\>/gs;
591: } split(/\,/,$ENV{'form.link'});
592: }
593: if ($ENV{'form.anchor'}) {
594: my $anchorname=$ENV{'form.anchor'};
595: my $matchthis=$anchorname;
596: $matchthis=~s/\_+/\\s\+/g;
597: $result=~s/($matchthis)/\<a name=\"$anchorname\"\>$1\<\/a\>/s;
598: $result.=(<<"ENDSCRIPT");
599: <script>
600: document.location.hash='$anchorname';
601: </script>
602: ENDSCRIPT
603: }
604: return $result;
605: }
606:
1.79 www 607: sub storefile {
608: my ($file,$contents)=@_;
609: if (my $fh=Apache::File->new('>'.$file)) {
610: print $fh $contents;
611: $fh->close();
612: }
613: }
614:
1.78 www 615: sub inserteditinfo {
616: my ($result,$filecontents)=@_;
617: unless ($filecontents) {
618: $filecontents=(<<SIMPLECONTENT);
619: <html>
620: <head>
621: <title>
622: Title of Document Goes Here
623: </title>
624: </head>
625: <body bgcolor="#FFFFFF">
626:
627: Body of Document Goes Here
628:
629: </body>
630: </html>
631: SIMPLECONTENT
632: }
633: my $editheader='<a href="#editsection">Edit below</a><hr />';
634: my $editfooter=(<<ENDFOOTER);
635: <hr />
636: <a name="editsection" />
637: <form method="post">
638: <textarea cols="80" rows="40" name="filecont">$filecontents</textarea>
639: <br />
640: <input type="submit" name="savethisfile" value="Save this file" />
641: </form>
642: ENDFOOTER
643: $result=~s/(\<body[^\>]*\>)/$1$editheader/is;
644: $result=~s/(\<\/body\>)/$editfooter/is;
645: return $result;
646: }
647:
1.24 sakharuk 648: sub handler {
649: my $request=shift;
1.68 www 650:
1.64 albertel 651: my $target='web';
1.68 www 652:
1.65 albertel 653: $Apache::lonxml::debug=0;
1.68 www 654:
1.25 sakharuk 655: if ($ENV{'browser.mathml'}) {
1.27 albertel 656: $request->content_type('text/xml');
657: } else {
658: $request->content_type('text/html');
1.25 sakharuk 659: }
1.64 albertel 660:
1.27 albertel 661: $request->send_http_header;
1.64 albertel 662:
1.45 www 663: return OK if $request->header_only;
1.27 albertel 664:
1.79 www 665:
666: my $file=&Apache::lonnet::filelocation("",$request->uri);
1.78 www 667: #
668: # Edit action? Save file.
669: #
670: unless ($ENV{'request.state'} eq 'published') {
671: if ($ENV{'form.savethisfile'}) {
1.79 www 672: &storefile($file,$ENV{'form.filecont'});
1.78 www 673: }
674: }
1.24 sakharuk 675: my %mystyle;
1.50 albertel 676: my $result = '';
677: my $filecontents=&Apache::lonnet::getfile($file);
678: if ($filecontents == -1) {
1.78 www 679: $result=(<<ENDNOTFOUND);
680: <html>
681: <head>
682: <title>File not found</title>
683: </head>
684: <body bgcolor="#FFFFFF">
685: <b>File not found: $file</b>
686: </body>
687: </html>
688: ENDNOTFOUND
1.50 albertel 689: $filecontents='';
690: } else {
691: $result = &Apache::lonxml::xmlparse($target,$filecontents,'',%mystyle);
1.78 www 692: }
693:
694: #
695: # Edit action? Insert editing commands
696: #
697: unless ($ENV{'request.state'} eq 'published') {
698: $result=&inserteditinfo($result,$filecontents);
1.66 www 699: }
1.50 albertel 700:
1.67 www 701: $request->print($result);
1.64 albertel 702:
1.34 www 703: writeallows($request->uri);
1.45 www 704: return OK;
1.24 sakharuk 705: }
706:
1.22 albertel 707: sub debug {
708: if ($Apache::lonxml::debug eq 1) {
1.54 albertel 709: print "DEBUG:".$_[0]."<br />\n";
1.22 albertel 710: }
711: }
1.49 albertel 712:
1.22 albertel 713: sub error {
1.74 albertel 714: if (($Apache::lonxml::debug eq 1) || ($ENV{'request.state'} eq 'construct') ) {
1.55 albertel 715: print "<b>ERROR:</b>".$_[0]."<br />\n";
1.52 albertel 716: } else {
717: print "<b>An Error occured while processing this resource. The instructor has been notified.</b> <br />";
718: #notify author
719: &Apache::lonmsg::author_res_msg($ENV{'request.filename'},$_[0]);
720: #notify course
721: if ( $ENV{'request.course.id'} ) {
722: my $users=$ENV{'course.'.$ENV{'request.course.id'}.'.comment.email'};
723: foreach my $user (split /\,/, $users) {
724: ($user,my $domain) = split /:/, $user;
1.54 albertel 725: &Apache::lonmsg::user_normal_msg($user,$domain,"Error in $ENV{'request.filename'}",$_[0]);
1.52 albertel 726: }
727: }
1.74 albertel 728:
1.52 albertel 729: #FIXME probably shouldn't have me get everything forever.
1.54 albertel 730: &Apache::lonmsg::user_normal_msg('albertel','msu',"Error in $ENV{'request.filename'}",$_[0]);
1.74 albertel 731: #&Apache::lonmsg::user_normal_msg('albertel','103',"Error in $ENV{'request.filename'}",$_[0]);
1.52 albertel 732: }
1.22 albertel 733: }
1.49 albertel 734:
1.22 albertel 735: sub warning {
1.73 harris41 736: if ($ENV{'request.state'} eq 'construct') {
1.55 albertel 737: print "<b>W</b>ARNING<b>:</b>".$_[0]."<br />\n";
1.73 harris41 738: }
1.83 albertel 739: }
740:
741: sub get_param {
742: my ($param,$parstack,$safeeval,$context) = @_;
743: if ( ! $context ) { $context = -1; }
744: my $args ='';
745: if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; }
746: return &Apache::run::run("{$args;".'return $'.$param.'}',$safeeval); #'
1.22 albertel 747: }
748:
1.74 albertel 749: sub register_insert {
1.75 albertel 750: my @data = split /\n/, &Apache::lonnet::getfile('/home/httpd/lonTabs/insertlist.tab');
1.74 albertel 751: my $i;
1.76 albertel 752: my $tagnum=0;
1.74 albertel 753: my @order;
754: for ($i=0;$i < $#data; $i++) {
755: my $line = $data[$i];
756: if ( $line =~ /^\#/ || $line =~ /^\s*\n/) { next; }
757: if ( $line =~ /TABLE/ ) { last; }
758: my ($tag,$descrip,$function,$show) = split(/,/, $line);
1.76 albertel 759: $insertlist{"$tagnum.tag"} = $tag;
760: $insertlist{"$tagnum.description"} = $descrip;
761: $insertlist{"$tagnum.function"} = $function;
762: $insertlist{"$tagnum.show"}= $show;
763: $tagnum++;
1.74 albertel 764: }
1.76 albertel 765: $i++; #skipping TABLE line
766: $tagnum = 0;
1.74 albertel 767: for (;$i < $#data;$i++) {
768: my $line = $data[$i];
1.76 albertel 769: my ($mnemonic,@which) = split(/ +/,$line);
770: my $tag = $insertlist{"$tagnum.tag"};
1.74 albertel 771: for (my $j=0;$j <$#which;$j++) {
772: if ( $which[$j] eq 'Y' ) {
1.76 albertel 773: if ($insertlist{"$j.show"} ne 'no') {
774: push(@{ $insertlist{"$tag.which"} },$j);
775: }
1.74 albertel 776: }
777: }
1.76 albertel 778: $tagnum++;
1.74 albertel 779: }
780: }
1.1 sakharuk 781: 1;
782: __END__
1.68 www 783:
784:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>