Annotation of loncom/xml/lonxml.pm, revision 1.195
1.2 sakharuk 1: # The LearningOnline Network with CAPA
1.3 sakharuk 2: # XML Parser Module
1.2 sakharuk 3: #
1.195 ! www 4: # $Id: lonxml.pm,v 1.194 2002/09/10 20:53:36 www Exp $
1.139 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: #
28: # Copyright for TtHfunc and TtMfunc by Ian Hutchinson.
29: # TtHfunc and TtMfunc (the "Code") may be compiled and linked into
30: # binary executable programs or libraries distributed by the
31: # Michigan State University (the "Licensee"), but any binaries so
32: # distributed are hereby licensed only for use in the context
33: # of a program or computational system for which the Licensee is the
34: # primary author or distributor, and which performs substantial
35: # additional tasks beyond the translation of (La)TeX into HTML.
36: # The C source of the Code may not be distributed by the Licensee
37: # to any other parties under any circumstances.
38: #
1.3 sakharuk 39: # last modified 06/26/00 by Alexander Sakharuk
1.33 www 40: # 11/6 Gerd Kortemeyer
1.45 www 41: # 6/1/1 Gerd Kortemeyer
1.56 albertel 42: # 2/21,3/13 Guy
1.68 www 43: # 3/29,5/4 Gerd Kortemeyer
1.73 harris41 44: # 5/10 Scott Harrison
1.78 www 45: # 5/26 Gerd Kortemeyer
1.80 harris41 46: # 5/27 H. K. Ng
1.89 www 47: # 6/2,6/3,6/8,6/9 Gerd Kortemeyer
1.93 ng 48: # 6/12,6/13 H. K. Ng
1.95 www 49: # 6/16 Gerd Kortemeyer
1.104 ng 50: # 7/27 H. K. Ng
1.127 www 51: # 8/7,8/9,8/10,8/11,8/15,8/16,8/17,8/18,8/20,8/23,8/24 Gerd Kortemeyer
1.130 www 52: # Guy Albertelli
53: # 9/26 Gerd Kortemeyer
1.143 www 54: # Dec Guy Albertelli
55: # YEAR=2002
56: # 1/1 Gerd Kortemeyer
1.145 www 57: # 1/2 Matthew Hall
58: # 1/3 Gerd Kortemeyer
1.143 www 59: #
1.2 sakharuk 60:
1.4 albertel 61: package Apache::lonxml;
1.33 www 62: use vars
1.169 albertel 63: qw(@pwd @outputstack $redirection $import @extlinks $metamode $evaluate %insertlist @namespace $prevent_entity_encode);
1.1 sakharuk 64: use strict;
1.167 albertel 65: use HTML::LCParser();
1.161 albertel 66: use HTML::TreeBuilder();
67: use HTML::Entities();
68: use Safe();
69: use Safe::Hole();
70: use Math::Cephes();
71: use Math::Random();
72: use Opcode();
1.72 albertel 73:
74: sub register {
1.141 albertel 75: my ($space,@taglist) = @_;
76: foreach my $temptag (@taglist) {
77: push(@{ $Apache::lonxml::alltags{$temptag} },$space);
1.72 albertel 78: }
79: }
80:
1.141 albertel 81: sub deregister {
82: my ($space,@taglist) = @_;
83: foreach my $temptag (@taglist) {
84: my $tempspace = $Apache::lonxml::alltags{$temptag}[-1];
85: if ($tempspace eq $space) {
86: pop(@{ $Apache::lonxml::alltags{$temptag} });
87: }
88: }
1.142 albertel 89: #&printalltags();
1.141 albertel 90: }
91:
1.46 www 92: use Apache::Constants qw(:common);
1.161 albertel 93: use Apache::lontexconvert();
94: use Apache::style();
95: use Apache::run();
96: use Apache::londefdef();
97: use Apache::scripttag();
98: use Apache::edit();
99: use Apache::lonnet();
100: use Apache::File();
101: use Apache::loncommon();
1.79 www 102:
1.72 albertel 103: #================================================== Main subroutine: xmlparse
104: #debugging control, to turn on debugging modify the correct handler
105: $Apache::lonxml::debug=0;
106:
107: #path to the directory containing the file currently being processed
108: @pwd=();
109:
110: #these two are used for capturing a subset of the output for later processing,
111: #don't touch them directly use &startredirection and &endredirection
112: @outputstack = ();
113: $redirection = 0;
114:
115: #controls wheter the <import> tag actually does
116: $import = 1;
117: @extlinks=();
118:
119: # meta mode is a bit weird only some output is to be turned off
120: #<output> tag turns metamode off (defined in londefdef.pm)
121: $metamode = 0;
122:
123: # turns on and of run::evaluate actually derefencing var refs
124: $evaluate = 1;
1.7 albertel 125:
1.74 albertel 126: # data structure for eidt mode, determines what tags can go into what other tags
127: %insertlist=();
1.68 www 128:
1.99 albertel 129: # stores the list of active tag namespaces
1.76 albertel 130: @namespace=();
131:
1.169 albertel 132: # if 0 all high ASCII characters will be encoded into HTML Entities
133: $prevent_entity_encode=0;
134:
1.99 albertel 135: # has the dynamic menu been updated to know about this resource
136: $Apache::lonxml::registered=0;
137:
1.172 albertel 138: # a pointer the the Apache request object
139: $Apache::lonxml::request='';
140:
1.68 www 141: sub xmlbegin {
142: my $output='';
143: if ($ENV{'browser.mathml'}) {
144: $output='<?xml version="1.0"?>'
145: .'<?xml-stylesheet type="text/css" href="/adm/MathML/mathml.css"?>'
146: .'<!DOCTYPE html SYSTEM "/adm/MathML/mathml.dtd" '
147: .'[<!ENTITY mathns "http://www.w3.org/1998/Math/MathML">]>'
148: .'<html xmlns:math="http://www.w3.org/1998/Math/MathML" '
149: .'xmlns="http://www.w3.org/TR/REC-html40">';
150: } else {
151: $output='<html>';
152: }
153: return $output;
154: }
155:
156: sub xmlend {
1.194 www 157: my ($discussiononly,$symb)=@_;
1.103 www 158: my $discussion='';
159: if ($ENV{'request.course.id'}) {
1.109 www 160: my $crs='/'.$ENV{'request.course.id'};
161: if ($ENV{'request.course.sec'}) {
162: $crs.='_'.$ENV{'request.course.sec'};
163: }
164: $crs=~s/\_/\//g;
165: my $seeid=&Apache::lonnet::allowed('rin',$crs);
1.194 www 166: unless ($symb) {
167: $symb=&Apache::lonnet::symbread();
168: }
1.103 www 169: if ($symb) {
170: my %contrib=&Apache::lonnet::restore($symb,$ENV{'request.course.id'},
171: $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
172: $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
173: if ($contrib{'version'}) {
1.194 www 174: unless ($discussiononly) {
175: $discussion.=
176: '<address><hr />';
177: }
1.103 www 178: my $idx;
179: for ($idx=1;$idx<=$contrib{'version'};$idx++) {
1.110 www 180: my $hidden=($contrib{'hidden'}=~/\.$idx\./);
181: unless (($hidden) && (!$seeid)) {
182: my $message=$contrib{$idx.':message'};
183: $message=~s/\n/\<br \/\>/g;
1.186 www 184: $message=&Apache::lontexconvert::msgtexconverted($message);
1.110 www 185: if ($message) {
186: if ($hidden) {
187: $message='<font color="#888888">'.$message.'</font>';
188: }
1.109 www 189: my $sender='Anonymous';
190: if ((!$contrib{$idx.':anonymous'}) || ($seeid)) {
1.194 www 191: $sender=&Apache::loncommon::aboutmewrapper(
192: $contrib{$idx.':plainname'},
193: $contrib{$idx.':sendername'},
194: $contrib{$idx.':senderdomain'}).' ('.
1.164 www 195: $contrib{$idx.':sendername'}.' at '.
196: $contrib{$idx.':senderdomain'}.')';
1.109 www 197: if ($contrib{$idx.':anonymous'}) {
1.164 www 198: $sender.=' [anonymous] '.
199: $contrib{$idx.':screenname'};
1.110 www 200: }
201: if ($seeid) {
202: if ($hidden) {
203: $sender.=' <a href="/adm/feedback?unhide='.
204: $symb.':::'.$idx.'">Make Visible</a>';
205: } else {
206: $sender.=' <a href="/adm/feedback?hide='.
207: $symb.':::'.$idx.'">Hide</a>';
208: }
1.109 www 209: }
1.164 www 210: } else {
211: if ($contrib{$idx.':screenname'}) {
212: $sender='<i>'.$contrib{$idx.':screenname'}.'</i>';
213: }
1.109 www 214: }
215: $discussion.='<p><b>'.$sender.'</b> ('.
1.103 www 216: localtime($contrib{$idx.':timestamp'}).
217: '):<blockquote>'.$message.
1.110 www 218: '</blockquote></p>';
219: }
220: }
1.103 www 221: }
1.194 www 222: unless ($discussiononly) {
223: $discussion.='</address>';
224: }
225: }
226: if ($discussiononly) {
1.195 ! www 227: $discussion.=(<<ENDDISCUSS);
! 228: <form action="/adm/feedback" method="post">
! 229: <input type="submit" name="discuss" value="Post Discussion" />
! 230: <input type="submit" name="anondiscuss" value="Post Anonymous Discussion" />
! 231: <input type="hidden" name="symb" value="$symb" />
! 232: <input type="hidden" name="sendit" value="true" />
! 233: <br />
! 234: <font size="1">Note: in anonymous discussion, your name is visible only to
! 235: course faculty</font><br />
! 236: <textarea name=comment cols=60 rows=10 wrap=hard></textarea>
! 237: </form>
! 238: ENDDISCUSS
1.103 www 239: }
240: }
241: }
1.194 www 242: return $discussion.($discussiononly?'':'</html>');
1.119 www 243: }
244:
245: sub tokeninputfield {
1.120 www 246: my $defhost=$Apache::lonnet::perlvar{'lonHostID'};
247: $defhost=~tr/a-z/A-Z/;
1.119 www 248: return (<<ENDINPUTFIELD)
1.120 www 249: <script>
250: function updatetoken() {
251: var comp=new Array;
252: var barcode=unescape(document.tokeninput.barcode.value);
253: comp=barcode.split('*');
254: if (typeof(comp[0])!="undefined") {
255: document.tokeninput.codeone.value=comp[0];
256: }
257: if (typeof(comp[1])!="undefined") {
258: document.tokeninput.codetwo.value=comp[1];
259: }
260: if (typeof(comp[2])!="undefined") {
261: comp[2]=comp[2].toUpperCase();
262: document.tokeninput.codethree.value=comp[2];
263: }
264: document.tokeninput.barcode.value='';
265: }
266: </script>
267: <form method="post" name="tokeninput">
1.119 www 268: <table border="2" bgcolor="#FFFFBB">
269: <tr><th>DocID Checkin</th></tr>
270: <tr><td>
271: <table>
272: <tr>
273: <td>Scan in Barcode</td>
1.120 www 274: <td><input type="text" size="22" name="barcode"
275: onChange="updatetoken()"/></td>
1.119 www 276: </tr>
277: <tr><td><i>or</i> Type in DocID</td>
278: <td>
279: <input type="text" size="5" name="codeone" />
1.120 www 280: <b><font size="+2">*</font></b>
1.119 www 281: <input type="text" size="5" name="codetwo" />
1.120 www 282: <b><font size="+2">*</font></b>
283: <input type="text" size="10" name="codethree" value="$defhost"
284: onChange="this.value=this.value.toUpperCase()" />
1.119 www 285: </td></tr>
286: </table>
287: </td></tr>
288: <tr><td><input type="submit" value="Check in DocID" /></td></tr>
289: </table>
290: </form>
291: ENDINPUTFIELD
1.112 www 292: }
293:
1.116 www 294: sub maketoken {
1.118 www 295: my ($symb,$tuname,$tudom,$tcrsid)=@_;
1.112 www 296: unless ($symb) {
297: $symb=&Apache::lonnet::symbread();
298: }
299: unless ($tuname) {
300: $tuname=$ENV{'user.name'};
301: $tudom=$ENV{'user.domain'};
302: $tcrsid=$ENV{'request.course.id'};
303: }
1.116 www 304:
1.118 www 305: return &Apache::lonnet::checkout($symb,$tuname,$tudom,$tcrsid);
306: }
307:
308: sub printtokenheader {
1.133 albertel 309: my ($target,$token,$tsymb,$tcrsid,$tudom,$tuname)=@_;
1.116 www 310: unless ($token) { return ''; }
1.118 www 311:
1.133 albertel 312: my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
313: unless ($tsymb) {
314: $tsymb=$symb;
1.118 www 315: }
316: unless ($tuname) {
1.133 albertel 317: $tuname=$name;
318: $tudom=$domain;
319: $tcrsid=$courseid;
1.118 www 320: }
1.114 www 321:
322: my %reply=&Apache::lonnet::get('environment',
323: ['firstname','middlename','lastname','generation'],
324: $tudom,$tuname);
325: my $plainname=$reply{'firstname'}.' '.
326: $reply{'middlename'}.' '.
327: $reply{'lastname'}.' '.
328: $reply{'generation'};
329:
1.112 www 330: if ($target eq 'web') {
1.145 www 331: my %idhash=&Apache::lonnet::idrget($tudom,($tuname));
1.115 www 332: return
333: '<img align="right" src="/cgi-bin/barcode.gif?encode='.$token.'" />'.
334: 'Checked out for '.$plainname.
1.114 www 335: '<br />User: '.$tuname.' at '.$tudom.
1.145 www 336: '<br />ID: '.$idhash{$tuname}.
1.117 www 337: '<br />CourseID: '.$tcrsid.
1.145 www 338: '<br />Course: '.$ENV{'course.'.$tcrsid.'.description'}.
1.114 www 339: '<br />DocID: '.$token.
1.116 www 340: '<br />Time: '.localtime().'<hr />';
1.112 www 341: } else {
1.121 albertel 342: return $token;
1.112 www 343: }
1.68 www 344: }
345:
1.70 www 346: sub fontsettings() {
347: my $headerstring='';
348: if (($ENV{'browser.os'} eq 'mac') && (!$ENV{'browser.mathml'})) {
349: $headerstring.=
350: '<meta Content-Type="text/html; charset=x-mac-roman">';
351: }
352: return $headerstring;
353: }
354:
1.68 www 355: sub registerurl {
1.100 www 356: my $forcereg=shift;
1.155 matthew 357: my $target = shift;
358: my $result = '';
1.179 matthew 359: if ($target eq 'edit') {
1.187 matthew 360: $result .="<script>\n".
361: "if (typeof menu != 'undefined') {menu.currentURL=null;}\n".
1.179 matthew 362: &Apache::loncommon::browser_and_searcher_javascript().
363: "\n</script>\n";
364: }
1.176 www 365: if ((($ENV{'request.publicaccess'}) ||
366: (!&Apache::lonnet::is_on_map($ENV{'REQUEST_URI'}))) &&
367: (!$forcereg)) {
1.179 matthew 368: return $result.
1.130 www 369: '<script>function LONCAPAreg(){} function LONCAPAstale(){}</script>';
370: }
1.128 albertel 371: if ($Apache::lonxml::registered && !$forcereg) { return ''; }
1.105 albertel 372: $Apache::lonxml::registered=1;
1.159 www 373: my $nothing='';
374: if ($ENV{'browser.type'} eq 'explorer') { $nothing='javascript:void(0);'; }
1.181 www 375: my $timesync='menu.syncclock(1000*'.time.');';
1.100 www 376: if (($ENV{'REQUEST_URI'}!~/^\/(res\/)*adm\//) || ($forcereg)) {
1.87 www 377: my $hwkadd='';
1.168 albertel 378: if ($ENV{'request.filename'}=~/\.(problem|exam|quiz|assess|survey|form)$/) {
1.87 www 379: if (&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'})) {
380: $hwkadd.=(<<ENDSUBM);
1.168 albertel 381: menu.switchbutton(7,1,'subm.gif','view sub','missions','gocmd("/adm/grades","submission")');
1.87 www 382: ENDSUBM
383: }
384: if (&Apache::lonnet::allowed('mgr',$ENV{'request.course.id'})) {
385: $hwkadd.=(<<ENDGRDS);
1.171 albertel 386: menu.switchbutton(7,2,'pgrd.gif','problem','grades','gocmd("/adm/grades","gradingmenu")');
1.87 www 387: ENDGRDS
388: }
389: if (&Apache::lonnet::allowed('opa',$ENV{'request.course.id'})) {
390: $hwkadd.=(<<ENDPARM);
1.168 albertel 391: menu.switchbutton(7,3,'pparm.gif','problem','parms','gocmd("/adm/parmset","set")');
1.87 www 392: ENDPARM
393: }
394: }
1.155 matthew 395: $result = (<<ENDREGTHIS);
1.87 www 396:
1.68 www 397: <script language="JavaScript">
1.71 www 398: // BEGIN LON-CAPA Internal
1.86 www 399:
1.69 www 400: function LONCAPAreg() {
1.159 www 401: menu=window.open("$nothing","LONCAPAmenu","",false);
1.86 www 402: menu.clearTimeout(menu.menucltim);
1.174 www 403: $timesync
1.69 www 404: menu.currentURL=window.location.pathname;
1.175 www 405: menu.reloadURL=window.location.pathname;
1.69 www 406: menu.currentStale=0;
1.85 www 407: menu.clearbut(3,1);
408: menu.switchbutton
1.108 www 409: (6,3,'catalog.gif','catalog','info','catalog_info()');
410: menu.switchbutton
1.85 www 411: (8,1,'eval.gif','evaluate','this','gopost("/adm/evaluate",currentURL)');
412: menu.switchbutton
1.184 www 413: (8,2,'fdbk.gif','feedback','discuss','gopost("/adm/feedback",currentURL)');
1.85 www 414: menu.switchbutton
415: (8,3,'prt.gif','prepare','printout','gopost("/adm/printout",currentURL)');
416: menu.switchbutton
417: (2,1,'back.gif','backward','','gopost("/adm/flip","back:"+currentURL)');
418: menu.switchbutton
419: (2,3,'forw.gif','forward','','gopost("/adm/flip","forward:"+currentURL)');
1.94 www 420: menu.switchbutton
421: (9,1,'sbkm.gif','set','bookmark','set_bookmark()');
422: menu.switchbutton
423: (9,2,'vbkm.gif','view','bookmark','edit_bookmarks()');
424: menu.switchbutton
1.95 www 425: (9,3,'anot.gif','anno-','tations','annotate()');
1.87 www 426: $hwkadd
1.69 www 427: }
1.86 www 428:
1.69 www 429: function LONCAPAstale() {
1.159 www 430: menu=window.open("$nothing","LONCAPAmenu","",false);
1.86 www 431: menu.currentStale=1;
1.175 www 432: if (menu.reloadURL!='' && menu.reloadURL!= null) {
433: menu.switchbutton
434: (3,1,'reload.gif','return','location','go(reloadURL)');
435: }
1.127 www 436: menu.clearbut(7,1);
437: menu.clearbut(7,2);
438: menu.clearbut(7,3);
1.86 www 439: menu.menucltim=menu.setTimeout(
1.94 www 440: 'clearbut(2,1);clearbut(2,3);clearbut(8,1);clearbut(8,2);clearbut(8,3);'+
1.108 www 441: 'clearbut(9,1);clearbut(9,2);clearbut(9,3);clearbut(6,3)',
1.86 www 442: 2000);
443:
1.87 www 444: }
1.86 www 445:
446: // END LON-CAPA Internal
447: </script>
448: ENDREGTHIS
449:
450: } else {
1.155 matthew 451: $result = (<<ENDDONOTREGTHIS);
1.86 www 452:
453: <script language="JavaScript">
454: // BEGIN LON-CAPA Internal
455:
456: function LONCAPAreg() {
1.159 www 457: menu=window.open("$nothing","LONCAPAmenu","",false);
1.174 www 458: $timesync
1.69 www 459: menu.currentStale=1;
1.85 www 460: menu.clearbut(2,1);
461: menu.clearbut(2,3);
462: menu.clearbut(8,1);
463: menu.clearbut(8,2);
464: menu.clearbut(8,3);
1.86 www 465: if (menu.currentURL) {
466: menu.switchbutton
467: (3,1,'reload.gif','return','location','go(currentURL)');
468: } else {
469: menu.clearbut(3,1);
470: }
471: }
472:
473: function LONCAPAstale() {
1.68 www 474: }
1.86 www 475:
1.71 www 476: // END LON-CAPA Internal
1.68 www 477: </script>
1.86 www 478: ENDDONOTREGTHIS
1.155 matthew 479: }
480: return $result;
1.69 www 481: }
482:
483: sub loadevents() {
484: return 'LONCAPAreg();';
485: }
486:
487: sub unloadevents() {
488: return 'LONCAPAstale();';
1.68 www 489: }
490:
1.48 albertel 491: sub printalltags {
492: my $temp;
493: foreach $temp (sort keys %Apache::lonxml::alltags) {
1.141 albertel 494: &Apache::lonxml::debug("$temp -- ".
495: join(',',@{ $Apache::lonxml::alltags{$temp} }));
1.48 albertel 496: }
497: }
1.31 sakharuk 498:
1.3 sakharuk 499: sub xmlparse {
1.172 albertel 500: my ($request,$target,$content_file_string,$safeinit,%style_for_target) = @_;
1.96 albertel 501:
1.172 albertel 502: &setup_globals($request,$target);
1.178 www 503: #
504: # do we have a course style file?
505: #
506:
507: if ($ENV{'request.course.id'}) {
508: my $bodytext=
509: $ENV{'course.'.$ENV{'request.course.id'}.'.default_xml_style'};
510: if ($bodytext) {
511: my $location=&Apache::lonnet::filelocation('',$bodytext);
512: my $styletext=&Apache::lonnet::getfile($location);
513: if ($styletext ne '-1') {
514: %style_for_target = (%style_for_target,
515: &Apache::style::styleparser($target,$styletext));
516: }
517: }
518: }
519:
1.48 albertel 520: #&printalltags();
1.16 albertel 521: my @pars = ();
1.23 albertel 522: my $pwd=$ENV{'request.filename'};
523: $pwd =~ s:/[^/]*$::;
524: &newparser(\@pars,\$content_file_string,$pwd);
1.24 sakharuk 525:
1.3 sakharuk 526: my $safeeval = new Safe;
1.40 albertel 527: my $safehole = new Safe::Hole;
1.82 ng 528: &init_safespace($target,$safeeval,$safehole,$safeinit);
1.3 sakharuk 529: #-------------------- Redefinition of the target in the case of compound target
530:
531: ($target, my @tenta) = split('&&',$target);
532:
1.150 albertel 533: my @stack = ();
1.3 sakharuk 534: my @parstack = ();
1.17 albertel 535: &initdepth;
1.67 www 536:
1.101 albertel 537: my $finaloutput = &inner_xmlparse($target,\@stack,\@parstack,\@pars,
538: $safeeval,\%style_for_target);
1.125 www 539: if ($ENV{'request.uri'}) {
540: &writeallows($ENV{'request.uri'});
541: }
1.3 sakharuk 542: return $finaloutput;
1.106 www 543: }
544:
545: sub htmlclean {
1.107 www 546: my ($raw,$full)=@_;
1.106 www 547:
548: my $tree = HTML::TreeBuilder->new;
549: $tree->ignore_unknown(0);
1.140 albertel 550:
1.106 www 551: $tree->parse($raw);
552:
1.107 www 553: my $output= $tree->as_HTML(undef,' ');
1.140 albertel 554:
1.161 albertel 555: $output=~s/\<(br|hr|img|meta|allow)(.*?)\>/\<$1$2 \/\>/gis;
1.111 www 556: $output=~s/\<\/(br|hr|img|meta|allow)\>//gis;
1.107 www 557: unless ($full) {
558: $output=~s/\<[\/]*(body|head|html)\>//gis;
559: }
1.106 www 560:
561: $tree = $tree->delete;
562:
563: return $output;
1.15 albertel 564: }
565:
1.191 albertel 566: sub latex_special_symbols {
1.188 sakharuk 567: my ($current_token,$stack,$parstack)=@_;
1.192 albertel 568: $current_token=~s/\\/\\char92 /g;
569: $current_token=~s/\^/\\char94 /g;
570: $current_token=~s/\~/\\char126 /g;
571: $current_token=~s/(&[^a-z#])/\\$1/g;
572: $current_token=~s/([^&]\#)/\\$1/g;
573: $current_token=~s/(\$|_|{|})/\\$1/g;
574: $current_token=~s/\\char92 /\\texttt{\\char92}/g;
575: $current_token=~s/>/\$>\$/g; #more
576: $current_token=~s/</\$<\$/g; #less
577: if ($current_token=~m/\d%/) {$current_token =~ s/(\d)%/$1\\%/g;} #percent after digit
578: if ($current_token=~m/\s%/) {$current_token =~ s/(\s)%/$1\\%/g;} #persent after space
1.188 sakharuk 579: return $current_token;
580: }
581:
1.101 albertel 582: sub inner_xmlparse {
583: my ($target,$stack,$parstack,$pars,$safeeval,$style_for_target)=@_;
584: my $finaloutput = '';
585: my $result;
586: my $token;
587: while ( $#$pars > -1 ) {
588: while ($token = $$pars['-1']->get_token) {
589: if (($token->[0] eq 'T') || ($token->[0] eq 'C') || ($token->[0] eq 'D') ) {
590: if ($metamode<1) {
1.190 albertel 591: my $text=$token->[1];
1.193 albertel 592: if ($token->[0] eq 'C' && $target eq 'tex') {
1.190 albertel 593: $text = '%'.$text;
594: $text =~ s/[\n\r]//g;
1.182 sakharuk 595: }
1.190 albertel 596: $result.=$text;
1.101 albertel 597: }
598: } elsif ($token->[0] eq 'PI') {
599: if ($metamode<1) {
600: $result=$token->[2];
601: }
602: } elsif ($token->[0] eq 'S') {
1.140 albertel 603: # add tag to stack
1.101 albertel 604: push (@$stack,$token->[1]);
605: # add parameters list to another stack
606: push (@$parstack,&parstring($token));
1.140 albertel 607: &increasedepth($token);
1.101 albertel 608: if (exists $$style_for_target{$token->[1]}) {
609: if ($Apache::lonxml::redirection) {
1.140 albertel 610: $Apache::lonxml::outputstack['-1'] .=
1.101 albertel 611: &recurse($$style_for_target{$token->[1]},$target,$safeeval,
612: $style_for_target,@$parstack);
613: } else {
614: $finaloutput .= &recurse($$style_for_target{$token->[1]},$target,
615: $safeeval,$style_for_target,@$parstack);
616: }
617: } else {
618: $result = &callsub("start_$token->[1]", $target, $token, $stack,
619: $parstack, $pars, $safeeval, $style_for_target);
1.140 albertel 620: }
1.101 albertel 621: } elsif ($token->[0] eq 'E') {
622: #clear out any tags that didn't end
623: while ($token->[1] ne $$stack['-1'] && ($#$stack > -1)) {
1.150 albertel 624: my $lasttag=$$stack[-1];
625: if ($token->[1] =~ /^$lasttag$/i) {
626: &Apache::lonxml::warning('Using tag </'.$token->[1].'> as end tag to <'.$$stack[-1].'>');
627: last;
628: } else {
1.152 albertel 629: &Apache::lonxml::warning('Found tag </'.$token->[1].'> when looking for </'.$$stack[-1].'> in file');
1.150 albertel 630: &end_tag($stack,$parstack,$token);
631: }
1.101 albertel 632: }
1.140 albertel 633:
634: if (exists($$style_for_target{'/'."$token->[1]"})) {
1.101 albertel 635: if ($Apache::lonxml::redirection) {
636: $Apache::lonxml::outputstack['-1'] .=
637: &recurse($$style_for_target{'/'."$token->[1]"},
638: $target,$safeeval,$style_for_target,@$parstack);
639: } else {
640: $finaloutput .= &recurse($$style_for_target{'/'."$token->[1]"},
641: $target,$safeeval,$style_for_target,
642: @$parstack);
643: }
644: } else {
645: $result = &callsub("end_$token->[1]", $target, $token, $stack,
646: $parstack, $pars,$safeeval, $style_for_target);
647: }
648: } else {
649: &Apache::lonxml::error("Unknown token event :$token->[0]:$token->[1]:");
650: }
651: #evaluate variable refs in result
652: if ($result ne "") {
653: if ( $#$parstack > -1 ) {
1.169 albertel 654: $result=&Apache::run::evaluate($result,$safeeval,$$parstack[-1]);
1.101 albertel 655: } else {
1.169 albertel 656: $result= &Apache::run::evaluate($result,$safeeval,'');
1.101 albertel 657: }
1.163 albertel 658: }
1.190 albertel 659: if (($token->[0] eq 'T') || ($token->[0] eq 'C') || ($token->[0] eq 'D') ) {
660: if ($target eq 'tex') {
1.191 albertel 661: $result=&latex_special_symbols($result,$stack,$parstack);
1.190 albertel 662: }
663: }
664:
1.169 albertel 665: # Encode any high ASCII characters
666: if (!$Apache::lonxml::prevent_entity_encode) {
667: $result=&HTML::Entities::encode($result,"\200-\377");
668: }
669: if ($Apache::lonxml::redirection) {
670: $Apache::lonxml::outputstack['-1'] .= $result;
671: } else {
672: $finaloutput.=$result;
673: }
674: $result = '';
675:
1.101 albertel 676: if ($token->[0] eq 'E') {
677: &end_tag($stack,$parstack,$token);
678: }
679: }
680: pop @$pars;
681: pop @Apache::lonxml::pwd;
682: }
683:
684: # if ($target eq 'meta') {
685: # $finaloutput.=&endredirection;
686: # }
687:
1.169 albertel 688:
1.101 albertel 689: if (($ENV{'QUERY_STRING'}) && ($target eq 'web')) {
690: $finaloutput=&afterburn($finaloutput);
691: }
692: return $finaloutput;
693: }
1.67 www 694:
1.15 albertel 695: sub recurse {
696: my @innerstack = ();
697: my @innerparstack = ();
698: my ($newarg,$target,$safeeval,$style_for_target,@parstack) = @_;
1.16 albertel 699: my @pat = ();
1.23 albertel 700: &newparser(\@pat,\$newarg);
1.15 albertel 701: my $tokenpat;
702: my $partstring = '';
703: my $output='';
1.16 albertel 704: my $decls='';
1.140 albertel 705: &Apache::lonxml::debug("Recursing");
1.16 albertel 706: while ( $#pat > -1 ) {
707: while ($tokenpat = $pat[$#pat]->get_token) {
1.57 albertel 708: if (($tokenpat->[0] eq 'T') || ($tokenpat->[0] eq 'C') || ($tokenpat->[0] eq 'D') ) {
1.61 albertel 709: if ($metamode<1) { $partstring=$tokenpat->[1]; }
1.57 albertel 710: } elsif ($tokenpat->[0] eq 'PI') {
1.61 albertel 711: if ($metamode<1) { $partstring=$tokenpat->[2]; }
1.16 albertel 712: } elsif ($tokenpat->[0] eq 'S') {
713: push (@innerstack,$tokenpat->[1]);
714: push (@innerparstack,&parstring($tokenpat));
1.19 albertel 715: &increasedepth($tokenpat);
1.84 albertel 716: $partstring = &callsub("start_$tokenpat->[1]", $target, $tokenpat,
717: \@innerstack, \@innerparstack, \@pat,
718: $safeeval, $style_for_target);
1.16 albertel 719: } elsif ($tokenpat->[0] eq 'E') {
720: #clear out any tags that didn't end
1.150 albertel 721: while ($tokenpat->[1] ne $innerstack[$#innerstack]
1.43 albertel 722: && ($#innerstack > -1)) {
1.150 albertel 723: my $lasttag=$innerstack[-1];
724: if ($tokenpat->[1] =~ /^$lasttag$/i) {
725: &Apache::lonxml::warning('Using tag </'.$tokenpat->[1].'> as end tag to <'.$innerstack[-1].'>');
726: last;
727: } else {
1.152 albertel 728: &Apache::lonxml::warning('Found tag </'.$tokenpat->[1].'> when looking for </'.$innerstack[-1].'> in file');
1.150 albertel 729: &end_tag(\@innerstack,\@innerparstack,$tokenpat);
730: }
1.43 albertel 731: }
1.84 albertel 732: $partstring = &callsub("end_$tokenpat->[1]", $target, $tokenpat,
733: \@innerstack, \@innerparstack, \@pat,
734: $safeeval, $style_for_target);
1.57 albertel 735: } else {
736: &Apache::lonxml::error("Unknown token event :$tokenpat->[0]:$tokenpat->[1]:");
1.16 albertel 737: }
738: #pass both the variable to the style tag, and the tag we
739: #are processing inside the <definedtag>
740: if ( $partstring ne "" ) {
741: if ( $#parstack > -1 ) {
742: if ( $#innerparstack > -1 ) {
743: $decls= $parstack[$#parstack].$innerparstack[$#innerparstack];
744: } else {
745: $decls= $parstack[$#parstack];
746: }
747: } else {
748: if ( $#innerparstack > -1 ) {
749: $decls=$innerparstack[$#innerparstack];
750: } else {
751: $decls='';
752: }
753: }
754: $output .= &Apache::run::evaluate($partstring,$safeeval,$decls);
755: $partstring = '';
756: }
1.17 albertel 757: if ($tokenpat->[0] eq 'E') { pop @innerstack;pop @innerparstack;
1.19 albertel 758: &decreasedepth($tokenpat);}
1.15 albertel 759: }
1.16 albertel 760: pop @pat;
1.23 albertel 761: pop @Apache::lonxml::pwd;
1.15 albertel 762: }
1.140 albertel 763: &Apache::lonxml::debug("Exiting Recursing");
1.15 albertel 764: return $output;
1.7 albertel 765: }
766:
767: sub callsub {
1.84 albertel 768: my ($sub,$target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.7 albertel 769: my $currentstring='';
1.72 albertel 770: my $nodefault;
1.7 albertel 771: {
1.59 albertel 772: my $sub1;
1.7 albertel 773: no strict 'refs';
1.68 www 774: my $tag=$token->[1];
1.141 albertel 775: my $space=$Apache::lonxml::alltags{$tag}[-1];
1.68 www 776: if (!$space) {
1.141 albertel 777: $tag=~tr/A-Z/a-z/;
1.68 www 778: $sub=~tr/A-Z/a-z/;
1.141 albertel 779: $space=$Apache::lonxml::alltags{$tag}[-1]
1.68 www 780: }
1.97 albertel 781:
782: my $deleted=0;
783: $Apache::lonxml::curdepth=join('_',@Apache::lonxml::depthcounter);
784: if (($token->[0] eq 'S') && ($target eq 'modified')) {
785: $deleted=&Apache::edit::handle_delete($space,$target,$token,$tagstack,
786: $parstack,$parser,$safeeval,
787: $style);
788: }
789: if (!$deleted) {
790: if ($space) {
1.189 sakharuk 791: &Apache::lonxml::debug("Calling sub $sub in $space $metamode");
1.97 albertel 792: $sub1="$space\:\:$sub";
793: ($currentstring,$nodefault) = &$sub1($target,$token,$tagstack,
794: $parstack,$parser,$safeeval,
795: $style);
796: } else {
1.189 sakharuk 797: &Apache::lonxml::debug("NOT Calling sub $sub in $space $metamode");
1.97 albertel 798: if ($metamode <1) {
799: if (defined($token->[4]) && ($metamode < 1)) {
800: $currentstring = $token->[4];
801: } else {
802: $currentstring = $token->[2];
803: }
1.62 sakharuk 804: }
1.7 albertel 805: }
1.97 albertel 806: # &Apache::lonxml::debug("nodefalt:$nodefault:");
807: if ($currentstring eq '' && $nodefault eq '') {
808: if ($target eq 'edit') {
809: &Apache::lonxml::debug("doing default edit for $token->[1]");
810: if ($token->[0] eq 'S') {
811: $currentstring = &Apache::edit::tag_start($target,$token);
812: } elsif ($token->[0] eq 'E') {
813: $currentstring = &Apache::edit::tag_end($target,$token);
814: }
815: } elsif ($target eq 'modified') {
816: if ($token->[0] eq 'S') {
817: $currentstring = $token->[4];
818: $currentstring.=&Apache::edit::handle_insert();
819: } else {
820: $currentstring = $token->[2];
821: }
1.72 albertel 822: }
823: }
1.7 albertel 824: }
825: use strict 'refs';
826: }
827: return $currentstring;
1.82 ng 828: }
829:
1.96 albertel 830: sub setup_globals {
1.172 albertel 831: my ($request,$target)=@_;
832: $Apache::lonxml::request=$request;
1.99 albertel 833: $Apache::lonxml::registered = 0;
1.101 albertel 834: @Apache::lonxml::pwd=();
1.124 albertel 835: @Apache::lonxml::extlinks=();
1.96 albertel 836: if ($target eq 'meta') {
837: $Apache::lonxml::redirection = 0;
838: $Apache::lonxml::metamode = 1;
839: $Apache::lonxml::evaluate = 1;
840: $Apache::lonxml::import = 0;
1.129 albertel 841: } elsif ($target eq 'answer') {
842: $Apache::lonxml::redirection = 0;
843: $Apache::lonxml::metamode = 1;
844: $Apache::lonxml::evaluate = 1;
845: $Apache::lonxml::import = 1;
1.96 albertel 846: } elsif ($target eq 'grade') {
847: &startredirection;
848: $Apache::lonxml::metamode = 0;
849: $Apache::lonxml::evaluate = 1;
850: $Apache::lonxml::import = 1;
851: } elsif ($target eq 'modified') {
852: $Apache::lonxml::redirection = 0;
853: $Apache::lonxml::metamode = 0;
854: $Apache::lonxml::evaluate = 0;
855: $Apache::lonxml::import = 0;
856: } elsif ($target eq 'edit') {
857: $Apache::lonxml::redirection = 0;
858: $Apache::lonxml::metamode = 0;
859: $Apache::lonxml::evaluate = 0;
860: $Apache::lonxml::import = 0;
1.163 albertel 861: } elsif ($target eq 'analyze') {
862: $Apache::lonxml::redirection = 0;
863: $Apache::lonxml::metamode = 0;
864: $Apache::lonxml::evaluate = 1;
865: $Apache::lonxml::import = 1;
1.96 albertel 866: } else {
867: $Apache::lonxml::redirection = 0;
868: $Apache::lonxml::metamode = 0;
869: $Apache::lonxml::evaluate = 1;
870: $Apache::lonxml::import = 1;
871: }
872: }
873:
1.82 ng 874: sub init_safespace {
875: my ($target,$safeeval,$safehole,$safeinit) = @_;
876: $safeeval->permit("entereval");
877: $safeeval->permit(":base_math");
878: $safeeval->permit("sort");
879: $safeeval->deny(":base_io");
1.102 albertel 880: $safehole->wrap(\&Apache::scripttag::xmlparse,$safeeval,'&xmlparse');
1.82 ng 881: $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
882:
883: $safehole->wrap(\&Math::Cephes::asin,$safeeval,'&asin');
884: $safehole->wrap(\&Math::Cephes::acos,$safeeval,'&acos');
885: $safehole->wrap(\&Math::Cephes::atan,$safeeval,'&atan');
886: $safehole->wrap(\&Math::Cephes::sinh,$safeeval,'&sinh');
887: $safehole->wrap(\&Math::Cephes::cosh,$safeeval,'&cosh');
888: $safehole->wrap(\&Math::Cephes::tanh,$safeeval,'&tanh');
889: $safehole->wrap(\&Math::Cephes::asinh,$safeeval,'&asinh');
890: $safehole->wrap(\&Math::Cephes::acosh,$safeeval,'&acosh');
891: $safehole->wrap(\&Math::Cephes::atanh,$safeeval,'&atanh');
892: $safehole->wrap(\&Math::Cephes::erf,$safeeval,'&erf');
893: $safehole->wrap(\&Math::Cephes::erfc,$safeeval,'&erfc');
894: $safehole->wrap(\&Math::Cephes::j0,$safeeval,'&j0');
895: $safehole->wrap(\&Math::Cephes::j1,$safeeval,'&j1');
896: $safehole->wrap(\&Math::Cephes::jn,$safeeval,'&jn');
897: $safehole->wrap(\&Math::Cephes::jv,$safeeval,'&jv');
898: $safehole->wrap(\&Math::Cephes::y0,$safeeval,'&y0');
899: $safehole->wrap(\&Math::Cephes::y1,$safeeval,'&y1');
900: $safehole->wrap(\&Math::Cephes::yn,$safeeval,'&yn');
901: $safehole->wrap(\&Math::Cephes::yv,$safeeval,'&yv');
1.91 ng 902: $safehole->wrap(\&Math::Random::random_beta,$safeeval,'&math_random_beta');
903: $safehole->wrap(\&Math::Random::random_chi_square,$safeeval,'&math_random_chi_square');
904: $safehole->wrap(\&Math::Random::random_exponential,$safeeval,'&math_random_exponential');
905: $safehole->wrap(\&Math::Random::random_f,$safeeval,'&math_random_f');
906: $safehole->wrap(\&Math::Random::random_gamma,$safeeval,'&math_random_gamma');
907: $safehole->wrap(\&Math::Random::random_multivariate_normal,$safeeval,'&math_random_multivariate_normal');
908: $safehole->wrap(\&Math::Random::random_multinomial,$safeeval,'&math_random_multinomial');
909: $safehole->wrap(\&Math::Random::random_noncentral_chi_square,$safeeval,'&math_random_noncentral_chi_square');
910: $safehole->wrap(\&Math::Random::random_noncentral_f,$safeeval,'&math_random_noncentral_f');
911: $safehole->wrap(\&Math::Random::random_normal,$safeeval,'&math_random_normal');
912: $safehole->wrap(\&Math::Random::random_permutation,$safeeval,'&math_random_permutation');
1.93 ng 913: $safehole->wrap(\&Math::Random::random_permuted_index,$safeeval,'&math_random_permuted_index');
1.91 ng 914: $safehole->wrap(\&Math::Random::random_uniform,$safeeval,'&math_random_uniform');
915: $safehole->wrap(\&Math::Random::random_poisson,$safeeval,'&math_random_poisson');
916: $safehole->wrap(\&Math::Random::random_uniform_integer,$safeeval,'&math_random_uniform_integer');
917: $safehole->wrap(\&Math::Random::random_negative_binomial,$safeeval,'&math_random_negative_binomial');
918: $safehole->wrap(\&Math::Random::random_binomial,$safeeval,'&math_random_binomial');
919: $safehole->wrap(\&Math::Random::random_seed_from_phrase,$safeeval,'&random_seed_from_phrase');
920: $safehole->wrap(\&Math::Random::random_set_seed_from_phrase,$safeeval,'&random_set_seed_from_phrase');
921: $safehole->wrap(\&Math::Random::random_get_seed,$safeeval,'&random_get_seed');
922: $safehole->wrap(\&Math::Random::random_set_seed,$safeeval,'&random_set_seed');
923:
1.82 ng 924: #need to inspect this class of ops
925: # $safeeval->deny(":base_orig");
1.91 ng 926: $safeinit .= ';$external::target="'.$target.'";';
1.121 albertel 927: my $rndseed;
1.123 albertel 928: my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
929: $rndseed=&Apache::lonnet::rndseed($symb,$courseid,$domain,$name);
1.121 albertel 930: $safeinit .= ';$external::randomseed='.$rndseed.';';
1.82 ng 931: &Apache::run::run($safeinit,$safeeval);
1.17 albertel 932: }
933:
1.55 albertel 934: sub startredirection {
935: $Apache::lonxml::redirection++;
936: push (@Apache::lonxml::outputstack, '');
937: }
938:
939: sub endredirection {
940: if (!$Apache::lonxml::redirection) {
1.72 albertel 941: &Apache::lonxml::error("Endredirection was called, before a startredirection, perhaps you have unbalanced tags. Some debuging information:".join ":",caller);
1.55 albertel 942: return '';
943: }
944: $Apache::lonxml::redirection--;
945: pop @Apache::lonxml::outputstack;
1.97 albertel 946: }
947:
948: sub end_tag {
949: my ($tagstack,$parstack,$token)=@_;
950: pop(@$tagstack);
951: pop(@$parstack);
952: &decreasedepth($token);
1.55 albertel 953: }
954:
1.17 albertel 955: sub initdepth {
956: @Apache::lonxml::depthcounter=();
957: $Apache::lonxml::depth=-1;
958: $Apache::lonxml::olddepth=-1;
959: }
960:
961: sub increasedepth {
1.19 albertel 962: my ($token) = @_;
1.17 albertel 963: $Apache::lonxml::depth++;
964: $Apache::lonxml::depthcounter[$Apache::lonxml::depth]++;
965: if ($Apache::lonxml::depthcounter[$Apache::lonxml::depth]==1) {
966: $Apache::lonxml::olddepth=$Apache::lonxml::depth;
967: }
1.42 albertel 968: my $curdepth=join('_',@Apache::lonxml::depthcounter);
1.64 albertel 969: &Apache::lonxml::debug("s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n");
1.54 albertel 970: #print "<br />s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n";
1.17 albertel 971: }
972:
973: sub decreasedepth {
1.19 albertel 974: my ($token) = @_;
1.17 albertel 975: $Apache::lonxml::depth--;
1.36 albertel 976: if ($Apache::lonxml::depth<$Apache::lonxml::olddepth-1) {
977: $#Apache::lonxml::depthcounter--;
978: $Apache::lonxml::olddepth=$Apache::lonxml::depth+1;
979: }
1.43 albertel 980: if ( $Apache::lonxml::depth < -1) {
1.140 albertel 981: &Apache::lonxml::warning("Missing tags, unable to properly run file.");
1.43 albertel 982: $Apache::lonxml::depth='-1';
983: }
1.42 albertel 984: my $curdepth=join('_',@Apache::lonxml::depthcounter);
1.64 albertel 985: &Apache::lonxml::debug("e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n");
1.54 albertel 986: #print "<br />e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n";
1.1 sakharuk 987: }
1.19 albertel 988:
1.180 albertel 989: sub get_all_text_unbalanced {
1.190 albertel 990: #there is a copy of this in lonpublisher.pm
1.180 albertel 991: my($tag,$pars)= @_;
992: my $token;
993: my $result='';
994: $tag='<'.$tag.'>';
995: while ($token = $$pars[-1]->get_token) {
996: if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
997: $result.=$token->[1];
998: } elsif ($token->[0] eq 'PI') {
999: $result.=$token->[2];
1000: } elsif ($token->[0] eq 'S') {
1001: $result.=$token->[4];
1002: } elsif ($token->[0] eq 'E') {
1003: $result.=$token->[2];
1004: }
1005: if ($result =~ /(.*)$tag(.*)/) {
1006: &Apache::lonxml::debug('Got a winner with leftovers ::'.$2);
1007: &Apache::lonxml::debug('Result is :'.$1);
1008: $result=$1;
1009: my $redo=$tag.$2;
1010: &Apache::lonxml::newparser($pars,\$redo);
1011: last;
1012: }
1013: }
1014: return $result
1015: }
1016:
1.19 albertel 1017: sub get_all_text {
1018: my($tag,$pars)= @_;
1019: my $depth=0;
1020: my $token;
1021: my $result='';
1.57 albertel 1022: if ( $tag =~ m:^/: ) {
1023: my $tag=substr($tag,1);
1024: # &Apache::lonxml::debug("have:$tag:");
1025: while (($depth >=0) && ($token = $pars->get_token)) {
1026: # &Apache::lonxml::debug("e token:$token->[0]:$depth:$token->[1]");
1027: if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
1028: $result.=$token->[1];
1029: } elsif ($token->[0] eq 'PI') {
1030: $result.=$token->[2];
1031: } elsif ($token->[0] eq 'S') {
1.151 albertel 1032: if ($token->[1] =~ /^$tag$/i) { $depth++; }
1.57 albertel 1033: $result.=$token->[4];
1034: } elsif ($token->[0] eq 'E') {
1.151 albertel 1035: if ( $token->[1] =~ /^$tag$/i) { $depth--; }
1.57 albertel 1036: #skip sending back the last end tag
1037: if ($depth > -1) { $result.=$token->[2]; } else {
1038: $pars->unget_token($token);
1039: }
1040: }
1041: }
1042: } else {
1043: while ($token = $pars->get_token) {
1044: # &Apache::lonxml::debug("s token:$token->[0]:$depth:$token->[1]");
1045: if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
1046: $result.=$token->[1];
1047: } elsif ($token->[0] eq 'PI') {
1048: $result.=$token->[2];
1049: } elsif ($token->[0] eq 'S') {
1.151 albertel 1050: if ( $token->[1] =~ /^$tag$/i) {
1.57 albertel 1051: $pars->unget_token($token); last;
1052: } else {
1053: $result.=$token->[4];
1054: }
1055: } elsif ($token->[0] eq 'E') {
1056: $result.=$token->[2];
1.36 albertel 1057: }
1.19 albertel 1058: }
1059: }
1.49 albertel 1060: # &Apache::lonxml::debug("Exit:$result:");
1.19 albertel 1061: return $result
1062: }
1063:
1.23 albertel 1064: sub newparser {
1065: my ($parser,$contentref,$dir) = @_;
1.167 albertel 1066: push (@$parser,HTML::LCParser->new($contentref));
1.56 albertel 1067: $$parser['-1']->xml_mode('1');
1.23 albertel 1068: if ( $dir eq '' ) {
1069: push (@Apache::lonxml::pwd, $Apache::lonxml::pwd[$#Apache::lonxml::pwd]);
1070: } else {
1071: push (@Apache::lonxml::pwd, $dir);
1072: }
1073: # &Apache::lonxml::debug("pwd:$#Apache::lonxml::pwd");
1074: # &Apache::lonxml::debug("pwd:$Apache::lonxml::pwd[$#Apache::lonxml::pwd]");
1075: }
1.1 sakharuk 1076:
1.8 albertel 1077: sub parstring {
1078: my ($token) = @_;
1079: my $temp='';
1.142 albertel 1080: foreach (@{$token->[3]}) {
1.35 www 1081: unless ($_=~/\W/) {
1.42 albertel 1082: my $val=$token->[2]->{$_};
1.150 albertel 1083: $val =~ s/([\%\@\\\"])/\\$1/g;
1.51 albertel 1084: #if ($val =~ m/^[\%\@]/) { $val="\\".$val; }
1.42 albertel 1085: $temp .= "my \$$_=\"$val\";"
1.20 albertel 1086: }
1.142 albertel 1087: }
1.8 albertel 1088: return $temp;
1089: }
1.22 albertel 1090:
1.34 www 1091: sub writeallows {
1.126 www 1092: unless ($#extlinks>=0) { return; }
1.34 www 1093: my $thisurl='/res/'.&Apache::lonnet::declutter(shift);
1.111 www 1094: if ($ENV{'httpref.'.$thisurl}) {
1095: $thisurl=$ENV{'httpref.'.$thisurl};
1096: }
1.34 www 1097: my $thisdir=$thisurl;
1098: $thisdir=~s/\/[^\/]+$//;
1099: my %httpref=();
1.142 albertel 1100: foreach (@extlinks) {
1.34 www 1101: $httpref{'httpref.'.
1.125 www 1102: &Apache::lonnet::hreflocation($thisdir,$_)}=$thisurl;
1.142 albertel 1103: }
1.126 www 1104: @extlinks=();
1.34 www 1105: &Apache::lonnet::appenv(%httpref);
1106: }
1107:
1.66 www 1108: #
1109: # Afterburner handles anchors, highlights and links
1110: #
1111: sub afterburn {
1112: my $result=shift;
1.154 albertel 1113: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1114: ['highlight','anchor','link']);
1.66 www 1115: if ($ENV{'form.highlight'}) {
1.142 albertel 1116: foreach (split(/\,/,$ENV{'form.highlight'})) {
1.66 www 1117: my $anchorname=$_;
1118: my $matchthis=$anchorname;
1119: $matchthis=~s/\_+/\\s\+/g;
1120: $result=~s/($matchthis)/\<font color=\"red\"\>$1\<\/font\>/gs;
1.142 albertel 1121: }
1.66 www 1122: }
1123: if ($ENV{'form.link'}) {
1.142 albertel 1124: foreach (split(/\,/,$ENV{'form.link'})) {
1.66 www 1125: my ($anchorname,$linkurl)=split(/\>/,$_);
1126: my $matchthis=$anchorname;
1127: $matchthis=~s/\_+/\\s\+/g;
1128: $result=~s/($matchthis)/\<a href=\"$linkurl\"\>$1\<\/a\>/gs;
1.142 albertel 1129: }
1.66 www 1130: }
1131: if ($ENV{'form.anchor'}) {
1132: my $anchorname=$ENV{'form.anchor'};
1133: my $matchthis=$anchorname;
1134: $matchthis=~s/\_+/\\s\+/g;
1135: $result=~s/($matchthis)/\<a name=\"$anchorname\"\>$1\<\/a\>/s;
1136: $result.=(<<"ENDSCRIPT");
1137: <script>
1138: document.location.hash='$anchorname';
1139: </script>
1140: ENDSCRIPT
1141: }
1142: return $result;
1143: }
1144:
1.79 www 1145: sub storefile {
1146: my ($file,$contents)=@_;
1147: if (my $fh=Apache::File->new('>'.$file)) {
1148: print $fh $contents;
1149: $fh->close();
1.147 albertel 1150: } else {
1151: &warning("Unable to save file $file");
1.79 www 1152: }
1153: }
1154:
1.151 albertel 1155: sub createnewhtml {
1156: my $filecontents=(<<SIMPLECONTENT);
1.78 www 1157: <html>
1158: <head>
1159: <title>
1160: Title of Document Goes Here
1161: </title>
1162: </head>
1163: <body bgcolor="#FFFFFF">
1164:
1165: Body of Document Goes Here
1166:
1167: </body>
1168: </html>
1169: SIMPLECONTENT
1.151 albertel 1170: return $filecontents;
1171: }
1172:
1.147 albertel 1173:
1.151 albertel 1174: sub inserteditinfo {
1175: my ($result,$filecontents)=@_;
1.157 albertel 1176: $filecontents = &HTML::Entities::encode($filecontents);
1.147 albertel 1177: # my $editheader='<a href="#editsection">Edit below</a><hr />';
1.161 albertel 1178: my $buttons=(<<BUTTONS);
1179: <input type="submit" name="attemptclean"
1180: value="Save and then attempt to clean HTML" />
1181: <input type="submit" name="savethisfile" value="Save this" />
1182: <input type="submit" name="viewmode" value="View" />
1183: BUTTONS
1.78 www 1184: my $editfooter=(<<ENDFOOTER);
1185: <hr />
1186: <a name="editsection" />
1187: <form method="post">
1.161 albertel 1188: <input type="hidden" name="editmode" value="Edit" />
1.170 www 1189: $buttons<br />
1.78 www 1190: <textarea cols="80" rows="40" name="filecont">$filecontents</textarea>
1.170 www 1191: <br />$buttons
1.78 www 1192: <br />
1193: </form>
1194: ENDFOOTER
1.147 albertel 1195: # $result=~s/(\<body[^\>]*\>)/$1$editheader/is;
1.78 www 1196: $result=~s/(\<\/body\>)/$editfooter/is;
1197: return $result;
1198: }
1199:
1.152 albertel 1200: sub get_target {
1201: my $viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
1202: if ( $ENV{'request.state'} eq 'published') {
1203: if ( defined($ENV{'form.grade_target'})
1204: && ($viewgrades == 'F' )) {
1205: return ($ENV{'form.grade_target'});
1.153 albertel 1206: } elsif (defined($ENV{'form.grade_target'})) {
1207: if (($ENV{'form.grade_target'} eq 'web') ||
1208: ($ENV{'form.grade_target'} eq 'tex') ) {
1209: return $ENV{'form.grade_target'}
1210: } else {
1211: return 'web';
1212: }
1.152 albertel 1213: } else {
1214: return 'web';
1215: }
1216: } elsif ($ENV{'request.state'} eq 'construct') {
1217: if ( defined($ENV{'form.grade_target'})) {
1218: return ($ENV{'form.grade_target'});
1219: } else {
1220: return 'web';
1221: }
1222: } else {
1223: return 'web';
1224: }
1225: }
1226:
1.24 sakharuk 1227: sub handler {
1228: my $request=shift;
1.68 www 1229:
1.152 albertel 1230: my $target=&get_target();
1.68 www 1231:
1.65 albertel 1232: $Apache::lonxml::debug=0;
1.68 www 1233:
1.25 sakharuk 1234: if ($ENV{'browser.mathml'}) {
1.27 albertel 1235: $request->content_type('text/xml');
1236: } else {
1237: $request->content_type('text/html');
1.25 sakharuk 1238: }
1.141 albertel 1239: &Apache::loncommon::no_cache($request);
1.27 albertel 1240: $request->send_http_header;
1.141 albertel 1241:
1.45 www 1242: return OK if $request->header_only;
1.27 albertel 1243:
1.79 www 1244:
1245: my $file=&Apache::lonnet::filelocation("",$request->uri);
1.78 www 1246: #
1247: # Edit action? Save file.
1248: #
1249: unless ($ENV{'request.state'} eq 'published') {
1.107 www 1250: if (($ENV{'form.savethisfile'}) || ($ENV{'form.attemptclean'})) {
1.79 www 1251: &storefile($file,$ENV{'form.filecont'});
1.78 www 1252: }
1253: }
1.24 sakharuk 1254: my %mystyle;
1.147 albertel 1255: my $result = '';
1.50 albertel 1256: my $filecontents=&Apache::lonnet::getfile($file);
1257: if ($filecontents == -1) {
1.78 www 1258: $result=(<<ENDNOTFOUND);
1259: <html>
1260: <head>
1261: <title>File not found</title>
1262: </head>
1263: <body bgcolor="#FFFFFF">
1264: <b>File not found: $file</b>
1265: </body>
1266: </html>
1267: ENDNOTFOUND
1.50 albertel 1268: $filecontents='';
1.151 albertel 1269: if ($ENV{'request.state'} ne 'published') {
1270: $filecontents=&createnewhtml();
1.161 albertel 1271: $ENV{'form.editmode'}='Edit'; #force edit mode
1.151 albertel 1272: }
1.50 albertel 1273: } else {
1.147 albertel 1274: unless ($ENV{'request.state'} eq 'published') {
1275: if ($ENV{'form.attemptclean'}) {
1276: $filecontents=&htmlclean($filecontents,1);
1.107 www 1277: }
1.147 albertel 1278: }
1.161 albertel 1279: if (!$ENV{'form.editmode'} || $ENV{'form.viewmode'}) {
1.172 albertel 1280: $result = &Apache::lonxml::xmlparse($request,$target,$filecontents,
1281: '',%mystyle);
1.147 albertel 1282: }
1.78 www 1283: }
1284:
1285: #
1286: # Edit action? Insert editing commands
1287: #
1288: unless ($ENV{'request.state'} eq 'published') {
1.161 albertel 1289: if ($ENV{'form.editmode'} && (!($ENV{'form.viewmode'}))) {
1.177 www 1290: my $displayfile=$request->uri;
1291: $displayfile=~s/^\/[^\/]*//;
1292: $result='<html><body bgcolor="#FFFFFF"><h3>'.$displayfile.
1293: '</h3></body></html>';
1.78 www 1294: $result=&inserteditinfo($result,$filecontents);
1.147 albertel 1295: }
1.66 www 1296: }
1.147 albertel 1297:
1.126 www 1298: writeallows($request->uri);
1.50 albertel 1299:
1.67 www 1300: $request->print($result);
1.64 albertel 1301:
1.45 www 1302: return OK;
1.24 sakharuk 1303: }
1.147 albertel 1304:
1.22 albertel 1305: sub debug {
1306: if ($Apache::lonxml::debug eq 1) {
1.146 albertel 1307: $|=1;
1.180 albertel 1308: print("DEBUG:".&HTML::Entities::encode($_[0])."<br />\n");
1.22 albertel 1309: }
1310: }
1.49 albertel 1311:
1.22 albertel 1312: sub error {
1.74 albertel 1313: if (($Apache::lonxml::debug eq 1) || ($ENV{'request.state'} eq 'construct') ) {
1.166 matthew 1314: # If printing in construction space, put the error inside <pre></pre>
1.167 albertel 1315: print "<b>ERROR:</b>".join("\n",@_)."\n";
1.52 albertel 1316: } else {
1317: print "<b>An Error occured while processing this resource. The instructor has been notified.</b> <br />";
1318: #notify author
1.146 albertel 1319: &Apache::lonmsg::author_res_msg($ENV{'request.filename'},join('<br />',@_));
1.52 albertel 1320: #notify course
1321: if ( $ENV{'request.course.id'} ) {
1322: my $users=$ENV{'course.'.$ENV{'request.course.id'}.'.comment.email'};
1.143 www 1323: my $declutter=&Apache::lonnet::declutter($ENV{'request.filename'});
1.52 albertel 1324: foreach my $user (split /\,/, $users) {
1325: ($user,my $domain) = split /:/, $user;
1.143 www 1326: &Apache::lonmsg::user_normal_msg($user,$domain,
1.146 albertel 1327: "Error [$declutter]",join('<br />',@_));
1.52 albertel 1328: }
1329: }
1.74 albertel 1330:
1.52 albertel 1331: #FIXME probably shouldn't have me get everything forever.
1.146 albertel 1332: &Apache::lonmsg::user_normal_msg('albertel','msu',"Error in $ENV{'request.filename'}",join('<br />',@_));
1.74 albertel 1333: #&Apache::lonmsg::user_normal_msg('albertel','103',"Error in $ENV{'request.filename'}",$_[0]);
1.52 albertel 1334: }
1.22 albertel 1335: }
1.49 albertel 1336:
1.22 albertel 1337: sub warning {
1.73 harris41 1338: if ($ENV{'request.state'} eq 'construct') {
1.146 albertel 1339: print "<b>W</b>ARNING<b>:</b>".join('<br />',@_)."<br />\n";
1.73 harris41 1340: }
1.83 albertel 1341: }
1342:
1343: sub get_param {
1344: my ($param,$parstack,$safeeval,$context) = @_;
1345: if ( ! $context ) { $context = -1; }
1346: my $args ='';
1347: if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; }
1.157 albertel 1348: if ( ! $args ) { return undef; }
1.131 albertel 1349: if ( $args =~ /my \$$param=\"/ ) {
1350: return &Apache::run::run("{$args;".'return $'.$param.'}',$safeeval); #'
1351: } else {
1352: return undef;
1353: }
1.22 albertel 1354: }
1355:
1.132 albertel 1356: sub get_param_var {
1357: my ($param,$parstack,$safeeval,$context) = @_;
1358: if ( ! $context ) { $context = -1; }
1359: my $args ='';
1360: if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; }
1361: if ( $args !~ /my \$$param=\"/ ) { return undef; }
1362: my $value=&Apache::run::run("{$args;".'return $'.$param.'}',$safeeval); #'
1363: if ($value =~ /^[\$\@\%]/) {
1364: return &Apache::run::run("return $value",$safeeval,1);
1365: } else {
1366: return $value;
1367: }
1368: }
1369:
1.74 albertel 1370: sub register_insert {
1.75 albertel 1371: my @data = split /\n/, &Apache::lonnet::getfile('/home/httpd/lonTabs/insertlist.tab');
1.74 albertel 1372: my $i;
1.76 albertel 1373: my $tagnum=0;
1.74 albertel 1374: my @order;
1375: for ($i=0;$i < $#data; $i++) {
1376: my $line = $data[$i];
1377: if ( $line =~ /^\#/ || $line =~ /^\s*\n/) { next; }
1378: if ( $line =~ /TABLE/ ) { last; }
1.92 albertel 1379: my ($tag,$descrip,$color,$function,$show) = split(/,/, $line);
1.135 albertel 1380: if ($tag) {
1381: $insertlist{"$tagnum.tag"} = $tag;
1382: $insertlist{"$tagnum.description"} = $descrip;
1383: $insertlist{"$tagnum.color"} = $color;
1384: $insertlist{"$tagnum.function"} = $function;
1385: if (!defined($show)) { $show='yes'; }
1386: $insertlist{"$tagnum.show"}= $show;
1387: $insertlist{"$tag.num"}=$tagnum;
1388: $tagnum++;
1389: }
1.74 albertel 1390: }
1.76 albertel 1391: $i++; #skipping TABLE line
1392: $tagnum = 0;
1.74 albertel 1393: for (;$i < $#data;$i++) {
1394: my $line = $data[$i];
1.76 albertel 1395: my ($mnemonic,@which) = split(/ +/,$line);
1396: my $tag = $insertlist{"$tagnum.tag"};
1.144 matthew 1397: for (my $j=0;$j <=$#which;$j++) {
1.74 albertel 1398: if ( $which[$j] eq 'Y' ) {
1.76 albertel 1399: if ($insertlist{"$j.show"} ne 'no') {
1400: push(@{ $insertlist{"$tag.which"} },$j);
1401: }
1.74 albertel 1402: }
1403: }
1.76 albertel 1404: $tagnum++;
1.74 albertel 1405: }
1406: }
1.98 albertel 1407:
1408: sub description {
1409: my ($token)=@_;
1.138 albertel 1410: my $tagnum;
1411: my $tag=$token->[1];
1412: foreach my $namespace (reverse @Apache::lonxml::namespace) {
1413: my $testtag=$namespace.'::'.$tag;
1414: $tagnum=$insertlist{"$testtag.num"};
1415: if (defined($tagnum)) { last; }
1416: }
1417: if (!defined ($tagnum)) { $tagnum=$Apache::lonxml::insertlist{"$tag.num"}; }
1418: return $insertlist{$tagnum.'.description'};
1.98 albertel 1419: }
1.123 albertel 1420:
1421: # ----------------------------------------------------------------- whichuser
1422: # returns a list of $symb, $courseid, $domain, $name that is correct for
1423: # calls to lonnet functions for this setup.
1424: # - looks for form.grade_ parameters
1425: sub whichuser {
1.134 albertel 1426: my ($symb,$courseid,$domain,$name);
1.123 albertel 1427: if (defined($ENV{'form.grade_symb'})) {
1428: my $tmp_courseid=$ENV{'form.grade_courseid'};
1429: my $allowed=&Apache::lonnet::allowed('mgr',$tmp_courseid);
1430: if ($allowed) {
1431: $symb=$ENV{'form.grade_symb'};
1432: $courseid=$ENV{'form.grade_courseid'};
1433: $domain=$ENV{'form.grade_domain'};
1434: $name=$ENV{'form.grade_username'};
1435: }
1.134 albertel 1436: } else {
1437: $symb=&Apache::lonnet::symbread();
1438: $courseid=$ENV{'request.course.id'};
1439: $domain=$ENV{'user.domain'};
1440: $name=$ENV{'user.name'};
1.123 albertel 1441: }
1442: return ($symb,$courseid,$domain,$name);
1443: }
1444:
1.1 sakharuk 1445: 1;
1446: __END__
1.68 www 1447:
1448:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>