1: # The LearningOnline Network with CAPA
2: # <script> definiton
3:
4:
5: package Apache::scripttag;
6:
7: use strict;
8: use Apache::lonnet;
9:
10: sub BEGIN {
11: &Apache::lonxml::register('Apache::scripttag',('script','scriptlib',
12: 'parserlib','import',
13: 'window','meta'));
14: 'footnote','display'));
15: }
16:
17: sub start_script {
18: my ($target,$token,$parstack,$parser,$safeeval)=@_;
19: my $args ='';
20: if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
21: my $type = &Apache::run::run("{$args;".'return $type}',$safeeval);
22: my $result='';
23: #&Apache::lonxml::debug("found type of $type");
24: if ($type eq "loncapa/perl") {
25: my $bodytext=&Apache::lonxml::get_all_text("/script",$$parser[$#$parser]);
26:
27: if ( $target eq "modified" ) {
28: } elsif ( $target eq "web" || $target eq "grade" ) {
29: &Apache::run::run($bodytext,$safeeval);
30: } elsif ($target eq "edit" ) {
31: &Apache::run::run($bodytext,$safeeval);
32: $result="<br> <$token->[1]> output: <br>$bodytext<br>Source:<br>";
33: $result.=&editfield($token->[1],$bodytext);
34: }
35: } else {
36: if ($target ne "meta") { $result = $token->[4]; }
37: }
38: return $result;
39: }
40:
41: sub end_script {
42: my ($target,$token,$parstack,$parser,$safeeval)=@_;
43: if ( $target eq "meta" ) { return ''; }
44: my $args ='';
45: if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
46: my $type = &Apache::run::run("{$args;".'return $type}',$safeeval);
47: my $result='';
48: #other script blocks need to survive
49: if ($type ne "loncapa/perl") { return $token->[2]; }
50: return '';
51: }
52:
53: sub start_display {
54: my ($target,$token,$parstack,$parser,$safeeval)=@_;
55: my $args ='';
56: if ( $#$parstack > -1 ) {
57: $args=$$parstack[$#$parstack];
58: }
59: my $bodytext=&Apache::lonxml::get_all_text("/display",$$parser[$#$parser]);
60:
61: if ( $target eq "modified" ) {
62: }
63: my $result=&Apache::run::run($bodytext,$safeeval);
64: if ($target eq 'grade' ) {
65: # grade should produce no output
66: $result='';
67: }
68: if ($target eq "edit" ) {
69: $result =
70: "<br> <$token->[1]> output: <br>$bodytext<br>Source:<br>";
71: $result.=&editfield($token->[1],$bodytext);
72: }
73: return $result;
74: }
75:
76: sub end_display {
77: }
78:
79: sub start_scriptlib {
80: my ($target,$token,$parstack,$parser,$safeeval)=@_;
81: my $bodytext=$$parser[$#$parser]->get_text("/scriptlib");
82: my $result ="";
83:
84: $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
85: $$parstack[$#$parstack]);
86: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
87: $bodytext);
88: my $script=&Apache::lonnet::getfile($location);
89: if ($script == -1) {
90: &Apache::lonxml::error("<b> Unable to find <i>$location</i> for scriptlib</b>");
91: return "";
92: }
93: &Apache::run::run($script,$safeeval);
94: #&Apache::lonxml::debug("ran $bodytext:<br>".&Apache::lonnet::getfile($bodytext)."<br>");
95:
96: if ($target eq "edit" ) {
97: $result.=&editfield($token->[1],$bodytext);
98: }
99: return $result;
100: }
101:
102: sub end_scriptlib {}
103:
104: sub start_parserlib {
105: my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
106: my $bodytext=$$parser[$#$parser]->get_text("/parserlib");
107: my $result ="";
108:
109: $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
110: $$parstack[$#$parstack]);
111: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
112: $bodytext);
113: my $styletext=&Apache::lonnet::getfile($location);
114: #&Apache::lonxml::debug("found :$bodytext: in :$location: with :$styletext:");
115: if ($styletext == -1) {
116: &Apache::lonxml::error("<b> Unable to find <i>$location</i> for parserlib</b>");
117: return "";
118: }
119: %$style = ( %$style , &Apache::style::styleparser($target,$styletext));
120:
121: if ($target eq "edit" ) {
122: $result=&editfield($token->[1],$bodytext);
123: }
124: return $result;
125: }
126:
127: sub end_parserlib {
128: }
129:
130: sub start_window {
131: my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
132: my $result = '';
133: if ($target eq 'web') {
134: $Apache::lonxml::redirection = 0;
135: } elsif ($target eq 'tex') {
136: $result = '\footnote{';
137: }
138: return $result;
139: }
140:
141: sub end_window {
142: my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
143: my $result;
144: if ($target eq 'web') {
145: $Apache::lonxml::outputstack =~ s/\"/\"\;/g;
146: $result = "<a href=\"javascript:newWindow=open(\'\',\'new_W\',\'width=500,height=200\');newWindow.document.open(\'text/html\',\'replace\');newWindow.document.writeln(\'<html><head><title>newwindow</title></head><body bgcolor="#FFFFFF"> $Apache::lonxml::outputstack </body></html>\');newWindow.document.close();void(0);\"><sup>*</sup></a>";
147: $Apache::lonxml::outputstack = "";
148: $Apache::lonxml::redirection = 1;
149: } elsif ($target eq 'tex') {
150: $result = '}';
151: } else {
152: $result = '';
153: }
154: return $result;
155: }
156:
157: sub start_import {
158: my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
159: my $bodytext=&Apache::lonxml::get_all_text("/import",$$parser[$#$parser]);
160: # my $bodytext=$$parser[$#$parser]->get_text("/import");
161: my $result ="";
162:
163: $bodytext=Apache::run::evaluate($bodytext,$safeeval,$$parstack[$#$parstack]);
164: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
165: my $file=&Apache::lonnet::getfile($location);
166: if ($file == -1) {
167: &Apache::lonxml::error("<b> Unable to find <i>$bodytext $location</i> for import</b>");
168: return "";
169: }
170:
171: my $dir=$location;
172: $dir=~s:/[^/]*$::;
173: # &Apache::lonxml::debug("directory $dir $location file $file \n<b>END</b>\n");
174: &Apache::lonxml::newparser($parser,\$file,$dir);
175:
176: if ($target eq "edit" ) {
177: $result.=&editfield($token->[1],$bodytext);
178: $result.="Click<a href=\"/res/$bodytext\">here</a> to edit<br></br>"
179: }
180: return '';
181: }
182:
183: sub end_import {
184: }
185:
186: sub start_meta {
187: my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
188: my $result = '';
189: if ($target ne 'meta') {
190: $result = &Apache::lonxml::get_all_text("/meta",$$parser[$#$parser]);
191: $result = '';
192: return $result;
193: } else {
194: return $result;
195: }
196: }
197:
198: sub end_meta {
199: }
200:
201:
202:
203:
204:
205:
206:
207: sub editfield {
208: my ($tag,$data)=@_;
209:
210: my $count=0;
211: my $maxlength=-1;
212: map { $count++;
213: if (length($_) > $maxlength) { $maxlength = length ($_); }
214: } split ("\n", $data);
215:
216: return "<br></br>\n<$tag><br></br>\n <textarea rows=\"$count\" cols=\"$maxlength\" name=homework_edit_".$Apache::lonxml::curdepth.">$data</textarea><br></br>\n</$tag><br></br>\n";
217: }
218:
219: sub getfilenothere {
220: my ($filename) = @_;
221: my $a="";
222:
223: $filename=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
224: $filename="/home/httpd/html/res".$filename;
225: if (! -e $filename ) {
226: &Apache::lonnet::subscribe($filename);
227: &Apache::lonnet::repcopy($filename);
228: }
229: if (! -e $filename ) { return -1; };
230: my $fh=Apache::File->new($filename);
231: while (<$fh>) {
232: $a .=$_;
233: }
234: return $a
235: }
236:
237: 1;
238: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>