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