--- loncom/xml/lonxml.pm 2002/03/21 20:51:10 1.159 +++ loncom/xml/lonxml.pm 2002/08/28 17:07:54 1.189 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # XML Parser Module # -# $Id: lonxml.pm,v 1.159 2002/03/21 20:51:10 www Exp $ +# $Id: lonxml.pm,v 1.189 2002/08/28 17:07:54 sakharuk Exp $ # # Copyright Michigan State University Board of Trustees # @@ -60,16 +60,16 @@ package Apache::lonxml; use vars -qw(@pwd @outputstack $redirection $import @extlinks $metamode $evaluate %insertlist @namespace); +qw(@pwd @outputstack $redirection $import @extlinks $metamode $evaluate %insertlist @namespace $prevent_entity_encode); use strict; -use HTML::TokeParser; -use HTML::TreeBuilder; -use HTML::Entities; -use Safe; -use Safe::Hole; -use Math::Cephes qw(:trigs :hypers :bessels erf erfc); -use Math::Random qw(:all); -use Opcode; +use HTML::LCParser(); +use HTML::TreeBuilder(); +use HTML::Entities(); +use Safe(); +use Safe::Hole(); +use Math::Cephes(); +use Math::Random(); +use Opcode(); sub register { my ($space,@taglist) = @_; @@ -90,15 +90,15 @@ sub deregister { } use Apache::Constants qw(:common); -use Apache::lontexconvert; -use Apache::style; -use Apache::run; -use Apache::londefdef; -use Apache::scripttag; -use Apache::edit; -use Apache::lonnet; -use Apache::File; -use Apache::loncommon; +use Apache::lontexconvert(); +use Apache::style(); +use Apache::run(); +use Apache::londefdef(); +use Apache::scripttag(); +use Apache::edit(); +use Apache::lonnet(); +use Apache::File(); +use Apache::loncommon(); #================================================== Main subroutine: xmlparse #debugging control, to turn on debugging modify the correct handler @@ -129,9 +129,15 @@ $evaluate = 1; # stores the list of active tag namespaces @namespace=(); +# if 0 all high ASCII characters will be encoded into HTML Entities +$prevent_entity_encode=0; + # has the dynamic menu been updated to know about this resource $Apache::lonxml::registered=0; +# a pointer the the Apache request object +$Apache::lonxml::request=''; + sub xmlbegin { my $output=''; if ($ENV{'browser.mathml'}) { @@ -170,16 +176,19 @@ sub xmlend { unless (($hidden) && (!$seeid)) { my $message=$contrib{$idx.':message'}; $message=~s/\n/\
/g; + $message=&Apache::lontexconvert::msgtexconverted($message); if ($message) { if ($hidden) { $message=''.$message.''; } my $sender='Anonymous'; if ((!$contrib{$idx.':anonymous'}) || ($seeid)) { - $sender=$contrib{$idx.':sendername'}.' at '. - $contrib{$idx.':senderdomain'}; + $sender=$contrib{$idx.':plainname'}.' ('. + $contrib{$idx.':sendername'}.' at '. + $contrib{$idx.':senderdomain'}.')'; if ($contrib{$idx.':anonymous'}) { - $sender.=' (anonymous)'; + $sender.=' [anonymous] '. + $contrib{$idx.':screenname'}; } if ($seeid) { if ($hidden) { @@ -190,6 +199,10 @@ sub xmlend { $symb.':::'.$idx.'">Hide'; } } + } else { + if ($contrib{$idx.':screenname'}) { + $sender=''.$contrib{$idx.':screenname'}.''; + } } $discussion.='

'.$sender.' ('. localtime($contrib{$idx.':timestamp'}). @@ -319,36 +332,39 @@ sub registerurl { my $forcereg=shift; my $target = shift; my $result = ''; - if ($ENV{'request.publicaccess'}) { - return + if ($target eq 'edit') { + $result .="\n"; + } + if ((($ENV{'request.publicaccess'}) || + (!&Apache::lonnet::is_on_map($ENV{'REQUEST_URI'}))) && + (!$forcereg)) { + return $result. ''; } if ($Apache::lonxml::registered && !$forcereg) { return ''; } $Apache::lonxml::registered=1; my $nothing=''; if ($ENV{'browser.type'} eq 'explorer') { $nothing='javascript:void(0);'; } + my $timesync='menu.syncclock(1000*'.time.');'; if (($ENV{'REQUEST_URI'}!~/^\/(res\/)*adm\//) || ($forcereg)) { my $hwkadd=''; - if ($ENV{'REQUEST_URI'}=~/\.(problem|exam|quiz|assess|survey|form)$/) { + if ($ENV{'request.filename'}=~/\.(problem|exam|quiz|assess|survey|form)$/) { if (&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'})) { $hwkadd.=(< ENDDONOTREGTHIS } - if ($target eq 'edit') { - # Javascript routines for construction space: - # openbrowser and opensearcher will start the file browser - # (lonindexer) and searcher (lonsearchcat) respectively. - # Inputs are the name of the html form being used - # and the name of the element the selected URL should - # be placed in. - $result .=<<"ENDBROWSERSCRIPT"; - -ENDBROWSERSCRIPT - } return $result; } @@ -496,9 +473,26 @@ sub printalltags { } sub xmlparse { - my ($target,$content_file_string,$safeinit,%style_for_target) = @_; + my ($request,$target,$content_file_string,$safeinit,%style_for_target) = @_; + + &setup_globals($request,$target); +# +# do we have a course style file? +# + + if ($ENV{'request.course.id'}) { + my $bodytext= + $ENV{'course.'.$ENV{'request.course.id'}.'.default_xml_style'}; + if ($bodytext) { + my $location=&Apache::lonnet::filelocation('',$bodytext); + my $styletext=&Apache::lonnet::getfile($location); + if ($styletext ne '-1') { + %style_for_target = (%style_for_target, + &Apache::style::styleparser($target,$styletext)); + } + } + } - &setup_globals($target); #&printalltags(); my @pars = (); my $pwd=$ENV{'request.filename'}; @@ -534,7 +528,7 @@ sub htmlclean { my $output= $tree->as_HTML(undef,' '); - $output=~s/\<(br|hr|img|meta|allow)([^\>\/]*)\>/\<$1$2 \/\>/gis; + $output=~s/\<(br|hr|img|meta|allow)(.*?)\>/\<$1$2 \/\>/gis; $output=~s/\<\/(br|hr|img|meta|allow)\>//gis; unless ($full) { $output=~s/\<[\/]*(body|head|html)\>//gis; @@ -545,6 +539,24 @@ sub htmlclean { return $output; } +sub latex_special_sumbols { + my ($current_token,$stack,$parstack)=@_; + my @temp_array = @$stack; + if ($temp_array[-1] ne 'tt') { + if ($current_token=~m/\^/) {$current_token=~s/\^/\\verb|\^|/g;} + } else { + if ($current_token=~m/\^/) {$current_token=~s/\^/}\\verb|\^|{/g;} + } + if ($current_token=~m/>/) {$current_token=~s/>/\$>\$/g;} #more + if ($current_token=~m/get_token) { if (($token->[0] eq 'T') || ($token->[0] eq 'C') || ($token->[0] eq 'D') ) { if ($metamode<1) { - $result=$token->[1]; + if ($target eq 'tex') { + my $text=$token->[1]; + if ($token->[0] eq 'C') { + $text = '%'.$text; + $text =~ s/[\n\r]//g; + } + $result.=&latex_special_sumbols($text,$stack,$parstack); + } else { + $result.=$token->[1]; + } } } elsif ($token->[0] eq 'PI') { if ($metamode<1) { @@ -612,18 +633,22 @@ sub inner_xmlparse { #evaluate variable refs in result if ($result ne "") { if ( $#$parstack > -1 ) { - if ($Apache::lonxml::redirection) { - $Apache::lonxml::outputstack['-1'] .= - &Apache::run::evaluate($result,$safeeval,$$parstack['-1']); - } else { - $finaloutput .= &Apache::run::evaluate($result,$safeeval, - $$parstack['-1']); - } + $result=&Apache::run::evaluate($result,$safeeval,$$parstack[-1]); } else { - $finaloutput .= &Apache::run::evaluate($result,$safeeval,''); + $result= &Apache::run::evaluate($result,$safeeval,''); } - $result = ''; - } + } + # Encode any high ASCII characters + if (!$Apache::lonxml::prevent_entity_encode) { + $result=&HTML::Entities::encode($result,"\200-\377"); + } + if ($Apache::lonxml::redirection) { + $Apache::lonxml::outputstack['-1'] .= $result; + } else { + $finaloutput.=$result; + } + $result = ''; + if ($token->[0] eq 'E') { &end_tag($stack,$parstack,$token); } @@ -636,6 +661,7 @@ sub inner_xmlparse { # $finaloutput.=&endredirection; # } + if (($ENV{'QUERY_STRING'}) && ($target eq 'web')) { $finaloutput=&afterburn($finaloutput); } @@ -738,13 +764,13 @@ sub callsub { } if (!$deleted) { if ($space) { - #&Apache::lonxml::debug("Calling sub $sub in $space $metamode"); + &Apache::lonxml::debug("Calling sub $sub in $space $metamode"); $sub1="$space\:\:$sub"; ($currentstring,$nodefault) = &$sub1($target,$token,$tagstack, $parstack,$parser,$safeeval, $style); } else { - #&Apache::lonxml::debug("NOT Calling sub $sub in $space $metamode"); + &Apache::lonxml::debug("NOT Calling sub $sub in $space $metamode"); if ($metamode <1) { if (defined($token->[4]) && ($metamode < 1)) { $currentstring = $token->[4]; @@ -778,7 +804,8 @@ sub callsub { } sub setup_globals { - my ($target)=@_; + my ($request,$target)=@_; + $Apache::lonxml::request=$request; $Apache::lonxml::registered = 0; @Apache::lonxml::pwd=(); @Apache::lonxml::extlinks=(); @@ -807,6 +834,11 @@ sub setup_globals { $Apache::lonxml::metamode = 0; $Apache::lonxml::evaluate = 0; $Apache::lonxml::import = 0; + } elsif ($target eq 'analyze') { + $Apache::lonxml::redirection = 0; + $Apache::lonxml::metamode = 0; + $Apache::lonxml::evaluate = 1; + $Apache::lonxml::import = 1; } else { $Apache::lonxml::redirection = 0; $Apache::lonxml::metamode = 0; @@ -930,8 +962,34 @@ sub decreasedepth { #print "
e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n"; } -sub get_all_text { +sub get_all_text_unbalanced { + my($tag,$pars)= @_; + my $token; + my $result=''; + $tag='<'.$tag.'>'; + while ($token = $$pars[-1]->get_token) { + if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) { + $result.=$token->[1]; + } elsif ($token->[0] eq 'PI') { + $result.=$token->[2]; + } elsif ($token->[0] eq 'S') { + $result.=$token->[4]; + } elsif ($token->[0] eq 'E') { + $result.=$token->[2]; + } + if ($result =~ /(.*)$tag(.*)/) { + &Apache::lonxml::debug('Got a winner with leftovers ::'.$2); + &Apache::lonxml::debug('Result is :'.$1); + $result=$1; + my $redo=$tag.$2; + &Apache::lonxml::newparser($pars,\$redo); + last; + } + } + return $result +} +sub get_all_text { my($tag,$pars)= @_; my $depth=0; my $token; @@ -980,7 +1038,7 @@ sub get_all_text { sub newparser { my ($parser,$contentref,$dir) = @_; - push (@$parser,HTML::TokeParser->new($contentref)); + push (@$parser,HTML::LCParser->new($contentref)); $$parser['-1']->xml_mode('1'); if ( $dir eq '' ) { push (@Apache::lonxml::pwd, $Apache::lonxml::pwd[$#Apache::lonxml::pwd]); @@ -1092,17 +1150,21 @@ sub inserteditinfo { my ($result,$filecontents)=@_; $filecontents = &HTML::Entities::encode($filecontents); # my $editheader='Edit below


'; + my $buttons=(< + + +BUTTONS my $editfooter=(<
+ +$buttons
+
$buttons
- - - -
ENDFOOTER # $result=~s/(\]*\>)/$1$editheader/is; @@ -1181,7 +1243,7 @@ ENDNOTFOUND $filecontents=''; if ($ENV{'request.state'} ne 'published') { $filecontents=&createnewhtml(); - $ENV{'form.showmode'}='Edit'; #force edit mode + $ENV{'form.editmode'}='Edit'; #force edit mode } } else { unless ($ENV{'request.state'} eq 'published') { @@ -1189,8 +1251,9 @@ ENDNOTFOUND $filecontents=&htmlclean($filecontents,1); } } - if ($ENV{'form.showmode'} ne 'Edit') { - $result = &Apache::lonxml::xmlparse($target,$filecontents,'',%mystyle); + if (!$ENV{'form.editmode'} || $ENV{'form.viewmode'}) { + $result = &Apache::lonxml::xmlparse($request,$target,$filecontents, + '',%mystyle); } } @@ -1198,8 +1261,11 @@ ENDNOTFOUND # Edit action? Insert editing commands # unless ($ENV{'request.state'} eq 'published') { - if ($ENV{'form.showmode'} eq 'Edit') { - $result=''; + if ($ENV{'form.editmode'} && (!($ENV{'form.viewmode'}))) { + my $displayfile=$request->uri; + $displayfile=~s/^\/[^\/]*//; + $result='

'.$displayfile. + '

'; $result=&inserteditinfo($result,$filecontents); } } @@ -1214,13 +1280,14 @@ ENDNOTFOUND sub debug { if ($Apache::lonxml::debug eq 1) { $|=1; - print("DEBUG:".join('
',@_)."
\n"); + print("DEBUG:".&HTML::Entities::encode($_[0])."
\n"); } } sub error { if (($Apache::lonxml::debug eq 1) || ($ENV{'request.state'} eq 'construct') ) { - print "ERROR:".join('
',@_)."
\n"; + # If printing in construction space, put the error inside

+    print "ERROR:".join("\n",@_)."\n";
   } else {
     print "An Error occured while processing this resource. The instructor has been notified. 
"; #notify author