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