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