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