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