Annotation of loncom/xml/lonxml.pm, revision 1.80
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.2 sakharuk 12:
1.4 albertel 13: package Apache::lonxml;
1.33 www 14: use vars
1.76 albertel 15: qw(@pwd @outputstack $redirection $import @extlinks $metamode $evaluate %insertlist @namespace);
1.1 sakharuk 16: use strict;
17: use HTML::TokeParser;
1.3 sakharuk 18: use Safe;
1.40 albertel 19: use Safe::Hole;
1.80 ! harris41 20: use Math::Trig;
! 21: use Math::Cephes qw(j0 j1 jn jv y0 y1 yn yv);
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;
110: }
111: }
112:
113: function LONCAPAstale() {
114: if (window.location.pathname!="/res/adm/pages/menu.html") {
115: menu=window.open("","LONCAPAmenu");
116: menu.currentStale=1;
117: }
1.68 www 118: }
1.71 www 119: // END LON-CAPA Internal
1.68 www 120: </script>
121: ENDSCRIPT
1.69 www 122: }
123:
124: sub loadevents() {
125: return 'LONCAPAreg();';
126: }
127:
128: sub unloadevents() {
129: return 'LONCAPAstale();';
1.68 www 130: }
131:
1.48 albertel 132: sub printalltags {
133: my $temp;
134: foreach $temp (sort keys %Apache::lonxml::alltags) {
1.64 albertel 135: &Apache::lonxml::debug("$temp -- $Apache::lonxml::alltags{$temp}");
1.48 albertel 136: }
137: }
1.31 sakharuk 138:
1.3 sakharuk 139: sub xmlparse {
140:
1.18 albertel 141: my ($target,$content_file_string,$safeinit,%style_for_target) = @_;
1.41 albertel 142: if ($target eq 'meta') {
1.59 albertel 143: $Apache::lonxml::redirection = 0;
144: $Apache::lonxml::metamode = 1;
1.72 albertel 145: $Apache::lonxml::evaluate = 1;
1.55 albertel 146: $Apache::lonxml::import = 0;
1.47 albertel 147: } elsif ($target eq 'grade') {
1.55 albertel 148: &startredirection;
1.59 albertel 149: $Apache::lonxml::metamode = 0;
1.72 albertel 150: $Apache::lonxml::evaluate = 1;
1.55 albertel 151: $Apache::lonxml::import = 1;
1.72 albertel 152: } elsif ($target eq 'modified') {
153: $Apache::lonxml::redirection = 0;
154: $Apache::lonxml::metamode = 0;
155: $Apache::lonxml::evaluate = 0;
156: $Apache::lonxml::import = 0;
1.44 albertel 157: } else {
1.72 albertel 158: $Apache::lonxml::redirection = 0;
1.59 albertel 159: $Apache::lonxml::metamode = 0;
1.72 albertel 160: $Apache::lonxml::evaluate = 1;
1.55 albertel 161: $Apache::lonxml::import = 1;
1.32 sakharuk 162: }
1.48 albertel 163: #&printalltags();
1.16 albertel 164: my @pars = ();
1.23 albertel 165: @Apache::lonxml::pwd=();
166: my $pwd=$ENV{'request.filename'};
167: $pwd =~ s:/[^/]*$::;
168: &newparser(\@pars,\$content_file_string,$pwd);
1.3 sakharuk 169: my $currentstring = '';
170: my $finaloutput = '';
171: my $newarg = '';
1.16 albertel 172: my $result;
1.24 sakharuk 173:
1.3 sakharuk 174: my $safeeval = new Safe;
1.40 albertel 175: my $safehole = new Safe::Hole;
1.6 albertel 176: $safeeval->permit("entereval");
1.13 albertel 177: $safeeval->permit(":base_math");
1.77 www 178: $safeeval->permit("sort");
1.19 albertel 179: $safeeval->deny(":base_io");
1.80 ! harris41 180: # $safe->share_from('Math::Trig',['acsc','asin','acos','atan']);
1.40 albertel 181: $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
1.80 ! harris41 182: $safehole->wrap(\&Math::Trig::acsc,$safeeval,'&acsc');
! 183: $safehole->wrap(\&Math::Trig::acosec,$safeeval,'&acosec');
! 184: $safehole->wrap(\&Math::Trig::asec,$safeeval,'&asec');
! 185: $safehole->wrap(\&Math::Trig::acot,$safeeval,'&acot');
! 186: $safehole->wrap(\&Math::Trig::acotan,$safeeval,'&acotan');
! 187: $safehole->wrap(\&Math::Trig::acsch,$safeeval,'&acsch');
! 188: $safehole->wrap(\&Math::Trig::acosech,$safeeval,'&acosech');
! 189: $safehole->wrap(\&Math::Trig::asech,$safeeval,'&asech');
! 190: $safehole->wrap(\&Math::Trig::acoth,$safeeval,'&acoth');
! 191: $safehole->wrap(\&Math::Trig::acotanh,$safeeval,'&acotanh');
! 192: $safehole->wrap(\&Math::Trig::asin,$safeeval,'&asin');
! 193: $safehole->wrap(\&Math::Trig::acos,$safeeval,'&acos');
! 194: $safehole->wrap(\&Math::Trig::atan,$safeeval,'&atan');
! 195: $safehole->wrap(\&Math::Trig::asinh,$safeeval,'&asinh');
! 196: $safehole->wrap(\&Math::Trig::acosh,$safeeval,'&acosh');
! 197: $safehole->wrap(\&Math::Trig::atanh,$safeeval,'&atanh');
! 198: $safehole->wrap(\&Math::Trig::atan2,$safeeval,'&atan2');
! 199: $safehole->wrap(\&Math::Trig::csc,$safeeval,'&csc');
! 200: $safehole->wrap(\&Math::Trig::cosec,$safeeval,'&cosec');
! 201: $safehole->wrap(\&Math::Trig::sec,$safeeval,'&sec');
! 202: $safehole->wrap(\&Math::Trig::cot,$safeeval,'&cot');
! 203: $safehole->wrap(\&Math::Trig::cotan,$safeeval,'&cotan');
! 204: $safehole->wrap(\&Math::Trig::csch,$safeeval,'&csch');
! 205: $safehole->wrap(\&Math::Trig::cosech,$safeeval,'&cosech');
! 206: $safehole->wrap(\&Math::Trig::sech,$safeeval,'&sech');
! 207: $safehole->wrap(\&Math::Trig::coth,$safeeval,'&coth');
! 208: $safehole->wrap(\&Math::Trig::cotanh,$safeeval,'&cptanh');
! 209: $safehole->wrap(\&Math::Trig::deg2rad,$safeeval,'°2rad');
! 210: $safehole->wrap(\&Math::Trig::deg2grad,$safeeval,'°2grad');
! 211: $safehole->wrap(\&Math::Trig::grad2deg,$safeeval,'&grad2deg');
! 212: $safehole->wrap(\&Math::Trig::grad2rad,$safeeval,'&grad2rad');
! 213: $safehole->wrap(\&Math::Trig::rad2deg,$safeeval,'&rad2deg');
! 214: $safehole->wrap(\&Math::Trig::rad2grad,$safeeval,'&rad2grad');
! 215: $safehole->wrap(\&Math::Trig::pi,$safeeval,'&pi');
! 216: $safehole->wrap(\&Math::Cephes::erf,$safeeval,'&erf');
! 217: $safehole->wrap(\&Math::Cephes::erfc,$safeeval,'&erfc');
! 218: $safehole->wrap(\&Math::Cephes::j0,$safeeval,'&j0');
! 219: $safehole->wrap(\&Math::Cephes::j1,$safeeval,'&j1');
! 220: $safehole->wrap(\&Math::Cephes::jn,$safeeval,'&jn');
! 221: $safehole->wrap(\&Math::Cephes::jv,$safeeval,'&jv');
! 222: $safehole->wrap(\&Math::Cephes::y0,$safeeval,'&y0');
! 223: $safehole->wrap(\&Math::Cephes::y1,$safeeval,'&y1');
! 224: $safehole->wrap(\&Math::Cephes::yn,$safeeval,'&yn');
! 225: $safehole->wrap(\&Math::Cephes::yv,$safeeval,'&yv');
! 226:
1.19 albertel 227: #need to inspect this class of ops
228: # $safeeval->deny(":base_orig");
1.21 albertel 229: $safeinit .= ';$external::target='.$target.';';
1.26 albertel 230: $safeinit .= ';$external::randomseed='.&Apache::lonnet::rndseed().';';
1.21 albertel 231: &Apache::run::run($safeinit,$safeeval);
1.3 sakharuk 232: #-------------------- Redefinition of the target in the case of compound target
233:
234: ($target, my @tenta) = split('&&',$target);
235:
236: my @stack = ();
237: my @parstack = ();
1.17 albertel 238: &initdepth;
1.3 sakharuk 239: my $token;
1.16 albertel 240: while ( $#pars > -1 ) {
241: while ($token = $pars[$#pars]->get_token) {
1.57 albertel 242: if (($token->[0] eq 'T') || ($token->[0] eq 'C') || ($token->[0] eq 'D') ) {
1.61 albertel 243: if ($metamode<1) { $result=$token->[1]; }
1.57 albertel 244: } elsif ($token->[0] eq 'PI') {
1.61 albertel 245: if ($metamode<1) { $result=$token->[2]; }
1.16 albertel 246: } elsif ($token->[0] eq 'S') {
247: # add tag to stack
248: push (@stack,$token->[1]);
249: # add parameters list to another stack
250: push (@parstack,&parstring($token));
1.19 albertel 251: &increasedepth($token);
1.16 albertel 252: if (exists $style_for_target{$token->[1]}) {
1.61 albertel 253: if ($Apache::lonxml::redirection) {
1.55 albertel 254: $Apache::lonxml::outputstack['-1'] .=
255: &recurse($style_for_target{$token->[1]},$target,$safeeval,
256: \%style_for_target,@parstack);
1.41 albertel 257: } else {
1.55 albertel 258: $finaloutput .= &recurse($style_for_target{$token->[1]},$target,
259: $safeeval,\%style_for_target,@parstack);
1.41 albertel 260: }
1.16 albertel 261: } else {
1.17 albertel 262: $result = &callsub("start_$token->[1]", $target, $token,\@parstack,
1.41 albertel 263: \@pars, $safeeval, \%style_for_target);
1.16 albertel 264: }
265: } elsif ($token->[0] eq 'E') {
266: #clear out any tags that didn't end
1.55 albertel 267: while ($token->[1] ne $stack[$#stack] && ($#stack > -1)) {
268: &Apache::lonxml::warning("Unbalanced tags in resource $stack['-1']");
1.43 albertel 269: pop @stack;pop @parstack;&decreasedepth($token);
270: }
1.16 albertel 271:
272: if (exists $style_for_target{'/'."$token->[1]"}) {
1.61 albertel 273: if ($Apache::lonxml::redirection) {
1.55 albertel 274: $Apache::lonxml::outputstack['-1'] .=
275: &recurse($style_for_target{'/'."$token->[1]"},
276: $target,$safeeval,\%style_for_target,@parstack);
277: } else {
278: $finaloutput .= &recurse($style_for_target{'/'."$token->[1]"},
279: $target,$safeeval,\%style_for_target,
280: @parstack);
281: }
1.59 albertel 282:
1.16 albertel 283: } else {
1.17 albertel 284: $result = &callsub("end_$token->[1]", $target, $token, \@parstack,
1.55 albertel 285: \@pars,$safeeval, \%style_for_target);
1.13 albertel 286: }
1.57 albertel 287: } else {
288: &Apache::lonxml::error("Unknown token event :$token->[0]:$token->[1]:");
1.16 albertel 289: }
1.55 albertel 290: #evaluate variable refs in result
1.25 sakharuk 291: if ($result ne "") {
1.24 sakharuk 292: if ( $#parstack > -1 ) {
1.55 albertel 293: if ($Apache::lonxml::redirection) {
294: $Apache::lonxml::outputstack['-1'] .=
295: &Apache::run::evaluate($result,$safeeval,$parstack[$#parstack]);
296: } else {
297: $finaloutput .= &Apache::run::evaluate($result,$safeeval,
298: $parstack[$#parstack]);
299: }
1.16 albertel 300: } else {
301: $finaloutput .= &Apache::run::evaluate($result,$safeeval,'');
1.13 albertel 302: }
1.16 albertel 303: $result = '';
1.55 albertel 304: }
305: if ($token->[0] eq 'E') {
306: pop @stack;pop @parstack;&decreasedepth($token);
1.2 sakharuk 307: }
1.5 albertel 308: }
1.16 albertel 309: pop @pars;
1.23 albertel 310: pop @Apache::lonxml::pwd;
1.3 sakharuk 311: }
1.24 sakharuk 312:
1.59 albertel 313: # if ($target eq 'meta') {
314: # $finaloutput.=&endredirection;
315: # }
1.67 www 316:
317: if (($ENV{'QUERY_STRING'}) && ($target eq 'web')) {
318: $finaloutput=&afterburn($finaloutput);
319: }
320:
1.3 sakharuk 321: return $finaloutput;
1.15 albertel 322: }
323:
1.67 www 324:
1.15 albertel 325: sub recurse {
326:
327: my @innerstack = ();
328: my @innerparstack = ();
329: my ($newarg,$target,$safeeval,$style_for_target,@parstack) = @_;
1.16 albertel 330: my @pat = ();
1.23 albertel 331: &newparser(\@pat,\$newarg);
1.15 albertel 332: my $tokenpat;
333: my $partstring = '';
334: my $output='';
1.16 albertel 335: my $decls='';
336: while ( $#pat > -1 ) {
337: while ($tokenpat = $pat[$#pat]->get_token) {
1.57 albertel 338: if (($tokenpat->[0] eq 'T') || ($tokenpat->[0] eq 'C') || ($tokenpat->[0] eq 'D') ) {
1.61 albertel 339: if ($metamode<1) { $partstring=$tokenpat->[1]; }
1.57 albertel 340: } elsif ($tokenpat->[0] eq 'PI') {
1.61 albertel 341: if ($metamode<1) { $partstring=$tokenpat->[2]; }
1.16 albertel 342: } elsif ($tokenpat->[0] eq 'S') {
343: push (@innerstack,$tokenpat->[1]);
344: push (@innerparstack,&parstring($tokenpat));
1.19 albertel 345: &increasedepth($tokenpat);
1.16 albertel 346: $partstring = &callsub("start_$tokenpat->[1]",
347: $target, $tokenpat, \@innerparstack,
348: \@pat, $safeeval, $style_for_target);
349: } elsif ($tokenpat->[0] eq 'E') {
350: #clear out any tags that didn't end
351: while ($tokenpat->[1] ne $innerstack[$#innerstack]
1.43 albertel 352: && ($#innerstack > -1)) {
1.49 albertel 353: &Apache::lonxml::warning("Unbalanced tags in resource $innerstack['-1']");
1.43 albertel 354: pop @innerstack;pop @innerparstack;&decreasedepth($tokenpat);
355: }
1.16 albertel 356: $partstring = &callsub("end_$tokenpat->[1]",
357: $target, $tokenpat, \@innerparstack,
358: \@pat, $safeeval, $style_for_target);
1.57 albertel 359: } else {
360: &Apache::lonxml::error("Unknown token event :$tokenpat->[0]:$tokenpat->[1]:");
1.16 albertel 361: }
362: #pass both the variable to the style tag, and the tag we
363: #are processing inside the <definedtag>
364: if ( $partstring ne "" ) {
365: if ( $#parstack > -1 ) {
366: if ( $#innerparstack > -1 ) {
367: $decls= $parstack[$#parstack].$innerparstack[$#innerparstack];
368: } else {
369: $decls= $parstack[$#parstack];
370: }
371: } else {
372: if ( $#innerparstack > -1 ) {
373: $decls=$innerparstack[$#innerparstack];
374: } else {
375: $decls='';
376: }
377: }
378: $output .= &Apache::run::evaluate($partstring,$safeeval,$decls);
379: $partstring = '';
380: }
1.17 albertel 381: if ($tokenpat->[0] eq 'E') { pop @innerstack;pop @innerparstack;
1.19 albertel 382: &decreasedepth($tokenpat);}
1.15 albertel 383: }
1.16 albertel 384: pop @pat;
1.23 albertel 385: pop @Apache::lonxml::pwd;
1.15 albertel 386: }
387: return $output;
1.7 albertel 388: }
389:
390: sub callsub {
1.14 albertel 391: my ($sub,$target,$token,$parstack,$parser,$safeeval,$style)=@_;
1.7 albertel 392: my $currentstring='';
1.72 albertel 393: my $nodefault;
1.7 albertel 394: {
1.59 albertel 395: my $sub1;
1.7 albertel 396: no strict 'refs';
1.59 albertel 397: if ($target eq 'edit' && $token->[0] eq 'S') {
398: $currentstring = &Apache::edit::tag_start($target,$token,$parstack,$parser,
399: $safeeval,$style);
400: }
1.68 www 401: my $tag=$token->[1];
402: my $space=$Apache::lonxml::alltags{$tag};
403: if (!$space) {
404: $tag=~tr/A-Z/a-z/;
405: $sub=~tr/A-Z/a-z/;
406: $space=$Apache::lonxml::alltags{$tag}
407: }
408: if ($space) {
1.72 albertel 409: #&Apache::lonxml::debug("Calling sub $sub in $space $metamode<br />\n");
1.24 sakharuk 410: $sub1="$space\:\:$sub";
1.17 albertel 411: $Apache::lonxml::curdepth=join('_',@Apache::lonxml::depthcounter);
1.72 albertel 412: ($currentstring,$nodefault) = &$sub1($target,$token,$parstack,$parser,
413: $safeeval,$style);
1.7 albertel 414: } else {
1.72 albertel 415: #&Apache::lonxml::debug("NOT Calling sub $sub in $space $metamode<br />\n");
1.62 sakharuk 416: if ($metamode <1) {
417: if (defined($token->[4]) && ($metamode < 1)) {
1.72 albertel 418: $currentstring = $token->[4];
1.62 sakharuk 419: } else {
1.72 albertel 420: $currentstring = $token->[2];
1.62 sakharuk 421: }
1.7 albertel 422: }
1.59 albertel 423: }
1.72 albertel 424: &Apache::lonxml::debug("nodefalt:$nodefault:");
425: if ($currentstring eq '' && $nodefault eq '') {
426: if ($target eq 'edit') {
1.74 albertel 427: &Apache::lonxml::debug("doing default edit for $token->[1]");
1.72 albertel 428: if ($token->[0] eq 'S') {
1.74 albertel 429: $currentstring = &Apache::edit::tag_start($target,$token);
1.72 albertel 430: } elsif ($token->[0] eq 'E') {
1.74 albertel 431: $currentstring = &Apache::edit::tag_end($target,$token);
1.72 albertel 432: }
433: } elsif ($target eq 'modified') {
434: if ($token->[0] eq 'S') {
435: $currentstring = $token->[4];
1.76 albertel 436: $currentstring.=&Apache::edit::handle_insert();
1.72 albertel 437: } else {
438: $currentstring = $token->[2];
439: }
440: }
1.7 albertel 441: }
442: use strict 'refs';
443: }
444: return $currentstring;
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.22 albertel 758: }
759:
1.74 albertel 760: sub register_insert {
1.75 albertel 761: my @data = split /\n/, &Apache::lonnet::getfile('/home/httpd/lonTabs/insertlist.tab');
1.74 albertel 762: my $i;
1.76 albertel 763: my $tagnum=0;
1.74 albertel 764: my @order;
765: for ($i=0;$i < $#data; $i++) {
766: my $line = $data[$i];
767: if ( $line =~ /^\#/ || $line =~ /^\s*\n/) { next; }
768: if ( $line =~ /TABLE/ ) { last; }
769: my ($tag,$descrip,$function,$show) = split(/,/, $line);
1.76 albertel 770: $insertlist{"$tagnum.tag"} = $tag;
771: $insertlist{"$tagnum.description"} = $descrip;
772: $insertlist{"$tagnum.function"} = $function;
773: $insertlist{"$tagnum.show"}= $show;
774: $tagnum++;
1.74 albertel 775: }
1.76 albertel 776: $i++; #skipping TABLE line
777: $tagnum = 0;
1.74 albertel 778: for (;$i < $#data;$i++) {
779: my $line = $data[$i];
1.76 albertel 780: my ($mnemonic,@which) = split(/ +/,$line);
781: my $tag = $insertlist{"$tagnum.tag"};
1.74 albertel 782: for (my $j=0;$j <$#which;$j++) {
783: if ( $which[$j] eq 'Y' ) {
1.76 albertel 784: if ($insertlist{"$j.show"} ne 'no') {
785: push(@{ $insertlist{"$tag.which"} },$j);
786: }
1.74 albertel 787: }
788: }
1.76 albertel 789: $tagnum++;
1.74 albertel 790: }
791: }
1.1 sakharuk 792: 1;
793: __END__
1.68 www 794:
795:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>