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