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