Annotation of loncom/xml/scripttag.pm, revision 1.170
1.1 albertel 1: # The LearningOnline Network with CAPA
2: # <script> definiton
1.58 www 3: #
1.170 ! raeburn 4: # $Id: scripttag.pm,v 1.169 2013/10/01 00:52:58 raeburn Exp $
1.58 www 5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
1.116 www 28:
1.47 albertel 29: package Apache::scripttag;
1.1 albertel 30:
31: use strict;
1.3 albertel 32: use Apache::lonnet;
1.145 albertel 33: use Apache::lonlocal;
1.167 raeburn 34: use Apache::lonxml();
35: use Apache::londefdef();
1.145 albertel 36: use Apache::style();
1.1 albertel 37:
1.47 albertel 38: #Globals
39: # this used to pass around the standard callsub arguments to a tag func
40: # so xmlparse can reenter the inner_xmlparse loop.
41:
42: @Apache::scripttag::parser_env = ();
1.61 harris41 43: BEGIN {
1.62 albertel 44: &Apache::lonxml::register('Apache::scripttag',
45: ('script','scriptlib','parserlib','import',
1.163 www 46: 'window','windowlink','togglebox','display','storetc','physnet',
1.123 albertel 47: 'standalone','comment','num','parse','algebra',
1.86 albertel 48: 'LONCAPA_INTERNAL_TURN_STYLE_ON',
1.150 raeburn 49: 'LONCAPA_INTERNAL_TURN_STYLE_OFF'));
1.81 albertel 50: }
51:
52: sub start_LONCAPA_INTERNAL_TURN_STYLE_ON {
53: $Apache::lonxml::usestyle=1;
1.89 albertel 54: $Apache::lonxml::style_values='';
1.81 albertel 55: return ('','no');
56: }
57:
58: sub end_LONCAPA_INTERNAL_TURN_STYLE_ON {
1.91 albertel 59: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
60: my $end=&Apache::lonxml::get_param('end',$parstack,$safeeval);
61: if (defined($end)) {
62: &Apache::lonxml::end_tag($tagstack,$parstack,$token);
63: }
1.100 albertel 64: return ('','no');
65: }
66:
67: sub start_LONCAPA_INTERNAL_TURN_STYLE_OFF {
68: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
69: $Apache::lonxml::usestyle=0;
70: my $end=&Apache::lonxml::get_param('end',$parstack,$safeeval);
71: if (!$end) {
72: $Apache::lonxml::style_values=$$parstack[-1];
73: $Apache::lonxml::style_end_values=$$parstack[-1];
74: } else {
75: $Apache::lonxml::style_values=$Apache::lonxml::style_end_values;
76: $Apache::lonxml::style_end_values='';
77: }
78: return ('','no');
79: }
80:
81: sub end_LONCAPA_INTERNAL_TURN_STYLE_OFF {
1.86 albertel 82: return ('','no');
83: }
84:
1.1 albertel 85: sub start_script {
1.40 albertel 86: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.47 albertel 87: @Apache::scripttag::parser_env = @_;
1.24 albertel 88: my $result='';
1.39 albertel 89: my $type= &Apache::lonxml::get_param('type',$parstack,$safeeval);
90: &Apache::lonxml::debug("found type of $type");
1.17 albertel 91: if ($type eq "loncapa/perl") {
92: if ( $target eq "modified" ) {
1.103 albertel 93: $result=$token->[4].&Apache::edit::modifiedfield('/script',$parser);
1.54 albertel 94: } elsif ( $target eq 'web' || $target eq 'tex' ||
1.130 albertel 95: $target eq 'grade' || $target eq 'webgrade' ||
96: $target eq 'answer' || $target eq 'analyze' ) {
1.107 albertel 97: my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/script",$parser);
1.79 albertel 98: if (!$Apache::lonxml::default_homework_loaded) {
99: &Apache::lonxml::default_homework_load($safeeval);
100: }
101: &Apache::run::run($bodytext,$safeeval);
1.88 albertel 102: if (($target eq 'answer') &&
1.126 albertel 103: ($env{'form.answer_output_mode'} ne 'tex') &&
1.88 albertel 104: ($Apache::lonhomework::viewgrades == 'F')) {
1.79 albertel 105: $Apache::lonxml::evaluate--;
1.138 albertel 106: my (undef,undef,$udom,$uname)=&Apache::lonnet::whichuser();
1.140 albertel 107: $uname =~s/\W//g;
108: $udom =~s/\W//g;
1.145 albertel 109: my $function_name =
110: join('_','LONCAPA_scriptvars',$uname,$udom,
111: $env{'form.counter'},$Apache::lonxml::curdepth);
1.158 www 112: &Apache::lonxml::add_script_result(
1.159 www 113: &Apache::loncommon::modal_adhoc_window($function_name,500,500,
1.166 raeburn 114: '<pre style="background-color:#ffffff;">'.
115: &Apache::run::dump($target,$safeeval).'</pre>',
1.159 www 116: &mt('Script Vars'))."<br />");
1.79 albertel 117: }
1.28 albertel 118: } elsif ($target eq "edit" ) {
1.41 albertel 119: #&Apache::run::run($bodytext,$safeeval);
120: #$result="<br /> <$token->[1]> output: <br />$bodytext<br />Source:<br />";
1.107 albertel 121: my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/script",$parser);
1.103 albertel 122: $result=&Apache::edit::tag_start($target,$token,'Script');
123: $result.=&Apache::edit::editfield($token->[1],$bodytext,'',80,4);
1.107 albertel 124: } elsif ($target eq 'meta') {
125: my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/script",$parser);
1.17 albertel 126: }
127: } else {
1.108 matthew 128: my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/script",$parser);
1.144 albertel 129: if ($target ne "meta" && $target ne 'tex' && $target ne 'answer') {
1.112 albertel 130: $result = $token->[4];
131: $result.=$bodytext;
1.168 raeburn 132: my $src=&Apache::lonxml::get_param('src',$parstack,$safeeval,undef,1);
133: my $url=&Apache::lonnet::hreflocation('',$env{'request.filename'});
134: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
135: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
136: if ($src ne '') {
137: if ($src !~ m{^(/|https?://)}) {
138: my $cleanhref = &Apache::londefdef::clean_docs_httpref($src,$url,$cdom,$cnum);
139: if ($cleanhref) {
140: &Apache::lonxml::extlink($cleanhref);
1.167 raeburn 141: }
1.168 raeburn 142: }
143: } elsif (($type eq 'text/javascript') && ($bodytext ne '')) {
144: if ($url =~ m{^\Q/uploaded/$cdom/$cnum/docs/\E}) {
1.170 ! raeburn 145: if ($bodytext =~ m{\.addMediaSrc\((["'])(?:(?!\1).)+\1\)}) {
! 146: my $quote = $1;
! 147: if ($bodytext =~ m{\Q.addMediaSrc($quote\E([^$quote]+)\Q$quote)\E}) {
! 148: my $fname = $1;
! 149: my $cleanhref =
! 150: &Apache::londefdef::clean_docs_httpref($fname,$url,$cdom,$cnum);
! 151: if ($cleanhref) {
! 152: &Apache::lonxml::extlink($cleanhref);
! 153: }
! 154: }
! 155: }
1.168 raeburn 156: if ($bodytext =~ m{\.set\w+(Src|Swf)\(["']}i) {
157: my @srcs = split(/\.set/,$bodytext);
158: if (scalar(@srcs) > 1) {
159: foreach my $item (@srcs) {
160: if ($item =~ m{^(FlashPlayerSwf|MediaSrc|XMPSrc|ConfigurationSrc|PosterImageSrc)\((['"])(?:(?!\2).)+\2\)}is) {
161: my $srctype = $1;
162: my $quote = $2;
163: my ($fname) = ($item =~ m{^\Q$srctype($quote\E([^$quote]+)\Q$quote)\E});
1.169 raeburn 164: my $cleanhref =
165: &Apache::londefdef::clean_docs_httpref($fname,$url,$cdom,$cnum);
1.168 raeburn 166: if ($cleanhref) {
167: &Apache::lonxml::extlink($cleanhref);
1.169 raeburn 168: if ($srctype eq 'ConfigurationSrc') {
169: if ($cleanhref =~ m{^(.+/)configuration_express\.xml$}) {
170: #
171: # Camtasia 8.1: express_show/spritesheet.png needed, and included in zip archive.
172: # Not referenced directly in <main>.html or <main>_player.html files,
173: # so call lonxml::extlink() here to include httpref for the uploaded file.
174: # (where <main> is name user gave to file/archive).
175: #
176:
177: my $spritesheet = $1.'express_show/spritesheet.png';
178: if (&Apache::lonnet::repcopy_userfile($spritesheet) eq 'ok') {
179: &Apache::lonxml::extlink($spritesheet);
180: }
181: }
1.170 ! raeburn 182: #
! 183: # Camtasia 8.4: express_show/spritesheet.min.css needed, and included in zip archive.
! 184: # Not referenced directly in <main>.html or <main>_player.html files,
! 185: # so call lonxml::extlink() here to include httpref for the uploaded file.
! 186: # (where <main> is name user gave to file/archive).
! 187: #
! 188: my $spritesheet_css = $1.'express_show/spritesheet.min.css';
! 189: if (&Apache::lonnet::repcopy_userfile($spritesheet_css) eq 'ok') {
! 190: &Apache::lonxml::extlink($spritesheet_css);
! 191: }
1.169 raeburn 192: } elsif ($srctype eq 'PosterImageSrc') {
193: if ($fname =~ m{^(.+)_First_Frame\.png$}) {
194: my $prefix = $1;
195: my ($path) = ($cleanhref =~ m{^(.+/)\Q$fname\E});
196: #
197: # Camtasia 8.1: <main>_Thumbnails.png needed, and included in zip archive.
198: # Not referenced directly in <main>.html or <main>_player.html files,
199: # so call lonxml::extlink() here to include httpref for the uploaded file
200: # (where <main> is name user gave to file/archive).
201: #
202: my $thumbnail = $path.$prefix.'_Thumbnails.png';
203: if (&Apache::lonnet::repcopy_userfile($thumbnail) eq 'ok') {
204: &Apache::lonxml::extlink($thumbnail);
205: }
206: }
207: }
1.167 raeburn 208: }
209: }
1.165 raeburn 210: }
211: }
212: }
1.170 ! raeburn 213: if ($bodytext =~ /\(document,\s*(['"])script\1,\s*\[([^\]]+)\]\);/s) {
! 214: my $scriptslist = $2;
! 215: my @srcs = split(/\s*,\s*/,$scriptslist);
! 216: foreach my $src (@srcs) {
! 217: if ($src =~ /(["'])(?:(?!\1).)+\.js\1/) {
! 218: my $quote = $1;
! 219: my ($fname) = ($src =~ m/\Q$quote\E([^$quote]+)\Q$quote\E/);
! 220: my $cleanhref =
! 221: &Apache::londefdef::clean_docs_httpref($fname,$url,$cdom,$cnum);
! 222: if ($cleanhref) {
! 223: &Apache::lonxml::extlink($cleanhref);
! 224: }
! 225: }
! 226: }
! 227: }
1.165 raeburn 228: }
229: }
1.112 albertel 230: }
1.13 albertel 231: }
1.24 albertel 232: return $result;
1.17 albertel 233: }
1.13 albertel 234:
1.17 albertel 235: sub end_script {
1.40 albertel 236: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.28 albertel 237: if ( $target eq "meta" ) { return ''; }
1.39 albertel 238: my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval);
1.29 albertel 239: my $result='';
240: #other script blocks need to survive
1.115 albertel 241: if ($type ne "loncapa/perl" && $target ne 'tex') {
1.53 albertel 242: return $token->[2];
243: } elsif ($target eq 'edit' ) {
244: return &Apache::edit::end_table();
1.78 albertel 245: } elsif ($target eq 'answer') {
1.53 albertel 246: $Apache::lonxml::evaluate++;
247: }
1.28 albertel 248: return '';
1.24 albertel 249: }
250:
251: sub start_display {
1.40 albertel 252: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.119 albertel 253: @Apache::scripttag::parser_env = @_;
1.48 albertel 254: my $result;
1.39 albertel 255:
1.24 albertel 256: if ( $target eq "modified" ) {
1.103 albertel 257: $result=$token->[4].&Apache::edit::modifiedfield("/display",$parser);
1.54 albertel 258: } elsif ( $target eq 'web' || $target eq 'tex' ||
1.130 albertel 259: $target eq 'grade' || $target eq 'webgrade' ||
260: $target eq 'answer' || $target eq 'analyze') {
1.107 albertel 261: my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/display",$parser);
1.80 albertel 262: if (!$Apache::lonxml::default_homework_loaded) {
263: &Apache::lonxml::default_homework_load($safeeval);
264: }
265: $result=&Apache::run::run($bodytext,$safeeval);
266: if ($target eq 'grade' || $target eq 'answer' ||
267: $target eq 'analyze') {
1.114 albertel 268: # grade/answer/analyxe should produce no output but if we
269: # are redirecting, the redirecter should know what to do
270: # with the output
271: if (!$Apache::lonxml::redirection) { $result=''; }
1.80 albertel 272: }
1.101 albertel 273: $Apache::lonxml::post_evaluate=0;
1.48 albertel 274: } elsif ($target eq "edit" ) {
1.110 albertel 275: my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/display",$parser);
1.48 albertel 276: #$result =
277: # "<br /> <$token->[1]> output: <br />$bodytext<br />Source:<br />";
278: #$result.=&Apache::edit::editfield($token->[1],$bodytext,'',40,1);
279: $result=&Apache::edit::tag_start($target,$token,'Script With Display');
1.77 albertel 280: $result.=&Apache::edit::editfield($token->[1],$bodytext,'',80,1)
1.107 albertel 281: } elsif ($target eq 'meta') {
282: my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/display",$parser);
1.24 albertel 283: }
284: return $result;
285: }
286:
287: sub end_display {
1.48 albertel 288: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
289: if ($target eq 'edit' ) { return &Apache::edit::end_table(); }
290: return '';
1.1 albertel 291: }
1.3 albertel 292:
293: sub start_scriptlib {
1.40 albertel 294: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.37 albertel 295: my $bodytext;
296: my $result ='';
297: my $error='';
298:
1.106 albertel 299: if ($target eq 'web' || $target eq 'tex' || $target eq 'grade' ||
300: $target eq 'meta' || $target eq 'edit' || $target eq 'answer' ||
1.137 albertel 301: $target eq 'analyze' || $target eq 'webgrade') {
1.37 albertel 302: $bodytext=$$parser[$#$parser]->get_text("/scriptlib");
303: $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
304: $$parstack[$#$parstack]);
305: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
306: $bodytext);
307: my $script=&Apache::lonnet::getfile($location);
308: if ($script == -1) {
309: if ($target eq 'edit') {
1.145 albertel 310: $error='</tr><tr><td>'.&mt('Errors').'</td><td colspan="2"><b>'.&mt(' Unable to find [_1]','<span class="LC_filename">'.$location.'</span>').'</b></td>'."\n";
1.37 albertel 311: } else {
312: &Apache::lonxml::error("<b> Unable to find <i>$location</i> for scriptlib</b>");
313: return "";
314: }
315: }
316: &Apache::run::run($script,$safeeval);
317: #&Apache::lonxml::debug("ran $bodytext:<br />".&Apache::lonnet::getfile($bodytext)."<br />");
1.18 albertel 318: }
1.13 albertel 319: if ($target eq "edit" ) {
1.37 albertel 320: $result=
1.49 albertel 321: &Apache::edit::tag_start($target,$token,'New Script Functions').
1.83 matthew 322: &Apache::edit::editline($token->[1],$bodytext,'scriptlib',40).
323: &Apache::edit::browse(undef,'textnode').
1.49 albertel 324: $error.'</td></tr>'.
325: &Apache::edit::end_table();
1.37 albertel 326: }
327: if ($target eq "modified" ) {
1.103 albertel 328: $result=$token->[4].&Apache::edit::modifiedfield("/scriptlib",$parser);
1.3 albertel 329: }
330: return $result;
331: }
332:
1.37 albertel 333: sub end_scriptlib {
1.40 albertel 334: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.49 albertel 335: my @result;
336: if ($target eq "edit" ) { $result[1]='no'; }
337: return @result;
1.37 albertel 338: }
1.4 albertel 339:
340: sub start_parserlib {
1.40 albertel 341: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.37 albertel 342: my $bodytext;
1.7 albertel 343: my $result ="";
1.37 albertel 344: my $error='';
1.106 albertel 345: if ($target eq 'web' || $target eq 'tex' || $target eq 'grade' ||
346: $target eq 'meta' || $target eq 'edit' || $target eq 'answer' ||
1.137 albertel 347: $target eq 'analyze' || $target eq 'webgrade') {
1.37 albertel 348: $bodytext=$$parser[$#$parser]->get_text("/parserlib");
349: $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
350: $$parstack[$#$parstack]);
351: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
352: $bodytext);
353: my $styletext=&Apache::lonnet::getfile($location);
354: #&Apache::lonxml::debug("found :$bodytext: in :$location: with :$styletext:");
355: if ($styletext == -1) {
356: if ($target eq 'edit') {
357: $error='</tr><tr><td>Errors</td><td colspan="2"><b> Unable to find <i>'.$location.'</i></b></td>'."\n";
358: } else {
359: &Apache::lonxml::error("<b> Unable to find <i>$location</i> for parserlib</b>");
360: return "";
361: }
362: }
363: %$style = ( %$style , &Apache::style::styleparser($target,$styletext));
1.18 albertel 364: }
1.13 albertel 365: if ($target eq "edit" ) {
1.37 albertel 366: $result=
1.49 albertel 367: &Apache::edit::tag_start($target,$token,'New Tag Definitions').
1.68 albertel 368: &Apache::edit::editline($token->[1],$bodytext,'',40).
1.49 albertel 369: $error.'</td></tr>'.
370: &Apache::edit::end_table();
1.37 albertel 371: }
372: if ($target eq "modified" ) {
1.103 albertel 373: $result=$token->[4].&Apache::edit::modifiedfield("/parserlib",$parser);
1.7 albertel 374: }
375: return $result;
1.4 albertel 376: }
377:
378: sub end_parserlib {
1.40 albertel 379: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.49 albertel 380: my @result;
381: if ($target eq "edit" ) { $result[1]='no'; }
382: return @result;
1.6 albertel 383: }
384:
1.30 sakharuk 385: sub start_window {
1.105 albertel 386: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
387: my $result = '';
1.137 albertel 388: if ($target eq 'web' || $target eq 'webgrade') {
1.105 albertel 389: &Apache::lonxml::startredirection;
390: } elsif ($target eq 'tex') {
1.164 www 391: my $printtext=&Apache::lonxml::get_param('printtext',$parstack,$safeeval);
392: if ($printtext=~/\w/) {
393: # If printtext is given, do not output any intervening information
394: &Apache::lonxml::startredirection;
395: } else {
396: $result = '\unskip\footnote{';
397: }
1.105 albertel 398: } elsif ($target eq 'edit') {
399: $result.=&Apache::edit::tag_start($target,$token);
400: $result.=&Apache::edit::text_arg('Text of Link:','linktext',$token,70);
1.131 albertel 401: $result.=&Apache::edit::text_arg('Height:','height',$token,5);
402: $result.=&Apache::edit::text_arg('Width:','width',$token,5);
1.164 www 403: $result.=&Apache::edit::text_arg('Printed text (optional):','printtext',$token,20);
1.105 albertel 404: $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
405: } elsif ($target eq 'modified') {
406: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.129 raeburn 407: $safeeval,'linktext',
1.161 www 408: 'width','height');
1.105 albertel 409: if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); }
410: }
411: return $result;
1.19 sakharuk 412: }
413:
1.30 sakharuk 414: sub end_window {
1.40 albertel 415: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.30 sakharuk 416: my $result;
1.137 albertel 417: if ($target eq 'web' || $target eq 'webgrade') {
1.34 albertel 418: my $output=&Apache::lonxml::endredirection;
1.92 albertel 419: my $linktext= &Apache::lonxml::get_param('linktext',$parstack,$safeeval);
420: if (!$linktext) { $linktext='<sup>*</sup>'; }
421: my $width= &Apache::lonxml::get_param('width',$parstack,$safeeval);
422: if (!$width) { $width='500'; }
423: my $height= &Apache::lonxml::get_param('height',$parstack,$safeeval);
424: if (!$height) { $height='200'; }
1.160 www 425: $result=&Apache::loncommon::modal_adhoc_window
426: ("LONCAPA_newwindow_$Apache::lonxml::curdepth",$width,$height,$output,$linktext);
1.35 sakharuk 427: } elsif ($target eq 'tex') {
1.164 www 428: my $printtext=&Apache::lonxml::get_param('printtext',$parstack,$safeeval);
429: if ($printtext=~/\w/) {
430: # If a "printtext" is given, proceed to retrieve all intervening information and trash it
431: my $output=&Apache::lonxml::endredirection;
432: # Use printtext instead
433: $result=$printtext;
434: } else {
435: $result='}';
436: }
1.30 sakharuk 437: } else {
1.35 sakharuk 438: $result = '';
1.30 sakharuk 439: }
1.20 sakharuk 440: return $result;
1.19 sakharuk 441: }
442:
1.161 www 443:
1.163 www 444: sub start_windowlink {
445: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
446: my $result = '';
447: if ($target eq 'web' || $target eq 'webgrade') {
448: &Apache::lonxml::startredirection;
449: } elsif ($target eq 'edit') {
450: $result.=&Apache::edit::tag_start($target,$token);
451: $result.=&Apache::edit::text_arg('Link:','href',$token,70);
452: $result.=&Apache::edit::text_arg('Height:','height',$token,5);
453: $result.=&Apache::edit::text_arg('Width:','width',$token,5);
454: $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
455: } elsif ($target eq 'modified') {
456: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
457: $safeeval,'href',
458: 'width','height');
459: if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); }
460: }
461: return $result;
462: }
463:
464: sub end_windowlink {
465: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
466: my $result;
467: if ($target eq 'web' || $target eq 'webgrade') {
468: my $output=&Apache::lonxml::endredirection;
469: my $href= &Apache::lonxml::get_param('href',$parstack,$safeeval);
470: if (!$href) { $href='/adm/rat/empty.html'; }
471: my $width= &Apache::lonxml::get_param('width',$parstack,$safeeval);
472: if (!$width) { $width='500'; }
473: my $height= &Apache::lonxml::get_param('height',$parstack,$safeeval);
474: if (!$height) { $height='200'; }
475: $result=&Apache::loncommon::modal_link($href,$output,$width,$height);
476: } else {
477: $result = '';
478: }
479: return $result;
480: }
481:
482:
1.161 www 483: sub start_togglebox {
484: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
485: my $result = '';
486: if ($target eq 'web' || $target eq 'webgrade') {
487: my $id="LONCAPA_togglebox_$Apache::lonxml::curdepth";
488: my $heading=&Apache::lonxml::get_param('heading',$parstack,$safeeval);
489: unless ($heading) { $heading=''; } else { $heading.=' '; }
490: my $showtext=&Apache::lonxml::get_param('showtext',$parstack,$safeeval);
491: my $hidetext=&Apache::lonxml::get_param('hidetext',$parstack,$safeeval);
492: my $headerbg=&Apache::lonxml::get_param('headerbg',$parstack,$safeeval);
1.162 www 493: $result=&Apache::loncommon::start_togglebox($id,$heading,$headerbg,$hidetext,$showtext);
1.161 www 494: } elsif ($target eq 'tex') {
495: my $heading=&Apache::lonxml::get_param('heading',$parstack,$safeeval);
496: unless ($heading) { $heading=''; } else { $heading.=' '; }
497: $result = "\n\n".'\fbox{{\bf '.$heading.'} \qquad '."\n";
498: } elsif ($target eq 'edit') {
499: $result.=&Apache::edit::tag_start($target,$token);
500: $result.=&Apache::edit::text_arg('Heading:','heading',$token,70);
501: $result.=&Apache::edit::text_arg('Header Background:','headerbg',$token,7);
502: $result.=&Apache::edit::text_arg('Show text:','showtext',$token,10);
503: $result.=&Apache::edit::text_arg('Hide text:','hidetext',$token,10);
504: $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
505: } elsif ($target eq 'modified') {
506: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
507: $safeeval,'heading',
508: 'showtext','hidetext',
509: 'headerbg','textbg');
510: if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); }
511: }
512: return $result;
513: }
514:
515: sub end_togglebox {
516: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
517: my $result;
518: if ($target eq 'web' || $target eq 'webgrade') {
1.162 www 519: $result=&Apache::loncommon::end_togglebox();
1.161 www 520: } elsif ($target eq 'tex') {
521: $result = "}\n\n";
522: } else {
523: $result = '';
524: }
525: return $result;
526: }
527:
528:
529:
1.6 albertel 530: sub start_import {
1.40 albertel 531: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.31 albertel 532: my $bodytext=$$parser[$#$parser]->get_text("/import");
1.7 albertel 533: my $result ="";
1.13 albertel 534:
1.153 www 535: $bodytext=&Apache::run::evaluate($bodytext,$safeeval,$$parstack[$#$parstack]);
1.14 albertel 536:
1.135 albertel 537: if ($target eq 'web' || $target eq 'webgrade' || $target eq 'grade'
538: || $target eq 'answer' || $target eq 'tex' || $target eq 'analyze' ) {
1.46 albertel 539: # FIXME this probably needs to be smart about construction vs.
540: # non construction space.
541: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
542: my $file=&Apache::lonnet::getfile($location);
543: if ($file == -1) {
544: &Apache::lonxml::error("<b> Unable to find <i>$bodytext as $location</i> for import</b>");
545: return "";
546: }
1.153 www 547: my $importmode=&Apache::lonxml::get_param('importmode',$parstack,$safeeval);
1.154 www 548: if (($importmode eq 'problem') || ($importmode eq 'part')) {
549: # We are using import to import published problems
550: if (($importmode eq 'problem') || ($file=~/<part[^<]*>/s)) {
551: # We explicitly don't want this to be a separate part or the problem already has parts
552: $file=~s/^\s*<problem>/<library>/s;
553: $file=~s/<\/problem>\s*$/<\/library>/s;
554: } else {
555: # We want this to be a separate part, but it currently is not
1.155 www 556: $file=~s/^\s*<problem>/<library><part>/s;
1.154 www 557: $file=~s/<\/problem>\s*$/<\/part><\/library>/s;
558: }
1.153 www 559: }
1.46 albertel 560: my $dir=$location;
561: $dir=~s:/[^/]*$::;
562: # &Apache::lonxml::debug("directory $dir $location file $file \n<b>END</b>\n");
1.142 albertel 563: my $id= &Apache::lonxml::get_id($parstack,$safeeval);
1.57 albertel 564: if (!$id) { $id=$Apache::lonxml::curdepth; }
565: push(@Apache::inputtags::import,$id);
1.90 albertel 566: push(@Apache::inputtags::importlist,$id);
1.87 sakharuk 567:
1.144 albertel 568: &Apache::lonxml::newparser($parser,\$file,$dir);
1.87 sakharuk 569:
1.56 albertel 570: } elsif ($target eq "edit" ) {
1.46 albertel 571: $result.=&Apache::edit::tag_start($target,$token);
1.153 www 572: my $location=$token->[1];
573: $location=~s/^\s*//s;
574: $location=~s/\s*$//s;
575: $result.=&Apache::edit::editline($location,$bodytext,'',40);
1.83 matthew 576: $result.=&Apache::edit::browse(undef,'textnode');
1.154 www 577: $result.= ' <label>'.&mt('Import as:').
1.153 www 578: '<select name="importmode_'.$Apache::lonxml::curdepth.'">';
1.154 www 579: my %options=&Apache::lonlocal::texthash('' => 'as standard library',
580: 'problem' => 'as problem',
581: 'part' => 'as problem part(s)');
582: foreach my $option (sort(keys(%options))) {
1.153 www 583: $result.='<option value="'.$option.'"';
584: if ($option eq &Apache::lonxml::get_param('importmode',$parstack,$safeeval)) {
585: $result.=' selected="selected"';
586: }
1.154 www 587: $result.='>'.$options{$option}.'</option>';
1.153 www 588: }
589: $result.='</select></label>';
1.46 albertel 590: #FIXME this need to convert $bodytext to be a contruction space reference
591: #my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
592: #$result.="Click<a href=\"$location\">here</a> to edit<br />"
1.56 albertel 593: } elsif ($target eq 'modified') {
1.153 www 594: &Apache::edit::get_new_args($token,$parstack,$safeeval,'importmode');
595: $result='<import id="'.$token->[2]{'id'}.'" importmode="'.$token->[2]{'importmode'}.'">';
596: $result.=&Apache::edit::modifiedfield("/import",$parser);
1.56 albertel 597: } elsif ($target eq 'meta') {
1.142 albertel 598: my $id= &Apache::lonxml::get_id($parstack,$safeeval);
1.57 albertel 599: $result.='<import part="'.$Apache::inputtags::part;
600: if ($id) {
601: $result.='" id="'.$id;
602: }
1.156 www 603: $result.='" importmode="'.$token->[2]{'importmode'}.'">';
1.56 albertel 604: $result.=$bodytext;
605: $result.='</import>';
1.46 albertel 606: }
607: return $result;
1.6 albertel 608: }
609:
610: sub end_import {
1.69 albertel 611: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.57 albertel 612: pop(@Apache::inputtags::import);
1.69 albertel 613: my $result;
1.120 albertel 614: if ($target eq 'edit' ) { $result=&Apache::edit::end_row.
615: &Apache::edit::end_table(); }
1.69 albertel 616: return $result;
1.1 albertel 617: }
1.42 sakharuk 618:
619: sub start_storetc {
1.43 albertel 620: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.42 sakharuk 621: my $result = '';
622: &Apache::lonxml::startredirection;
623: return $result;
624: }
625:
626: sub end_storetc {
1.43 albertel 627: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.42 sakharuk 628: my $result;
629: my $output=&Apache::lonxml::endredirection;
630: $output =~ s/\"/\"\;/g;
1.52 albertel 631: $result = '{\bf '.$output.'.}}\write\tcfile{\protect\tcpc{ '.$output.'.}{\the\value{relpage}}}';
632: return $result;
1.42 sakharuk 633: }
634:
635:
636: sub start_physnet {
1.45 sakharuk 637: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.50 www 638: my $bodytext = '/adm/includes/physnet.sty';
1.45 sakharuk 639: my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
640: my $cbistyletext=&Apache::lonnet::getfile($location);
1.42 sakharuk 641:
1.45 sakharuk 642: %$style = (%$style,&Apache::style::styleparser($target,$cbistyletext));
1.59 albertel 643: $$parser['-1']->unget_token($token);
644: # if ( defined($$style{'physnet'}) ) {
645: # &Apache::lonxml::newparser($parser,\$$style{'physnet'});
646: # }
1.45 sakharuk 647: return "";
648: }
1.42 sakharuk 649:
1.45 sakharuk 650: sub end_physnet {
1.47 albertel 651: return '';
1.42 sakharuk 652: }
1.62 albertel 653:
654: sub start_standalone {
655: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.75 albertel 656: my $result='';
1.137 albertel 657: if ($target eq 'web' || $target eq 'webgrade') {
1.126 albertel 658: if ( $env{'request.course.id'} ) {
1.133 albertel 659: my $inside = &Apache::lonxml::get_all_text("/standalone",$parser,$style);
1.75 albertel 660: } else {
661: $result='<table bgcolor="#E1E1E1" border="2"><tr><td>';
662: }
1.62 albertel 663: }
1.75 albertel 664: return $result;
1.62 albertel 665: }
666:
667: sub end_standalone {
1.75 albertel 668: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
669: my $result='';
1.137 albertel 670: if ($target eq 'web' || $target eq 'webgrade' ) {
1.126 albertel 671: if ( $env{'request.course.id'} ) {
1.75 albertel 672: } else {
673: $result='</td></tr></table>';
674: }
675: }
676: return $result;
1.74 albertel 677: }
678:
679: sub start_comment {
680: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
681: my $result='';
682: if ($target eq 'edit') {
683: $result=&Apache::edit::tag_start($target,$token);
1.133 albertel 684: my $bodytext=&Apache::lonxml::get_all_text("/comment",$parser,$style);
1.77 albertel 685: $result.=&Apache::edit::editfield($token->[1],$bodytext,'',80,4)
1.74 albertel 686: } elsif ( $target eq 'modified') {
1.103 albertel 687: $result=$token->[4].&Apache::edit::modifiedfield("/comment",$parser);
1.137 albertel 688: } elsif ( $target eq 'web' || $target eq 'tex' || $target eq 'grade' ||
689: $target eq 'answer' || $target eq 'meta' || $target eq 'analyze' ||
690: $target eq 'webgrade') {
1.74 albertel 691: #normally throw away comments
1.133 albertel 692: my $bodytext=&Apache::lonxml::get_all_text("/comment",$parser,$style);
1.74 albertel 693: }
694: return $result;
695: }
696:
697: sub end_comment {
698: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
699: if ($target eq 'edit' ) { return &Apache::edit::end_table(); }
1.62 albertel 700: return '';
701: }
702:
1.42 sakharuk 703:
1.47 albertel 704: sub xmlparse {
705: my ($string) = @_;
1.111 albertel 706: &Apache::lonxml::debug("xmlparse recursion starting with $string");
1.113 albertel 707: # Apache::run::evaluate does an 'eval' on the name of the subroutine
708: # if it detects something that looks like a subroutine, this ends up calling
709: # things without any arguments and since perl is nice enough to pass
710: # along the default arguments when you don't explicitly say no arguments
711: # if you call &xmlparse, it gets &xmlparse passed as it argument.
712: # Same thing soccurs with &chemparse.
1.111 albertel 713: if ($string eq '&xmlparse') { return '&xmlparse'; }
1.113 albertel 714: if ($string eq '&chemparse') { return '&chemparse'; }
1.47 albertel 715: my ($target,$token,$tagstack,$parstack,$oldparser,$safeeval,$style)=
716: @Apache::scripttag::parser_env;
717: my @parser;
718: &Apache::lonxml::newparser(\@parser,\$string);
1.132 albertel 719: &Apache::lonxml::startredirection();
1.47 albertel 720: my $result=&Apache::lonxml::inner_xmlparse($target,$tagstack,
721: $parstack,\@parser,
722: $safeeval,$style);
1.132 albertel 723: $result.=&Apache::lonxml::endredirection();
1.122 albertel 724: &Apache::lonxml::debug("target is $target xmlparse recursion ending with $result");
1.47 albertel 725: return $result;
726: }
1.3 albertel 727:
1.122 albertel 728: sub start_num {
729: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
730: my $result = '';
731: my $inside = &Apache::lonxml::get_all_text_unbalanced("/num",$parser);
1.137 albertel 732: if ($target eq 'tex' || $target eq 'web' || $target eq 'webgrade') {
1.122 albertel 733: $inside=&Apache::run::evaluate($inside,$safeeval,$$parstack[-1]);
734: if (!$Apache::lonxml::default_homework_loaded) {
735: &Apache::lonxml::default_homework_load($safeeval);
736: }
737: @Apache::scripttag::parser_env = @_;
738: my $format=&Apache::lonxml::get_param('format',$parstack,$safeeval);
739: $result=&Apache::run::run("return &prettyprint(q\0$inside\0,q\0$format\0);",$safeeval);
740: }
741: return $result;
742: }
743:
744: sub end_num {
745: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
746: my $result = '';
747: return $result;
748: }
749:
750: sub start_parse {
751: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
752: my $result = '';
1.137 albertel 753: if ( $target eq 'web' || $target eq 'tex' ||
754: $target eq 'grade' || $target eq 'answer' ||
755: $target eq 'analyze'|| $target eq 'webgrade') {
1.122 albertel 756: my $inside = &Apache::lonxml::get_all_text_unbalanced("/parse",$parser);
757: $inside=&Apache::run::evaluate($inside,$safeeval,$$parstack[-1]);
758: if (!$Apache::lonxml::default_homework_loaded) {
759: &Apache::lonxml::default_homework_load($safeeval);
760: }
761: @Apache::scripttag::parser_env = @_;
762: $result=&Apache::run::run("return &xmlparse(q\0$inside\0);",$safeeval);
1.134 albertel 763: if ($target eq 'grade' || $target eq 'answer' ||
764: $target eq 'analyze') {
765: # grade/answer/analyxe should produce no output but if we
766: # are redirecting, the redirecter should know what to do
767: # with the output
768: if (!$Apache::lonxml::redirection) { $result=''; }
769: }
1.122 albertel 770: }
771: return $result;
772: }
773:
774: sub end_parse {
775: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
776: my $result = '';
777: return $result;
778: }
1.123 albertel 779:
780: sub start_algebra {
781: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
782: my $result = '';
1.137 albertel 783: if ( $target eq 'web' || $target eq 'tex' ||
784: $target eq 'grade' || $target eq 'answer' ||
785: $target eq 'analyze' || $target eq 'webgrade') {
1.123 albertel 786: my $inside = &Apache::lonxml::get_all_text_unbalanced("/algebra",$parser);
787: $inside = &Apache::run::evaluate($inside,$safeeval,$$parstack[-1]);
788: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
1.125 albertel 789: my $style=&Apache::lonxml::get_param('style',$parstack,$safeeval);
1.157 raeburn 790: $result=&Apache::lontexconvert::algebra($inside,$target,$style,$parstack,$safeeval);
1.123 albertel 791: }
792: $Apache::lonxml::post_evaluate=0;
793: }
794: return $result;
795: }
796:
797: sub end_algebra {
798: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
799: my $result = '';
800: return $result;
801: }
802:
1.1 albertel 803: 1;
804: __END__
1.149 jms 805:
806: =pod
807:
808: =head1 NAME
809:
810: Apache::scripttag.pm
811:
812: =head1 SYNOPSIS
813:
814: implements <script>, <scriptlib>, <parserlib>,
815: and <import>
816:
817: This is part of the LearningOnline Network with CAPA project
818: described at http://www.lon-capa.org.
819:
820: =cut
821:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>