Annotation of loncom/xml/scripttag.pm, revision 1.82
1.1 albertel 1: # The LearningOnline Network with CAPA
2: # <script> definiton
1.58 www 3: #
1.82 ! albertel 4: # $Id: scripttag.pm,v 1.81 2002/11/06 23:05:02 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.68 albertel 179: &Apache::edit::editline($token->[1],$bodytext,'',40).
1.49 albertel 180: $error.'</td></tr>'.
181: &Apache::edit::end_table();
1.37 albertel 182: }
183: if ($target eq "modified" ) {
184: $bodytext=$$parser[$#$parser]->get_text("/scriptlib");
1.65 matthew 185: $result=$token->[4].&Apache::edit::modifiedfield($token);
1.37 albertel 186: &Apache::lonxml::debug($result);
1.3 albertel 187: }
188: return $result;
189: }
190:
1.37 albertel 191: sub end_scriptlib {
1.40 albertel 192: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.49 albertel 193: my @result;
194: if ($target eq "edit" ) { $result[1]='no'; }
195: return @result;
1.37 albertel 196: }
1.4 albertel 197:
198: sub start_parserlib {
1.40 albertel 199: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.37 albertel 200: my $bodytext;
1.7 albertel 201: my $result ="";
1.37 albertel 202: my $error='';
1.78 albertel 203: if ($target eq 'web' || $target eq 'grade' || $target eq 'meta' ||
204: $target eq 'edit' || $target eq 'answer' || $target eq 'analyze') {
1.37 albertel 205: $bodytext=$$parser[$#$parser]->get_text("/parserlib");
206: $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
207: $$parstack[$#$parstack]);
208: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
209: $bodytext);
210: my $styletext=&Apache::lonnet::getfile($location);
211: #&Apache::lonxml::debug("found :$bodytext: in :$location: with :$styletext:");
212: if ($styletext == -1) {
213: if ($target eq 'edit') {
214: $error='</tr><tr><td>Errors</td><td colspan="2"><b> Unable to find <i>'.$location.'</i></b></td>'."\n";
215: } else {
216: &Apache::lonxml::error("<b> Unable to find <i>$location</i> for parserlib</b>");
217: return "";
218: }
219: }
220: %$style = ( %$style , &Apache::style::styleparser($target,$styletext));
1.18 albertel 221: }
1.13 albertel 222: if ($target eq "edit" ) {
1.37 albertel 223: $result=
1.49 albertel 224: &Apache::edit::tag_start($target,$token,'New Tag Definitions').
1.68 albertel 225: &Apache::edit::editline($token->[1],$bodytext,'',40).
1.49 albertel 226: $error.'</td></tr>'.
227: &Apache::edit::end_table();
1.37 albertel 228: }
229: if ($target eq "modified" ) {
230: $bodytext=$$parser[$#$parser]->get_text("/parserlib");
1.65 matthew 231: $result=$token->[4].&Apache::edit::modifiedfield($token);
1.37 albertel 232: &Apache::lonxml::debug($result);
1.7 albertel 233: }
234: return $result;
1.4 albertel 235: }
236:
237: sub end_parserlib {
1.40 albertel 238: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.49 albertel 239: my @result;
240: if ($target eq "edit" ) { $result[1]='no'; }
241: return @result;
1.6 albertel 242: }
243:
1.30 sakharuk 244: sub start_window {
1.40 albertel 245: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.20 sakharuk 246: my $result = '';
1.31 albertel 247: if ($target eq 'web') {
1.34 albertel 248: &Apache::lonxml::startredirection;
1.35 sakharuk 249: } elsif ($target eq 'tex') {
1.42 sakharuk 250: $result = '\unskip\footnote{';
1.35 sakharuk 251: }
1.19 sakharuk 252: return $result;
253: }
254:
1.30 sakharuk 255: sub end_window {
1.40 albertel 256: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.30 sakharuk 257: my $result;
1.31 albertel 258: if ($target eq 'web') {
1.34 albertel 259: my $output=&Apache::lonxml::endredirection;
260: $output =~ s/\"/\"\;/g;
1.35 sakharuk 261: $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>";
262: } elsif ($target eq 'tex') {
263: $result = '}';
1.30 sakharuk 264: } else {
1.35 sakharuk 265: $result = '';
1.30 sakharuk 266: }
1.20 sakharuk 267: return $result;
1.19 sakharuk 268: }
269:
1.6 albertel 270: sub start_import {
1.40 albertel 271: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.31 albertel 272: my $bodytext=$$parser[$#$parser]->get_text("/import");
1.7 albertel 273: my $result ="";
1.13 albertel 274:
275: $bodytext=Apache::run::evaluate($bodytext,$safeeval,$$parstack[$#$parstack]);
1.14 albertel 276:
1.78 albertel 277: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
278: $target eq 'tex' || $target eq 'analyze' ) {
1.46 albertel 279: # FIXME this probably needs to be smart about construction vs.
280: # non construction space.
281: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
282: my $file=&Apache::lonnet::getfile($location);
283: if ($file == -1) {
284: &Apache::lonxml::error("<b> Unable to find <i>$bodytext as $location</i> for import</b>");
285: return "";
286: }
1.13 albertel 287:
1.46 albertel 288: my $dir=$location;
289: $dir=~s:/[^/]*$::;
290: # &Apache::lonxml::debug("directory $dir $location file $file \n<b>END</b>\n");
1.57 albertel 291: my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
292: if (!$id) { $id=$Apache::lonxml::curdepth; }
293: push(@Apache::inputtags::import,$id);
1.46 albertel 294: &Apache::lonxml::newparser($parser,\$file,$dir);
1.56 albertel 295: } elsif ($target eq "edit" ) {
1.46 albertel 296: $result.=&Apache::edit::tag_start($target,$token);
1.68 albertel 297: $result.=&Apache::edit::editline($token->[1],$bodytext,'',40);
1.46 albertel 298: #FIXME this need to convert $bodytext to be a contruction space reference
299: #my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
300: #$result.="Click<a href=\"$location\">here</a> to edit<br />"
1.56 albertel 301: } elsif ($target eq 'modified') {
1.46 albertel 302: $bodytext=$$parser[$#$parser]->get_text("/import");
1.65 matthew 303: $result=$token->[4].&Apache::edit::modifiedfield($token);
1.46 albertel 304: &Apache::lonxml::debug($result);
1.56 albertel 305: } elsif ($target eq 'meta') {
1.57 albertel 306: my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
307: $result.='<import part="'.$Apache::inputtags::part;
308: if ($id) {
309: $result.='" id="'.$id;
310: }
311: $result.='">';
1.56 albertel 312: $result.=$bodytext;
313: $result.='</import>';
1.46 albertel 314: }
315: return $result;
1.6 albertel 316: }
317:
318: sub end_import {
1.69 albertel 319: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.57 albertel 320: pop(@Apache::inputtags::import);
1.69 albertel 321: my $result;
322: if ($target eq 'edit' ) { $result=&Apache::edit::end_table(); }
323: return $result;
1.1 albertel 324: }
1.42 sakharuk 325:
326: sub start_storetc {
1.43 albertel 327: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.42 sakharuk 328: my $result = '';
329: &Apache::lonxml::startredirection;
330: return $result;
331: }
332:
333: sub end_storetc {
1.43 albertel 334: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.42 sakharuk 335: my $result;
336: my $output=&Apache::lonxml::endredirection;
337: $output =~ s/\"/\"\;/g;
1.52 albertel 338: $result = '{\bf '.$output.'.}}\write\tcfile{\protect\tcpc{ '.$output.'.}{\the\value{relpage}}}';
339: return $result;
1.42 sakharuk 340: }
341:
342:
343: sub start_physnet {
1.45 sakharuk 344: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.50 www 345: my $bodytext = '/adm/includes/physnet.sty';
1.45 sakharuk 346: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
347: my $cbistyletext=&Apache::lonnet::getfile($location);
1.42 sakharuk 348:
1.45 sakharuk 349: %$style = (%$style,&Apache::style::styleparser($target,$cbistyletext));
1.59 albertel 350: $$parser['-1']->unget_token($token);
351: # if ( defined($$style{'physnet'}) ) {
352: # &Apache::lonxml::newparser($parser,\$$style{'physnet'});
353: # }
1.45 sakharuk 354: return "";
355: }
1.42 sakharuk 356:
1.45 sakharuk 357: sub end_physnet {
1.47 albertel 358: return '';
1.42 sakharuk 359: }
1.62 albertel 360:
361: sub start_standalone {
362: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.75 albertel 363: my $result='';
364: if ($target eq 'web' ) {
365: if ( $ENV{'request.course.id'} ) {
366: my $inside = &Apache::lonxml::get_all_text("/standalone",$$parser[-1]);
367: } else {
368: $result='<table bgcolor="#E1E1E1" border="2"><tr><td>';
369: }
1.62 albertel 370: }
1.75 albertel 371: return $result;
1.62 albertel 372: }
373:
374: sub end_standalone {
1.75 albertel 375: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
376: my $result='';
377: if ($target eq 'web' ) {
378: if ( $ENV{'request.course.id'} ) {
379: } else {
380: $result='</td></tr></table>';
381: }
382: }
383: return $result;
1.74 albertel 384: }
385:
386: sub start_comment {
387: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
388: my $result='';
389: if ($target eq 'edit') {
390: $result=&Apache::edit::tag_start($target,$token);
391: my $bodytext=&Apache::lonxml::get_all_text("/comment",$$parser[$#$parser]);
1.77 albertel 392: $result.=&Apache::edit::editfield($token->[1],$bodytext,'',80,4)
1.74 albertel 393: } elsif ( $target eq 'modified') {
394: $result=$token->[4].&Apache::edit::modifiedfield($token);
1.78 albertel 395: } elsif ( $target eq 'web' || $target eq 'tex' || $target eq 'grade' ||
396: $target eq 'answer' || $target eq 'meta' || $target eq 'analyze') {
1.74 albertel 397: #normally throw away comments
398: my $bodytext=&Apache::lonxml::get_all_text("/comment",$$parser[$#$parser]);
399: }
400: return $result;
401: }
402:
403: sub end_comment {
404: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
405: if ($target eq 'edit' ) { return &Apache::edit::end_table(); }
1.62 albertel 406: return '';
407: }
408:
1.42 sakharuk 409:
1.47 albertel 410: sub xmlparse {
411: my ($string) = @_;
412: &Apache::lonxml::debug("Got $string");
413: my ($target,$token,$tagstack,$parstack,$oldparser,$safeeval,$style)=
414: @Apache::scripttag::parser_env;
415: my @parser;
416: &Apache::lonxml::newparser(\@parser,\$string);
417: my $result=&Apache::lonxml::inner_xmlparse($target,$tagstack,
418: $parstack,\@parser,
419: $safeeval,$style);
420: return $result;
421: }
1.3 albertel 422:
1.1 albertel 423: 1;
424: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>