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