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