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 $result;
62: my $bodytext=&Apache::lonxml::get_all_text("/display",$$parser[$#$parser]);
63:
64: if ( $target eq "modified" ) {
65: $result=$token->[4].&Apache::edit::modifiedfield();
66: } elsif ( $target eq "web" || $target eq "grade" ) {
67: $result=&Apache::run::run($bodytext,$safeeval);
68: if ($target eq 'grade' ) {
69: $result=''; # grade should produce no output
70: }
71: } elsif ($target eq "edit" ) {
72: #$result =
73: # "<br /> <$token->[1]> output: <br />$bodytext<br />Source:<br />";
74: #$result.=&Apache::edit::editfield($token->[1],$bodytext,'',40,1);
75: $result=&Apache::edit::tag_start($target,$token,'Script With Display');
76: $result.=&Apache::edit::editfield($token->[1],$bodytext,'',40,1)
77: }
78: return $result;
79: }
80:
81: sub end_display {
82: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
83: if ($target eq 'edit' ) { return &Apache::edit::end_table(); }
84: return '';
85: }
86:
87: sub start_scriptlib {
88: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
89: my $bodytext;
90: my $result ='';
91: my $error='';
92:
93: if ($target eq 'web' || $target eq 'grade' || $target eq 'meta' || $target eq 'edit') {
94: $bodytext=$$parser[$#$parser]->get_text("/scriptlib");
95: $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
96: $$parstack[$#$parstack]);
97: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
98: $bodytext);
99: my $script=&Apache::lonnet::getfile($location);
100: if ($script == -1) {
101: if ($target eq 'edit') {
102: $error='</tr><tr><td>Errors</td><td colspan="2"><b> Unable to find <i>'.$location.'</i></b></td>'."\n";
103: } else {
104: &Apache::lonxml::error("<b> Unable to find <i>$location</i> for scriptlib</b>");
105: return "";
106: }
107: }
108: &Apache::run::run($script,$safeeval);
109: #&Apache::lonxml::debug("ran $bodytext:<br />".&Apache::lonnet::getfile($bodytext)."<br />");
110: }
111: if ($target eq "edit" ) {
112: $result=
113: &Apache::edit::tag_start($target,$token,'New Script Functions').
114: &Apache::edit::editfield($token->[1],$bodytext,'',40,1).
115: $error.'</td></tr>'.
116: &Apache::edit::end_table();
117: }
118: if ($target eq "modified" ) {
119: $bodytext=$$parser[$#$parser]->get_text("/scriptlib");
120: $result=&Apache::edit::modifiedfield($token);
121: &Apache::lonxml::debug($result);
122: }
123: return $result;
124: }
125:
126: sub end_scriptlib {
127: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
128: my @result;
129: if ($target eq "edit" ) { $result[1]='no'; }
130: return @result;
131: }
132:
133: sub start_parserlib {
134: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
135: my $bodytext;
136: my $result ="";
137: my $error='';
138: if ($target eq 'web' || $target eq 'grade' || $target eq 'meta' || $target eq 'edit') {
139: $bodytext=$$parser[$#$parser]->get_text("/parserlib");
140: $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
141: $$parstack[$#$parstack]);
142: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
143: $bodytext);
144: my $styletext=&Apache::lonnet::getfile($location);
145: #&Apache::lonxml::debug("found :$bodytext: in :$location: with :$styletext:");
146: if ($styletext == -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 parserlib</b>");
151: return "";
152: }
153: }
154: %$style = ( %$style , &Apache::style::styleparser($target,$styletext));
155: }
156: if ($target eq "edit" ) {
157: $result=
158: &Apache::edit::tag_start($target,$token,'New Tag Definitions').
159: &Apache::edit::editfield($token->[1],$bodytext,'',40,1).
160: $error.'</td></tr>'.
161: &Apache::edit::end_table();
162: }
163: if ($target eq "modified" ) {
164: $bodytext=$$parser[$#$parser]->get_text("/parserlib");
165: $result=&Apache::edit::modifiedfield($token);
166: &Apache::lonxml::debug($result);
167: }
168: return $result;
169: }
170:
171: sub end_parserlib {
172: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
173: my @result;
174: if ($target eq "edit" ) { $result[1]='no'; }
175: return @result;
176: }
177:
178: sub start_window {
179: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
180: my $result = '';
181: if ($target eq 'web') {
182: &Apache::lonxml::startredirection;
183: } elsif ($target eq 'tex') {
184: $result = '\unskip\footnote{';
185: }
186: return $result;
187: }
188:
189: sub end_window {
190: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
191: my $result;
192: if ($target eq 'web') {
193: my $output=&Apache::lonxml::endredirection;
194: $output =~ s/\"/\"\;/g;
195: $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>";
196: } elsif ($target eq 'tex') {
197: $result = '}';
198: } else {
199: $result = '';
200: }
201: return $result;
202: }
203:
204: sub start_import {
205: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
206: my $bodytext=$$parser[$#$parser]->get_text("/import");
207: my $result ="";
208:
209: $bodytext=Apache::run::evaluate($bodytext,$safeeval,$$parstack[$#$parstack]);
210:
211: if ($target eq 'web' || $target eq 'grade') {
212: # FIXME this probably needs to be smart about construction vs.
213: # non construction space.
214: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
215: my $file=&Apache::lonnet::getfile($location);
216: if ($file == -1) {
217: &Apache::lonxml::error("<b> Unable to find <i>$bodytext as $location</i> for import</b>");
218: return "";
219: }
220:
221: my $dir=$location;
222: $dir=~s:/[^/]*$::;
223: # &Apache::lonxml::debug("directory $dir $location file $file \n<b>END</b>\n");
224: &Apache::lonxml::newparser($parser,\$file,$dir);
225: }
226: if ($target eq "edit" ) {
227: $result.=&Apache::edit::tag_start($target,$token);
228: $result.=&Apache::edit::editfield($token->[1],$bodytext,'',40,1);
229: #FIXME this need to convert $bodytext to be a contruction space reference
230: #my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
231: #$result.="Click<a href=\"$location\">here</a> to edit<br />"
232: }
233: if ($target eq 'modified') {
234: $bodytext=$$parser[$#$parser]->get_text("/import");
235: $result=&Apache::edit::modifiedfield($token);
236: &Apache::lonxml::debug($result);
237: }
238: return $result;
239: }
240:
241: sub end_import {
242: return '';
243: }
244:
245: sub start_storetc {
246: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
247: my $result = '';
248: &Apache::lonxml::startredirection;
249: return $result;
250: }
251:
252: sub end_storetc {
253: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
254: my $result;
255: my $output=&Apache::lonxml::endredirection;
256: $output =~ s/\"/\"\;/g;
257: $result = "{\bf $output.}}\write\tcfile{\protect\tcpc{ $output.}{\the\value{relpage}}}";
258: return $result;
259: }
260:
261:
262: sub start_physnet {
263: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
264: my $bodytext = '/msu/physnet/cbi.sty';
265: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
266: my $cbistyletext=&Apache::lonnet::getfile($location);
267:
268: %$style = (%$style,&Apache::style::styleparser($target,$cbistyletext));
269: if ( defined($$style{'physnet'}) ) {
270: &Apache::lonxml::newparser($parser,\$$style{'physnet'});
271: }
272: return "";
273: }
274:
275: sub end_physnet {
276: return '';
277: }
278:
279: sub xmlparse {
280: my ($string) = @_;
281: &Apache::lonxml::debug("Got $string");
282: my ($target,$token,$tagstack,$parstack,$oldparser,$safeeval,$style)=
283: @Apache::scripttag::parser_env;
284: my @parser;
285: &Apache::lonxml::newparser(\@parser,\$string);
286: my $result=&Apache::lonxml::inner_xmlparse($target,$tagstack,
287: $parstack,\@parser,
288: $safeeval,$style);
289: return $result;
290: }
291:
292: 1;
293: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>