1: # The LearningOnline Network with CAPA
2: # <script> definiton
3: #
4: # $Id: scripttag.pm,v 1.83 2002/11/14 16:47:41 matthew 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: # 2/21 Guy
29: # 8/20 Gerd Kortemeyer
30:
31: package Apache::scripttag;
32:
33: use strict;
34: use Apache::lonnet;
35: use Apache::style;
36:
37: #Globals
38: # this used to pass around the standard callsub arguments to a tag func
39: # so xmlparse can reenter the inner_xmlparse loop.
40:
41: @Apache::scripttag::parser_env = ();
42: BEGIN {
43: &Apache::lonxml::register('Apache::scripttag',
44: ('script','scriptlib','parserlib','import',
45: 'window','display','storetc','physnet',
46: 'standalone','comment',
47: 'LONCAPA_INTERNAL_TURN_STYLE_ON'));
48: }
49:
50: sub start_LONCAPA_INTERNAL_TURN_STYLE_ON {
51: $Apache::lonxml::usestyle=1;
52: return ('','no');
53: }
54:
55: sub end_LONCAPA_INTERNAL_TURN_STYLE_ON {
56: return ('','no');
57: }
58:
59: sub start_script {
60: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
61: @Apache::scripttag::parser_env = @_;
62: my $result='';
63: my $type= &Apache::lonxml::get_param('type',$parstack,$safeeval);
64: &Apache::lonxml::debug("found type of $type");
65: if ($type eq "loncapa/perl") {
66: my $bodytext=&Apache::lonxml::get_all_text("/script",$$parser[$#$parser]);
67: if ( $target eq "modified" ) {
68: $result=$token->[4].&Apache::edit::modifiedfield();
69: } elsif ( $target eq 'web' || $target eq 'tex' ||
70: $target eq 'grade' || $target eq 'answer' ||
71: $target eq 'analyze' ) {
72: if (!$Apache::lonxml::default_homework_loaded) {
73: &Apache::lonxml::default_homework_load($safeeval);
74: }
75: &Apache::run::run($bodytext,$safeeval);
76: if (($target eq 'answer') && ($Apache::lonhomework::viewgrades == 'F')) {
77: $Apache::lonxml::evaluate--;
78: $result.="<a href=\"javascript:newWindow=open(\'\',\'new_W\',\'width=500,height=200,scrollbars=1\');newWindow.document.open(\'text/html\',\'replace\');newWindow.document.writeln(\'<html><head><title>newwindow</title></head><body bgcolor="#FFFFFF"><pre>";
79: my $listing= &HTML::Entities::encode(&Apache::run::dump($target,$safeeval));
80:
81: $result.=$listing;
82: $result.= "</pre></body></html>\');newWindow.document.close();void(0);\">Script Vars</a><br />";
83: }
84: } elsif ($target eq "edit" ) {
85: #&Apache::run::run($bodytext,$safeeval);
86: #$result="<br /> <$token->[1]> output: <br />$bodytext<br />Source:<br />";
87: $result=&Apache::edit::tag_start($target,$token,'Script');
88: $result.=&Apache::edit::editfield($token->[1],$bodytext,'',80,4);
89: }
90: } else {
91: if ($target ne "meta") {
92: $result = $token->[4];
93: my $bodytext=&Apache::lonxml::get_all_text("/script",$$parser[$#$parser]);
94: $result.=$bodytext;
95: }
96: }
97: return $result;
98: }
99:
100: sub end_script {
101: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
102: if ( $target eq "meta" ) { return ''; }
103: my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval);
104: my $result='';
105: #other script blocks need to survive
106: if ($type ne "loncapa/perl") {
107: return $token->[2];
108: } elsif ($target eq 'edit' ) {
109: return &Apache::edit::end_table();
110: } elsif ($target eq 'answer') {
111: $Apache::lonxml::evaluate++;
112: }
113: return '';
114: }
115:
116: sub start_display {
117: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
118:
119: my $result;
120: my $bodytext=&Apache::lonxml::get_all_text("/display",$$parser[$#$parser]);
121:
122: if ( $target eq "modified" ) {
123: $result=$token->[4].&Apache::edit::modifiedfield();
124: } elsif ( $target eq 'web' || $target eq 'tex' ||
125: $target eq 'grade' || $target eq 'answer' ||
126: $target eq 'analyze') {
127: if (!$Apache::lonxml::default_homework_loaded) {
128: &Apache::lonxml::default_homework_load($safeeval);
129: }
130: $result=&Apache::run::run($bodytext,$safeeval);
131: if ($target eq 'grade' || $target eq 'answer' ||
132: $target eq 'analyze') {
133: $result=''; # grade should produce no output
134: }
135: } elsif ($target eq "edit" ) {
136: #$result =
137: # "<br /> <$token->[1]> output: <br />$bodytext<br />Source:<br />";
138: #$result.=&Apache::edit::editfield($token->[1],$bodytext,'',40,1);
139: $result=&Apache::edit::tag_start($target,$token,'Script With Display');
140: $result.=&Apache::edit::editfield($token->[1],$bodytext,'',80,1)
141: }
142: return $result;
143: }
144:
145: sub end_display {
146: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
147: if ($target eq 'edit' ) { return &Apache::edit::end_table(); }
148: return '';
149: }
150:
151: sub start_scriptlib {
152: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
153: my $bodytext;
154: my $result ='';
155: my $error='';
156:
157: if ($target eq 'web' || $target eq 'grade' || $target eq 'meta' ||
158: $target eq 'edit' || $target eq 'answer' || $target eq 'analyze') {
159: $bodytext=$$parser[$#$parser]->get_text("/scriptlib");
160: $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
161: $$parstack[$#$parstack]);
162: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
163: $bodytext);
164: my $script=&Apache::lonnet::getfile($location);
165: if ($script == -1) {
166: if ($target eq 'edit') {
167: $error='</tr><tr><td>Errors</td><td colspan="2"><b> Unable to find <i>'.$location.'</i></b></td>'."\n";
168: } else {
169: &Apache::lonxml::error("<b> Unable to find <i>$location</i> for scriptlib</b>");
170: return "";
171: }
172: }
173: &Apache::run::run($script,$safeeval);
174: #&Apache::lonxml::debug("ran $bodytext:<br />".&Apache::lonnet::getfile($bodytext)."<br />");
175: }
176: if ($target eq "edit" ) {
177: $result=
178: &Apache::edit::tag_start($target,$token,'New Script Functions').
179: &Apache::edit::editline($token->[1],$bodytext,'scriptlib',40).
180: &Apache::edit::browse(undef,'textnode').
181: $error.'</td></tr>'.
182: &Apache::edit::end_table();
183: }
184: if ($target eq "modified" ) {
185: $bodytext=$$parser[$#$parser]->get_text("/scriptlib");
186: $result=$token->[4].&Apache::edit::modifiedfield($token);
187: &Apache::lonxml::debug($result);
188: }
189: return $result;
190: }
191:
192: sub end_scriptlib {
193: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
194: my @result;
195: if ($target eq "edit" ) { $result[1]='no'; }
196: return @result;
197: }
198:
199: sub start_parserlib {
200: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
201: my $bodytext;
202: my $result ="";
203: my $error='';
204: if ($target eq 'web' || $target eq 'grade' || $target eq 'meta' ||
205: $target eq 'edit' || $target eq 'answer' || $target eq 'analyze') {
206: $bodytext=$$parser[$#$parser]->get_text("/parserlib");
207: $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
208: $$parstack[$#$parstack]);
209: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
210: $bodytext);
211: my $styletext=&Apache::lonnet::getfile($location);
212: #&Apache::lonxml::debug("found :$bodytext: in :$location: with :$styletext:");
213: if ($styletext == -1) {
214: if ($target eq 'edit') {
215: $error='</tr><tr><td>Errors</td><td colspan="2"><b> Unable to find <i>'.$location.'</i></b></td>'."\n";
216: } else {
217: &Apache::lonxml::error("<b> Unable to find <i>$location</i> for parserlib</b>");
218: return "";
219: }
220: }
221: %$style = ( %$style , &Apache::style::styleparser($target,$styletext));
222: }
223: if ($target eq "edit" ) {
224: $result=
225: &Apache::edit::tag_start($target,$token,'New Tag Definitions').
226: &Apache::edit::editline($token->[1],$bodytext,'',40).
227: $error.'</td></tr>'.
228: &Apache::edit::end_table();
229: }
230: if ($target eq "modified" ) {
231: $bodytext=$$parser[$#$parser]->get_text("/parserlib");
232: $result=$token->[4].&Apache::edit::modifiedfield($token);
233: &Apache::lonxml::debug($result);
234: }
235: return $result;
236: }
237:
238: sub end_parserlib {
239: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
240: my @result;
241: if ($target eq "edit" ) { $result[1]='no'; }
242: return @result;
243: }
244:
245: sub start_window {
246: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
247: my $result = '';
248: if ($target eq 'web') {
249: &Apache::lonxml::startredirection;
250: } elsif ($target eq 'tex') {
251: $result = '\unskip\footnote{';
252: }
253: return $result;
254: }
255:
256: sub end_window {
257: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
258: my $result;
259: if ($target eq 'web') {
260: my $output=&Apache::lonxml::endredirection;
261: $output =~ s/\"/\"\;/g;
262: $result = "<a href=\"javascript:newWindow=open(\'\',\'new_W\',\'width=500,height=200,scrollbars=1\');newWindow.document.open(\'text/html\',\'replace\');newWindow.document.writeln(\'<html><head><title>newwindow</title></head><body bgcolor="#FFFFFF"> $output </body></html>\');newWindow.document.close();void(0);\"><sup>*</sup></a>";
263: } elsif ($target eq 'tex') {
264: $result = '}';
265: } else {
266: $result = '';
267: }
268: return $result;
269: }
270:
271: sub start_import {
272: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
273: my $bodytext=$$parser[$#$parser]->get_text("/import");
274: my $result ="";
275:
276: $bodytext=Apache::run::evaluate($bodytext,$safeeval,$$parstack[$#$parstack]);
277:
278: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
279: $target eq 'tex' || $target eq 'analyze' ) {
280: # FIXME this probably needs to be smart about construction vs.
281: # non construction space.
282: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
283: my $file=&Apache::lonnet::getfile($location);
284: if ($file == -1) {
285: &Apache::lonxml::error("<b> Unable to find <i>$bodytext as $location</i> for import</b>");
286: return "";
287: }
288:
289: my $dir=$location;
290: $dir=~s:/[^/]*$::;
291: # &Apache::lonxml::debug("directory $dir $location file $file \n<b>END</b>\n");
292: my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
293: if (!$id) { $id=$Apache::lonxml::curdepth; }
294: push(@Apache::inputtags::import,$id);
295: &Apache::lonxml::newparser($parser,\$file,$dir);
296: } elsif ($target eq "edit" ) {
297: $result.=&Apache::edit::tag_start($target,$token);
298: $result.=&Apache::edit::editline($token->[1],$bodytext,'',40);
299: $result.=&Apache::edit::browse(undef,'textnode');
300: #FIXME this need to convert $bodytext to be a contruction space reference
301: #my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
302: #$result.="Click<a href=\"$location\">here</a> to edit<br />"
303: } elsif ($target eq 'modified') {
304: $bodytext=$$parser[$#$parser]->get_text("/import");
305: $result=$token->[4].&Apache::edit::modifiedfield($token);
306: &Apache::lonxml::debug($result);
307: } elsif ($target eq 'meta') {
308: my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
309: $result.='<import part="'.$Apache::inputtags::part;
310: if ($id) {
311: $result.='" id="'.$id;
312: }
313: $result.='">';
314: $result.=$bodytext;
315: $result.='</import>';
316: }
317: return $result;
318: }
319:
320: sub end_import {
321: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
322: pop(@Apache::inputtags::import);
323: my $result;
324: if ($target eq 'edit' ) { $result=&Apache::edit::end_table(); }
325: return $result;
326: }
327:
328: sub start_storetc {
329: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
330: my $result = '';
331: &Apache::lonxml::startredirection;
332: return $result;
333: }
334:
335: sub end_storetc {
336: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
337: my $result;
338: my $output=&Apache::lonxml::endredirection;
339: $output =~ s/\"/\"\;/g;
340: $result = '{\bf '.$output.'.}}\write\tcfile{\protect\tcpc{ '.$output.'.}{\the\value{relpage}}}';
341: return $result;
342: }
343:
344:
345: sub start_physnet {
346: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
347: my $bodytext = '/adm/includes/physnet.sty';
348: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
349: my $cbistyletext=&Apache::lonnet::getfile($location);
350:
351: %$style = (%$style,&Apache::style::styleparser($target,$cbistyletext));
352: $$parser['-1']->unget_token($token);
353: # if ( defined($$style{'physnet'}) ) {
354: # &Apache::lonxml::newparser($parser,\$$style{'physnet'});
355: # }
356: return "";
357: }
358:
359: sub end_physnet {
360: return '';
361: }
362:
363: sub start_standalone {
364: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
365: my $result='';
366: if ($target eq 'web' ) {
367: if ( $ENV{'request.course.id'} ) {
368: my $inside = &Apache::lonxml::get_all_text("/standalone",$$parser[-1]);
369: } else {
370: $result='<table bgcolor="#E1E1E1" border="2"><tr><td>';
371: }
372: }
373: return $result;
374: }
375:
376: sub end_standalone {
377: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
378: my $result='';
379: if ($target eq 'web' ) {
380: if ( $ENV{'request.course.id'} ) {
381: } else {
382: $result='</td></tr></table>';
383: }
384: }
385: return $result;
386: }
387:
388: sub start_comment {
389: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
390: my $result='';
391: if ($target eq 'edit') {
392: $result=&Apache::edit::tag_start($target,$token);
393: my $bodytext=&Apache::lonxml::get_all_text("/comment",$$parser[$#$parser]);
394: $result.=&Apache::edit::editfield($token->[1],$bodytext,'',80,4)
395: } elsif ( $target eq 'modified') {
396: $result=$token->[4].&Apache::edit::modifiedfield($token);
397: } elsif ( $target eq 'web' || $target eq 'tex' || $target eq 'grade' ||
398: $target eq 'answer' || $target eq 'meta' || $target eq 'analyze') {
399: #normally throw away comments
400: my $bodytext=&Apache::lonxml::get_all_text("/comment",$$parser[$#$parser]);
401: }
402: return $result;
403: }
404:
405: sub end_comment {
406: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
407: if ($target eq 'edit' ) { return &Apache::edit::end_table(); }
408: return '';
409: }
410:
411:
412: sub xmlparse {
413: my ($string) = @_;
414: &Apache::lonxml::debug("Got $string");
415: my ($target,$token,$tagstack,$parstack,$oldparser,$safeeval,$style)=
416: @Apache::scripttag::parser_env;
417: my @parser;
418: &Apache::lonxml::newparser(\@parser,\$string);
419: my $result=&Apache::lonxml::inner_xmlparse($target,$tagstack,
420: $parstack,\@parser,
421: $safeeval,$style);
422: return $result;
423: }
424:
425: 1;
426: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>