Annotation of loncom/xml/scripttag.pm, revision 1.79
1.1 albertel 1: # The LearningOnline Network with CAPA
2: # <script> definiton
1.58 www 3: #
1.79 ! albertel 4: # $Id: scripttag.pm,v 1.78 2002/10/14 20:47:11 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.48 albertel 117: $result=&Apache::run::run($bodytext,$safeeval);
1.78 albertel 118: if ($target eq 'grade' || $target eq 'answer' ||
119: $target eq 'analyze') {
1.48 albertel 120: $result=''; # grade should produce no output
121: }
122: } elsif ($target eq "edit" ) {
123: #$result =
124: # "<br /> <$token->[1]> output: <br />$bodytext<br />Source:<br />";
125: #$result.=&Apache::edit::editfield($token->[1],$bodytext,'',40,1);
126: $result=&Apache::edit::tag_start($target,$token,'Script With Display');
1.77 albertel 127: $result.=&Apache::edit::editfield($token->[1],$bodytext,'',80,1)
1.24 albertel 128: }
129: return $result;
130: }
131:
132: sub end_display {
1.48 albertel 133: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
134: if ($target eq 'edit' ) { return &Apache::edit::end_table(); }
135: return '';
1.1 albertel 136: }
1.3 albertel 137:
138: sub start_scriptlib {
1.40 albertel 139: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.37 albertel 140: my $bodytext;
141: my $result ='';
142: my $error='';
143:
1.78 albertel 144: if ($target eq 'web' || $target eq 'grade' || $target eq 'meta' ||
145: $target eq 'edit' || $target eq 'answer' || $target eq 'analyze') {
1.37 albertel 146: $bodytext=$$parser[$#$parser]->get_text("/scriptlib");
147: $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
148: $$parstack[$#$parstack]);
149: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
150: $bodytext);
151: my $script=&Apache::lonnet::getfile($location);
152: if ($script == -1) {
153: if ($target eq 'edit') {
154: $error='</tr><tr><td>Errors</td><td colspan="2"><b> Unable to find <i>'.$location.'</i></b></td>'."\n";
155: } else {
156: &Apache::lonxml::error("<b> Unable to find <i>$location</i> for scriptlib</b>");
157: return "";
158: }
159: }
160: &Apache::run::run($script,$safeeval);
161: #&Apache::lonxml::debug("ran $bodytext:<br />".&Apache::lonnet::getfile($bodytext)."<br />");
1.18 albertel 162: }
1.13 albertel 163: if ($target eq "edit" ) {
1.37 albertel 164: $result=
1.49 albertel 165: &Apache::edit::tag_start($target,$token,'New Script Functions').
1.68 albertel 166: &Apache::edit::editline($token->[1],$bodytext,'',40).
1.49 albertel 167: $error.'</td></tr>'.
168: &Apache::edit::end_table();
1.37 albertel 169: }
170: if ($target eq "modified" ) {
171: $bodytext=$$parser[$#$parser]->get_text("/scriptlib");
1.65 matthew 172: $result=$token->[4].&Apache::edit::modifiedfield($token);
1.37 albertel 173: &Apache::lonxml::debug($result);
1.3 albertel 174: }
175: return $result;
176: }
177:
1.37 albertel 178: sub end_scriptlib {
1.40 albertel 179: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.49 albertel 180: my @result;
181: if ($target eq "edit" ) { $result[1]='no'; }
182: return @result;
1.37 albertel 183: }
1.4 albertel 184:
185: sub start_parserlib {
1.40 albertel 186: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.37 albertel 187: my $bodytext;
1.7 albertel 188: my $result ="";
1.37 albertel 189: my $error='';
1.78 albertel 190: if ($target eq 'web' || $target eq 'grade' || $target eq 'meta' ||
191: $target eq 'edit' || $target eq 'answer' || $target eq 'analyze') {
1.37 albertel 192: $bodytext=$$parser[$#$parser]->get_text("/parserlib");
193: $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
194: $$parstack[$#$parstack]);
195: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
196: $bodytext);
197: my $styletext=&Apache::lonnet::getfile($location);
198: #&Apache::lonxml::debug("found :$bodytext: in :$location: with :$styletext:");
199: if ($styletext == -1) {
200: if ($target eq 'edit') {
201: $error='</tr><tr><td>Errors</td><td colspan="2"><b> Unable to find <i>'.$location.'</i></b></td>'."\n";
202: } else {
203: &Apache::lonxml::error("<b> Unable to find <i>$location</i> for parserlib</b>");
204: return "";
205: }
206: }
207: %$style = ( %$style , &Apache::style::styleparser($target,$styletext));
1.18 albertel 208: }
1.13 albertel 209: if ($target eq "edit" ) {
1.37 albertel 210: $result=
1.49 albertel 211: &Apache::edit::tag_start($target,$token,'New Tag Definitions').
1.68 albertel 212: &Apache::edit::editline($token->[1],$bodytext,'',40).
1.49 albertel 213: $error.'</td></tr>'.
214: &Apache::edit::end_table();
1.37 albertel 215: }
216: if ($target eq "modified" ) {
217: $bodytext=$$parser[$#$parser]->get_text("/parserlib");
1.65 matthew 218: $result=$token->[4].&Apache::edit::modifiedfield($token);
1.37 albertel 219: &Apache::lonxml::debug($result);
1.7 albertel 220: }
221: return $result;
1.4 albertel 222: }
223:
224: sub end_parserlib {
1.40 albertel 225: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.49 albertel 226: my @result;
227: if ($target eq "edit" ) { $result[1]='no'; }
228: return @result;
1.6 albertel 229: }
230:
1.30 sakharuk 231: sub start_window {
1.40 albertel 232: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.20 sakharuk 233: my $result = '';
1.31 albertel 234: if ($target eq 'web') {
1.34 albertel 235: &Apache::lonxml::startredirection;
1.35 sakharuk 236: } elsif ($target eq 'tex') {
1.42 sakharuk 237: $result = '\unskip\footnote{';
1.35 sakharuk 238: }
1.19 sakharuk 239: return $result;
240: }
241:
1.30 sakharuk 242: sub end_window {
1.40 albertel 243: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.30 sakharuk 244: my $result;
1.31 albertel 245: if ($target eq 'web') {
1.34 albertel 246: my $output=&Apache::lonxml::endredirection;
247: $output =~ s/\"/\"\;/g;
1.35 sakharuk 248: $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>";
249: } elsif ($target eq 'tex') {
250: $result = '}';
1.30 sakharuk 251: } else {
1.35 sakharuk 252: $result = '';
1.30 sakharuk 253: }
1.20 sakharuk 254: return $result;
1.19 sakharuk 255: }
256:
1.6 albertel 257: sub start_import {
1.40 albertel 258: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.31 albertel 259: my $bodytext=$$parser[$#$parser]->get_text("/import");
1.7 albertel 260: my $result ="";
1.13 albertel 261:
262: $bodytext=Apache::run::evaluate($bodytext,$safeeval,$$parstack[$#$parstack]);
1.14 albertel 263:
1.78 albertel 264: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
265: $target eq 'tex' || $target eq 'analyze' ) {
1.46 albertel 266: # FIXME this probably needs to be smart about construction vs.
267: # non construction space.
268: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
269: my $file=&Apache::lonnet::getfile($location);
270: if ($file == -1) {
271: &Apache::lonxml::error("<b> Unable to find <i>$bodytext as $location</i> for import</b>");
272: return "";
273: }
1.13 albertel 274:
1.46 albertel 275: my $dir=$location;
276: $dir=~s:/[^/]*$::;
277: # &Apache::lonxml::debug("directory $dir $location file $file \n<b>END</b>\n");
1.57 albertel 278: my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
279: if (!$id) { $id=$Apache::lonxml::curdepth; }
280: push(@Apache::inputtags::import,$id);
1.46 albertel 281: &Apache::lonxml::newparser($parser,\$file,$dir);
1.56 albertel 282: } elsif ($target eq "edit" ) {
1.46 albertel 283: $result.=&Apache::edit::tag_start($target,$token);
1.68 albertel 284: $result.=&Apache::edit::editline($token->[1],$bodytext,'',40);
1.46 albertel 285: #FIXME this need to convert $bodytext to be a contruction space reference
286: #my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
287: #$result.="Click<a href=\"$location\">here</a> to edit<br />"
1.56 albertel 288: } elsif ($target eq 'modified') {
1.46 albertel 289: $bodytext=$$parser[$#$parser]->get_text("/import");
1.65 matthew 290: $result=$token->[4].&Apache::edit::modifiedfield($token);
1.46 albertel 291: &Apache::lonxml::debug($result);
1.56 albertel 292: } elsif ($target eq 'meta') {
1.57 albertel 293: my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
294: $result.='<import part="'.$Apache::inputtags::part;
295: if ($id) {
296: $result.='" id="'.$id;
297: }
298: $result.='">';
1.56 albertel 299: $result.=$bodytext;
300: $result.='</import>';
1.46 albertel 301: }
302: return $result;
1.6 albertel 303: }
304:
305: sub end_import {
1.69 albertel 306: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.57 albertel 307: pop(@Apache::inputtags::import);
1.69 albertel 308: my $result;
309: if ($target eq 'edit' ) { $result=&Apache::edit::end_table(); }
310: return $result;
1.1 albertel 311: }
1.42 sakharuk 312:
313: sub start_storetc {
1.43 albertel 314: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.42 sakharuk 315: my $result = '';
316: &Apache::lonxml::startredirection;
317: return $result;
318: }
319:
320: sub end_storetc {
1.43 albertel 321: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.42 sakharuk 322: my $result;
323: my $output=&Apache::lonxml::endredirection;
324: $output =~ s/\"/\"\;/g;
1.52 albertel 325: $result = '{\bf '.$output.'.}}\write\tcfile{\protect\tcpc{ '.$output.'.}{\the\value{relpage}}}';
326: return $result;
1.42 sakharuk 327: }
328:
329:
330: sub start_physnet {
1.45 sakharuk 331: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.50 www 332: my $bodytext = '/adm/includes/physnet.sty';
1.45 sakharuk 333: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
334: my $cbistyletext=&Apache::lonnet::getfile($location);
1.42 sakharuk 335:
1.45 sakharuk 336: %$style = (%$style,&Apache::style::styleparser($target,$cbistyletext));
1.59 albertel 337: $$parser['-1']->unget_token($token);
338: # if ( defined($$style{'physnet'}) ) {
339: # &Apache::lonxml::newparser($parser,\$$style{'physnet'});
340: # }
1.45 sakharuk 341: return "";
342: }
1.42 sakharuk 343:
1.45 sakharuk 344: sub end_physnet {
1.47 albertel 345: return '';
1.42 sakharuk 346: }
1.62 albertel 347:
348: sub start_standalone {
349: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.75 albertel 350: my $result='';
351: if ($target eq 'web' ) {
352: if ( $ENV{'request.course.id'} ) {
353: my $inside = &Apache::lonxml::get_all_text("/standalone",$$parser[-1]);
354: } else {
355: $result='<table bgcolor="#E1E1E1" border="2"><tr><td>';
356: }
1.62 albertel 357: }
1.75 albertel 358: return $result;
1.62 albertel 359: }
360:
361: sub end_standalone {
1.75 albertel 362: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
363: my $result='';
364: if ($target eq 'web' ) {
365: if ( $ENV{'request.course.id'} ) {
366: } else {
367: $result='</td></tr></table>';
368: }
369: }
370: return $result;
1.74 albertel 371: }
372:
373: sub start_comment {
374: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
375: my $result='';
376: if ($target eq 'edit') {
377: $result=&Apache::edit::tag_start($target,$token);
378: my $bodytext=&Apache::lonxml::get_all_text("/comment",$$parser[$#$parser]);
1.77 albertel 379: $result.=&Apache::edit::editfield($token->[1],$bodytext,'',80,4)
1.74 albertel 380: } elsif ( $target eq 'modified') {
381: $result=$token->[4].&Apache::edit::modifiedfield($token);
1.78 albertel 382: } elsif ( $target eq 'web' || $target eq 'tex' || $target eq 'grade' ||
383: $target eq 'answer' || $target eq 'meta' || $target eq 'analyze') {
1.74 albertel 384: #normally throw away comments
385: my $bodytext=&Apache::lonxml::get_all_text("/comment",$$parser[$#$parser]);
386: }
387: return $result;
388: }
389:
390: sub end_comment {
391: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
392: if ($target eq 'edit' ) { return &Apache::edit::end_table(); }
1.62 albertel 393: return '';
394: }
395:
1.42 sakharuk 396:
1.47 albertel 397: sub xmlparse {
398: my ($string) = @_;
399: &Apache::lonxml::debug("Got $string");
400: my ($target,$token,$tagstack,$parstack,$oldparser,$safeeval,$style)=
401: @Apache::scripttag::parser_env;
402: my @parser;
403: &Apache::lonxml::newparser(\@parser,\$string);
404: my $result=&Apache::lonxml::inner_xmlparse($target,$tagstack,
405: $parstack,\@parser,
406: $safeeval,$style);
407: return $result;
408: }
1.3 albertel 409:
1.1 albertel 410: 1;
411: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>