Annotation of loncom/xml/scripttag.pm, revision 1.29
1.1 albertel 1: # The LearningOnline Network with CAPA
2: # <script> definiton
3:
4:
5: package Apache::scripttag;
6:
7: use strict;
1.3 albertel 8: use Apache::lonnet;
1.1 albertel 9:
10: sub BEGIN {
1.6 albertel 11: &Apache::lonxml::register('Apache::scripttag',('script','scriptlib',
1.20 sakharuk 12: 'parserlib','import',
1.24 albertel 13: 'footnote','display'));
1.1 albertel 14: }
15:
16: sub start_script {
1.2 albertel 17: my ($target,$token,$parstack,$parser,$safeeval)=@_;
1.17 albertel 18: my $args ='';
1.29 ! albertel 19: if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
1.17 albertel 20: my $type = &Apache::run::run("{$args;".'return $type}',$safeeval);
1.24 albertel 21: my $result='';
1.18 albertel 22: #&Apache::lonxml::debug("found type of $type");
1.17 albertel 23: if ($type eq "loncapa/perl") {
1.21 albertel 24: my $bodytext=&Apache::lonxml::get_all_text("/script",$$parser[$#$parser]);
1.17 albertel 25:
26: if ( $target eq "modified" ) {
1.28 albertel 27:
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);
1.24 albertel 32: $result="<br> <$token->[1]> output: <br>$bodytext<br>Source:<br>";
33: $result.=&editfield($token->[1],$bodytext);
1.17 albertel 34: }
35: } else {
1.28 albertel 36: if ($target ne "meta") { $result = $token->[4]; }
1.13 albertel 37: }
1.24 albertel 38: return $result;
1.17 albertel 39: }
1.13 albertel 40:
1.17 albertel 41: sub end_script {
42: my ($target,$token,$parstack,$parser,$safeeval)=@_;
1.28 albertel 43: if ( $target eq "meta" ) { return ''; }
1.29 ! albertel 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]; }
1.28 albertel 50: return '';
1.24 albertel 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);
1.26 albertel 64: if ($target eq 'grade' ) {
65: # grade should produce no output
66: $result='';
67: }
1.24 albertel 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 {
1.1 albertel 77: }
1.3 albertel 78:
79: sub start_scriptlib {
80: my ($target,$token,$parstack,$parser,$safeeval)=@_;
1.5 albertel 81: my $bodytext=$$parser[$#$parser]->get_text("/scriptlib");
1.3 albertel 82: my $result ="";
1.13 albertel 83:
1.18 albertel 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>");
1.13 albertel 95:
96: if ($target eq "edit" ) {
97: $result.=&editfield($token->[1],$bodytext);
1.3 albertel 98: }
99: return $result;
100: }
101:
102: sub end_scriptlib {}
1.4 albertel 103:
104: sub start_parserlib {
105: my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
1.6 albertel 106: my $bodytext=$$parser[$#$parser]->get_text("/parserlib");
1.7 albertel 107: my $result ="";
1.13 albertel 108:
1.18 albertel 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);
1.28 albertel 114: #&Apache::lonxml::debug("found :$bodytext: in :$location: with :$styletext:");
1.18 albertel 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));
1.13 albertel 120:
121: if ($target eq "edit" ) {
1.7 albertel 122: $result=&editfield($token->[1],$bodytext);
123: }
124: return $result;
1.4 albertel 125: }
126:
127: sub end_parserlib {
1.6 albertel 128: }
129:
1.19 sakharuk 130: sub start_footnote {
131: my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
1.20 sakharuk 132: my $result = '';
133: $Apache::lonxml::redirection = 0;
1.19 sakharuk 134: return $result;
135: }
136:
137: sub end_footnote {
138: my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
1.20 sakharuk 139: $Apache::lonxml::outputstack =~ s/\"/\"\;/g;
140: my $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>";
141: $Apache::lonxml::outputstack = "";
142: $Apache::lonxml::redirection = 1;
143: return $result;
1.19 sakharuk 144: }
145:
1.6 albertel 146: sub start_import {
1.7 albertel 147: my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
1.6 albertel 148: my $bodytext=$$parser[$#$parser]->get_text("/import");
1.7 albertel 149: my $result ="";
1.13 albertel 150:
151: $bodytext=Apache::run::evaluate($bodytext,$safeeval,$$parstack[$#$parstack]);
1.18 albertel 152: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
153: my $file=&Apache::lonnet::getfile($location);
154: if ($file == -1) {
155: &Apache::lonxml::error("<b> Unable to find <i>$bodytext $location</i> for import</b>");
156: return "";
1.14 albertel 157: }
158:
1.18 albertel 159: my $dir=$location;
160: $dir=~s:/[^/]*$::;
161: &Apache::lonxml::newparser($parser,\$file,$dir);
1.13 albertel 162:
163: if ($target eq "edit" ) {
164: $result.=&editfield($token->[1],$bodytext);
1.8 albertel 165: $result.="Click<a href=\"/res/$bodytext\">here</a> to edit<br></br>"
1.7 albertel 166: }
1.27 albertel 167: return '';
1.6 albertel 168: }
169:
170: sub end_import {
1.7 albertel 171: }
172:
173: sub editfield {
174: my ($tag,$data)=@_;
175:
176: my $count=0;
177: my $maxlength=-1;
178: map { $count++;
179: if (length($_) > $maxlength) { $maxlength = length ($_); }
180: } split ("\n", $data);
181:
182: 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";
1.4 albertel 183: }
1.3 albertel 184:
1.18 albertel 185: sub getfilenothere {
1.3 albertel 186: my ($filename) = @_;
187: my $a="";
1.11 tsai 188:
1.12 tsai 189: $filename=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
1.10 albertel 190: $filename="/home/httpd/html/res".$filename;
191: if (! -e $filename ) {
192: &Apache::lonnet::subscribe($filename);
193: &Apache::lonnet::repcopy($filename);
194: }
1.14 albertel 195: if (! -e $filename ) { return -1; };
1.11 tsai 196: my $fh=Apache::File->new($filename);
197: while (<$fh>) {
1.3 albertel 198: $a .=$_;
199: }
200: return $a
1.1 albertel 201: }
1.3 albertel 202:
1.1 albertel 203: 1;
204: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>