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