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