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