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: }
14:
15: $Apache::scripttag::SCRIPT_RESULT='';
16: sub start_script {
17: my ($target,$token,$parstack,$parser,$safeeval)=@_;
18: my $args ='';
19: if ( $#$parstack > -1 ) {
20: $args=$$parstack[$#$parstack];
21: }
22: my $type = &Apache::run::run("{$args;".'return $type}',$safeeval);
23: #&Apache::lonxml::debug("found type of $type");
24: $Apache::scripttag::SCRIPT_RESULT='';
25: if ($type eq "loncapa/perl") {
26: $safeeval->share('$SCRIPT_RESULT');
27: my $bodytext=&Apache::lonxml::get_all_text("script",$$parser[$#$parser]);
28:
29: if ( $target eq "modified" ) {
30: }
31: &Apache::run::run($bodytext,$safeeval);
32:
33: if ($target eq "edit" ) {
34: $Apache::scripttag::SCRIPT_RESULT =
35: "<br> <$token->[1]> output: <br>$bodytext<br>Source:<br>";
36: $Apache::scripttag::SCRIPT_RESULT.=&editfield($token->[1],$bodytext);
37: }
38: } else {
39: $Apache::scripttag::SCRIPT_RESULT = $token->[4];
40: }
41: return $Apache::scripttag::SCRIPT_RESULT;
42: }
43:
44: sub end_script {
45: my ($target,$token,$parstack,$parser,$safeeval)=@_;
46: return $token->[2];
47: }
48:
49: sub start_scriptlib {
50: my ($target,$token,$parstack,$parser,$safeeval)=@_;
51: my $bodytext=$$parser[$#$parser]->get_text("/scriptlib");
52: my $result ="";
53:
54: $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
55: $$parstack[$#$parstack]);
56: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
57: $bodytext);
58: my $script=&Apache::lonnet::getfile($location);
59: if ($script == -1) {
60: &Apache::lonxml::error("<b> Unable to find <i>$location</i> for scriptlib</b>");
61: return "";
62: }
63: &Apache::run::run($script,$safeeval);
64: #&Apache::lonxml::debug("ran $bodytext:<br>".&Apache::lonnet::getfile($bodytext)."<br>");
65:
66: if ($target eq "edit" ) {
67: $result.=&editfield($token->[1],$bodytext);
68: }
69: return $result;
70: }
71:
72: sub end_scriptlib {}
73:
74: sub start_parserlib {
75: my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
76: my $bodytext=$$parser[$#$parser]->get_text("/parserlib");
77: my $result ="";
78:
79: $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
80: $$parstack[$#$parstack]);
81: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
82: $bodytext);
83: my $styletext=&Apache::lonnet::getfile($location);
84: if ($styletext == -1) {
85: &Apache::lonxml::error("<b> Unable to find <i>$location</i> for parserlib</b>");
86: return "";
87: }
88: %$style = ( %$style , &Apache::style::styleparser($target,$styletext));
89:
90: if ($target eq "edit" ) {
91: $result=&editfield($token->[1],$bodytext);
92: }
93: return $result;
94: }
95:
96: sub end_parserlib {
97: }
98:
99: sub start_footnote {
100: my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
101: my $result ="footnote";
102: return $result;
103: }
104:
105: sub end_footnote {
106: my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
107:
108: }
109:
110: sub start_import {
111: my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
112: my $bodytext=$$parser[$#$parser]->get_text("/import");
113: my $result ="";
114:
115: $bodytext=Apache::run::evaluate($bodytext,$safeeval,$$parstack[$#$parstack]);
116: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
117: my $file=&Apache::lonnet::getfile($location);
118: if ($file == -1) {
119: &Apache::lonxml::error("<b> Unable to find <i>$bodytext $location</i> for import</b>");
120: return "";
121: }
122:
123: my $dir=$location;
124: $dir=~s:/[^/]*$::;
125: &Apache::lonxml::newparser($parser,\$file,$dir);
126:
127: if ($target eq "edit" ) {
128: $result.=&editfield($token->[1],$bodytext);
129: $result.="Click<a href=\"/res/$bodytext\">here</a> to edit<br></br>"
130: }
131: }
132:
133: sub end_import {
134: }
135:
136: sub editfield {
137: my ($tag,$data)=@_;
138:
139: my $count=0;
140: my $maxlength=-1;
141: map { $count++;
142: if (length($_) > $maxlength) { $maxlength = length ($_); }
143: } split ("\n", $data);
144:
145: 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";
146: }
147:
148: sub getfilenothere {
149: my ($filename) = @_;
150: my $a="";
151:
152: $filename=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
153: $filename="/home/httpd/html/res".$filename;
154: if (! -e $filename ) {
155: &Apache::lonnet::subscribe($filename);
156: &Apache::lonnet::repcopy($filename);
157: }
158: if (! -e $filename ) { return -1; };
159: my $fh=Apache::File->new($filename);
160: while (<$fh>) {
161: $a .=$_;
162: }
163: return $a
164: }
165:
166: 1;
167: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>