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