--- loncom/xml/lonxml.pm 2001/02/14 02:57:02 1.53
+++ loncom/xml/lonxml.pm 2008/03/07 21:17:04 1.473
@@ -1,252 +1,633 @@
# The LearningOnline Network with CAPA
# XML Parser Module
#
-# last modified 06/26/00 by Alexander Sakharuk
-# 11/6 Gerd Kortemeyer
-# 6/1/1 Gerd Kortemeyer
+# $Id: lonxml.pm,v 1.473 2008/03/07 21:17:04 www Exp $
+#
+# Copyright Michigan State University Board of Trustees
+#
+# This file is part of the LearningOnline Network with CAPA (LON-CAPA).
+#
+# LON-CAPA is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# LON-CAPA is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with LON-CAPA; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+#
+# /home/httpd/html/adm/gpl.txt
+#
+# http://www.lon-capa.org/
+#
+# Copyright for TtHfunc and TtMfunc by Ian Hutchinson.
+# TtHfunc and TtMfunc (the "Code") may be compiled and linked into
+# binary executable programs or libraries distributed by the
+# Michigan State University (the "Licensee"), but any binaries so
+# distributed are hereby licensed only for use in the context
+# of a program or computational system for which the Licensee is the
+# primary author or distributor, and which performs substantial
+# additional tasks beyond the translation of (La)TeX into HTML.
+# The C source of the Code may not be distributed by the Licensee
+# to any other parties under any circumstances.
+#
+
package Apache::lonxml;
use vars
-qw(@pwd $outputstack $redirection $textredirection $on_offimport @extlinks);
+qw(@pwd @outputstack $redirection $import @extlinks $metamode $evaluate %insertlist @namespace $errorcount $warningcount);
use strict;
-use HTML::TokeParser;
-use Safe;
-use Safe::Hole;
-use Opcode;
-use Apache::Constants qw(:common);
+use LONCAPA;
+use HTML::LCParser();
+use HTML::TreeBuilder();
+use HTML::Entities();
+use Safe();
+use Safe::Hole();
+use Math::Cephes();
+use Math::Random();
+use Opcode();
+use POSIX qw(strftime);
+use Time::HiRes qw( gettimeofday tv_interval );
+use Symbol();
sub register {
- my $space;
- my @taglist;
- my $temptag;
- ($space,@taglist) = @_;
- foreach $temptag (@taglist) {
- $Apache::lonxml::alltags{$temptag}=$space;
+ my ($space,@taglist) = @_;
+ foreach my $temptag (@taglist) {
+ push(@{ $Apache::lonxml::alltags{$temptag} },$space);
+ }
+}
+
+sub deregister {
+ my ($space,@taglist) = @_;
+ foreach my $temptag (@taglist) {
+ my $tempspace = $Apache::lonxml::alltags{$temptag}[-1];
+ if ($tempspace eq $space) {
+ pop(@{ $Apache::lonxml::alltags{$temptag} });
+ }
}
+ #&printalltags();
+}
+
+use Apache::Constants qw(:common);
+use Apache::lontexconvert();
+use Apache::style();
+use Apache::run();
+use Apache::londefdef();
+use Apache::scripttag();
+use Apache::languagetags();
+use Apache::edit();
+use Apache::inputtags();
+use Apache::outputtags();
+use Apache::lonnet;
+use Apache::File();
+use Apache::loncommon();
+use Apache::lonfeedback();
+use Apache::lonmsg();
+use Apache::loncacc();
+use Apache::lonmaxima();
+use Apache::lonlocal;
+
+#==================================== Main subroutine: xmlparse
+
+#debugging control, to turn on debugging modify the correct handler
+
+$Apache::lonxml::debug=0;
+
+# keeps count of the number of warnings and errors generated in a parse
+$warningcount=0;
+$errorcount=0;
+
+#path to the directory containing the file currently being processed
+@pwd=();
+
+#these two are used for capturing a subset of the output for later processing,
+#don't touch them directly use &startredirection and &endredirection
+@outputstack = ();
+$redirection = 0;
+
+#controls wheter the tag actually does
+$import = 1;
+@extlinks=();
+
+# meta mode is a bit weird only some output is to be turned off
+#
';
+ }
+ my $editfooter=(<
+
+
+ENDFOOTER
+ return ($editfooter,$add_to_onload,$add_to_onresize);;
+}
+
+sub get_target {
+ my $viewgrades=&Apache::lonnet::allowed('vgr',$env{'request.course.id'});
+ if ( $env{'request.state'} eq 'published') {
+ if ( defined($env{'form.grade_target'})
+ && ($viewgrades == 'F' )) {
+ return ($env{'form.grade_target'});
+ } elsif (defined($env{'form.grade_target'})) {
+ if (($env{'form.grade_target'} eq 'web') ||
+ ($env{'form.grade_target'} eq 'tex') ) {
+ return $env{'form.grade_target'}
+ } else {
+ return 'web';
+ }
+ } else {
+ return 'web';
+ }
+ } elsif ($env{'request.state'} eq 'construct') {
+ if ( defined($env{'form.grade_target'})) {
+ return ($env{'form.grade_target'});
+ } else {
+ return 'web';
+ }
} else {
- $result = &Apache::lonxml::xmlparse($target,$filecontents,'',%mystyle);
+ return 'web';
}
- $request->print($result);
+}
+
+sub handler {
+ my $request=shift;
+
+ my $target=&get_target();
+
+ $Apache::lonxml::debug=$env{'user.debug'};
+
+ &Apache::loncommon::content_type($request,'text/html');
+ &Apache::loncommon::no_cache($request);
+ if ($env{'request.state'} eq 'published') {
+ $request->set_last_modified(&Apache::lonnet::metadata($request->uri,
+ 'lastrevisiondate'));
+ }
+ $request->send_http_header;
+
+ return OK if $request->header_only;
+
+ my $file=&Apache::lonnet::filelocation("",$request->uri);
+ my $filetype;
+ if ($file =~ /\.sty$/) {
+ $filetype='sty';
+ } else {
+ $filetype='html';
+ }
+#
+# Edit action? Save file.
+#
+ if (!($env{'request.state'} eq 'published')) {
+ if ($env{'form.savethisfile'} || $env{'form.viewmode'} || $env{'form.Undo'}) {
+ my $html_file=&Apache::lonnet::getfile($file);
+ my $error = &Apache::lonhomework::handle_save_or_undo($request, \$html_file, \$env{'form.filecont'});
+ if ($env{'form.savethisfile'}) {
+ $env{'form.editmode'}='Edit'; #force edit mode
+ }
+ }
+ }
+ my %mystyle;
+ my $result = '';
+ my $filecontents=&Apache::lonnet::getfile($file);
+ if ($filecontents eq -1) {
+ my $start_page=&Apache::loncommon::start_page('File Error');
+ my $end_page=&Apache::loncommon::end_page();
+ my $fnf=&mt('File not found');
+ $result=(<$fnf: $file
+$end_page
+ENDNOTFOUND
+ $filecontents='';
+ if ($env{'request.state'} ne 'published') {
+ if ($filetype eq 'sty') {
+ $filecontents=&createnewsty();
+ } else {
+ $filecontents=&createnewhtml();
+ }
+ $env{'form.editmode'}='Edit'; #force edit mode
+ }
+ } else {
+ unless ($env{'request.state'} eq 'published') {
+ if ($filecontents=~/BEGIN LON-CAPA Internal/) {
+ &Apache::lonxml::error(&mt('This file appears to be a rendering of a LON-CAPA resource. If this is correct, this resource will act very oddly and incorrectly.'));
+ }
+#
+# we are in construction space, see if edit mode forced
+ &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
+ ['editmode']);
+ }
+ if (!$env{'form.editmode'} || $env{'form.viewmode'} || $env{'form.discardview'}) {
+ &Apache::structuretags::reset_problem_globals();
+ $result = &Apache::lonxml::xmlparse($request,$target,$filecontents,
+ '',%mystyle);
+ # .html files may contain or need to clean
+ # up if it did
+ &Apache::structuretags::reset_problem_globals();
+ &Apache::lonhomework::finished_parsing();
+ &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
+ ['rawmode']);
+ if ($env{'form.rawmode'}) { $result = $filecontents; }
+ if ($filetype eq 'sty') {
+ my $controls =
+ ($env{'request.state'} eq 'construct') ? &Apache::londefdef::edit_controls()
+ : '';
+ my %options = ('bgcolor' => '#FFFFFF');
+ $result =
+ &Apache::loncommon::start_page(undef,undef,\%options).
+ $controls.
+ $result.
+ &Apache::loncommon::end_page();
+ }
+ }
+ }
- $request->print('