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