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