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