Annotation of loncom/xml/scripttag.pm, revision 1.65
1.1 albertel 1: # The LearningOnline Network with CAPA
2: # <script> definiton
1.58 www 3: #
1.65 ! matthew 4: # $Id: scripttag.pm,v 1.64 2002/01/17 16:51:07 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.63 albertel 46: 'standalone'));
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' ||
60: $target eq 'grade' || $target eq 'answer') {
1.28 albertel 61: &Apache::run::run($bodytext,$safeeval);
1.60 albertel 62: if (($target eq 'answer') && ($Apache::lonhomework::viewgrades == 'F')) {
1.53 albertel 63: $Apache::lonxml::evaluate--;
64: $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>";
65: my $listing= &Apache::run::dump($target,$safeeval);
66: $listing =~ s/\n/\\n/g;
1.60 albertel 67: $listing =~ s/\"/"/g;
68: $listing =~ s/\'/’/g;
69: $listing =~ s/</</g;
70: $listing =~ s/>/>/g;
71: $listing =~ s/&/&/g;
1.53 albertel 72: $result.=$listing;
1.55 albertel 73: $result.= "</pre></body></html>\');newWindow.document.close();void(0);\">Script Vars</a><br />";
1.53 albertel 74: }
1.28 albertel 75: } elsif ($target eq "edit" ) {
1.41 albertel 76: #&Apache::run::run($bodytext,$safeeval);
77: #$result="<br /> <$token->[1]> output: <br />$bodytext<br />Source:<br />";
78: $result=&Apache::edit::tag_start($target,$token,'Script');
79: $result.=&Apache::edit::editfield($token->[1],$bodytext,'',50,4);
1.17 albertel 80: }
81: } else {
1.28 albertel 82: if ($target ne "meta") { $result = $token->[4]; }
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.55 albertel 97: } elsif (($ENV{'request.state'} eq 'construct') && ($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' ||
112: $target eq 'grade' || $target eq 'answer') {
1.48 albertel 113: $result=&Apache::run::run($bodytext,$safeeval);
1.55 albertel 114: if ($target eq 'grade' || $target eq 'answer' ) {
1.48 albertel 115: $result=''; # grade should produce no output
116: }
117: } elsif ($target eq "edit" ) {
118: #$result =
119: # "<br /> <$token->[1]> output: <br />$bodytext<br />Source:<br />";
120: #$result.=&Apache::edit::editfield($token->[1],$bodytext,'',40,1);
121: $result=&Apache::edit::tag_start($target,$token,'Script With Display');
122: $result.=&Apache::edit::editfield($token->[1],$bodytext,'',40,1)
1.24 albertel 123: }
124: return $result;
125: }
126:
127: sub end_display {
1.48 albertel 128: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
129: if ($target eq 'edit' ) { return &Apache::edit::end_table(); }
130: return '';
1.1 albertel 131: }
1.3 albertel 132:
133: sub start_scriptlib {
1.40 albertel 134: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.37 albertel 135: my $bodytext;
136: my $result ='';
137: my $error='';
138:
1.51 albertel 139: if ($target eq 'web' || $target eq 'grade' || $target eq 'meta' || $target eq 'edit' || $target eq 'answer') {
1.37 albertel 140: $bodytext=$$parser[$#$parser]->get_text("/scriptlib");
141: $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
142: $$parstack[$#$parstack]);
143: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
144: $bodytext);
145: my $script=&Apache::lonnet::getfile($location);
146: if ($script == -1) {
147: if ($target eq 'edit') {
148: $error='</tr><tr><td>Errors</td><td colspan="2"><b> Unable to find <i>'.$location.'</i></b></td>'."\n";
149: } else {
150: &Apache::lonxml::error("<b> Unable to find <i>$location</i> for scriptlib</b>");
151: return "";
152: }
153: }
154: &Apache::run::run($script,$safeeval);
155: #&Apache::lonxml::debug("ran $bodytext:<br />".&Apache::lonnet::getfile($bodytext)."<br />");
1.18 albertel 156: }
1.13 albertel 157: if ($target eq "edit" ) {
1.37 albertel 158: $result=
1.49 albertel 159: &Apache::edit::tag_start($target,$token,'New Script Functions').
160: &Apache::edit::editfield($token->[1],$bodytext,'',40,1).
161: $error.'</td></tr>'.
162: &Apache::edit::end_table();
1.37 albertel 163: }
164: if ($target eq "modified" ) {
165: $bodytext=$$parser[$#$parser]->get_text("/scriptlib");
1.65 ! matthew 166: $result=$token->[4].&Apache::edit::modifiedfield($token);
1.37 albertel 167: &Apache::lonxml::debug($result);
1.3 albertel 168: }
169: return $result;
170: }
171:
1.37 albertel 172: sub end_scriptlib {
1.40 albertel 173: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.49 albertel 174: my @result;
175: if ($target eq "edit" ) { $result[1]='no'; }
176: return @result;
1.37 albertel 177: }
1.4 albertel 178:
179: sub start_parserlib {
1.40 albertel 180: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.37 albertel 181: my $bodytext;
1.7 albertel 182: my $result ="";
1.37 albertel 183: my $error='';
1.51 albertel 184: if ($target eq 'web' || $target eq 'grade' || $target eq 'meta' || $target eq 'edit' || $target eq 'answer') {
1.37 albertel 185: $bodytext=$$parser[$#$parser]->get_text("/parserlib");
186: $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
187: $$parstack[$#$parstack]);
188: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
189: $bodytext);
190: my $styletext=&Apache::lonnet::getfile($location);
191: #&Apache::lonxml::debug("found :$bodytext: in :$location: with :$styletext:");
192: if ($styletext == -1) {
193: if ($target eq 'edit') {
194: $error='</tr><tr><td>Errors</td><td colspan="2"><b> Unable to find <i>'.$location.'</i></b></td>'."\n";
195: } else {
196: &Apache::lonxml::error("<b> Unable to find <i>$location</i> for parserlib</b>");
197: return "";
198: }
199: }
200: %$style = ( %$style , &Apache::style::styleparser($target,$styletext));
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 Tag Definitions').
205: &Apache::edit::editfield($token->[1],$bodytext,'',40,1).
206: $error.'</td></tr>'.
207: &Apache::edit::end_table();
1.37 albertel 208: }
209: if ($target eq "modified" ) {
210: $bodytext=$$parser[$#$parser]->get_text("/parserlib");
1.65 ! matthew 211: $result=$token->[4].&Apache::edit::modifiedfield($token);
1.37 albertel 212: &Apache::lonxml::debug($result);
1.7 albertel 213: }
214: return $result;
1.4 albertel 215: }
216:
217: sub end_parserlib {
1.40 albertel 218: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.49 albertel 219: my @result;
220: if ($target eq "edit" ) { $result[1]='no'; }
221: return @result;
1.6 albertel 222: }
223:
1.30 sakharuk 224: sub start_window {
1.40 albertel 225: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.20 sakharuk 226: my $result = '';
1.31 albertel 227: if ($target eq 'web') {
1.34 albertel 228: &Apache::lonxml::startredirection;
1.35 sakharuk 229: } elsif ($target eq 'tex') {
1.42 sakharuk 230: $result = '\unskip\footnote{';
1.35 sakharuk 231: }
1.19 sakharuk 232: return $result;
233: }
234:
1.30 sakharuk 235: sub end_window {
1.40 albertel 236: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.30 sakharuk 237: my $result;
1.31 albertel 238: if ($target eq 'web') {
1.34 albertel 239: my $output=&Apache::lonxml::endredirection;
240: $output =~ s/\"/\"\;/g;
1.35 sakharuk 241: $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>";
242: } elsif ($target eq 'tex') {
243: $result = '}';
1.30 sakharuk 244: } else {
1.35 sakharuk 245: $result = '';
1.30 sakharuk 246: }
1.20 sakharuk 247: return $result;
1.19 sakharuk 248: }
249:
1.6 albertel 250: sub start_import {
1.40 albertel 251: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.31 albertel 252: my $bodytext=$$parser[$#$parser]->get_text("/import");
1.7 albertel 253: my $result ="";
1.13 albertel 254:
255: $bodytext=Apache::run::evaluate($bodytext,$safeeval,$$parstack[$#$parstack]);
1.14 albertel 256:
1.51 albertel 257: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer') {
1.46 albertel 258: # FIXME this probably needs to be smart about construction vs.
259: # non construction space.
260: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
261: my $file=&Apache::lonnet::getfile($location);
262: if ($file == -1) {
263: &Apache::lonxml::error("<b> Unable to find <i>$bodytext as $location</i> for import</b>");
264: return "";
265: }
1.13 albertel 266:
1.46 albertel 267: my $dir=$location;
268: $dir=~s:/[^/]*$::;
269: # &Apache::lonxml::debug("directory $dir $location file $file \n<b>END</b>\n");
1.57 albertel 270: my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
271: if (!$id) { $id=$Apache::lonxml::curdepth; }
272: push(@Apache::inputtags::import,$id);
1.46 albertel 273: &Apache::lonxml::newparser($parser,\$file,$dir);
1.56 albertel 274: } elsif ($target eq "edit" ) {
1.46 albertel 275: $result.=&Apache::edit::tag_start($target,$token);
1.38 albertel 276: $result.=&Apache::edit::editfield($token->[1],$bodytext,'',40,1);
1.46 albertel 277: #FIXME this need to convert $bodytext to be a contruction space reference
278: #my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
279: #$result.="Click<a href=\"$location\">here</a> to edit<br />"
1.56 albertel 280: } elsif ($target eq 'modified') {
1.46 albertel 281: $bodytext=$$parser[$#$parser]->get_text("/import");
1.65 ! matthew 282: $result=$token->[4].&Apache::edit::modifiedfield($token);
1.46 albertel 283: &Apache::lonxml::debug($result);
1.56 albertel 284: } elsif ($target eq 'meta') {
1.57 albertel 285: my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
286: $result.='<import part="'.$Apache::inputtags::part;
287: if ($id) {
288: $result.='" id="'.$id;
289: }
290: $result.='">';
1.56 albertel 291: $result.=$bodytext;
292: $result.='</import>';
1.46 albertel 293: }
294: return $result;
1.6 albertel 295: }
296:
297: sub end_import {
1.57 albertel 298: pop(@Apache::inputtags::import);
1.45 sakharuk 299: return '';
1.1 albertel 300: }
1.42 sakharuk 301:
302: sub start_storetc {
1.43 albertel 303: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.42 sakharuk 304: my $result = '';
305: &Apache::lonxml::startredirection;
306: return $result;
307: }
308:
309: sub end_storetc {
1.43 albertel 310: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.42 sakharuk 311: my $result;
312: my $output=&Apache::lonxml::endredirection;
313: $output =~ s/\"/\"\;/g;
1.52 albertel 314: $result = '{\bf '.$output.'.}}\write\tcfile{\protect\tcpc{ '.$output.'.}{\the\value{relpage}}}';
315: return $result;
1.42 sakharuk 316: }
317:
318:
319: sub start_physnet {
1.45 sakharuk 320: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.50 www 321: my $bodytext = '/adm/includes/physnet.sty';
1.45 sakharuk 322: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
323: my $cbistyletext=&Apache::lonnet::getfile($location);
1.42 sakharuk 324:
1.45 sakharuk 325: %$style = (%$style,&Apache::style::styleparser($target,$cbistyletext));
1.59 albertel 326: $$parser['-1']->unget_token($token);
327: # if ( defined($$style{'physnet'}) ) {
328: # &Apache::lonxml::newparser($parser,\$$style{'physnet'});
329: # }
1.45 sakharuk 330: return "";
331: }
1.42 sakharuk 332:
1.45 sakharuk 333: sub end_physnet {
1.47 albertel 334: return '';
1.42 sakharuk 335: }
1.62 albertel 336:
337: sub start_standalone {
338: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.64 albertel 339: if ($target eq 'web' && $ENV{'request.course.id'} ) {
1.62 albertel 340: my $inside = &Apache::lonxml::get_all_text("/standalone",$$parser[-1]);
341: }
342: return '';
343: }
344:
345: sub end_standalone {
346: return '';
347: }
348:
1.42 sakharuk 349:
1.47 albertel 350: sub xmlparse {
351: my ($string) = @_;
352: &Apache::lonxml::debug("Got $string");
353: my ($target,$token,$tagstack,$parstack,$oldparser,$safeeval,$style)=
354: @Apache::scripttag::parser_env;
355: my @parser;
356: &Apache::lonxml::newparser(\@parser,\$string);
357: my $result=&Apache::lonxml::inner_xmlparse($target,$tagstack,
358: $parstack,\@parser,
359: $safeeval,$style);
360: return $result;
361: }
1.3 albertel 362:
1.1 albertel 363: 1;
364: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>