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