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