1: # The LearningOnline Network with CAPA
2: # XML Parser Module
3: #
4: # $Id: lonxml.pm,v 1.439 2007/02/18 02:10:04 albertel Exp $
5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
28: # Copyright for TtHfunc and TtMfunc by Ian Hutchinson.
29: # TtHfunc and TtMfunc (the "Code") may be compiled and linked into
30: # binary executable programs or libraries distributed by the
31: # Michigan State University (the "Licensee"), but any binaries so
32: # distributed are hereby licensed only for use in the context
33: # of a program or computational system for which the Licensee is the
34: # primary author or distributor, and which performs substantial
35: # additional tasks beyond the translation of (La)TeX into HTML.
36: # The C source of the Code may not be distributed by the Licensee
37: # to any other parties under any circumstances.
38: #
39:
40:
41: package Apache::lonxml;
42: use vars
43: qw(@pwd @outputstack $redirection $import @extlinks $metamode $evaluate %insertlist @namespace $errorcount $warningcount);
44: use strict;
45: use HTML::LCParser();
46: use HTML::TreeBuilder();
47: use HTML::Entities();
48: use Safe();
49: use Safe::Hole();
50: use Math::Cephes();
51: use Math::Random();
52: use Opcode();
53: use POSIX qw(strftime);
54: use Time::HiRes qw( gettimeofday tv_interval );
55: use Symbol();
56:
57: sub register {
58: my ($space,@taglist) = @_;
59: foreach my $temptag (@taglist) {
60: push(@{ $Apache::lonxml::alltags{$temptag} },$space);
61: }
62: }
63:
64: sub deregister {
65: my ($space,@taglist) = @_;
66: foreach my $temptag (@taglist) {
67: my $tempspace = $Apache::lonxml::alltags{$temptag}[-1];
68: if ($tempspace eq $space) {
69: pop(@{ $Apache::lonxml::alltags{$temptag} });
70: }
71: }
72: #&printalltags();
73: }
74:
75: use Apache::Constants qw(:common);
76: use Apache::lontexconvert();
77: use Apache::style();
78: use Apache::run();
79: use Apache::londefdef();
80: use Apache::scripttag();
81: use Apache::languagetags();
82: use Apache::edit();
83: use Apache::inputtags();
84: use Apache::outputtags();
85: use Apache::lonnet;
86: use Apache::File();
87: use Apache::loncommon();
88: use Apache::lonfeedback();
89: use Apache::lonmsg();
90: use Apache::loncacc();
91: use Apache::lonmaxima();
92: use Apache::lonlocal;
93:
94: #================================================== Main subroutine: xmlparse
95: #debugging control, to turn on debugging modify the correct handler
96: $Apache::lonxml::debug=0;
97:
98: # keeps count of the number of warnings and errors generated in a parse
99: $warningcount=0;
100: $errorcount=0;
101:
102: #path to the directory containing the file currently being processed
103: @pwd=();
104:
105: #these two are used for capturing a subset of the output for later processing,
106: #don't touch them directly use &startredirection and &endredirection
107: @outputstack = ();
108: $redirection = 0;
109:
110: #controls wheter the <import> tag actually does
111: $import = 1;
112: @extlinks=();
113:
114: # meta mode is a bit weird only some output is to be turned off
115: #<output> tag turns metamode off (defined in londefdef.pm)
116: $metamode = 0;
117:
118: # turns on and of run::evaluate actually derefencing var refs
119: $evaluate = 1;
120:
121: # data structure for eidt mode, determines what tags can go into what other tags
122: %insertlist=();
123:
124: # stores the list of active tag namespaces
125: @namespace=();
126:
127: # a pointer the the Apache request object
128: $Apache::lonxml::request='';
129:
130: # a problem number counter, and check on ether it is used
131: $Apache::lonxml::counter=1;
132: $Apache::lonxml::counter_changed=0;
133:
134: #internal check on whether to look at style defs
135: $Apache::lonxml::usestyle=1;
136:
137: #locations used to store the parameter string for style substitutions
138: $Apache::lonxml::style_values='';
139: $Apache::lonxml::style_end_values='';
140:
141: #array of ssi calls that need to occur after we are done parsing
142: @Apache::lonxml::ssi_info=();
143:
144: #should we do the postag variable interpolation
145: $Apache::lonxml::post_evaluate=1;
146:
147: #a header message to emit in the case of any generated warning or errors
148: $Apache::lonxml::warnings_error_header='';
149:
150: # Control whether or not LaTeX symbols should be substituted for their
151: # \ style equivalents...this may be turned off e.g. in an verbatim
152: # environment.
153:
154: $Apache::lonxml::substitute_LaTeX_symbols = 1; # Starts out on.
155:
156: sub enable_LaTeX_substitutions {
157: $Apache::lonxml::substitute_LaTeX_symbols = 1;
158: }
159: sub disable_LaTeX_substitutions {
160: $Apache::lonxml::substitute_LaTeX_symbols = 0;
161: }
162:
163: sub xmlend {
164: my ($target,$parser)=@_;
165: my $mode='xml';
166: my $status='OPEN';
167: if ($Apache::lonhomework::parsing_a_problem ||
168: $Apache::lonhomework::parsing_a_task ) {
169: $mode='problem';
170: $status=$Apache::inputtags::status[-1];
171: }
172: my $discussion;
173: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
174: ['LONCAPA_INTERNAL_no_discussion']);
175: if (! exists($env{'form.LONCAPA_INTERNAL_no_discussion'}) ||
176: $env{'form.LONCAPA_INTERNAL_no_discussion'} ne 'true') {
177: $discussion=&Apache::lonfeedback::list_discussion($mode,$status);
178: }
179: if ($target eq 'tex') {
180: $discussion.='<tex>\keephidden{ENDOFPROBLEM}\vskip 0.5mm\noindent\makebox[\textwidth/$number_of_columns][b]{\hrulefill}\end{document}</tex>';
181: &Apache::lonxml::newparser($parser,\$discussion,'');
182: return '';
183: }
184:
185: return $discussion;
186: }
187:
188: sub tokeninputfield {
189: my $defhost=$Apache::lonnet::perlvar{'lonHostID'};
190: $defhost=~tr/a-z/A-Z/;
191: return (<<ENDINPUTFIELD)
192: <script type="text/javascript">
193: function updatetoken() {
194: var comp=new Array;
195: var barcode=unescape(document.tokeninput.barcode.value);
196: comp=barcode.split('*');
197: if (typeof(comp[0])!="undefined") {
198: document.tokeninput.codeone.value=comp[0];
199: }
200: if (typeof(comp[1])!="undefined") {
201: document.tokeninput.codetwo.value=comp[1];
202: }
203: if (typeof(comp[2])!="undefined") {
204: comp[2]=comp[2].toUpperCase();
205: document.tokeninput.codethree.value=comp[2];
206: }
207: document.tokeninput.barcode.value='';
208: }
209: </script>
210: <form method="post" name="tokeninput">
211: <table border="2" bgcolor="#FFFFBB">
212: <tr><th>DocID Checkin</th></tr>
213: <tr><td>
214: <table>
215: <tr>
216: <td>Scan in Barcode</td>
217: <td><input type="text" size="22" name="barcode"
218: onChange="updatetoken()"/></td>
219: </tr>
220: <tr><td><i>or</i> Type in DocID</td>
221: <td>
222: <input type="text" size="5" name="codeone" />
223: <b><font size="+2">*</font></b>
224: <input type="text" size="5" name="codetwo" />
225: <b><font size="+2">*</font></b>
226: <input type="text" size="10" name="codethree" value="$defhost"
227: onChange="this.value=this.value.toUpperCase()" />
228: </td></tr>
229: </table>
230: </td></tr>
231: <tr><td><input type="submit" value="Check in DocID" /></td></tr>
232: </table>
233: </form>
234: ENDINPUTFIELD
235: }
236:
237: sub maketoken {
238: my ($symb,$tuname,$tudom,$tcrsid)=@_;
239: unless ($symb) {
240: $symb=&Apache::lonnet::symbread();
241: }
242: unless ($tuname) {
243: $tuname=$env{'user.name'};
244: $tudom=$env{'user.domain'};
245: $tcrsid=$env{'request.course.id'};
246: }
247:
248: return &Apache::lonnet::checkout($symb,$tuname,$tudom,$tcrsid);
249: }
250:
251: sub printtokenheader {
252: my ($target,$token,$tsymb,$tcrsid,$tudom,$tuname)=@_;
253: unless ($token) { return ''; }
254:
255: my ($symb,$courseid,$domain,$name) = &Apache::lonnet::whichuser();
256: unless ($tsymb) {
257: $tsymb=$symb;
258: }
259: unless ($tuname) {
260: $tuname=$name;
261: $tudom=$domain;
262: $tcrsid=$courseid;
263: }
264:
265: my $plainname=&Apache::loncommon::plainname($tuname,$tudom);
266:
267: if ($target eq 'web') {
268: my %idhash=&Apache::lonnet::idrget($tudom,($tuname));
269: return
270: '<img align="right" src="/cgi-bin/barcode.png?encode='.$token.'" />'.
271: &mt('Checked out for').' '.$plainname.
272: '<br />'.&mt('User').': '.$tuname.' at '.$tudom.
273: '<br />'.&mt('ID').': '.$idhash{$tuname}.
274: '<br />'.&mt('CourseID').': '.$tcrsid.
275: '<br />'.&mt('Course').': '.$env{'course.'.$tcrsid.'.description'}.
276: '<br />'.&mt('DocID').': '.$token.
277: '<br />'.&mt('Time').': '.&Apache::lonlocal::locallocaltime().'<hr />';
278: } else {
279: return $token;
280: }
281: }
282:
283: sub printalltags {
284: my $temp;
285: foreach $temp (sort keys %Apache::lonxml::alltags) {
286: &Apache::lonxml::debug("$temp -- ".
287: join(',',@{ $Apache::lonxml::alltags{$temp} }));
288: }
289: }
290:
291: sub xmlparse {
292: my ($request,$target,$content_file_string,$safeinit,%style_for_target) = @_;
293:
294: &setup_globals($request,$target);
295: &Apache::inputtags::initialize_inputtags();
296: &Apache::bridgetask::initialize_bridgetask();
297: &Apache::outputtags::initialize_outputtags();
298: &Apache::edit::initialize_edit();
299: &Apache::londefdef::initialize_londefdef();
300:
301: #
302: # do we have a course style file?
303: #
304:
305: if ($env{'request.course.id'} && $env{'request.state'} ne 'construct') {
306: my $bodytext=
307: $env{'course.'.$env{'request.course.id'}.'.default_xml_style'};
308: if ($bodytext) {
309: foreach my $file (split(',',$bodytext)) {
310: my $location=&Apache::lonnet::filelocation('',$file);
311: my $styletext=&Apache::lonnet::getfile($location);
312: if ($styletext ne '-1') {
313: %style_for_target = (%style_for_target,
314: &Apache::style::styleparser($target,$styletext));
315: }
316: }
317: }
318: } elsif ($env{'construct.style'} && ($env{'request.state'} eq 'construct')) {
319: my $location=&Apache::lonnet::filelocation('',$env{'construct.style'});
320: my $styletext=&Apache::lonnet::getfile($location);
321: if ($styletext ne '-1') {
322: %style_for_target = (%style_for_target,
323: &Apache::style::styleparser($target,$styletext));
324: }
325: }
326: #&printalltags();
327: my @pars = ();
328: my $pwd=$env{'request.filename'};
329: $pwd =~ s:/[^/]*$::;
330: &newparser(\@pars,\$content_file_string,$pwd);
331:
332: my $safeeval = new Safe;
333: my $safehole = new Safe::Hole;
334: &init_safespace($target,$safeeval,$safehole,$safeinit);
335: #-------------------- Redefinition of the target in the case of compound target
336:
337: ($target, my @tenta) = split('&&',$target);
338:
339: my @stack = ();
340: my @parstack = ();
341: &initdepth();
342: &init_alarm();
343: my $finaloutput = &inner_xmlparse($target,\@stack,\@parstack,\@pars,
344: $safeeval,\%style_for_target,1);
345:
346: if (@stack) {
347: &warning("At end of file some tags were still left unclosed, ".
348: '<tt><'.join('></tt>, <tt><',reverse(@stack)).
349: '></tt>');
350: }
351: if ($env{'request.uri'}) {
352: &writeallows($env{'request.uri'});
353: }
354: &do_registered_ssi();
355: if ($Apache::lonxml::counter_changed) { &store_counter() }
356:
357: &clean_safespace($safeeval);
358:
359: if ($env{'form.return_only_error_and_warning_counts'}) {
360: return "$errorcount:$warningcount";
361: }
362: return $finaloutput;
363: }
364:
365: sub latex_special_symbols {
366: my ($string,$where)=@_;
367: #
368: # If e.g. in verbatim mode, then don't substitute.
369: # but return original string.
370: #
371: if (!($Apache::lonxml::substitute_LaTeX_symbols)) {
372: return $string;
373: }
374: if ($where eq 'header') {
375: $string =~ s/\\/\$\\backslash\$/g; # \ -> $\backslash$ per LaTex line by line pg 10.
376: $string =~ s/(\$|%|\{|\})/\\$1/g;
377: $string=&Apache::lonprintout::character_chart($string);
378: # any & or # leftover should be safe to just escape
379: $string=~s/([^\\])\&/$1\\\&/g;
380: $string=~s/([^\\])\#/$1\\\#/g;
381: $string =~ s/_/\\_/g; # _ -> \_
382: $string =~ s/\^/\\\^{}/g; # ^ -> \^{}
383: } else {
384: $string=~s/\\/\\ensuremath{\\backslash}/g;
385: $string=~s/\\\%|\%/\\\%/g;
386: $string=~s/\\{|{/\\{/g;
387: $string=~s/\\}|}/\\}/g;
388: $string=~s/\\ensuremath\\{\\backslash\\}/\\ensuremath{\\backslash}/g;
389: $string=~s/\\\$|\$/\\\$/g;
390: $string=~s/\\\_|\_/\\\_/g;
391: $string=~s/([^\\]|^)(\~|\^)/$1\\$2\\strut /g;
392: $string=~s/(>|<)/\\ensuremath\{$1\}/g; #more or less
393: $string=&Apache::lonprintout::character_chart($string);
394: # any & or # leftover should be safe to just escape
395: $string=~s/\\\&|\&/\\\&/g;
396: $string=~s/\\\#|\#/\\\#/g;
397: $string=~s/\|/\$\\mid\$/g;
398: #single { or } How to escape?
399: }
400: return $string;
401: }
402:
403: sub inner_xmlparse {
404: my ($target,$stack,$parstack,$pars,$safeeval,$style_for_target,$start)=@_;
405: my $finaloutput = '';
406: my $result;
407: my $token;
408: my $dontpop=0;
409: my $startredirection = $Apache::lonxml::redirection;
410: while ( $#$pars > -1 ) {
411: while ($token = $$pars['-1']->get_token) {
412: if (($token->[0] eq 'T') || ($token->[0] eq 'C') ) {
413: if ($metamode<1) {
414: my $text=$token->[1];
415: if ($token->[0] eq 'C' && $target eq 'tex') {
416: $text = '';
417: # $text = '%'.$text."\n";
418: }
419: $result.=$text;
420: }
421: } elsif (($token->[0] eq 'D')) {
422: if ($metamode<1 && $target eq 'web') {
423: my $text=$token->[1];
424: $result.=$text;
425: }
426: } elsif ($token->[0] eq 'PI') {
427: if ($metamode<1 && $target eq 'web') {
428: $result=$token->[2];
429: }
430: } elsif ($token->[0] eq 'S') {
431: # add tag to stack
432: push (@$stack,$token->[1]);
433: # add parameters list to another stack
434: push (@$parstack,&parstring($token));
435: &increasedepth($token);
436: if ($Apache::lonxml::usestyle &&
437: exists($$style_for_target{$token->[1]})) {
438: $Apache::lonxml::usestyle=0;
439: my $string=$$style_for_target{$token->[1]}.
440: '<LONCAPA_INTERNAL_TURN_STYLE_ON />';
441: &Apache::lonxml::newparser($pars,\$string);
442: $Apache::lonxml::style_values=$$parstack[-1];
443: $Apache::lonxml::style_end_values=$$parstack[-1];
444: } else {
445: $result = &callsub("start_$token->[1]", $target, $token, $stack,
446: $parstack, $pars, $safeeval, $style_for_target);
447: }
448: } elsif ($token->[0] eq 'E') {
449: if ($Apache::lonxml::usestyle &&
450: exists($$style_for_target{'/'."$token->[1]"})) {
451: $Apache::lonxml::usestyle=0;
452: my $string=$$style_for_target{'/'.$token->[1]}.
453: '<LONCAPA_INTERNAL_TURN_STYLE_ON end="'.$token->[1].'" />';
454: &Apache::lonxml::newparser($pars,\$string);
455: $Apache::lonxml::style_values=$Apache::lonxml::style_end_values;
456: $Apache::lonxml::style_end_values='';
457: $dontpop=1;
458: } else {
459: #clear out any tags that didn't end
460: while ($token->[1] ne $$stack['-1'] && ($#$stack > -1)) {
461: my $lasttag=$$stack[-1];
462: if ($token->[1] =~ /^\Q$lasttag\E$/i) {
463: &Apache::lonxml::warning('Using tag </'.$token->[1].'> on line '.$token->[3].' as end tag to <'.$$stack[-1].'>');
464: last;
465: } else {
466: &Apache::lonxml::warning('Found tag </'.$token->[1].'> on line '.$token->[3].' when looking for </'.$$stack[-1].'> in file');
467: &end_tag($stack,$parstack,$token);
468: }
469: }
470: $result = &callsub("end_$token->[1]", $target, $token, $stack,
471: $parstack, $pars,$safeeval, $style_for_target);
472: }
473: } else {
474: &Apache::lonxml::error("Unknown token event :$token->[0]:$token->[1]:");
475: }
476: #evaluate variable refs in result
477: if ($Apache::lonxml::post_evaluate &&$result ne "") {
478: my $extras;
479: if (!$Apache::lonxml::usestyle) {
480: $extras=$Apache::lonxml::style_values;
481: }
482: if ( $#$parstack > -1 ) {
483: $result=&Apache::run::evaluate($result,$safeeval,$extras.$$parstack[-1]);
484: } else {
485: $result= &Apache::run::evaluate($result,$safeeval,$extras);
486: }
487: }
488: $Apache::lonxml::post_evaluate=1;
489:
490: if (($token->[0] eq 'T') || ($token->[0] eq 'C') || ($token->[0] eq 'D') ) {
491: #Style file definitions should be correct
492: if ($target eq 'tex' && ($Apache::lonxml::usestyle)) {
493: $result=&latex_special_symbols($result);
494: }
495: }
496:
497: if ($Apache::lonxml::redirection) {
498: $Apache::lonxml::outputstack['-1'] .= $result;
499: } else {
500: $finaloutput.=$result;
501: }
502: $result = '';
503:
504: if ($token->[0] eq 'E' && !$dontpop) {
505: &end_tag($stack,$parstack,$token);
506: }
507: $dontpop=0;
508: }
509: if ($#$pars > -1) {
510: pop @$pars;
511: pop @Apache::lonxml::pwd;
512: }
513: }
514:
515: # if ($target eq 'meta') {
516: # $finaloutput.=&endredirection;
517: # }
518:
519: if ( $start && $target eq 'grade') { &endredirection(); }
520: if ( $Apache::lonxml::redirection > $startredirection) {
521: while ($Apache::lonxml::redirection > $startredirection) {
522: $finaloutput .= &endredirection();
523: }
524: }
525: if (($ENV{'QUERY_STRING'}) && ($target eq 'web')) {
526: $finaloutput=&afterburn($finaloutput);
527: }
528: return $finaloutput;
529: }
530:
531: ##
532: ## Looks to see if there is a subroutine defined for this tag. If so, call it,
533: ## otherwise do not call it as we do not know what it is.
534: ##
535: sub callsub {
536: my ($sub,$target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
537: my $currentstring='';
538: my $nodefault;
539: {
540: my $sub1;
541: no strict 'refs';
542: my $tag=$token->[1];
543: # get utterly rid of extended html tags
544: if ($tag=~/^x\-/i) { return ''; }
545: my $space=$Apache::lonxml::alltags{$tag}[-1];
546: if (!$space) {
547: $tag=~tr/A-Z/a-z/;
548: $sub=~tr/A-Z/a-z/;
549: $space=$Apache::lonxml::alltags{$tag}[-1]
550: }
551:
552: my $deleted=0;
553: if (($token->[0] eq 'S') && ($target eq 'modified')) {
554: $deleted=&Apache::edit::handle_delete($space,$target,$token,$tagstack,
555: $parstack,$parser,$safeeval,
556: $style);
557: }
558: if (!$deleted) {
559: if ($space) {
560: #&Apache::lonxml::debug("Calling sub $sub in $space $metamode");
561: $sub1="$space\:\:$sub";
562: ($currentstring,$nodefault) = &$sub1($target,$token,$tagstack,
563: $parstack,$parser,$safeeval,
564: $style);
565: } else {
566: if ($target eq 'tex') {
567: # throw away tag name
568: return '';
569: }
570: #&Apache::lonxml::debug("NOT Calling sub $sub in $space $metamode");
571: if ($metamode <1) {
572: if (defined($token->[4]) && ($metamode < 1)) {
573: $currentstring = $token->[4];
574: } else {
575: $currentstring = $token->[2];
576: }
577: }
578: }
579: # &Apache::lonxml::debug("nodefalt:$nodefault:");
580: if ($currentstring eq '' && $nodefault eq '') {
581: if ($target eq 'edit') {
582: #&Apache::lonxml::debug("doing default edit for $token->[1]");
583: if ($token->[0] eq 'S') {
584: $currentstring = &Apache::edit::tag_start($target,$token);
585: } elsif ($token->[0] eq 'E') {
586: $currentstring = &Apache::edit::tag_end($target,$token);
587: }
588: } elsif ($target eq 'modified') {
589: if ($token->[0] eq 'S') {
590: $currentstring = $token->[4];
591: $currentstring.=&Apache::edit::handle_insert();
592: } elsif ($token->[0] eq 'E') {
593: $currentstring = $token->[2];
594: $currentstring.=&Apache::edit::handle_insertafter($token->[1]);
595: } else {
596: $currentstring = $token->[2];
597: }
598: }
599: }
600: }
601: use strict 'refs';
602: }
603: return $currentstring;
604: }
605:
606: sub setup_globals {
607: my ($request,$target)=@_;
608: $Apache::lonxml::request=$request;
609: $errorcount=0;
610: $warningcount=0;
611: $Apache::lonxml::default_homework_loaded=0;
612: $Apache::lonxml::usestyle=1;
613: &init_counter();
614: @Apache::lonxml::pwd=();
615: @Apache::lonxml::extlinks=();
616: @Apache::lonxml::ssi_info=();
617: $Apache::lonxml::post_evaluate=1;
618: $Apache::lonxml::warnings_error_header='';
619: $Apache::lonxml::substitute_LaTeX_symbols = 1;
620: if ($target eq 'meta') {
621: $Apache::lonxml::redirection = 0;
622: $Apache::lonxml::metamode = 1;
623: $Apache::lonxml::evaluate = 1;
624: $Apache::lonxml::import = 0;
625: } elsif ($target eq 'answer') {
626: $Apache::lonxml::redirection = 0;
627: $Apache::lonxml::metamode = 1;
628: $Apache::lonxml::evaluate = 1;
629: $Apache::lonxml::import = 1;
630: } elsif ($target eq 'grade') {
631: &startredirection(); #ended in inner_xmlparse on exit
632: $Apache::lonxml::metamode = 0;
633: $Apache::lonxml::evaluate = 1;
634: $Apache::lonxml::import = 1;
635: } elsif ($target eq 'modified') {
636: $Apache::lonxml::redirection = 0;
637: $Apache::lonxml::metamode = 0;
638: $Apache::lonxml::evaluate = 0;
639: $Apache::lonxml::import = 0;
640: } elsif ($target eq 'edit') {
641: $Apache::lonxml::redirection = 0;
642: $Apache::lonxml::metamode = 0;
643: $Apache::lonxml::evaluate = 0;
644: $Apache::lonxml::import = 0;
645: } elsif ($target eq 'analyze') {
646: $Apache::lonxml::redirection = 0;
647: $Apache::lonxml::metamode = 0;
648: $Apache::lonxml::evaluate = 1;
649: $Apache::lonxml::import = 1;
650: } else {
651: $Apache::lonxml::redirection = 0;
652: $Apache::lonxml::metamode = 0;
653: $Apache::lonxml::evaluate = 1;
654: $Apache::lonxml::import = 1;
655: }
656: }
657:
658: sub init_safespace {
659: my ($target,$safeeval,$safehole,$safeinit) = @_;
660: $safeeval->deny_only(':dangerous');
661: $safeeval->reval('use Math::Complex;');
662: $safeeval->permit_only(":default");
663: $safeeval->permit("entereval");
664: $safeeval->permit(":base_math");
665: $safeeval->permit("sort");
666: $safeeval->permit("time");
667: $safeeval->deny("rand");
668: $safeeval->deny("srand");
669: $safeeval->deny(":base_io");
670: $safehole->wrap(\&Apache::scripttag::xmlparse,$safeeval,'&xmlparse');
671: $safehole->wrap(\&Apache::outputtags::multipart,$safeeval,'&multipart');
672: $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
673: $safehole->wrap(\&Apache::chemresponse::chem_standard_order,$safeeval,
674: '&chem_standard_order');
675: $safehole->wrap(\&Apache::response::check_status,$safeeval,'&check_status');
676:
677: $safehole->wrap(\&Apache::lonmaxima::maxima_eval,$safeeval,'&maxima_eval');
678: $safehole->wrap(\&Apache::lonmaxima::maxima_check,$safeeval,'&maxima_check');
679: $safehole->wrap(\&Apache::lonmaxima::maxima_cas_formula_fix,$safeeval,
680: '&maxima_cas_formula_fix');
681:
682: $safehole->wrap(\&Apache::caparesponse::capa_formula_fix,$safeeval,
683: '&capa_formula_fix');
684:
685: $safehole->wrap(\&Math::Cephes::asin,$safeeval,'&asin');
686: $safehole->wrap(\&Math::Cephes::acos,$safeeval,'&acos');
687: $safehole->wrap(\&Math::Cephes::atan,$safeeval,'&atan');
688: $safehole->wrap(\&Math::Cephes::sinh,$safeeval,'&sinh');
689: $safehole->wrap(\&Math::Cephes::cosh,$safeeval,'&cosh');
690: $safehole->wrap(\&Math::Cephes::tanh,$safeeval,'&tanh');
691: $safehole->wrap(\&Math::Cephes::asinh,$safeeval,'&asinh');
692: $safehole->wrap(\&Math::Cephes::acosh,$safeeval,'&acosh');
693: $safehole->wrap(\&Math::Cephes::atanh,$safeeval,'&atanh');
694: $safehole->wrap(\&Math::Cephes::erf,$safeeval,'&erf');
695: $safehole->wrap(\&Math::Cephes::erfc,$safeeval,'&erfc');
696: $safehole->wrap(\&Math::Cephes::j0,$safeeval,'&j0');
697: $safehole->wrap(\&Math::Cephes::j1,$safeeval,'&j1');
698: $safehole->wrap(\&Math::Cephes::jn,$safeeval,'&jn');
699: $safehole->wrap(\&Math::Cephes::jv,$safeeval,'&jv');
700: $safehole->wrap(\&Math::Cephes::y0,$safeeval,'&y0');
701: $safehole->wrap(\&Math::Cephes::y1,$safeeval,'&y1');
702: $safehole->wrap(\&Math::Cephes::yn,$safeeval,'&yn');
703: $safehole->wrap(\&Math::Cephes::yv,$safeeval,'&yv');
704:
705: $safehole->wrap(\&Math::Cephes::bdtr ,$safeeval,'&bdtr' );
706: $safehole->wrap(\&Math::Cephes::bdtrc ,$safeeval,'&bdtrc' );
707: $safehole->wrap(\&Math::Cephes::bdtri ,$safeeval,'&bdtri' );
708: $safehole->wrap(\&Math::Cephes::btdtr ,$safeeval,'&btdtr' );
709: $safehole->wrap(\&Math::Cephes::chdtr ,$safeeval,'&chdtr' );
710: $safehole->wrap(\&Math::Cephes::chdtrc,$safeeval,'&chdtrc');
711: $safehole->wrap(\&Math::Cephes::chdtri,$safeeval,'&chdtri');
712: $safehole->wrap(\&Math::Cephes::fdtr ,$safeeval,'&fdtr' );
713: $safehole->wrap(\&Math::Cephes::fdtrc ,$safeeval,'&fdtrc' );
714: $safehole->wrap(\&Math::Cephes::fdtri ,$safeeval,'&fdtri' );
715: $safehole->wrap(\&Math::Cephes::gdtr ,$safeeval,'&gdtr' );
716: $safehole->wrap(\&Math::Cephes::gdtrc ,$safeeval,'&gdtrc' );
717: $safehole->wrap(\&Math::Cephes::nbdtr ,$safeeval,'&nbdtr' );
718: $safehole->wrap(\&Math::Cephes::nbdtrc,$safeeval,'&nbdtrc');
719: $safehole->wrap(\&Math::Cephes::nbdtri,$safeeval,'&nbdtri');
720: $safehole->wrap(\&Math::Cephes::ndtr ,$safeeval,'&ndtr' );
721: $safehole->wrap(\&Math::Cephes::ndtri ,$safeeval,'&ndtri' );
722: $safehole->wrap(\&Math::Cephes::pdtr ,$safeeval,'&pdtr' );
723: $safehole->wrap(\&Math::Cephes::pdtrc ,$safeeval,'&pdtrc' );
724: $safehole->wrap(\&Math::Cephes::pdtri ,$safeeval,'&pdtri' );
725: $safehole->wrap(\&Math::Cephes::stdtr ,$safeeval,'&stdtr' );
726: $safehole->wrap(\&Math::Cephes::stdtri,$safeeval,'&stdtri');
727:
728: $safehole->wrap(\&Math::Cephes::Matrix::mat,$safeeval,'&mat');
729: $safehole->wrap(\&Math::Cephes::Matrix::new,$safeeval,
730: '&Math::Cephes::Matrix::new');
731: $safehole->wrap(\&Math::Cephes::Matrix::coef,$safeeval,
732: '&Math::Cephes::Matrix::coef');
733: $safehole->wrap(\&Math::Cephes::Matrix::clr,$safeeval,
734: '&Math::Cephes::Matrix::clr');
735: $safehole->wrap(\&Math::Cephes::Matrix::add,$safeeval,
736: '&Math::Cephes::Matrix::add');
737: $safehole->wrap(\&Math::Cephes::Matrix::sub,$safeeval,
738: '&Math::Cephes::Matrix::sub');
739: $safehole->wrap(\&Math::Cephes::Matrix::mul,$safeeval,
740: '&Math::Cephes::Matrix::mul');
741: $safehole->wrap(\&Math::Cephes::Matrix::div,$safeeval,
742: '&Math::Cephes::Matrix::div');
743: $safehole->wrap(\&Math::Cephes::Matrix::inv,$safeeval,
744: '&Math::Cephes::Matrix::inv');
745: $safehole->wrap(\&Math::Cephes::Matrix::transp,$safeeval,
746: '&Math::Cephes::Matrix::transp');
747: $safehole->wrap(\&Math::Cephes::Matrix::simq,$safeeval,
748: '&Math::Cephes::Matrix::simq');
749: $safehole->wrap(\&Math::Cephes::Matrix::mat_to_vec,$safeeval,
750: '&Math::Cephes::Matrix::mat_to_vec');
751: $safehole->wrap(\&Math::Cephes::Matrix::vec_to_mat,$safeeval,
752: '&Math::Cephes::Matrix::vec_to_mat');
753: $safehole->wrap(\&Math::Cephes::Matrix::check,$safeeval,
754: '&Math::Cephes::Matrix::check');
755: $safehole->wrap(\&Math::Cephes::Matrix::check,$safeeval,
756: '&Math::Cephes::Matrix::check');
757:
758: # $safehole->wrap(\&Math::Cephes::new_fract,$safeeval,'&new_fract');
759: # $safehole->wrap(\&Math::Cephes::radd,$safeeval,'&radd');
760: # $safehole->wrap(\&Math::Cephes::rsub,$safeeval,'&rsub');
761: # $safehole->wrap(\&Math::Cephes::rmul,$safeeval,'&rmul');
762: # $safehole->wrap(\&Math::Cephes::rdiv,$safeeval,'&rdiv');
763: # $safehole->wrap(\&Math::Cephes::euclid,$safeeval,'&euclid');
764:
765: $safehole->wrap(\&Math::Random::random_beta,$safeeval,'&math_random_beta');
766: $safehole->wrap(\&Math::Random::random_chi_square,$safeeval,'&math_random_chi_square');
767: $safehole->wrap(\&Math::Random::random_exponential,$safeeval,'&math_random_exponential');
768: $safehole->wrap(\&Math::Random::random_f,$safeeval,'&math_random_f');
769: $safehole->wrap(\&Math::Random::random_gamma,$safeeval,'&math_random_gamma');
770: $safehole->wrap(\&Math::Random::random_multivariate_normal,$safeeval,'&math_random_multivariate_normal');
771: $safehole->wrap(\&Math::Random::random_multinomial,$safeeval,'&math_random_multinomial');
772: $safehole->wrap(\&Math::Random::random_noncentral_chi_square,$safeeval,'&math_random_noncentral_chi_square');
773: $safehole->wrap(\&Math::Random::random_noncentral_f,$safeeval,'&math_random_noncentral_f');
774: $safehole->wrap(\&Math::Random::random_normal,$safeeval,'&math_random_normal');
775: $safehole->wrap(\&Math::Random::random_permutation,$safeeval,'&math_random_permutation');
776: $safehole->wrap(\&Math::Random::random_permuted_index,$safeeval,'&math_random_permuted_index');
777: $safehole->wrap(\&Math::Random::random_uniform,$safeeval,'&math_random_uniform');
778: $safehole->wrap(\&Math::Random::random_poisson,$safeeval,'&math_random_poisson');
779: $safehole->wrap(\&Math::Random::random_uniform_integer,$safeeval,'&math_random_uniform_integer');
780: $safehole->wrap(\&Math::Random::random_negative_binomial,$safeeval,'&math_random_negative_binomial');
781: $safehole->wrap(\&Math::Random::random_binomial,$safeeval,'&math_random_binomial');
782: $safehole->wrap(\&Math::Random::random_seed_from_phrase,$safeeval,'&random_seed_from_phrase');
783: $safehole->wrap(\&Math::Random::random_set_seed_from_phrase,$safeeval,'&random_set_seed_from_phrase');
784: $safehole->wrap(\&Math::Random::random_get_seed,$safeeval,'&random_get_seed');
785: $safehole->wrap(\&Math::Random::random_set_seed,$safeeval,'&random_set_seed');
786: $safehole->wrap(\&Apache::lonxml::error,$safeeval,'&LONCAPA_INTERNAL_ERROR');
787: $safehole->wrap(\&Apache::lonxml::debug,$safeeval,'&LONCAPA_INTERNAL_DEBUG');
788: $safehole->wrap(\&Apache::lonnet::logthis,$safeeval,'&LONCAPA_INTERNAL_LOGTHIS');
789: $safehole->wrap(\&Apache::inputtags::finalizeawards,$safeeval,'&LONCAPA_INTERNAL_FINALIZEAWARDS');
790: $safehole->wrap(\&Apache::caparesponse::get_sigrange,$safeeval,'&LONCAPA_INTERNAL_get_sigrange');
791: # use Data::Dumper;
792: # $safehole->wrap(\&Data::Dumper::Dumper,$safeeval,'&LONCAPA_INTERNAL_Dumper');
793: #need to inspect this class of ops
794: # $safeeval->deny(":base_orig");
795: $safeeval->permit("require");
796: $safeinit .= ';$external::target="'.$target.'";';
797: &Apache::run::run($safeinit,$safeeval);
798: &initialize_rndseed($safeeval);
799: }
800:
801: sub clean_safespace {
802: my ($safeeval) = @_;
803: delete_package_recurse($safeeval->{Root});
804: }
805:
806: sub delete_package_recurse {
807: my ($package) = @_;
808: my @subp;
809: {
810: no strict 'refs';
811: while (my ($key,$val) = each(%{*{"$package\::"}})) {
812: if (!defined($val)) { next; }
813: local (*ENTRY) = $val;
814: if (defined *ENTRY{HASH} && $key =~ /::$/ &&
815: $key ne "main::" && $key ne "<none>::")
816: {
817: my ($p) = $package ne "main" ? "$package\::" : "";
818: ($p .= $key) =~ s/::$//;
819: push(@subp,$p);
820: }
821: }
822: }
823: foreach my $p (@subp) {
824: delete_package_recurse($p);
825: }
826: Symbol::delete_package($package);
827: }
828:
829: sub initialize_rndseed {
830: my ($safeeval)=@_;
831: my $rndseed;
832: my ($symb,$courseid,$domain,$name) = &Apache::lonnet::whichuser();
833: $rndseed=&Apache::lonnet::rndseed($symb,$courseid,$domain,$name);
834: my $safeinit = '$external::randomseed="'.$rndseed.'";';
835: &Apache::lonxml::debug("Setting rndseed to $rndseed");
836: &Apache::run::run($safeinit,$safeeval);
837: }
838:
839: sub default_homework_load {
840: my ($safeeval)=@_;
841: &Apache::lonxml::debug('Loading default_homework');
842: my $default=&Apache::lonnet::getfile('/home/httpd/html/res/adm/includes/default_homework.lcpm');
843: if ($default eq -1) {
844: &Apache::lonxml::error("<b>Unable to find <i>default_homework.lcpm</i></b>");
845: } else {
846: &Apache::run::run($default,$safeeval);
847: $Apache::lonxml::default_homework_loaded=1;
848: }
849: }
850:
851: {
852: my $alarm_depth;
853: sub init_alarm {
854: alarm(0);
855: $alarm_depth=0;
856: }
857:
858: sub start_alarm {
859: if ($alarm_depth<1) {
860: my $old=alarm($Apache::lonnet::perlvar{'lonScriptTimeout'});
861: if ($old) {
862: &Apache::lonxml::error("Cancelled an alarm of $old, this shouldn't occur.");
863: }
864: }
865: $alarm_depth++;
866: }
867:
868: sub end_alarm {
869: $alarm_depth--;
870: if ($alarm_depth<1) { alarm(0); }
871: }
872: }
873: my $metamode_was;
874: sub startredirection {
875: if (!$Apache::lonxml::redirection) {
876: $metamode_was=$Apache::lonxml::metamode;
877: }
878: $Apache::lonxml::metamode=0;
879: $Apache::lonxml::redirection++;
880: push (@Apache::lonxml::outputstack, '');
881: }
882:
883: sub endredirection {
884: if (!$Apache::lonxml::redirection) {
885: &Apache::lonxml::error("Endredirection was called before a startredirection, perhaps you have unbalanced tags. Some debugging information:".join ":",caller);
886: return '';
887: }
888: $Apache::lonxml::redirection--;
889: if (!$Apache::lonxml::redirection) {
890: $Apache::lonxml::metamode=$metamode_was;
891: }
892: pop @Apache::lonxml::outputstack;
893: }
894:
895: sub end_tag {
896: my ($tagstack,$parstack,$token)=@_;
897: pop(@$tagstack);
898: pop(@$parstack);
899: &decreasedepth($token);
900: }
901:
902: sub initdepth {
903: @Apache::lonxml::depthcounter=();
904: undef($Apache::lonxml::last_depth_count);
905: }
906:
907:
908: my @timers;
909: my $lasttime;
910: # @Apache::lonxml::depthcounter -> count of tags that exist so
911: # far at each level
912: # $Apache::lonxml::last_depth_count -> when ascending, need to
913: # remember the count for the level below the current level (for
914: # example going from 1_2 -> 1 -> 1_3 need to remember the 2 )
915:
916: sub increasedepth {
917: my ($token) = @_;
918: push(@Apache::lonxml::depthcounter,$Apache::lonxml::last_depth_count+1);
919: undef($Apache::lonxml::last_depth_count);
920: my $time;
921: if ($Apache::lonxml::debug eq "1") {
922: push(@timers,[&gettimeofday()]);
923: $time=&tv_interval($lasttime);
924: $lasttime=[&gettimeofday()];
925: }
926: my $spacing=' 'x($#Apache::lonxml::depthcounter);
927: $Apache::lonxml::curdepth=join('_',@Apache::lonxml::depthcounter);
928: # &Apache::lonxml::debug("s$spacing$Apache::lonxml::depth : $Apache::lonxml::olddepth : $Apache::lonxml::curdepth : $token->[1] : $time");
929: #print "<br />s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n";
930: }
931:
932: sub decreasedepth {
933: my ($token) = @_;
934: if ( $#Apache::lonxml::depthcounter == -1) {
935: &Apache::lonxml::warning(&mt("Missing tags, unable to properly run file."));
936: }
937: $Apache::lonxml::last_depth_count = pop(@Apache::lonxml::depthcounter);
938:
939: my ($timer,$time);
940: if ($Apache::lonxml::debug eq "1") {
941: $timer=pop(@timers);
942: $time=&tv_interval($lasttime);
943: $lasttime=[&gettimeofday()];
944: }
945: my $spacing=' 'x($#Apache::lonxml::depthcounter);
946: $Apache::lonxml::curdepth = join('_',@Apache::lonxml::depthcounter);
947: # &Apache::lonxml::debug("e$spacing$Apache::lonxml::depth : $Apache::lonxml::olddepth : $Apache::lonxml::curdepth : $token->[1] : $time : ".&tv_interval($timer));
948: #print "<br />e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n";
949: }
950:
951: sub get_id {
952: my ($parstack,$safeeval)=@_;
953: my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
954: if ($env{'request.state'} eq 'construct' && $id =~ /(\.|_)/) {
955: &error(&mt("IDs are not allowed to contain "<tt>_</tt>" or "<tt>.</tt>""));
956: }
957: if ($id =~ /^\s*$/) { $id = $Apache::lonxml::curdepth; }
958: return $id;
959: }
960:
961: sub get_all_text_unbalanced {
962: #there is a copy of this in lonpublisher.pm
963: my($tag,$pars)= @_;
964: my $token;
965: my $result='';
966: $tag='<'.$tag.'>';
967: while ($token = $$pars[-1]->get_token) {
968: if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
969: if ($token->[0] eq 'T' && $token->[2]) {
970: $result.='<![CDATA['.$token->[1].']]>';
971: } else {
972: $result.=$token->[1];
973: }
974: } elsif ($token->[0] eq 'PI') {
975: $result.=$token->[2];
976: } elsif ($token->[0] eq 'S') {
977: $result.=$token->[4];
978: } elsif ($token->[0] eq 'E') {
979: $result.=$token->[2];
980: }
981: if ($result =~ /\Q$tag\E/is) {
982: ($result,my $redo)=$result =~ /(.*)\Q$tag\E(.*)/is;
983: #&Apache::lonxml::debug('Got a winner with leftovers ::'.$2);
984: #&Apache::lonxml::debug('Result is :'.$1);
985: $redo=$tag.$redo;
986: &Apache::lonxml::newparser($pars,\$redo);
987: last;
988: }
989: }
990: return $result
991: }
992:
993: sub increment_counter {
994: my ($increment) = @_;
995: if (defined($increment) && $increment gt 0) {
996: $Apache::lonxml::counter+=$increment;
997: } else {
998: $Apache::lonxml::counter++;
999: }
1000: $Apache::lonxml::counter_changed=1;
1001: }
1002:
1003: sub init_counter {
1004: if ($env{'request.state'} eq 'construct') {
1005: $Apache::lonxml::counter=1;
1006: $Apache::lonxml::counter_changed=1;
1007: } elsif (defined($env{'form.counter'})) {
1008: $Apache::lonxml::counter=$env{'form.counter'};
1009: $Apache::lonxml::counter_changed=0;
1010: } else {
1011: $Apache::lonxml::counter=1;
1012: $Apache::lonxml::counter_changed=1;
1013: }
1014: }
1015:
1016: sub store_counter {
1017: &Apache::lonnet::appenv(('form.counter' => $Apache::lonxml::counter));
1018: $Apache::lonxml::counter_changed=0;
1019: return '';
1020: }
1021:
1022: {
1023: my $state;
1024: sub clear_problem_counter {
1025: undef($state);
1026: &Apache::lonnet::delenv('form.counter');
1027: &Apache::lonxml::init_counter();
1028: &Apache::lonxml::store_counter();
1029: }
1030:
1031: sub remember_problem_counter {
1032: &Apache::lonnet::transfer_profile_to_env(undef,undef,1);
1033: $state = $env{'form.counter'};
1034: }
1035:
1036: sub restore_problem_counter {
1037: if (defined($state)) {
1038: &Apache::lonnet::appenv(('form.counter' => $state));
1039: }
1040: }
1041: sub get_problem_counter {
1042: if ($Apache::lonxml::counter_changed) { &store_counter() }
1043: &Apache::lonnet::transfer_profile_to_env(undef,undef,1);
1044: return $env{'form.counter'};
1045: }
1046: }
1047:
1048: sub get_all_text {
1049: my($tag,$pars,$style)= @_;
1050: my $gotfullstack=1;
1051: if (ref($pars) ne 'ARRAY') {
1052: $gotfullstack=0;
1053: $pars=[$pars];
1054: }
1055: if (ref($style) ne 'HASH') {
1056: $style={};
1057: }
1058: my $depth=0;
1059: my $token;
1060: my $result='';
1061: if ( $tag =~ m:^/: ) {
1062: my $tag=substr($tag,1);
1063: #&Apache::lonxml::debug("have:$tag:");
1064: my $top_empty=0;
1065: while (($depth >=0) && ($#$pars > -1) && (!$top_empty)) {
1066: while (($depth >=0) && ($token = $$pars[-1]->get_token)) {
1067: #&Apache::lonxml::debug("e token:$token->[0]:$depth:$token->[1]:".$#$pars.":".$#Apache::lonxml::pwd);
1068: if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
1069: if ($token->[2]) {
1070: $result.='<![CDATA['.$token->[1].']]>';
1071: } else {
1072: $result.=$token->[1];
1073: }
1074: } elsif ($token->[0] eq 'PI') {
1075: $result.=$token->[2];
1076: } elsif ($token->[0] eq 'S') {
1077: if ($token->[1] =~ /^\Q$tag\E$/i) { $depth++; }
1078: if ($token->[1] =~ /^LONCAPA_INTERNAL_TURN_STYLE_ON$/) { $Apache::lonxml::usestyle=1; }
1079: if ($token->[1] =~ /^LONCAPA_INTERNAL_TURN_STYLE_OFF$/) { $Apache::lonxml::usestyle=0; }
1080: $result.=$token->[4];
1081: } elsif ($token->[0] eq 'E') {
1082: if ( $token->[1] =~ /^\Q$tag\E$/i) { $depth--; }
1083: #skip sending back the last end tag
1084: if ($depth == 0 && exists($$style{'/'.$token->[1]}) && $Apache::lonxml::usestyle) {
1085: my $string=
1086: '<LONCAPA_INTERNAL_TURN_STYLE_OFF end="yes" />'.
1087: $$style{'/'.$token->[1]}.
1088: $token->[2].
1089: '<LONCAPA_INTERNAL_TURN_STYLE_ON />';
1090: &Apache::lonxml::newparser($pars,\$string);
1091: #&Apache::lonxml::debug("reParsing $string");
1092: next;
1093: }
1094: if ($depth > -1) {
1095: $result.=$token->[2];
1096: } else {
1097: $$pars[-1]->unget_token($token);
1098: }
1099: }
1100: }
1101: if (($depth >=0) && ($#$pars == 0) ) { $top_empty=1; }
1102: if (($depth >=0) && ($#$pars > 0) ) {
1103: pop(@$pars);
1104: pop(@Apache::lonxml::pwd);
1105: }
1106: }
1107: if ($top_empty && $depth >= 0) {
1108: #never found the end tag ran out of text, throw error send back blank
1109: &error('Never found end tag for <'.$tag.
1110: '> current string <pre>'.
1111: &HTML::Entities::encode($result,'<>&"').
1112: '</pre>');
1113: if ($gotfullstack) {
1114: my $newstring='</'.$tag.'>'.$result;
1115: &Apache::lonxml::newparser($pars,\$newstring);
1116: }
1117: $result='';
1118: }
1119: } else {
1120: while ($#$pars > -1) {
1121: while ($token = $$pars[-1]->get_token) {
1122: #&Apache::lonxml::debug("s token:$token->[0]:$depth:$token->[1]");
1123: if (($token->[0] eq 'T')||($token->[0] eq 'C')||
1124: ($token->[0] eq 'D')) {
1125: if ($token->[2]) {
1126: $result.='<![CDATA['.$token->[1].']]>';
1127: } else {
1128: $result.=$token->[1];
1129: }
1130: } elsif ($token->[0] eq 'PI') {
1131: $result.=$token->[2];
1132: } elsif ($token->[0] eq 'S') {
1133: if ( $token->[1] =~ /^\Q$tag\E$/i) {
1134: $$pars[-1]->unget_token($token); last;
1135: } else {
1136: $result.=$token->[4];
1137: }
1138: if ($token->[1] =~ /^LONCAPA_INTERNAL_TURN_STYLE_ON$/) { $Apache::lonxml::usestyle=1; }
1139: if ($token->[1] =~ /^LONCAPA_INTERNAL_TURN_STYLE_OFF$/) { $Apache::lonxml::usestyle=0; }
1140: } elsif ($token->[0] eq 'E') {
1141: $result.=$token->[2];
1142: }
1143: }
1144: if (($#$pars > 0) ) {
1145: pop(@$pars);
1146: pop(@Apache::lonxml::pwd);
1147: } else { last; }
1148: }
1149: }
1150: #&Apache::lonxml::debug("Exit:$result:");
1151: return $result
1152: }
1153:
1154: sub newparser {
1155: my ($parser,$contentref,$dir) = @_;
1156: push (@$parser,HTML::LCParser->new($contentref));
1157: $$parser[-1]->xml_mode(1);
1158: $$parser[-1]->marked_sections(1);
1159: if ( $dir eq '' ) {
1160: push (@Apache::lonxml::pwd, $Apache::lonxml::pwd[$#Apache::lonxml::pwd]);
1161: } else {
1162: push (@Apache::lonxml::pwd, $dir);
1163: }
1164: }
1165:
1166: sub parstring {
1167: my ($token) = @_;
1168: my (@vars,@values);
1169: foreach my $attr (@{$token->[3]}) {
1170: if ($attr!~/\W/) {
1171: my $val=$token->[2]->{$attr};
1172: $val =~ s/([\%\@\\\"\'])/\\$1/g;
1173: $val =~ s/(\$[^\{a-zA-Z_])/\\$1/g;
1174: $val =~ s/(\$)$/\\$1/;
1175: #if ($val =~ m/^[\%\@]/) { $val="\\".$val; }
1176: push(@vars,"\$$attr");
1177: push(@values,"\"$val\"");
1178: }
1179: }
1180: my $var_init =
1181: (@vars) ? 'my ('.join(',',@vars).') = ('.join(',',@values).');'
1182: : '';
1183: return $var_init;
1184: }
1185:
1186: sub extlink {
1187: my ($res,$exact)=@_;
1188: if (!$exact) {
1189: $res=&Apache::lonnet::hreflocation($Apache::lonxml::pwd[-1],$res);
1190: }
1191: push(@Apache::lonxml::extlinks,$res)
1192: }
1193:
1194: sub writeallows {
1195: unless ($#extlinks>=0) { return; }
1196: my $thisurl = &Apache::lonnet::clutter(shift);
1197: if ($env{'httpref.'.$thisurl}) {
1198: $thisurl=$env{'httpref.'.$thisurl};
1199: }
1200: my $thisdir=$thisurl;
1201: $thisdir=~s/\/[^\/]+$//;
1202: my %httpref=();
1203: foreach (@extlinks) {
1204: $httpref{'httpref.'.
1205: &Apache::lonnet::hreflocation($thisdir,$_)}=$thisurl;
1206: }
1207: @extlinks=();
1208: &Apache::lonnet::appenv(%httpref);
1209: }
1210:
1211: sub register_ssi {
1212: my ($url,%form)=@_;
1213: push (@Apache::lonxml::ssi_info,{'url'=>$url,'form'=>\%form});
1214: return '';
1215: }
1216:
1217: sub do_registered_ssi {
1218: foreach my $info (@Apache::lonxml::ssi_info) {
1219: my %form=%{ $info->{'form'}};
1220: my $url=$info->{'url'};
1221: &Apache::lonnet::ssi($url,%form);
1222: }
1223: }
1224: #
1225: # Afterburner handles anchors, highlights and links
1226: #
1227: sub afterburn {
1228: my $result=shift;
1229: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1230: ['highlight','anchor','link']);
1231: if ($env{'form.highlight'}) {
1232: foreach (split(/\,/,$env{'form.highlight'})) {
1233: my $anchorname=$_;
1234: my $matchthis=$anchorname;
1235: $matchthis=~s/\_+/\\s\+/g;
1236: $result=~s/(\Q$matchthis\E)/\<font color=\"red\"\>$1\<\/font\>/gs;
1237: }
1238: }
1239: if ($env{'form.link'}) {
1240: foreach (split(/\,/,$env{'form.link'})) {
1241: my ($anchorname,$linkurl)=split(/\>/,$_);
1242: my $matchthis=$anchorname;
1243: $matchthis=~s/\_+/\\s\+/g;
1244: $result=~s/(\Q$matchthis\E)/\<a href=\"$linkurl\"\>$1\<\/a\>/gs;
1245: }
1246: }
1247: if ($env{'form.anchor'}) {
1248: my $anchorname=$env{'form.anchor'};
1249: my $matchthis=$anchorname;
1250: $matchthis=~s/\_+/\\s\+/g;
1251: $result=~s/(\Q$matchthis\E)/\<a name=\"$anchorname\"\>$1\<\/a\>/s;
1252: $result.=(<<"ENDSCRIPT");
1253: <script type="text/javascript">
1254: document.location.hash='$anchorname';
1255: </script>
1256: ENDSCRIPT
1257: }
1258: return $result;
1259: }
1260:
1261: sub storefile {
1262: my ($file,$contents)=@_;
1263: &Apache::lonnet::correct_line_ends(\$contents);
1264: if (my $fh=Apache::File->new('>'.$file)) {
1265: print $fh $contents;
1266: $fh->close();
1267: return 1;
1268: } else {
1269: &warning("Unable to save file $file");
1270: return 0;
1271: }
1272: }
1273:
1274: sub createnewhtml {
1275: my $title=&mt('Title of document goes here');
1276: my $body=&mt('Body of document goes here');
1277: my $filecontents=(<<SIMPLECONTENT);
1278: <html>
1279: <head>
1280: <title>$title</title>
1281: </head>
1282: <body bgcolor="#FFFFFF">
1283: $body
1284: </body>
1285: </html>
1286: SIMPLECONTENT
1287: return $filecontents;
1288: }
1289:
1290: sub createnewsty {
1291: my $filecontents=(<<SIMPLECONTENT);
1292: <definetag name="">
1293: <render>
1294: <web></web>
1295: <tex></tex>
1296: </render>
1297: </definetag>
1298: SIMPLECONTENT
1299: return $filecontents;
1300: }
1301:
1302:
1303: sub inserteditinfo {
1304: my ($result,$filecontents,$filetype)=@_;
1305: $filecontents = &HTML::Entities::encode($filecontents,'<>&"');
1306: # my $editheader='<a href="#editsection">Edit below</a><hr />';
1307: my $xml_help = '';
1308: my $initialize='';
1309: if ($filetype eq 'html') {
1310: my $addbuttons=&Apache::lonhtmlcommon::htmlareaaddbuttons();
1311: $initialize=&Apache::lonhtmlcommon::spellheader();
1312: if (!&Apache::lonhtmlcommon::htmlareablocked() &&
1313: &Apache::lonhtmlcommon::htmlareabrowser()) {
1314: $initialize.=(<<FULLPAGE);
1315: <script type="text/javascript">
1316: $addbuttons
1317:
1318: HTMLArea.loadPlugin("FullPage");
1319:
1320: function initDocument() {
1321: var editor=new HTMLArea("filecont",config);
1322: editor.registerPlugin(FullPage);
1323: editor.generate();
1324: }
1325: </script>
1326: FULLPAGE
1327: } else {
1328: $initialize.=(<<FULLPAGE);
1329: <script type="text/javascript">
1330: $addbuttons
1331: function initDocument() {
1332: }
1333: </script>
1334: FULLPAGE
1335: }
1336: $result=~s/\<body([^\>]*)\>/\<body onload="initDocument()" $1\>/i;
1337: $xml_help=&Apache::loncommon::helpLatexCheatsheet();
1338: }
1339: my $cleanbut = '';
1340:
1341: my $titledisplay=&display_title();
1342: my %lt=&Apache::lonlocal::texthash('st' => 'Save and Edit',
1343: 'vi' => 'Save and View',
1344: 'dv' => 'Discard Edits and View',
1345: 'un' => 'undo',
1346: 'ed' => 'Edit');
1347: my $buttons=(<<BUTTONS);
1348: $cleanbut
1349: <input type="submit" name="discardview" accesskey="d" value="$lt{'dv'}" />
1350: <input type="submit" name="Undo" accesskey="u" value="$lt{'un'}" /><hr>
1351: <input type="submit" name="savethisfile" accesskey="s" value="$lt{'st'}" />
1352: <input type="submit" name="viewmode" accesskey="v" value="$lt{'vi'}" />
1353: BUTTONS
1354: $buttons.=&Apache::lonhtmlcommon::spelllink('xmledit','filecont');
1355: my $editfooter=(<<ENDFOOTER);
1356: $initialize
1357: <hr />
1358: <a name="editsection" />
1359: <form method="post" name="xmledit">
1360: $xml_help
1361: <input type="hidden" name="editmode" value="$lt{'ed'}" />
1362: $buttons<br />
1363: <textarea style="width:100%" cols="80" rows="44" name="filecont" id="filecont">$filecontents</textarea>
1364: <br />$buttons
1365: <br />
1366: </form>
1367: $titledisplay
1368: </body>
1369: ENDFOOTER
1370: # $result=~s/(\<body[^\>]*\>)/$1$editheader/is;
1371: $result=~s/(\<\/body\>)/$editfooter/is;
1372: return $result;
1373: }
1374:
1375: sub get_target {
1376: my $viewgrades=&Apache::lonnet::allowed('vgr',$env{'request.course.id'});
1377: if ( $env{'request.state'} eq 'published') {
1378: if ( defined($env{'form.grade_target'})
1379: && ($viewgrades == 'F' )) {
1380: return ($env{'form.grade_target'});
1381: } elsif (defined($env{'form.grade_target'})) {
1382: if (($env{'form.grade_target'} eq 'web') ||
1383: ($env{'form.grade_target'} eq 'tex') ) {
1384: return $env{'form.grade_target'}
1385: } else {
1386: return 'web';
1387: }
1388: } else {
1389: return 'web';
1390: }
1391: } elsif ($env{'request.state'} eq 'construct') {
1392: if ( defined($env{'form.grade_target'})) {
1393: return ($env{'form.grade_target'});
1394: } else {
1395: return 'web';
1396: }
1397: } else {
1398: return 'web';
1399: }
1400: }
1401:
1402: sub handler {
1403: my $request=shift;
1404:
1405: my $target=&get_target();
1406:
1407: $Apache::lonxml::debug=$env{'user.debug'};
1408:
1409: &Apache::loncommon::content_type($request,'text/html');
1410: &Apache::loncommon::no_cache($request);
1411: if ($env{'request.state'} eq 'published') {
1412: $request->set_last_modified(&Apache::lonnet::metadata($request->uri,
1413: 'lastrevisiondate'));
1414: }
1415: $request->send_http_header;
1416:
1417: return OK if $request->header_only;
1418:
1419:
1420: my $file=&Apache::lonnet::filelocation("",$request->uri);
1421: my $filetype;
1422: if ($file =~ /\.sty$/) {
1423: $filetype='sty';
1424: } else {
1425: $filetype='html';
1426: }
1427: #
1428: # Edit action? Save file.
1429: #
1430: if (!($env{'request.state'} eq 'published')) {
1431: if ($env{'form.savethisfile'} || $env{'form.viewmode'} || $env{'form.Undo'}) {
1432: my $html_file=&Apache::lonnet::getfile($file);
1433: my $error = &Apache::lonhomework::handle_save_or_undo($request, \$html_file, \$env{'form.filecont'});
1434: }
1435: }
1436: my %mystyle;
1437: my $result = '';
1438: my $filecontents=&Apache::lonnet::getfile($file);
1439: if ($filecontents eq -1) {
1440: my $start_page=&Apache::loncommon::start_page('File Error');
1441: my $end_page=&Apache::loncommon::end_page();
1442: my $fnf=&mt('File not found');
1443: $result=(<<ENDNOTFOUND);
1444: $start_page
1445: <b>$fnf: $file</b>
1446: $end_page
1447: ENDNOTFOUND
1448: $filecontents='';
1449: if ($env{'request.state'} ne 'published') {
1450: if ($filetype eq 'sty') {
1451: $filecontents=&createnewsty();
1452: } else {
1453: $filecontents=&createnewhtml();
1454: }
1455: $env{'form.editmode'}='Edit'; #force edit mode
1456: }
1457: } else {
1458: unless ($env{'request.state'} eq 'published') {
1459: if ($filecontents=~/BEGIN LON-CAPA Internal/) {
1460: &Apache::lonxml::error(&mt('This file appears to be a rendering of a LON-CAPA resource. If this is correct, this resource will act very oddly and incorrectly.'));
1461: }
1462: #
1463: # we are in construction space, see if edit mode forced
1464: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1465: ['editmode']);
1466: }
1467: if (!$env{'form.editmode'} || $env{'form.viewmode'} || $env{'form.discardview'}) {
1468: $result = &Apache::lonxml::xmlparse($request,$target,$filecontents,
1469: '',%mystyle);
1470: undef($Apache::lonhomework::parsing_a_task);
1471: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1472: ['rawmode']);
1473: if ($env{'form.rawmode'}) { $result = $filecontents; }
1474: }
1475: }
1476:
1477: #
1478: # Edit action? Insert editing commands
1479: #
1480: unless ($env{'request.state'} eq 'published') {
1481: if ($env{'form.editmode'} && (!($env{'form.viewmode'})) && (!($env{'form.discardview'})))
1482: {
1483: my $displayfile=$request->uri;
1484: $displayfile=~s/^\/[^\/]*//;
1485: my %options = ();
1486: if ($env{'environment.remote'} ne 'off') {
1487: $options{'bgcolor'} = '#FFFFFF';
1488: }
1489: my $start_page = &Apache::loncommon::start_page(undef,undef,
1490: \%options);
1491: $result=$start_page.
1492: &Apache::lonxml::message_location().'<h3>'.
1493: $displayfile.
1494: '</h3>'.&Apache::loncommon::end_page();
1495: $result=&inserteditinfo($result,$filecontents,$filetype);
1496: }
1497: }
1498: if ($filetype eq 'html') { &writeallows($request->uri); }
1499:
1500:
1501: &Apache::lonxml::add_messages(\$result);
1502: $request->print($result);
1503:
1504: return OK;
1505: }
1506:
1507: sub display_title {
1508: my $result;
1509: if ($env{'request.state'} eq 'construct') {
1510: my $title=&Apache::lonnet::gettitle();
1511: if (!defined($title) || $title eq '') {
1512: $title = $env{'request.filename'};
1513: $title = substr($title, rindex($title, '/') + 1);
1514: }
1515: $result = "<script type='text/javascript'>top.document.title = '$title - LON-CAPA Construction Space';</script>";
1516: }
1517: return $result;
1518: }
1519:
1520: sub debug {
1521: if ($Apache::lonxml::debug eq "1") {
1522: $|=1;
1523: my $request=$Apache::lonxml::request;
1524: if (!$request) {
1525: eval { $request=Apache->request; };
1526: }
1527: if (!$request) {
1528: eval { $request=Apache2::RequestUtil->request; };
1529: }
1530: $request->print('<font size="-2"><pre>DEBUG:'.&HTML::Entities::encode($_[0],'<>&"')."</pre></font>\n");
1531: #&Apache::lonnet::logthis($_[0]);
1532: }
1533: }
1534:
1535: sub show_error_warn_msg {
1536: if ($env{'request.filename'} eq '/home/httpd/html/res/lib/templates/simpleproblem.problem' &&
1537: &Apache::lonnet::allowed('mdc',$env{'request.course.id'})) {
1538: return 1;
1539: }
1540: return (($Apache::lonxml::debug eq 1) ||
1541: ($env{'request.state'} eq 'construct') ||
1542: ($Apache::lonhomework::browse eq 'F'
1543: &&
1544: $env{'form.show_errors'} eq 'on'));
1545: }
1546:
1547: sub error {
1548: $errorcount++;
1549: if ( &show_error_warn_msg() ) {
1550: # If printing in construction space, put the error inside <pre></pre>
1551: push(@Apache::lonxml::error_messages,
1552: $Apache::lonxml::warnings_error_header.
1553: "<b>ERROR:</b>".join("<br />\n",@_)."<br />\n");
1554: $Apache::lonxml::warnings_error_header='';
1555: } else {
1556: my $errormsg;
1557: my ($symb)=&Apache::lonnet::symbread();
1558: if ( !$symb ) {
1559: #public or browsers
1560: $errormsg=&mt("An error occured while processing this resource. The author has been notified.");
1561: }
1562: my $host=$Apache::lonnet::perlvar{'lonHostID'};
1563: my $msg = join('<br />',(@_,"The error occurred on host <tt>$host</tt>"));
1564: #notify author
1565: &Apache::lonmsg::author_res_msg($env{'request.filename'},$msg);
1566: #notify course
1567: if ( $symb && $env{'request.course.id'} ) {
1568: my $cnum=$env{'course.'.$env{'request.course.id'}.'.num'};
1569: my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1570: my (undef,%users)=&Apache::lonfeedback::decide_receiver(undef,0,1,1,1);
1571: my $declutter=&Apache::lonnet::declutter($env{'request.filename'});
1572: my $baseurl = &Apache::lonnet::clutter($declutter);
1573: my @userlist;
1574: foreach (keys %users) {
1575: my ($user,$domain) = split(/:/, $_);
1576: push(@userlist,"$user\@$domain");
1577: my $key=$declutter.'_'.$user.'_'.$domain;
1578: my %lastnotified=&Apache::lonnet::get('nohist_xmlerrornotifications',
1579: [$key],
1580: $cdom,$cnum);
1581: my $now=time;
1582: if ($now-$lastnotified{$key}>86400) {
1583: my $title = &Apache::lonnet::gettitle($symb);
1584: my $sentmessage;
1585: &Apache::lonmsg::user_normal_msg($user,$domain,
1586: "Error [$title]",$msg,'',$baseurl,'','',
1587: \$sentmessage,$symb,$title,1);
1588: &Apache::lonnet::put('nohist_xmlerrornotifications',
1589: {$key => $now},
1590: $cdom,$cnum);
1591: }
1592: }
1593: if ($env{'request.role.adv'}) {
1594: $errormsg=&mt("An error occured while processing this resource. The course personnel ([_1]) and the author have been notified.",join(', ',@userlist));
1595: } else {
1596: $errormsg=&mt("An error occured while processing this resource. The instructor has been notified.");
1597: }
1598: }
1599: push(@Apache::lonxml::error_messages,"<b>$errormsg</b> <br />");
1600: }
1601: }
1602:
1603: sub warning {
1604: $warningcount++;
1605:
1606: if ($env{'form.grade_target'} ne 'tex') {
1607: if ( &show_error_warn_msg() ) {
1608: push(@Apache::lonxml::warning_messages,
1609: $Apache::lonxml::warnings_error_header.
1610: "<b>W</b>ARNING<b>:</b>".join('<br />',@_)."<br />\n");
1611: $Apache::lonxml::warnings_error_header='';
1612: }
1613: }
1614: }
1615:
1616: sub info {
1617: if ($env{'form.grade_target'} ne 'tex'
1618: && $env{'request.state'} eq 'construct') {
1619: push(@Apache::lonxml::info_messages,join('<br />',@_)."<br />\n");
1620: }
1621: }
1622:
1623: sub message_location {
1624: return '__LONCAPA_INTERNAL_MESSAGE_LOCATION__';
1625: }
1626:
1627: sub add_messages {
1628: my ($msg)=@_;
1629: my $result=join(' ',
1630: @Apache::lonxml::info_messages,
1631: @Apache::lonxml::error_messages,
1632: @Apache::lonxml::warning_messages);
1633: undef(@Apache::lonxml::info_messages);
1634: undef(@Apache::lonxml::error_messages);
1635: undef(@Apache::lonxml::warning_messages);
1636: $$msg=~s/__LONCAPA_INTERNAL_MESSAGE_LOCATION__/$result/;
1637: $$msg=~s/__LONCAPA_INTERNAL_MESSAGE_LOCATION__//g;
1638: }
1639:
1640: sub get_param {
1641: my ($param,$parstack,$safeeval,$context,$case_insensitive) = @_;
1642: if ( ! $context ) { $context = -1; }
1643: my $args ='';
1644: if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; }
1645: if ( ! $Apache::lonxml::usestyle ) {
1646: $args=$Apache::lonxml::style_values.$args;
1647: }
1648: if ( ! $args ) { return undef; }
1649: if ( $case_insensitive ) {
1650: if ($args =~ s/(my (?:.*))(\$\Q$param\E[,\)])/$1.lc($2)/ei) {
1651: return &Apache::run::run("{$args;".'return $'.$param.'}',
1652: $safeeval); #'
1653: } else {
1654: return undef;
1655: }
1656: } else {
1657: if ( $args =~ /my .*\$\Q$param\E[,\)]/ ) {
1658: return &Apache::run::run("{$args;".'return $'.$param.'}',
1659: $safeeval); #'
1660: } else {
1661: return undef;
1662: }
1663: }
1664: }
1665:
1666: sub get_param_var {
1667: my ($param,$parstack,$safeeval,$context,$case_insensitive) = @_;
1668: if ( ! $context ) { $context = -1; }
1669: my $args ='';
1670: if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; }
1671: if ( ! $Apache::lonxml::usestyle ) {
1672: $args=$Apache::lonxml::style_values.$args;
1673: }
1674: &Apache::lonxml::debug("Args are $args param is $param");
1675: if ($case_insensitive) {
1676: if (! ($args=~s/(my (?:.*))(\$\Q$param\E[,\)])/$1.lc($2)/ei)) {
1677: return undef;
1678: }
1679: } elsif ( $args !~ /my .*\$\Q$param\E[,\)]/ ) { return undef; }
1680: my $value=&Apache::run::run("{$args;".'return $'.$param.'}',$safeeval); #'
1681: &Apache::lonxml::debug("first run is $value");
1682: if ($value =~ /^[\$\@\%][a-zA-Z_]\w*$/) {
1683: &Apache::lonxml::debug("doing second");
1684: my @result=&Apache::run::run("return $value",$safeeval,1);
1685: if (!defined($result[0])) {
1686: return $value
1687: } else {
1688: if (wantarray) { return @result; } else { return $result[0]; }
1689: }
1690: } else {
1691: return $value;
1692: }
1693: }
1694:
1695: sub register_insert_tab {
1696: my @data = split /\n/, &Apache::lonnet::getfile('/home/httpd/lonTabs/insertlist.tab');
1697: my $i;
1698: my $tagnum=0;
1699: my @order;
1700: for ($i=0;$i < $#data; $i++) {
1701: my $line = $data[$i];
1702: if ( $line =~ /^\#/ || $line =~ /^\s*\n/) { next; }
1703: if ( $line =~ /TABLE/ ) { last; }
1704: my ($tag,$descrip,$color,$function,$show,$helpfile,$helpdesc) = split(/,/, $line);
1705: if ($tag) {
1706: $insertlist{"$tagnum.tag"} = $tag;
1707: $insertlist{"$tag.description"} = $descrip;
1708: $insertlist{"$tag.color"} = $color;
1709: $insertlist{"$tag.function"} = $function;
1710: if (!defined($show)) { $show='yes'; }
1711: $insertlist{"$tag.show"}= $show;
1712: $insertlist{"$tag.helpfile"} = $helpfile;
1713: $insertlist{"$tag.helpdesc"} = $helpdesc;
1714: $insertlist{"$tag.num"}=$tagnum;
1715: $tagnum++;
1716: }
1717: }
1718: $i++; #skipping TABLE line
1719: $tagnum = 0;
1720: for (;$i < $#data;$i++) {
1721: my $line = $data[$i];
1722: my ($mnemonic,@which) = split(/ +/,$line);
1723: my $tag = $insertlist{"$tagnum.tag"};
1724: for (my $j=0;$j <=$#which;$j++) {
1725: if ( $which[$j] eq 'Y' ) {
1726: if ($insertlist{"$j.show"} ne 'no') {
1727: push(@{ $insertlist{"$tag.which"} },$insertlist{"$j.tag"});
1728: }
1729: }
1730: }
1731: $tagnum++;
1732: }
1733: }
1734:
1735: sub register_insert_xml {
1736: my $parser = HTML::LCParser->new($Apache::lonnet::perlvar{'lonTabDir'}
1737: .'/insertlist.xml');
1738: my ($tagnum,$in_help)=(0,0);
1739: my $tag;
1740: while (my $token = $parser->get_token()) {
1741: if ($token->[0] eq 'S') {
1742: my $key;
1743: if ($token->[1] eq 'tag') {
1744: $tag = $token->[2]{'name'};
1745: $insertlist{"$tagnum.tag"} = $tag;
1746: $insertlist{"$tag.num"} = $tagnum;
1747: } elsif ($in_help && $token->[1] eq 'file') {
1748: $key = $tag.'.helpfile';
1749: } elsif ($in_help && $token->[1] eq 'description') {
1750: $key = $tag.'.helpdesc';
1751: } elsif ($token->[1] eq 'description' ||
1752: $token->[1] eq 'color' ||
1753: $token->[1] eq 'show' ) {
1754: $key = $tag.'.'.$token->[1];
1755: } elsif ($token->[1] eq 'insert_sub') {
1756: $key = $tag.'.function';
1757: } elsif ($token->[1] eq 'help') {
1758: $in_help=1;
1759: } elsif ($token->[1] eq 'allow') {
1760: my $allow = $parser->get_text();
1761: foreach my $element (split(',',$allow)) {
1762: $element =~ s/(^\s*|\s*$ )//gx;
1763: push(@{ $insertlist{$tag.'.which'} },$element);
1764: }
1765: }
1766: if (defined($key)) {
1767: $insertlist{$key} = $parser->get_text();
1768: $insertlist{$key} =~ s/(^\s*|\s*$ )//gx;
1769: }
1770: } elsif ($token->[0] eq 'E') {
1771: if ($token->[1] eq 'tag') {
1772: undef($tag);
1773: $tagnum++;
1774: } elsif ($token->[1] eq 'help') {
1775: undef($in_help);
1776: }
1777: }
1778: }
1779: }
1780:
1781: sub register_insert {
1782: # ®ister_insert_tab(@_);
1783: # &dump_insertlist('1');
1784: # undef(%insertlist);
1785: return ®ister_insert_xml(@_);
1786: # &dump_insertlist('2');
1787: }
1788:
1789: sub dump_insertlist {
1790: my ($ext) = @_;
1791: open(XML,">/tmp/insertlist.xml.$ext");
1792: print XML ("<insertlist>");
1793: my $i=0;
1794:
1795: while (exists($insertlist{"$i.tag"})) {
1796: my $tag = $insertlist{"$i.tag"};
1797: print XML ("
1798: \t<tag name=\"$tag\">");
1799: if (defined($insertlist{"$tag.description"})) {
1800: print XML ("
1801: \t\t<description>".$insertlist{"$tag.description"}."</description>");
1802: }
1803: if (defined($insertlist{"$tag.color"})) {
1804: print XML ("
1805: \t\t<color>".$insertlist{"$tag.color"}."</color>");
1806: }
1807: if (defined($insertlist{"$tag.function"})) {
1808: print XML ("
1809: \t\t<insert_sub>".$insertlist{"$tag.function"}."</insert_sub>");
1810: }
1811: if (defined($insertlist{"$tag.show"})
1812: && $insertlist{"$tag.show"} ne 'yes') {
1813: print XML ("
1814: \t\t<show>".$insertlist{"$tag.show"}."</show>");
1815: }
1816: if (defined($insertlist{"$tag.helpfile"})) {
1817: print XML ("
1818: \t\t<help>
1819: \t\t\t<file>".$insertlist{"$tag.helpfile"}."</file>");
1820: if ($insertlist{"$tag.helpdesc"} ne '') {
1821: print XML ("
1822: \t\t\t<description>".$insertlist{"$tag.helpdesc"}."</description>");
1823: }
1824: print XML ("
1825: \t\t</help>");
1826: }
1827: if (defined($insertlist{"$tag.which"})) {
1828: print XML ("
1829: \t\t<allow>".join(',',sort(@{ $insertlist{"$tag.which"} }))."</allow>");
1830: }
1831: print XML ("
1832: \t</tag>");
1833: $i++;
1834: }
1835: print XML ("\n</insertlist>\n");
1836: close(XML);
1837: }
1838:
1839: sub description {
1840: my ($token)=@_;
1841: my $tag = &get_tag($token);
1842: return $insertlist{$tag.'.description'};
1843: }
1844:
1845: # Returns a list containing the help file, and the description
1846: sub helpinfo {
1847: my ($token)=@_;
1848: my $tag = &get_tag($token);
1849: return ($insertlist{$tag.'.helpfile'}, $insertlist{$tag.'.helpdesc'});
1850: }
1851:
1852: sub get_tag {
1853: my ($token)=@_;
1854: my $tagnum;
1855: my $tag=$token->[1];
1856: foreach my $namespace (reverse(@Apache::lonxml::namespace)) {
1857: my $testtag = $namespace.'::'.$tag;
1858: $tagnum = $insertlist{"$testtag.num"};
1859: last if (defined($tagnum));
1860: }
1861: if (!defined($tagnum)) {
1862: $tagnum = $Apache::lonxml::insertlist{"$tag.num"};
1863: }
1864: return $insertlist{"$tagnum.tag"};
1865: }
1866:
1867: 1;
1868: __END__
1869:
1870:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>