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