1: # The LearningOnline Network with CAPA
2: # <script> definiton
3: # 2/21 Guy
4:
5: package Apache::scripttag;
6:
7: use strict;
8: use Apache::lonnet;
9: use Apache::style;
10:
11: #Globals
12: # this used to pass around the standard callsub arguments to a tag func
13: # so xmlparse can reenter the inner_xmlparse loop.
14:
15: @Apache::scripttag::parser_env = ();
16: sub BEGIN {
17: &Apache::lonxml::register('Apache::scripttag',('script','scriptlib',
18: 'parserlib','import',
19: 'window','display',
20: 'storetc','physnet'));
21: }
22:
23: sub start_script {
24: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
25: @Apache::scripttag::parser_env = @_;
26: my $result='';
27: my $type= &Apache::lonxml::get_param('type',$parstack,$safeeval);
28: &Apache::lonxml::debug("found type of $type");
29: if ($type eq "loncapa/perl") {
30: my $bodytext=&Apache::lonxml::get_all_text("/script",$$parser[$#$parser]);
31: if ( $target eq "modified" ) {
32: $result=$token->[4].&Apache::edit::modifiedfield();
33: } elsif ( $target eq "web" || $target eq "grade" ) {
34: &Apache::run::run($bodytext,$safeeval);
35: } elsif ($target eq "edit" ) {
36: #&Apache::run::run($bodytext,$safeeval);
37: #$result="<br /> <$token->[1]> output: <br />$bodytext<br />Source:<br />";
38: $result=&Apache::edit::tag_start($target,$token,'Script');
39: $result.=&Apache::edit::editfield($token->[1],$bodytext,'',50,4);
40: }
41: } else {
42: if ($target ne "meta") { $result = $token->[4]; }
43: }
44: return $result;
45: }
46:
47: sub end_script {
48: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
49: if ( $target eq "meta" ) { return ''; }
50: my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval);
51: my $result='';
52: #other script blocks need to survive
53: if ($type ne "loncapa/perl") { return $token->[2]; }
54: if ($target eq 'edit' ) { return &Apache::edit::end_table(); }
55: return '';
56: }
57:
58: sub start_display {
59: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
60:
61: my $bodytext=&Apache::lonxml::get_all_text("/display",$$parser[$#$parser]);
62:
63: if ( $target eq "modified" ) {
64: }
65: my $result=&Apache::run::run($bodytext,$safeeval);
66: if ($target eq 'grade' ) {
67: # grade should produce no output
68: $result='';
69: }
70: if ($target eq "edit" ) {
71: $result =
72: "<br /> <$token->[1]> output: <br />$bodytext<br />Source:<br />";
73: $result.=&Apache::edit::editfield($token->[1],$bodytext,'',40,1);
74: }
75: return $result;
76: }
77:
78: sub end_display {
79: return ''
80: }
81:
82: sub start_scriptlib {
83: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
84: my $bodytext;
85: my $result ='';
86: my $error='';
87:
88: if ($target eq 'web' || $target eq 'grade' || $target eq 'meta' || $target eq 'edit') {
89: $bodytext=$$parser[$#$parser]->get_text("/scriptlib");
90: $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
91: $$parstack[$#$parstack]);
92: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
93: $bodytext);
94: my $script=&Apache::lonnet::getfile($location);
95: if ($script == -1) {
96: if ($target eq 'edit') {
97: $error='</tr><tr><td>Errors</td><td colspan="2"><b> Unable to find <i>'.$location.'</i></b></td>'."\n";
98: } else {
99: &Apache::lonxml::error("<b> Unable to find <i>$location</i> for scriptlib</b>");
100: return "";
101: }
102: }
103: &Apache::run::run($script,$safeeval);
104: #&Apache::lonxml::debug("ran $bodytext:<br />".&Apache::lonnet::getfile($bodytext)."<br />");
105: }
106: if ($target eq "edit" ) {
107: $result=
108: &Apache::edit::tag_start($target,$token).
109: &Apache::edit::editfield($token->[1],$bodytext,'New Script Functions',40,1).
110: $error;
111: }
112: if ($target eq "modified" ) {
113: $bodytext=$$parser[$#$parser]->get_text("/scriptlib");
114: $result=&Apache::edit::modifiedfield($token);
115: &Apache::lonxml::debug($result);
116: }
117: return $result;
118: }
119:
120: sub end_scriptlib {
121: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
122: my $result='';
123: # if ($target eq "edit" ) { $result=" "; }
124: return $result;
125: }
126:
127: sub start_parserlib {
128: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
129: my $bodytext;
130: my $result ="";
131: my $error='';
132: if ($target eq 'web' || $target eq 'grade' || $target eq 'meta' || $target eq 'edit') {
133: $bodytext=$$parser[$#$parser]->get_text("/parserlib");
134: $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
135: $$parstack[$#$parstack]);
136: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
137: $bodytext);
138: my $styletext=&Apache::lonnet::getfile($location);
139: #&Apache::lonxml::debug("found :$bodytext: in :$location: with :$styletext:");
140: if ($styletext == -1) {
141: if ($target eq 'edit') {
142: $error='</tr><tr><td>Errors</td><td colspan="2"><b> Unable to find <i>'.$location.'</i></b></td>'."\n";
143: } else {
144: &Apache::lonxml::error("<b> Unable to find <i>$location</i> for parserlib</b>");
145: return "";
146: }
147: }
148: %$style = ( %$style , &Apache::style::styleparser($target,$styletext));
149: }
150: if ($target eq "edit" ) {
151: $result=
152: &Apache::edit::tag_start($target,$token).
153: &Apache::edit::editfield($token->[1],$bodytext,"New Tag Definitions",40,1).
154: $error;
155: }
156: if ($target eq "modified" ) {
157: $bodytext=$$parser[$#$parser]->get_text("/parserlib");
158: $result=&Apache::edit::modifiedfield($token);
159: &Apache::lonxml::debug($result);
160: }
161: return $result;
162: }
163:
164: sub end_parserlib {
165: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
166: my $result='';
167: # if ($target eq "edit" ) {
168: # $result=&Apache::edit::tag_end($target,$token);
169: # }
170: return $result;
171: }
172:
173: sub start_window {
174: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
175: my $result = '';
176: if ($target eq 'web') {
177: &Apache::lonxml::startredirection;
178: } elsif ($target eq 'tex') {
179: $result = '\unskip\footnote{';
180: }
181: return $result;
182: }
183:
184: sub end_window {
185: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
186: my $result;
187: if ($target eq 'web') {
188: my $output=&Apache::lonxml::endredirection;
189: $output =~ s/\"/\"\;/g;
190: $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>";
191: } elsif ($target eq 'tex') {
192: $result = '}';
193: } else {
194: $result = '';
195: }
196: return $result;
197: }
198:
199: sub start_import {
200: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
201: my $bodytext=$$parser[$#$parser]->get_text("/import");
202: my $result ="";
203:
204: $bodytext=Apache::run::evaluate($bodytext,$safeeval,$$parstack[$#$parstack]);
205:
206: if ($target eq 'web' || $target eq 'grade') {
207: # FIXME this probably needs to be smart about construction vs.
208: # non construction space.
209: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
210: my $file=&Apache::lonnet::getfile($location);
211: if ($file == -1) {
212: &Apache::lonxml::error("<b> Unable to find <i>$bodytext as $location</i> for import</b>");
213: return "";
214: }
215:
216: my $dir=$location;
217: $dir=~s:/[^/]*$::;
218: # &Apache::lonxml::debug("directory $dir $location file $file \n<b>END</b>\n");
219: &Apache::lonxml::newparser($parser,\$file,$dir);
220: }
221: if ($target eq "edit" ) {
222: $result.=&Apache::edit::tag_start($target,$token);
223: $result.=&Apache::edit::editfield($token->[1],$bodytext,'',40,1);
224: #FIXME this need to convert $bodytext to be a contruction space reference
225: #my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
226: #$result.="Click<a href=\"$location\">here</a> to edit<br />"
227: }
228: if ($target eq 'modified') {
229: $bodytext=$$parser[$#$parser]->get_text("/import");
230: $result=&Apache::edit::modifiedfield($token);
231: &Apache::lonxml::debug($result);
232: }
233: return $result;
234: }
235:
236: sub end_import {
237: return '';
238: }
239:
240: sub start_storetc {
241: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
242: my $result = '';
243: &Apache::lonxml::startredirection;
244: return $result;
245: }
246:
247: sub end_storetc {
248: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
249: my $result;
250: my $output=&Apache::lonxml::endredirection;
251: $output =~ s/\"/\"\;/g;
252: $result = "{\bf $output.}}\write\tcfile{\protect\tcpc{ $output.}{\the\value{relpage}}}";
253: return $result;
254: }
255:
256:
257: sub start_physnet {
258: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
259: my $bodytext = '/msu/physnet/cbi.sty';
260: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
261: my $cbistyletext=&Apache::lonnet::getfile($location);
262:
263: %$style = (%$style,&Apache::style::styleparser($target,$cbistyletext));
264: if ( defined($$style{'physnet'}) ) {
265: &Apache::lonxml::newparser($parser,\$$style{'physnet'});
266: }
267: return "";
268: }
269:
270: sub end_physnet {
271: return '';
272: }
273:
274: sub xmlparse {
275: my ($string) = @_;
276: &Apache::lonxml::debug("Got $string");
277: my ($target,$token,$tagstack,$parstack,$oldparser,$safeeval,$style)=
278: @Apache::scripttag::parser_env;
279: my @parser;
280: &Apache::lonxml::newparser(\@parser,\$string);
281: my $result=&Apache::lonxml::inner_xmlparse($target,$tagstack,
282: $parstack,\@parser,
283: $safeeval,$style);
284: return $result;
285: }
286:
287: 1;
288: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>