File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.431: download - view: text, annotated - select for diffs
Thu Jul 21 03:31:16 2011 UTC (12 years, 10 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
IMS Export.
  - Exclude author-specified metadata and instructorcomment when rendering
    problem in a course for export to testbank file ("text" format).
    (MSU requirement for third party customized secure exam platform).
  - Correct letter used to identify which foil in multople-choice is answer.

    1: 
    2: # The LearningOnline Network with CAPA
    3: # Tags Default Definition Module 
    4: #
    5: # $Id: londefdef.pm,v 1.431 2011/07/21 03:31:16 raeburn Exp $
    6: # 
    7: #
    8: # Copyright Michigan State University Board of Trustees
    9: #
   10: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
   11: #
   12: # LON-CAPA is free software; you can redistribute it and/or modify
   13: # it under the terms of the GNU General Public License as published by
   14: # the Free Software Foundation; either version 2 of the License, or
   15: # (at your option) any later version.
   16: #
   17: # LON-CAPA is distributed in the hope that it will be useful,
   18: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   19: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   20: # GNU General Public License for more details.
   21: #
   22: # You should have received a copy of the GNU General Public License
   23: # along with LON-CAPA; if not, write to the Free Software
   24: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   25: #
   26: # /home/httpd/html/adm/gpl.txt
   27: #
   28: # http://www.lon-capa.org/
   29: ## Copyright for TtHfunc and TtMfunc by Ian Hutchinson. 
   30: # TtHfunc and TtMfunc (the "Code") may be compiled and linked into 
   31: # binary executable programs or libraries distributed by the 
   32: # Michigan State University (the "Licensee"), but any binaries so 
   33: # distributed are hereby licensed only for use in the context
   34: # of a program or computational system for which the Licensee is the 
   35: # primary author or distributor, and which performs substantial 
   36: # additional tasks beyond the translation of (La)TeX into HTML.
   37: # The C source of the Code may not be distributed by the Licensee
   38: # to any other parties under any circumstances.
   39: #
   40: 
   41: package Apache::londefdef; 
   42: 
   43: use Apache::lonnet;
   44: use strict;
   45: use Apache::lonxml;
   46: use Apache::lontable;
   47: use Image::Magick;
   48: use Apache::lonmenu();
   49: use Apache::lonmeta();
   50: use Apache::lonlocal;
   51: use Apache::loncommon();
   52: use Apache::Constants qw(:common);
   53: use File::Basename;
   54: use LONCAPA();
   55: # use Data::Dumper;
   56: 
   57: BEGIN {
   58: 
   59:     &Apache::lonxml::register('Apache::londefdef',('a','abbr','acronym','accessrule','address','allow','applet','area','b','base','basefont','bgo','bgsound','big','blink','blockquote','blankspace','body','br','button','caption','center','cite','code','col','colgroup','dd','del','dfn','dir','div','dl','dt','em','embed','externallink','fieldset','font','form','frame','frameset','h1','h2','h3','h4','h5','h6','head','hr','html','i','iframe','img','input','ins','insert','isindex','kbd','keygen','label','layer','legend','li','link','m','map','marquee','menu','meta','multicol','nobr','noembed','noframes','nolayer','noscript','object','ol','optgroup','option','output','p','param','pre','q','s','samp','select','server','small','spacer','span','strike','strong','sub','sup','table','tbody','td','textarea','tfoot','th','thead','title','tr','tt','tthoption','u','ul','var','wbr','hideweboutput'));
   60: 
   61: }
   62: 
   63: 
   64: sub initialize_londefdef {
   65:     $Apache::londefdef::TD_redirection=0;
   66:     @Apache::londefdef::table = ();
   67:     $Apache::londefdef::select=0;
   68:     undef(@Apache::londefdef::description);
   69:     @Apache::londefdef::DD=(0);
   70:     @Apache::londefdef::DT=(0);
   71:     @Apache::londefdef::seenDT=(0);
   72:     $Apache::londefdef::list_index=0;
   73:     undef($Apache::londefdef::head);
   74:     undef($Apache::londefdef::title);
   75: }
   76: 
   77: #======================= TAG SUBROUTINES =====================
   78: #-- <output>
   79: sub start_output {
   80:     my ($target) = @_;
   81:     if ($target eq 'meta') { $Apache::lonxml::metamode--; }
   82:     return '';
   83: }
   84: sub end_output {
   85:     my ($target) = @_;
   86:     if ($target eq 'meta') { $Apache::lonxml::metamode++; }
   87:     return '';
   88: }
   89: #-- <m> tag
   90: sub start_m {
   91:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
   92:     my $currentstring = '';
   93:     my $inside = &Apache::lonxml::get_all_text_unbalanced("/m",$parser);
   94:     if ($target eq 'web' || $target eq 'analyze') {
   95: 	&Apache::lonxml::debug("M is starting with:$inside:");
   96: 	my $eval=&Apache::lonxml::get_param('eval',$parstack,$safeeval);
   97: 	if ($eval eq 'on') {
   98: 	    $inside=&Apache::run::evaluate($inside,$safeeval,$$parstack[-1]);
   99: 	    #&Apache::lonxml::debug("M is evaluated to:$inside:");
  100: 	}
  101: 	my $tex = $inside;
  102: 	my $display=&Apache::lonxml::get_param('display',$parstack,$safeeval);
  103: 	$currentstring = &Apache::lontexconvert::converted(\$inside,$display);
  104: 	if ($Apache::lontexconvert::errorstring) {
  105: 	    my $errormsg='<pre>'.&HTML::Entities::encode($Apache::lontexconvert::errorstring,'<>&"').'</pre> occurred while attempting to convert this TeX: <pre>';
  106: 	    $tex = &HTML::Entities::encode($tex,'<>&"');
  107: 	    my ($linenumber) =
  108: 		($Apache::lontexconvert::errorstring =~ /Line (\d+)/);
  109: 	    if (defined($linenumber)) {
  110: 		my @tex=split("\n",$tex);
  111: 		$tex[$linenumber]='<b><font color="red">'.
  112: 		    $tex[$linenumber].'</font></b>';
  113: 		$tex=join("\n",@tex);
  114: 	    }
  115: 	    &Apache::lonxml::warning($errormsg.$tex.'</pre>');
  116: 	    $Apache::lontexconvert::errorstring='';
  117: 	}
  118: 	#&Apache::lonxml::debug("M is ends with:$currentstring:");
  119: 	$Apache::lonxml::post_evaluate=0;
  120:     } elsif ($target eq 'tex') {
  121: 
  122: 	$currentstring = $inside;
  123: 	my $eval=&Apache::lonxml::get_param('eval',$parstack,$safeeval);
  124: 	if ($eval eq 'on') {
  125: 	    $currentstring=&Apache::run::evaluate($currentstring,$safeeval,$$parstack[-1]);
  126: 	}
  127: 	if ($currentstring=~/^(\s*\\\\\s*)*$/) {$currentstring = ' \vskip 0 mm ';}
  128: 	# detect simple math mode entry exits, and convert them
  129:         # to use \ensuremath ... unless there's a \verb inside.
  130: 	if (! ($currentstring=~/\\verb/)) {
  131: 	    if ($currentstring=~/^\s*\$[^\$].*\$\s*$/) {
  132: 		$currentstring=~s/^(\s*)\$/$1/;
  133: 		$currentstring=~s/\$(\s*)$/$1/;
  134: 		$currentstring='\ensuremath{'.$currentstring.'}';
  135: 	    }
  136: 	}
  137: 	$Apache::lonxml::post_evaluate=0;
  138:     }
  139:     return $currentstring;
  140: }
  141: 
  142: sub end_m {
  143:     my ($target,$token) = @_;
  144:     my $currentstring = '';
  145:     if ($target eq 'tex') {
  146: 	$currentstring = "";
  147:     }
  148:     return $currentstring;
  149: }
  150: 
  151: sub start_tthoption {
  152:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
  153:     my $result;
  154:     if ($target eq 'web' || $target eq 'webgrade') {
  155: 	my $inside = &Apache::lonxml::get_all_text("/tthoption",$parser,
  156: 						   $style);
  157: 	$inside=~s/^\s*//;
  158: 	if ($env{'browser.mathml'}) {
  159: 	    &tth::ttmoptions($inside);
  160: 	} else {
  161: 	    &tth::tthoptions($inside);
  162: 	}
  163:     }
  164:     return $result;
  165: }
  166: 
  167: sub end_tthoption {
  168:     my ($target,$token) = @_;
  169:     my $result;
  170:     return $result;
  171: }
  172: 
  173: #-- <html> tag (end tag optional)
  174: sub start_html {
  175:     my ($target,$token) = @_;
  176:     my $currentstring = '';
  177:     if ($target eq 'web' || $target eq 'edit' || $target eq 'webgrade' ) {
  178: 	# start_body() takes care of emitting the <html> 
  179:     } elsif ($target eq 'tex') {
  180: 
  181: 	$currentstring .= &latex_header();
  182:     }
  183:     return $currentstring;
  184: }
  185: 
  186: sub end_html {
  187:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  188:     my $currentstring = '';
  189:     if ($target eq 'web' || $target eq 'webgrade') {
  190: 	# end_body takes care of the </html>
  191:     }
  192:     return $currentstring;
  193: }
  194: 
  195: #-- <head> tag (end tag optional)
  196: sub start_head {
  197:     my ($target,$token) = @_;
  198:     my $currentstring = '';
  199:     if ($target eq 'web' || $target eq 'webgrade') {
  200: 	&Apache::lonxml::startredirection();
  201:     } 
  202:     return $currentstring;
  203: }
  204: 
  205: sub end_head {
  206:     my ($target,$token) = @_;
  207:     my $currentstring = '';
  208:     if (($target eq 'web'      && $env{'request.state'} eq 'published') ||
  209: 	($target eq 'webgrade' && $env{'request.state'} eq 'published')) {
  210: 	# in case there is a </head> but no <head>
  211: 	if ($Apache::lonxml::redirection) {
  212: 	    $Apache::londefdef::head = &Apache::lonxml::endredirection();
  213: 	}
  214:     } 
  215:     return $currentstring;
  216: }
  217: 
  218: #-- <map> tag (end tag required)
  219: sub start_map {
  220:     my ($target,$token) = @_;
  221:     my $currentstring = '';
  222:     if ($target eq 'web' || $target eq 'webgrade') {
  223: 	$currentstring = $token->[4];     
  224:     } 
  225:     return $currentstring;
  226: }
  227: 
  228: sub end_map {
  229:     my ($target,$token) = @_;
  230:     my $currentstring = '';
  231:     if ($target eq 'web' || $target eq 'webgrade') {
  232: 	$currentstring = $token->[2];    
  233:     } 
  234:     return $currentstring;
  235: }
  236: 
  237: #-- <select> tag (end tag required)
  238: sub start_select {
  239:     my ($target,$token) = @_;
  240:     my $currentstring = '';
  241:     if ($target eq 'web' || $target eq 'webgrade') {
  242: 	$currentstring = $token->[4];     
  243:     }  elsif ($target eq 'tex') {
  244: 	$Apache::londefdef::select=0;
  245:     }
  246:     return $currentstring;
  247: }
  248: 
  249: sub end_select {
  250:     my ($target,$token) = @_;
  251:     my $currentstring = '';
  252:     if ($target eq 'web' || $target eq 'webgrade') {
  253: 	$currentstring = $token->[2];    
  254:     } 
  255:     return $currentstring;
  256: }
  257: 
  258: #-- <option> tag (end tag optional)
  259: sub start_option {
  260:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  261:     my $currentstring = '';
  262:     if ($target eq 'web' || $target eq 'webgrade') {
  263: 	$currentstring = $token->[4];     
  264:     } elsif ($target eq 'tex') {
  265: 	$Apache::londefdef::select++;
  266: 	if ($Apache::londefdef::select == 1) {
  267: 	    $currentstring='\noindent\fbox{'.&Apache::lonxml::get_param('value',$parstack,$safeeval).'}\keephidden{';
  268: 	} else {
  269: 	    $currentstring='\keephidden{';
  270: 	}
  271:     }
  272:     return $currentstring;
  273: }
  274: 
  275: sub end_option {
  276:     my ($target,$token) = @_;
  277:     my $currentstring = '';
  278:     if ($target eq 'web' || $target eq 'webgrade') {
  279: 	$currentstring = $token->[2];    
  280:     }  elsif ($target eq 'tex') {
  281: 	$currentstring='}';
  282:     }
  283:     return $currentstring;
  284: }
  285: 
  286: #-- <input> tag (end tag forbidden)
  287: sub start_input {
  288:     my ($target,$token) = @_;
  289:     my $currentstring = '';
  290:     if ($target eq 'web' || $target eq 'webgrade') {
  291: 	$currentstring = $token->[4];     
  292:     } 
  293:     return $currentstring;
  294: }
  295: 
  296: sub end_input {
  297:     my ($target,$token) = @_;
  298:     my $currentstring = '';
  299:     if ($target eq 'web' || $target eq 'webgrade') {
  300: 	$currentstring = $token->[2];    
  301:     } 
  302:     return $currentstring;
  303: }
  304: 
  305: #-- <textarea> tag (end tag required)
  306: sub start_textarea {
  307:     my ($target,$token) = @_;
  308:     my $currentstring = '';
  309:     if ($target eq 'web' || $target eq 'webgrade') {
  310: 	$currentstring = $token->[4];     
  311:     } 
  312:     return $currentstring;
  313: }
  314: 
  315: sub end_textarea {
  316:     my ($target,$token) = @_;
  317:     my $currentstring = '';
  318:     if ($target eq 'web' || $target eq 'webgrade') {
  319: 	$currentstring = $token->[2];    
  320:     } 
  321:     return $currentstring;
  322: }
  323: 
  324: #-- <form> tag (end tag required)
  325: sub start_form {
  326:     my ($target,$token) = @_;
  327:     my $currentstring = '';
  328:     if ($target eq 'web' || $target eq 'webgrade') {
  329: 	$currentstring = $token->[4];     
  330:     } 
  331:     return $currentstring;
  332: }
  333: 
  334: sub end_form {
  335:     my ($target,$token) = @_;
  336:     my $currentstring = '';
  337:     if ($target eq 'web' || $target eq 'webgrade') {
  338: 	$currentstring = $token->[2];    
  339:     } 
  340:     return $currentstring;
  341: }
  342: 
  343: #-- <title> tag (end tag required)
  344: sub start_title {
  345:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
  346:     my $currentstring = '';
  347:     if ($target eq 'web' || $target eq 'webgrade') {
  348: 	$Apache::londefdef::title = 
  349: 	    &Apache::lonxml::get_all_text('/title',$parser,$style);
  350:     } elsif ($target eq 'tex') {
  351: 	$currentstring .= '\keephidden{Title of the document:  ' 
  352:     }
  353:     if ($target eq 'meta') {
  354: 	$currentstring='<title>';
  355: 	&start_output($target);
  356:     }
  357:     return $currentstring;
  358: }
  359: 
  360: sub end_title {
  361:     my ($target,$token) = @_;
  362:     my $currentstring = '';
  363:     if ($target eq 'web' || $target eq 'webgrade') {
  364: 	# start_title takes care of swallowing the title
  365:     } elsif ($target eq 'tex') {
  366: 	$currentstring .= '}';
  367:     }  
  368:     if ($target eq 'meta') {
  369: 	&end_output($target);
  370: 	$currentstring='</title>';
  371:     } 
  372:     return $currentstring;
  373: }
  374: 
  375: #-- <meta> tag (end tag forbidden)
  376: sub start_meta {
  377:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
  378:     my $currentstring = '';
  379:     if ($target eq 'web' || $target eq 'webgrade') {
  380: 	my $args='';
  381: 	if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
  382: 	if ($args eq '') {
  383: 	    &Apache::lonxml::get_all_text("/meta",$parser,$style);
  384: 	} else {
  385: 	    $currentstring = $token->[4];
  386: 	}
  387:         if ($env{'form.grade_imsexport'}) {
  388:             $currentstring = '';
  389:         }
  390:     } elsif ($target eq 'meta') {
  391: 	unless (&Apache::lonxml::get_param
  392: 		('http-equiv',$parstack,$safeeval,undef,1)) {
  393: 	    my $name=$token->[2]->{'name'};
  394: 	    $name=~tr/A-Z/a-z/;
  395: 	    $name=~s/\s/\_/gs;
  396: 	    $name=~s/\W//gs;
  397: 	    if ($name) {
  398: 		$currentstring='<'.$name;
  399:                  my $display=&Apache::lonxml::get_param
  400: 		('display',$parstack,$safeeval,undef,1);
  401:                 if ($display) {
  402:                     $display=~s/\"/\'/g;
  403: 		    $currentstring.=' display="'.$display.'"';
  404:                 }
  405: 		$currentstring.='>'.
  406: 		    &Apache::lonxml::get_param
  407: 			('content',$parstack,$safeeval,undef,1).
  408: 			'</'.$name.'>';
  409: 	    }
  410:             my $display=&Apache::lonxml::get_param
  411: 		('display',$parstack,$safeeval,undef,1);
  412:             if ($display) {
  413: 		$display=&HTML::Entities::encode($display,'<>&"');
  414: 		$currentstring.='<'.$name.'.display>'.$display.
  415:                                '</'.$name.'.display>';
  416:             }
  417: 	}
  418:     } elsif ($target eq 'tex') {
  419: 	my $content=&Apache::lonxml::get_param('content',$parstack,$safeeval);
  420: 	my $name=&Apache::lonxml::get_param('name',$parstack,$safeeval);
  421: 	if ((not defined $content) && (not defined $name)) {
  422: 	    &Apache::lonxml::startredirection();
  423: 	}
  424:     } elsif ($target eq 'edit') {
  425: 	$currentstring .= &Apache::edit::tag_start($target,$token);
  426: 	$currentstring .= &Apache::edit::text_arg('Name:','name',$token,30);
  427: 	$currentstring .= &Apache::edit::text_arg('Content:','content',$token,70);
  428: 	$currentstring .= &Apache::edit::end_row();
  429:     } elsif ($target eq 'modified') {
  430: 	my $constructtag =
  431: 	    &Apache::edit::get_new_args($token,$parstack,$safeeval,
  432: 					'name','content');
  433: 	if ($constructtag) { $currentstring = &Apache::edit::rebuild_tag($token); }
  434:     }
  435:     return $currentstring;
  436: }
  437: 
  438: sub end_meta {
  439:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  440:     my $currentstring = '';
  441:     if ($target eq 'web' || $target eq 'webgrade') {
  442: 	my $args='';
  443: 	if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
  444: 	if ($args ne '') {
  445: 	    $currentstring = $token->[4];
  446: 	}
  447:     } elsif ($target eq 'tex') {
  448: 	my $content=&Apache::lonxml::get_param('content',$parstack,$safeeval);
  449: 	my $name=&Apache::lonxml::get_param('name',$parstack,$safeeval);
  450: 	if ((not defined $content) && (not defined $name)) {
  451: 	    &Apache::lonxml::endredirection();
  452: 	}
  453:     }
  454:     return $currentstring;
  455: }
  456: 
  457: sub insert_meta {
  458:     return '
  459:     <meta />';
  460: }
  461: 
  462: # accessrule
  463: sub start_accessrule {
  464:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
  465:     my $currentstring = '';
  466:     my $eff  =&Apache::lonxml::get_param('effect',$parstack,$safeeval,undef,1);
  467:     my $realm=&Apache::lonxml::get_param('realm', $parstack,$safeeval,undef,1);
  468:     my $role =&Apache::lonxml::get_param('role',  $parstack,$safeeval,undef,1);
  469:     my $type =&Apache::lonxml::get_param('type',  $parstack,$safeeval,undef,1);
  470: 
  471:     my ($dom,$crs,$sec,$separator);
  472:     if ($type eq 'user') {
  473: 	($dom,$crs,$sec)=split(m{/},$realm);
  474: 	$crs = &LONCAPA::clean_username($crs);
  475: 	$separator = '/';
  476:     } else {
  477: 	($dom,$crs,$sec)=split(/\_/,$realm);
  478: 	$crs = &LONCAPA::clean_courseid($crs);
  479: 	$separator = '_';
  480:     }
  481:     $dom = &LONCAPA::clean_domain($dom);
  482: 
  483:     $sec =~s/\W//;
  484:     $realm = $dom;
  485:     if ($crs =~ /\S/) { $realm .= $separator.$crs; }
  486:     if ($sec =~ /\S/) { $realm .= $separator.$sec; }
  487:     $role=~s/\W//g;
  488: 
  489:     if ($target eq 'web') {
  490: 	my $args='';
  491: 	if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
  492: 	if ($args eq '') {
  493: 	    &Apache::lonxml::get_all_text("/accessrule",$parser,$style);
  494: 	} else {
  495: 	    $currentstring = $token->[4];
  496: 	}
  497:     }
  498:     if ($target eq 'meta') {
  499: 	$currentstring='<rule>'.$eff.':'.$realm.':'.$role.':'.$type.'</rule>';
  500:     }
  501:     return $currentstring;
  502: }
  503: 
  504: sub end_accessrule {
  505:     my ($target,$token,$tagstack,$parstack,$parser) = @_;
  506:     my $currentstring = '';
  507:     if ($target eq 'web') {
  508: 	my $args='';
  509: 	if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
  510: 	if ($args ne '') {
  511: 	    $currentstring = $token->[4];
  512: 	}
  513:     } 
  514:     return $currentstring;
  515: }
  516: 
  517: sub generate_css_links {
  518:     my $links;
  519:     my $css_href = &Apache::lonnet::EXT('resource.0.cssfile');
  520:     if ($css_href =~ /\S/) {
  521: 	&Apache::lonxml::extlink($css_href);
  522: 	$links .= 
  523: 	    '<link rel="stylesheet" type="text/css" href="'.$css_href.'" />';
  524:     }
  525:     return $links;
  526: }
  527: 
  528: #-- <body> tag (end tag required)
  529: sub start_body {
  530:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  531:     my $currentstring = '';
  532: 
  533:     if ($target eq 'web' || $target eq 'webgrade') {
  534: 	if ($Apache::lonhomework::parsing_a_problem) {
  535: 	    &Apache::lonxml::warning("<body> tag found inside of <problem> tag this can cause problems.");
  536: 	    return '';
  537: 	}
  538: 	
  539: 	if (&is_inside_of($tagstack, "head")) {
  540: 	    &end_head(@_);
  541: 	}
  542: 	
  543: 	my $extra_head = &generate_css_links();
  544: 
  545:     # Breadcrumbs
  546:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  547:     if ($env{'request.state'} eq 'construct') {
  548:         &Apache::lonhtmlcommon::add_breadcrumb({
  549:             'text'  => 'Construction Space',
  550:             'href'  => &Apache::loncommon::authorspace(),
  551:         });
  552:         &Apache::lonhtmlcommon::add_breadcrumb({
  553:             'text'  => 'HTML Editor',
  554:             'href'  => '',
  555:         });
  556:         # breadcrumbs (and tools) will be created 
  557:         # in start_page->bodytag->innerregister
  558:     } else {
  559:         # FIXME Where are we?
  560:     }
  561: 
  562: 	$currentstring = 
  563: 	    &Apache::loncommon::start_page($Apache::londefdef::title,
  564: 					   $Apache::londefdef::head
  565: 					      .$extra_head,
  566: 					   {'add_entries'    => $token->[2],
  567: #					    'no_title'       => 1,
  568: 					    'force_register' => 1});
  569: 
  570:         my $header = '';
  571:         if ($env{'request.state'} ne 'published' &&
  572:             $env{'request.state'} ne 'construct') {
  573:             $header=&Apache::lonmenu::constspaceform();
  574:         }
  575:         if ($env{'request.state'} ne 'published') {
  576:             $header.=&Apache::londefdef::edit_controls();
  577:         }
  578:         if ($env{'request.state'} eq 'construct') {
  579:             $currentstring.=&Apache::loncommon::head_subbox(
  580:                                 &Apache::loncommon::CSTR_pageheader()
  581:                                .$header);
  582:         } elsif ($env{'request.state'} eq 'edit') {
  583:             $currentstring.=&Apache::loncommon::head_subbox($header);
  584:         }
  585:         $currentstring.=&Apache::lonxml::message_location();
  586:     } elsif ($target eq 'tex') {
  587:         $currentstring = '';   #  '\begin{document}' is in header.
  588:     }
  589: 
  590:     return $currentstring;
  591: }
  592: 
  593: sub edit_controls {
  594:     my ($nochgview) = @_;
  595:     my $result .= '
  596: <form method="post" action="">
  597: <div class="LC_edit_problem_header">';
  598:     unless ($nochgview) {
  599:         $result .= '
  600: <div class="LC_edit_problem_header_row1">'.
  601: &Apache::lonxml::renderingoptions().'
  602: <input type="submit" name="changeproblemmode" value="'.&mt('Change View').'" />
  603: </div>';
  604:     }
  605:     $result .= '
  606: <div><input type="submit" name="editmode" accesskey="e" value="'.&mt('Edit').'" />';
  607:     if (($env{'request.course.id'}) && ($env{'form.forceedit'})) {
  608:         my $viewtext = &mt('Course View');
  609:         if (&Apache::loncommon::course_type() eq 'Community') {
  610:             $viewtext = &mt('Community View');
  611:         }
  612:         $result .= ('&nbsp;' x 3).'<input type="button" value="'.
  613:                    $viewtext.'" onclick="javascript:location.href=currentURL" />';
  614:     }
  615:     $result .= '</div>
  616: </div>
  617: </form>
  618: ';
  619:     return $result;
  620: }
  621: 
  622: sub end_body {
  623:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  624:     my $currentstring = &end_p();	# Close off unclosed <p>
  625:     if ($target eq 'web' || $target eq 'webgrade') {
  626: 	$currentstring .= &Apache::loncommon::end_page({'discussion' => 1});
  627:     } elsif ($target eq 'tex') {
  628: 	$currentstring .= '\strut\newline\noindent\makebox[\textwidth/$number_of_columns][b]{\hrulefill}\newline\noindent \end{document}';  
  629:     } 
  630:     return $currentstring;
  631: }
  632: 
  633: # \begin{center} causes a new paragprah spacing that looks odd inside 
  634: # of a table cell.  Same at the end of a \center but with a slightly
  635: # larger space .. hence center_correction and center_end_correction.
  636: #
  637: sub center_correction { return '\vspace*{-6 mm}'; } 
  638: sub center_end_correction { return '\vspace*{-7 mm}'; }
  639: 
  640: #-- <center> tag (end tag required)
  641: sub start_center {
  642:     my ($target,$token,$tagstack) = @_;
  643:     my $currentstring = &end_p();	# Close off any prior para.
  644:     if ($target eq 'web' || $target eq 'webgrade') {
  645: 	$currentstring .= $token->[4];     
  646:     } elsif ($target eq 'tex') {
  647: 	if (&is_inside_of($tagstack, "table")) {
  648: 	    $currentstring .= &center_correction();
  649: 	}
  650: 	$currentstring .= '\begin{center}';  
  651:     }
  652:     return $currentstring;
  653: }
  654: 
  655: sub end_center {
  656:     my ($target,$token,$tagstack) = @_;
  657:     my $currentstring = '';
  658:     if ($target eq 'web' || $target eq 'webgrade') {
  659: 	$currentstring = $token->[2];     
  660:     } elsif ($target eq 'tex') {
  661: 	$currentstring = '\end{center}';  
  662: 	if (&is_inside_of($tagstack, "table")) {
  663: 	    $currentstring .= &center_end_correction();
  664: 	}
  665:     }
  666:     return $currentstring;
  667: }
  668: 
  669: #-- <b> tag (end tag required)
  670: #      NOTE: In TeX mode disables internal <p>
  671: sub start_b {
  672:     my ($target,$token) = @_;
  673:     my $currentstring = '';
  674:     if ($target eq 'web' || $target eq 'webgrade') {
  675: 	$currentstring = $token->[4];     
  676:     } elsif ($target eq 'tex') {
  677: 	&disable_para();
  678: 	$currentstring .= '\textbf{';  
  679:     } 
  680:     return $currentstring;
  681: }
  682: 
  683: sub end_b {
  684:     my ($target,$token) = @_;
  685:     my $currentstring = '';
  686:     if ($target eq 'web' || $target eq 'webgrade') {
  687: 	$currentstring = $token->[2];     
  688:     } elsif ($target eq 'tex') {
  689: 	&enable_para();
  690: 	$currentstring = '}';
  691:     } 
  692:     return $currentstring;
  693: }
  694: 
  695: #-- <strong> tag (end tag required)
  696: #    NOTE: in TeX mode disables internal <p>
  697: sub start_strong {
  698:     my ($target,$token) = @_;
  699:     my $currentstring = '';
  700:     if ($target eq 'web' || $target eq 'webgrade') {
  701: 	$currentstring = $token->[4];     
  702:     } elsif ($target eq 'tex') {
  703: 	&disable_para();
  704: 	$currentstring = '\textbf{';  
  705:     } 
  706:     return $currentstring;
  707: }
  708: 
  709: sub end_strong {
  710:     my ($target,$token) = @_;
  711:     my $currentstring = '';
  712:     if ($target eq 'web' || $target eq 'webgrade') {	
  713: 	$currentstring = $token->[2];     
  714:     } elsif ($target eq 'tex') {
  715: 	&enable_para();
  716: 	$currentstring = '}';  
  717:     }
  718:     return $currentstring;
  719: }
  720: 
  721: #-- <h1> tag (end tag required)
  722: sub start_h1 {
  723:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  724:     my $currentstring = &end_p();	# Close off any prior para.
  725:     if ($target eq 'web' || $target eq 'webgrade') {
  726: 	$currentstring .= $token->[4];
  727:     } elsif ($target eq 'tex') {
  728: 	my $pre;
  729: 	my $align=lc(&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1));
  730: 	if ($align eq 'center') {
  731: 	    $pre='\begin{center}';
  732: 	} elsif ($align eq 'left') {
  733: 	    $pre='\rlap{';
  734: 	} elsif ($align eq 'right') {
  735: 	    $pre=' \hfill \llap{';
  736: 	}
  737: 	my $TeXsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval,undef,0);
  738: 	if (not defined $TeXsize) {$TeXsize="large";}
  739: 	$currentstring .= '\strut\newline '.$pre.'{\\'.$TeXsize.' \textbf{'; 
  740:     } elsif ($target eq 'meta') {
  741: 	$currentstring.='<subject>';
  742: 	&start_output($target);
  743:     }
  744:     return $currentstring;
  745: }
  746: 
  747: sub end_h1 {
  748:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  749:     my $currentstring = '';
  750:     if ($target eq 'web' || $target eq 'webgrade') {
  751: 	$currentstring .= $token->[2];
  752:     } elsif ($target eq 'tex') {
  753: 	my $post='\vskip 0 mm ';
  754: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
  755: 	if ($align eq 'center') {
  756: 	    $post='\end{center}';
  757: 	} elsif ($align eq 'left') {
  758: 	    $post='} \hfill'.'\vskip 0 mm ';
  759: 	} elsif ($align eq 'right') {
  760: 	    $post='}'.'\vskip 0 mm ';
  761: 	}
  762: 	$currentstring .= '}}'.$post;
  763:     } elsif ($target eq 'meta') {
  764: 	&end_output($target);
  765: 	$currentstring='</subject>';
  766:     } 
  767:     return $currentstring;
  768: }
  769: 
  770: #-- <h2> tag
  771: sub start_h2 {
  772:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  773:     my $currentstring = &end_p();	# Close off any prior para.
  774:     if ($target eq 'web' || $target eq 'webgrade') {
  775: 	$currentstring .= $token->[4];
  776:     } elsif ($target eq 'tex') {
  777: 	my $pre;
  778: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
  779: 	if ($align eq 'center') {
  780: 	    $pre='\begin{center}';
  781: 	} elsif ($align eq 'left') {
  782: 	    $pre='\rlap{';
  783: 	} elsif ($align eq 'right') {
  784: 	    $pre=' \hfill \llap{';
  785: 	}
  786: 	my $TeXsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval,undef,0);
  787: 	if (not defined $TeXsize) {$TeXsize="large";}
  788: 	$currentstring .= '\strut\newline '.$pre.'{\\'.$TeXsize.' \textbf{'; 
  789:     } 
  790:     return $currentstring;
  791: }
  792: 
  793: sub end_h2 {
  794:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  795:     my $currentstring = '';
  796:     if ($target eq 'web' || $target eq 'webgrade') {
  797: 	$currentstring .= $token->[2];
  798:     } elsif ($target eq 'tex') {
  799: 	my $post='\vskip 0 mm ';
  800: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
  801: 	if ($align eq 'center') {
  802: 	    $post='\end{center}';
  803: 	} elsif ($align eq 'left') {
  804: 	    $post='} \hfill'.'\vskip 0 mm ';
  805: 	} elsif ($align eq 'right') {
  806: 	    $post='}'.'\vskip 0 mm ';
  807: 	}
  808: 	$currentstring .= '}}'.$post;
  809:     } 
  810:     return $currentstring;
  811: }
  812: 
  813: #-- <h3> tag
  814: sub start_h3 {
  815:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  816:     my $currentstring = &end_p();	# Close off any prior para.
  817:     if ($target eq 'web' || $target eq 'webgrade') {
  818: 	$currentstring .= $token->[4];
  819:     } elsif ($target eq 'tex') {
  820: 	my $pre;
  821: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
  822: 	if ($align eq 'center') {
  823: 	    $pre='\begin{center}';
  824: 	} elsif ($align eq 'left') {
  825: 	    $pre='\rlap{';
  826: 	} elsif ($align eq 'right') {
  827: 	    $pre=' \hfill \llap{';
  828: 	}
  829: 	my $TeXsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval,undef,0);
  830: 	if (not defined $TeXsize) {$TeXsize="large";}
  831: 	$currentstring .= '\strut\newline '.$pre.'{\\'.$TeXsize.' \textbf{'; 
  832:     } 
  833:     return $currentstring;
  834: }
  835: 
  836: sub end_h3 {
  837:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  838:     my $currentstring = '';
  839:     if ($target eq 'web' || $target eq 'webgrade') {
  840: 	$currentstring .= $token->[2];
  841:     } elsif ($target eq 'tex') {
  842: 	my $post='\vskip 0 mm ';
  843: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
  844: 	if ($align eq 'center') {
  845: 	    $post='\end{center}';
  846: 	} elsif ($align eq 'left') {
  847: 	    $post='} \hfill'.'\vskip 0 mm ';
  848: 	} elsif ($align eq 'right') {
  849: 	    $post='}'.'\vskip 0 mm ';
  850: 	}
  851: 	$currentstring .= '}}'.$post;
  852:     } 
  853:     return $currentstring;
  854: }
  855: 
  856: #-- <h4> tag
  857: sub start_h4 {
  858:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  859:     my $currentstring = &end_p();	# Close off any prior para.
  860:     if ($target eq 'web' || $target eq 'webgrade') {
  861: 	$currentstring .= $token->[4];
  862:     } elsif ($target eq 'tex') {
  863: 	my $pre;
  864: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
  865: 	if ($align eq 'center') {
  866: 	    $pre='\begin{center}';
  867: 	} elsif ($align eq 'left') {
  868: 	    $pre='\rlap{';
  869: 	} elsif ($align eq 'right') {
  870: 	    $pre=' \hfill \llap{';
  871: 	}
  872: 	my $TeXsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval,undef,0);
  873: 	if (not defined $TeXsize) {$TeXsize="large";}
  874: 	$currentstring .= '\strut\newline '.$pre.'{\\'.$TeXsize.' \textbf{'; 
  875:     } 
  876:     return $currentstring;
  877: }
  878: 
  879: sub end_h4 {
  880:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  881:     my $currentstring = '';
  882:     if ($target eq 'web' || $target eq 'webgrade') {
  883: 	$currentstring .= $token->[2];
  884:     } elsif ($target eq 'tex') {
  885: 	my $post='\vskip 0 mm ';
  886: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
  887: 	if ($align eq 'center') {
  888: 	    $post='\end{center}';
  889: 	} elsif ($align eq 'left') {
  890: 	    $post='} \hfill'.'\vskip 0 mm ';
  891: 	} elsif ($align eq 'right') {
  892: 	    $post='}'.'\vskip 0 mm ';
  893: 	}
  894: 	$currentstring .= '}}'.$post;
  895:     } 
  896:     return $currentstring;
  897: }
  898: 
  899: #-- <h5> tag
  900: sub start_h5 {
  901:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  902:     my $currentstring = &end_p();	# Close off any prior paras.
  903:     if ($target eq 'web' || $target eq 'webgrade') {
  904: 	$currentstring .= $token->[4];
  905:     } elsif ($target eq 'tex') {
  906: 	my $pre;
  907: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
  908: 	if ($align eq 'center') {
  909: 	    $pre='\begin{center}';
  910: 	} elsif ($align eq 'left') {
  911: 	    $pre='\rlap{';
  912: 	} elsif ($align eq 'right') {
  913: 	    $pre=' \hfill \llap{';
  914: 	}
  915: 	my $TeXsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval,undef,0);
  916: 	if (not defined $TeXsize) {$TeXsize="large";}
  917: 	$currentstring .= '\strut\newline '.$pre.'{\\'.$TeXsize.' \textbf{'; 
  918:     } 
  919:     return $currentstring;
  920: }
  921: 
  922: sub end_h5 {
  923:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  924:     my $currentstring = '';
  925:     if ($target eq 'web' || $target eq 'webgrade') {
  926: 	$currentstring .= $token->[2];
  927:     } elsif ($target eq 'tex') {
  928: 	my $post='\vskip 0 mm ';
  929: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
  930: 	if ($align eq 'center') {
  931: 	    $post='\end{center}';
  932: 	} elsif ($align eq 'left') {
  933: 	    $post='} \hfill'.'\vskip 0 mm ';
  934: 	} elsif ($align eq 'right') {
  935: 	    $post='}'.'\vskip 0 mm ';
  936: 	}
  937: 	$currentstring .= '}}'.$post;
  938:     } 
  939:     return $currentstring;
  940: }
  941: 
  942: #-- <h6> tag
  943: sub start_h6 {
  944:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  945:     my $currentstring = &end_p();	# Close off any prior paras.
  946:     if ($target eq 'web' || $target eq 'webgrade') {
  947: 	$currentstring .= $token->[4];
  948:     } elsif ($target eq 'tex') {
  949: 	my $pre;
  950: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
  951: 	if ($align eq 'center') {
  952: 	    $pre='\begin{center}';
  953: 	} elsif ($align eq 'left') {
  954: 	    $pre='\rlap{';
  955: 	} elsif ($align eq 'right') {
  956: 	    $pre=' \hfill \llap{';
  957: 	}
  958: 	my $TeXsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval,undef,0);
  959: 	if (not defined $TeXsize) {$TeXsize="large";}
  960: 	$currentstring .= '\strut\newline '.$pre.'{\\'.$TeXsize.' \textbf{'; 
  961:     } 
  962:     return $currentstring;
  963: }
  964: 
  965: sub end_h6 {
  966:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  967:     my $currentstring = '';
  968:     if ($target eq 'web' || $target eq 'webgrade') {
  969: 	$currentstring .= $token->[2];
  970:     } elsif ($target eq 'tex') {
  971: 	my $post='\vskip 0 mm ';
  972: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
  973: 	if ($align eq 'center') {
  974: 	    $post='\end{center}';
  975: 	} elsif ($align eq 'left') {
  976: 	    $post='} \hfill'.'\vskip 0 mm ';
  977: 	} elsif ($align eq 'right') {
  978: 	    $post='}'.'\vskip 0 mm ';
  979: 	}
  980: 	$currentstring .= '}}'.$post;
  981:     } 
  982:     return $currentstring;
  983: }
  984: 
  985: #--- <cite> tag (end tag required)
  986: sub start_cite {
  987:     my ($target,$token) = @_;
  988:     my $currentstring = '';
  989:     if ($target eq 'web' || $target eq 'webgrade') {
  990: 	$currentstring .= $token->[4];
  991:     } elsif ($target eq 'tex') {
  992: 	$currentstring .= '\textit{';
  993:     }
  994:     return $currentstring;
  995: }
  996: 
  997: sub end_cite {
  998:     my ($target,$token) = @_;
  999:     my $currentstring = '';
 1000:     if ($target eq 'web' || $target eq 'webgrade') {
 1001: 	$currentstring .= $token->[2];
 1002:     } elsif ($target eq 'tex') {
 1003: 	$currentstring .= '}';
 1004:     }
 1005:     return $currentstring;
 1006: }
 1007: 
 1008: #-- <i> tag (end tag required)
 1009: sub start_i {
 1010:     my ($target,$token) = @_;
 1011:     my $currentstring = '';
 1012:     if ($target eq 'web' || $target eq 'webgrade') {
 1013: 	$currentstring .= $token->[4];
 1014:     } elsif ($target eq 'tex') {
 1015: 	$currentstring .= '\textit{';
 1016:     }
 1017:     return $currentstring;
 1018: }
 1019: 
 1020: sub end_i {
 1021:     my ($target,$token) = @_;
 1022:     my $currentstring = '';
 1023:     if ($target eq 'web' || $target eq 'webgrade') {
 1024: 	$currentstring .= $token->[2];
 1025:     } elsif ($target eq 'tex') {
 1026: 	$currentstring .= '}';
 1027:     } 
 1028:     return $currentstring;
 1029: }
 1030: 
 1031: #-- <address> tag (end tag required)
 1032: sub start_address {
 1033:     my ($target,$token) = @_;
 1034:     my $currentstring = '';
 1035:     if ($target eq 'web' || $target eq 'webgrade') {
 1036: 	$currentstring .= $token->[4];
 1037:     } elsif ($target eq 'tex') {
 1038: 	$currentstring .= '\textit{';
 1039:     }
 1040:     return $currentstring;
 1041: }
 1042: 
 1043: sub end_address {
 1044:     my ($target,$token) = @_;
 1045:     my $currentstring = '';
 1046:     if ($target eq 'web' || $target eq 'webgrade') {
 1047: 	$currentstring .= $token->[2];
 1048:     } elsif ($target eq 'tex') {
 1049: 	$currentstring .= '}';
 1050:     }
 1051:     return $currentstring;
 1052: }
 1053: 
 1054: #-- <dfn> tag (end tag required)
 1055: sub start_dfn {
 1056:     my ($target,$token) = @_;
 1057:     my $currentstring = '';
 1058:     if ($target eq 'web' || $target eq 'webgrade') {
 1059: 	$currentstring .= $token->[4];
 1060:     } elsif ($target eq 'tex') {
 1061: 	$currentstring .= '\textit{';
 1062:     } 
 1063:     return $currentstring;
 1064: }
 1065: 
 1066: sub end_dfn {
 1067:     my ($target,$token) = @_;
 1068:     my $currentstring = '';
 1069:     if ($target eq 'web' || $target eq 'webgrade') {
 1070: 	$currentstring .= $token->[2];
 1071:     } elsif ($target eq 'tex') {
 1072: 	$currentstring .= '}';
 1073:     }
 1074:     return $currentstring;
 1075: }
 1076: 
 1077: #-- <tt> tag (end tag required)
 1078: sub start_tt {
 1079:     my ($target,$token) = @_;
 1080:     my $currentstring = '';
 1081:     if ($target eq 'web' || $target eq 'webgrade') {
 1082: 	$currentstring .= $token->[4];
 1083:     } elsif ($target eq 'tex') {
 1084: 	$currentstring .= '\texttt{';
 1085:     }
 1086:     return $currentstring;
 1087: }
 1088: 
 1089: sub end_tt {
 1090:     my ($target,$token) = @_;
 1091:     my $currentstring = '';
 1092:     if ($target eq 'web' || $target eq 'webgrade') {
 1093: 	$currentstring .= $token->[2];
 1094:     } elsif ($target eq 'tex') {
 1095: 	$currentstring .= '}';
 1096:     }
 1097:     return $currentstring;
 1098: }
 1099: 
 1100: #-- <kbd> tag (end tag required)
 1101: sub start_kbd {
 1102:     my ($target,$token) = @_;
 1103:     my $currentstring = '';
 1104:     if ($target eq 'web' || $target eq 'webgrade') {
 1105: 	$currentstring .= $token->[4];
 1106:     } elsif ($target eq 'tex') {
 1107: 	$currentstring .= '\texttt{';
 1108:     }
 1109:     return $currentstring;
 1110: }
 1111: 
 1112: sub end_kbd {
 1113:     my ($target,$token) = @_;
 1114:     my $currentstring = '';
 1115:     if ($target eq 'web' || $target eq 'webgrade') {
 1116: 	$currentstring .= $token->[2];
 1117:     } elsif ($target eq 'tex') {
 1118: 	$currentstring .= '}';
 1119:     }
 1120:     return $currentstring;
 1121: }
 1122: 
 1123: #-- <code> tag (end tag required)
 1124: sub start_code {
 1125:     my ($target,$token) = @_;
 1126:     my $currentstring = '';
 1127:     if ($target eq 'web' || $target eq 'webgrade') {
 1128: 	$currentstring .= $token->[4];
 1129:     } elsif ($target eq 'tex') {
 1130: 	$currentstring .= '\texttt{';
 1131:     } 
 1132:     return $currentstring;
 1133: }
 1134: 
 1135: sub end_code {
 1136:     my ($target,$token) = @_;
 1137:     my $currentstring = '';
 1138:     if ($target eq 'web' || $target eq 'webgrade') {
 1139: 	$currentstring .= $token->[2];
 1140:     } elsif ($target eq 'tex') {
 1141: 	$currentstring .= '}';
 1142:     } 
 1143:     return $currentstring;
 1144: }
 1145: 
 1146: #-- <em> tag (end tag required)
 1147: sub start_em {
 1148:     my ($target,$token) = @_;
 1149:     my $currentstring = '';
 1150:     if ($target eq 'web' || $target eq 'webgrade') {
 1151: 	$currentstring .= $token->[4];
 1152:     } elsif ($target eq 'tex') {
 1153: 	$currentstring .= '\emph{';
 1154:     }
 1155:     return $currentstring;
 1156: }
 1157: 
 1158: sub end_em {
 1159:     my ($target,$token) = @_;
 1160:     my $currentstring = '';
 1161:     if ($target eq 'web' || $target eq 'webgrade') {
 1162: 	$currentstring .= $token->[2];
 1163:     } elsif ($target eq 'tex') {
 1164: 	$currentstring .= '}';
 1165:     } 
 1166:     return $currentstring;
 1167: }
 1168: 
 1169: #-- <q> tag (end tag required)
 1170: sub start_q {
 1171:     my ($target,$token) = @_;
 1172:     my $currentstring = '';
 1173:     if ($target eq 'web' || $target eq 'webgrade') {
 1174: 	$currentstring .= $token->[4];
 1175:     } elsif ($target eq 'tex') {
 1176: 	$currentstring .= '\emph{';
 1177:     }
 1178:     return $currentstring;
 1179: }
 1180: 
 1181: sub end_q {
 1182:     my ($target,$token) = @_;
 1183:     my $currentstring = '';
 1184:     if ($target eq 'web' || $target eq 'webgrade') {
 1185: 	$currentstring .= $token->[2];
 1186:     } elsif ($target eq 'tex') {
 1187: 	$currentstring .= '}';
 1188:     } 
 1189:     return $currentstring;
 1190: }
 1191: 
 1192: #  <p> is a bit strange since it does not require a closing </p>
 1193: #  However in latex, we must often output closing stuff to end
 1194: #  environments and {}'s etc.  Therefore we do all the work
 1195: #  of figuring out the ending strings in the start tag processing,
 1196: #  and provide a mechanism to output the stop text external
 1197: #  to tag processing.
 1198: #
 1199: {
 1200: 
 1201:     my $closing_string = '';		# String required to close <p>
 1202: 
 1203: #   Some tags are <p> fragile meaning that <p> inside of them
 1204: #   does not work within TeX mode.  This is managed via the 
 1205: #   counter below:
 1206: #
 1207: 
 1208:     my $para_disabled = 0;
 1209: 
 1210: sub disable_para {
 1211:     $para_disabled++;
 1212: }
 1213: sub enable_para {
 1214:     $para_disabled--;
 1215: }
 1216: 
 1217: 
 1218: #-- <p> tag (end tag optional)
 1219: #optional attribute - align="center|left|right"
 1220: sub start_p {
 1221:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1222:     my $currentstring = '';
 1223:     if ($target eq 'web' || $target eq 'webgrade') {
 1224: 	$currentstring .= &end_p();	# close off prior para if in progress.
 1225: 	$currentstring .= $token->[4];
 1226: 	if (! ($currentstring =~ /\//)) {
 1227: 	    $closing_string = '</p>'; # Deal correctly with <p /> e.g.
 1228: 	}
 1229:     } elsif ($target eq 'tex' && !$para_disabled) {
 1230: 
 1231: 	$currentstring .= &end_p();	# close off prior para if in progress.
 1232: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
 1233: 	if ($align eq 'center') {
 1234: 	    $currentstring .='\begin{center}\par ';
 1235: 	    $closing_string = '\end{center}';
 1236: 	    if (&is_inside_of($tagstack, "table")) {
 1237: 		$currentstring = &center_correction().$currentstring;
 1238: 	    }
 1239: 	} elsif ($align eq 'right') {
 1240: 	    $currentstring.="\n".'{\flushright ';
 1241: #	    $currentstring.='\makebox['.$env{'form.textwidth'}.']{\hfill\llap{';
 1242: 	    $closing_string= "}\n";
 1243: 	} elsif ($align eq 'left') {
 1244: 	    $currentstring.= "\n".'{\flushleft ';
 1245: #	    $currentstring.='\noindent\makebox['.$env{'form.textwidth'}.']{{';
 1246: 	    $closing_string = "}\n";
 1247: 	} else {
 1248:             $currentstring.='\par ';
 1249: 	    if (&is_inside_of($tagstack, 'table')) {
 1250: 		$closing_string = '\vskip 0pt'; # Seems to be consistent with <p> in tables.
 1251: 	    } else {
 1252: 		$closing_string = '\strut\\\\\strut ';
 1253: 	    }
 1254:         }
 1255: 
 1256:     }
 1257:     return $currentstring;
 1258: }
 1259: #
 1260: #  End paragraph processing just requires that we output the
 1261: #  closing string that was saved and blank it.
 1262: sub end_p {
 1263:     #  Note only 'tex' mode uses disable_para and enable_para
 1264:     #  so we don't need to know the target in the check below:
 1265: 
 1266:     if (!$para_disabled) {
 1267: 	my $current_string = $closing_string;
 1268: 	$closing_string = '';	# Not in a para anymore.
 1269: 	return $current_string;
 1270:     } else {
 1271: 	return '';
 1272:     }
 1273: 
 1274: }
 1275: }
 1276: #-- <br> tag (end tag forbidden)
 1277: sub start_br {
 1278:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
 1279:     my $currentstring = '';
 1280:     if ($target eq 'web' || $target eq 'webgrade') {
 1281: 	$currentstring .= $token->[4];
 1282:     } elsif ($target eq 'tex') {
 1283: 	my @tempo=@$tagstack;
 1284: 	my $signal=0;
 1285: 	#  Not going to factor this to is_inside_of since that would require
 1286:         #  multiple stack traversals.
 1287: 	#
 1288: 	for (my $i=$#tempo;$i>=0;$i--) {
 1289: 	    if (($tempo[$i] eq 'b') || ($tempo[$i] eq 'strong') ||
 1290:                 ($tempo[$i] eq 'ol') || ($tempo[$i] eq 'ul'))  {
 1291: 		$signal=1;
 1292: 	    }
 1293: 	    if (($tempo[$i] eq 'td') || ($tempo[$i] eq 'th')) {
 1294: 		$signal = 1;
 1295: 	    }
 1296: 	}
 1297: 	if ($signal != 1) {
 1298: 	    $currentstring .= '\strut \\\\ \strut ';
 1299: 	}
 1300:     
 1301:     }
 1302:     return $currentstring;
 1303: }
 1304: 
 1305: sub end_br {
 1306:     my ($target,$token) = @_;
 1307:     my $currentstring = '';
 1308:     if ($target eq 'web' || $target eq 'webgrade') {
 1309: 	$currentstring .= $token->[2];
 1310:     }
 1311:     return $currentstring;
 1312: }
 1313: 
 1314: #-- <big> tag (end tag required)
 1315: sub start_big {
 1316:     my ($target,$token) = @_;
 1317:     my $currentstring = '';
 1318:     if ($target eq 'web' || $target eq 'webgrade') {
 1319: 	$currentstring .= $token->[4];
 1320:     } elsif ($target eq 'tex') {
 1321: 	$currentstring .= '{\large ';
 1322:     } 
 1323:     return $currentstring;
 1324: }
 1325: 
 1326: sub end_big {
 1327:     my ($target,$token) = @_;
 1328:     my $currentstring = '';
 1329:     if ($target eq 'web' || $target eq 'webgrade') {
 1330: 	$currentstring .= $token->[2];
 1331:     } elsif ($target eq 'tex') {
 1332: 	$currentstring .= '}';
 1333:     }
 1334:     return $currentstring;
 1335: }
 1336: 
 1337: #-- <small> tag (end tag required)
 1338: sub start_small {
 1339:     my ($target,$token) = @_;
 1340:     my $currentstring = '';
 1341:     if ($target eq 'web' || $target eq 'webgrade') {
 1342: 	$currentstring .= $token->[4];
 1343:     } elsif ($target eq 'tex') {
 1344: 	$currentstring .= '{\footnotesize ';
 1345:     }
 1346:     return $currentstring;
 1347: }
 1348: 
 1349: sub end_small {
 1350:     my ($target,$token) = @_;
 1351:     my $currentstring = '';
 1352:     if ($target eq 'web' || $target eq 'webgrade') {
 1353: 	$currentstring .= $token->[2];
 1354:     } elsif ($target eq 'tex') {
 1355: 	$currentstring .= '}';
 1356:     }
 1357:     return $currentstring;
 1358: }
 1359: 
 1360: #-- <basefont> tag (end tag forbidden)
 1361: sub start_basefont {
 1362:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
 1363:     my $currentstring = '';
 1364:     if ($target eq 'web' || $target eq 'webgrade') {
 1365: 	$currentstring = $token->[4];     
 1366:     } elsif ($target eq 'tex') {
 1367: 	my $basesize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval);
 1368: 	if (defined $basesize) {
 1369: 	    $currentstring = '{\\'.$basesize.' ';
 1370: 	}
 1371:     }
 1372:     return $currentstring;
 1373: }
 1374: 
 1375: sub end_basefont {
 1376:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1377:     my $currentstring = '';
 1378:     if ($target eq 'web' || $target eq 'webgrade') {
 1379: 	$currentstring = $token->[4];     
 1380:     } elsif ($target eq 'tex') {
 1381: 	my $basesize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval);
 1382: 	if (defined $basesize) {
 1383: 	    $currentstring = '}';
 1384: 	}
 1385:     }
 1386:     return $currentstring;
 1387: }
 1388: 
 1389: #-- <font> tag (end tag required)
 1390: sub start_font {
 1391:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
 1392:     my $currentstring = '';
 1393:     if ($target eq 'web' || $target eq 'webgrade') {
 1394: 	my $face=&Apache::lonxml::get_param('face',$parstack,$safeeval);
 1395: 	$currentstring = $token->[4];     
 1396:     }  elsif ($target eq 'tex') {
 1397: 	my $fontsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval);
 1398: 	if (defined $fontsize) {
 1399: 	    $currentstring = '{\\'.$fontsize.' ';
 1400: 	}
 1401:     }
 1402:     return $currentstring;
 1403: }
 1404: 
 1405: sub end_font {
 1406:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
 1407:     my $currentstring = '';
 1408:     if ($target eq 'web' || $target eq 'webgrade') {
 1409: 	$currentstring = $token->[2];    
 1410:     }  elsif ($target eq 'tex') {
 1411: 	my $fontsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval);
 1412: 	if (defined $fontsize) {
 1413: 	    $currentstring = '}';
 1414: 	}
 1415:     }
 1416:     return $currentstring;
 1417: }
 1418:  
 1419: #-- <strike> tag (end tag required)
 1420: sub start_strike {
 1421:     my ($target,$token) = @_;
 1422:     my $currentstring = '';
 1423:     if ($target eq 'web' || $target eq 'webgrade') {
 1424: 	$currentstring .= $token->[4];
 1425:     } elsif ($target eq 'tex') {
 1426: 	&Apache::lonxml::startredirection();
 1427:     } 
 1428:     return $currentstring;
 1429: }
 1430: 
 1431: sub end_strike {
 1432:     my ($target,$token) = @_;
 1433:     my $currentstring = '';
 1434:     if ($target eq 'web' || $target eq 'webgrade') {
 1435: 	$currentstring .= $token->[2];
 1436:     } elsif ($target eq 'tex') {
 1437: 	$currentstring=&Apache::lonxml::endredirection();
 1438: 	$currentstring=~s/(\S)(\s+)(\S)/$1\}$2\\underline\{$3/g; 
 1439: 	$currentstring=~s/^\s*(\S)/\\underline\{$1/; 
 1440: 	$currentstring=~s/(\S)\s*$/$1\}/;
 1441:     }
 1442:     return $currentstring;
 1443: }
 1444: 
 1445: #-- <s> tag (end tag required)
 1446: sub start_s {
 1447:     my ($target,$token) = @_;
 1448:     my $currentstring = '';
 1449:     if ($target eq 'web' || $target eq 'webgrade') {
 1450: 	$currentstring .= $token->[4];
 1451:     } elsif ($target eq 'tex') {
 1452: 	&Apache::lonxml::startredirection();
 1453:     } 
 1454:     return $currentstring;
 1455: }
 1456: 
 1457: sub end_s {
 1458:     my ($target,$token) = @_;
 1459:     my $currentstring = '';
 1460:     if ($target eq 'web' || $target eq 'webgrade') {
 1461: 	$currentstring .= $token->[2];
 1462:     } elsif ($target eq 'tex') {
 1463: 	$currentstring=&Apache::lonxml::endredirection();
 1464: 	$currentstring=~s/(\S)(\s+)(\S)/$1\}$2\\underline\{$3/g;
 1465: 	$currentstring=~s/^\s*(\S)/\\underline\{$1/;
 1466: 	$currentstring=~s/(\S)\s*$/$1\}/;	
 1467:     }
 1468:     return $currentstring;
 1469: }
 1470: 
 1471: #-- <sub> tag (end tag required)
 1472: sub start_sub {
 1473:     my ($target,$token) = @_;
 1474:     my $currentstring = '';
 1475:     if ($target eq 'web' || $target eq 'webgrade') {
 1476: 	$currentstring .= $token->[4];
 1477:     } elsif ($target eq 'tex') {
 1478: 	$currentstring .= '\raisebox{-\smallskipamount}{\scriptsize{';
 1479:     } 
 1480:     return $currentstring;
 1481: }
 1482: 
 1483: sub end_sub {
 1484:     my ($target,$token) = @_;
 1485:     my $currentstring = '';
 1486:     if ($target eq 'web' || $target eq 'webgrade') {
 1487: 	$currentstring .= $token->[2];
 1488:     } elsif ($target eq 'tex') {
 1489: 	$currentstring .= '}}';
 1490:     }
 1491:     return $currentstring;
 1492: }
 1493: 
 1494: #-- <sup> tag (end tag required)
 1495: sub start_sup {
 1496:     my ($target,$token) = @_;
 1497:     my $currentstring = '';
 1498:     if ($target eq 'web' || $target eq 'webgrade') {
 1499: 	$currentstring .= $token->[4];
 1500:     } elsif ($target eq 'tex') {
 1501: 	$currentstring .= '\raisebox{\smallskipamount}{\scriptsize{';
 1502:     } 
 1503:     return $currentstring;
 1504: }
 1505: 
 1506: sub end_sup {
 1507:     my ($target,$token) = @_;
 1508:     my $currentstring = '';
 1509:     if ($target eq 'web' || $target eq 'webgrade') {
 1510: 	$currentstring .= $token->[2];
 1511:     } elsif ($target eq 'tex') {
 1512: 	$currentstring .= '}}';
 1513:     }
 1514:     return $currentstring;
 1515: }
 1516: 
 1517: #-- <hr> tag (end tag forbidden)
 1518: sub start_hr {
 1519:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1520:     my $currentstring = &end_p();	# End enclosing para.
 1521:     if ($target eq 'web' || $target eq 'webgrade') {
 1522: 	$currentstring .= $token->[4];
 1523:     } elsif ($target eq 'tex') {
 1524: 
 1525: 	# <hr /> can't be inside of <sup><sub> thank you LaTeX.
 1526: 	# 
 1527: 	my $restart_sub = 0;
 1528: 	my $restart_sup = 0;
 1529: 
 1530: 	# Since <sub> and <sup> are simple tags it's ok to turn off/on
 1531: 	# using the start_ stop_ functions.. those tags only care about
 1532: 	# $target.
 1533: 
 1534: 	if (&is_inside_of($tagstack, "sub")) {
 1535: 	    $restart_sub = 1;
 1536: 	    $currentstring .= &end_sub($target, $token, $tagstack, 
 1537: 				       $parstack, $parser, $safeeval);
 1538: 	}
 1539: 	if (&is_inside_of($tagstack, "sup")) {
 1540: 	    $restart_sup = 1;
 1541: 	    $currentstring .= &end_sup($target, $token, $tagstack,
 1542: 				       $parstack, $parser, $safeeval);
 1543: 	}	
 1544: 
 1545: 	my $LaTeXwidth = &Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval,undef,0);
 1546: 	if (defined $LaTeXwidth) {
 1547: 	    if ($LaTeXwidth=~/^%/) {
 1548: 		substr($LaTeXwidth,0,1)='';
 1549: 		$LaTeXwidth=($LaTeXwidth/100).'\textwidth';
 1550: 	    }
 1551: 	} else {
 1552: 	    $LaTeXwidth ='0.9\textwidth';
 1553: 	}
 1554: 	my ($pre,$post);
 1555: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
 1556: 	if (($align eq 'center') || (not defined $align)) {
 1557: 	    $pre=''; $post='';
 1558: 	} elsif ($align eq 'left') {
 1559: 	    $pre='\rlap{'; $post='} \hfill';
 1560: 	} elsif ($align eq 'right') {
 1561: 	    $pre=' \hfill \llap{'; $post='}';
 1562: 	}
 1563: 	$currentstring .= ' \vskip 0 mm \noindent\makebox['.$LaTeXwidth.']{'.$pre.'\makebox['.
 1564:                                     $LaTeXwidth.'][b]{\hrulefill}'.$post.'}\vskip 0 mm ';
 1565: 	# Turn stuff back on that we can't be inside of.
 1566: 
 1567: 	if ($restart_sub) {
 1568: 	    $currentstring .= &start_sub($target, $token, $tagstack,
 1569: 					$parstack, $parser, $safeeval);
 1570: 	}
 1571: 	if ($restart_sup) {
 1572: 	    $currentstring .= &start_sup($target, $token, $tagstack,
 1573: 					 $parstack, $parser, $safeeval);
 1574: 	}	
 1575:     } 
 1576:     return $currentstring;
 1577: }
 1578: 
 1579: sub end_hr {
 1580:     my ($target,$token) = @_;
 1581:     my $currentstring = '';
 1582:     if ($target eq 'web' || $target eq 'webgrade') {
 1583: 	$currentstring .= $token->[2];
 1584:     }
 1585:     return $currentstring;
 1586: }
 1587: 
 1588: #-- <div> tag (end tag required)
 1589: {
 1590: 
 1591: #  Since div can be nested, the stack below is used
 1592: #  in 'tex' mode to store the ending strings
 1593: #  for the div stack.
 1594: 
 1595:     my @div_end_stack;
 1596: 
 1597: sub start_div {
 1598:     my ($target,$token, $tagstack, $parstack, $parser, $safeeval) = @_;
 1599:     my $currentstring = &end_p();	# Close enclosing para.
 1600:     if ($target eq 'web' || $target eq 'webgrade') {
 1601: 	$currentstring .= $token->[4];
 1602:     } 
 1603:     if ($target eq 'tex') {
 1604: 	# 4 possible alignments: left, right, center, and -missing-.
 1605:         # If inside a table row, we must let the table logic
 1606: 	# do the alignment, however.
 1607: 	# 
 1608: 
 1609: 	my $endstring = '';
 1610: 
 1611: 	my $align = lc(&Apache::lonxml::get_param('align', $parstack,
 1612: 						  $safeeval, undef, 1));
 1613: 	if ($align eq 'center') {
 1614: 	    $currentstring .= '\begin{center}';
 1615: 	    $endstring      = '\end{center}';
 1616: 	    if (&is_inside_of($tagstack, "table")) {
 1617: 		$currentstring = &center_correction().$currentstring;
 1618: 		$endstring    .= &center_end_correction(); 
 1619: 	    }
 1620: 	}
 1621: 	elsif ($align eq 'right') {
 1622: 	    $currentstring .= '\begin{flushright}';
 1623: 	    $endstring     .= '\end{flushright}';
 1624: 	} elsif ($align eq 'left') {
 1625: 	    $currentstring .= '\begin{flushleft}';
 1626: 	    $endstring     = '\end{flushleft}';
 1627: 	} else {
 1628: 	
 1629: 	}
 1630: 	$currentstring .= "\n";   # For human readability.
 1631: 	$endstring       = "\n$endstring\n"; # For human readability
 1632: 	push(@div_end_stack, $endstring);
 1633:     }
 1634:     return $currentstring;
 1635: }
 1636: 
 1637: sub end_div {
 1638:     my ($target,$token) = @_;
 1639:     my $currentstring = '';
 1640:     if ($target eq 'web' || $target eq 'webgrade') {
 1641: 	$currentstring .= $token->[2];
 1642:     }
 1643:     if ($target eq 'tex') {
 1644: 	my $endstring = pop @div_end_stack;
 1645: 	$currentstring .= $endstring;
 1646:     }
 1647:     return $currentstring;
 1648: }
 1649: }
 1650: 
 1651: #-- <a> tag (end tag required)
 1652: sub start_a {
 1653:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1654:     my $currentstring = '';
 1655:     if ($target eq 'web' || $target eq 'webgrade') {
 1656: 	my $href=&Apache::lonxml::get_param('href',$parstack,$safeeval,
 1657: 					    undef,1);
 1658: 	$currentstring=&Apache::lonenc::encrypt_ref($token,{'href'=>$href});
 1659:         if ($href =~ /\S/) {
 1660:             if ($href !~ m{^https?://}) {
 1661:                 my $url=&Apache::lonnet::hreflocation('',$env{'request.filename'});
 1662:                 my $linkurl;
 1663:                 if ($href =~ m{^/uploaded/}) {
 1664:                     $linkurl = $href;
 1665:                 } elsif ($href =~ m{^[^/]}) {
 1666:                     my $path = $url;
 1667:                     $path  =~ s{[^/]*$}{};
 1668:                     $linkurl = $path.$href;
 1669:                 }
 1670:                 if ($linkurl =~ m{^/uploaded/}) {
 1671:                     if (!&Apache::lonnet::allowed('bre',$linkurl)) {
 1672:                         if (&Apache::lonnet::is_on_map($url)) {
 1673:                             &Apache::lonxml::extlink($linkurl);
 1674:                         }
 1675:                     }
 1676:                 }
 1677:             }
 1678:         }
 1679:     }
 1680:     return $currentstring;
 1681: }
 1682: 
 1683: sub end_a {
 1684:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1685:     my $currentstring = '';
 1686:     if ($target eq 'web' || $target eq 'webgrade') {
 1687: 	$currentstring .= $token->[2];
 1688:     }
 1689:     if ($target eq 'tex') {
 1690: 	my $href =
 1691: 	    &Apache::lonxml::get_param('href',$parstack,$safeeval,undef,1);
 1692: 	my $name =
 1693: 	    &Apache::lonxml::get_param('name',$parstack,$safeeval,undef,1);
 1694:         my $uriprint =
 1695:             &Apache::lonxml::get_param('uriprint',$parstack,$safeeval,undef,1);
 1696:         my $anchorprint =
 1697:             &Apache::lonxml::get_param('anchorprint',$parstack,$safeeval,undef,1);
 1698: 	if (($href =~ /\S/) && ($uriprint=~/^on|uriprint|yes|1$/i)) {
 1699: 	    $href =~ s/([^\\])%/$1\\\%/g;
 1700: 	    # Substitute special symbols... and allow line breaks at each /
 1701: 	    #
 1702: 	    $href = &Apache::lonxml::latex_special_symbols($href);
 1703: 	    $href =~ s/\//\/\\-/g;              # Map / to /\- to allow hyphenation.
 1704: 	    $currentstring .= ' ({\tt URI:'.$href.'})';
 1705: 	} elsif (($name =~ /\S/) && ($anchorprint=~/^on|anchorprint|yes|1$/i)) {
 1706: 	    $currentstring .= ' ({\tt Anchor:'.&Apache::lonxml::latex_special_symbols($name).'})';
 1707: 	} else {
 1708: 	    $currentstring.='';
 1709: 	}	
 1710:     }
 1711:     return $currentstring;
 1712: }
 1713: 
 1714: #-- <li> tag (end tag optional)
 1715: sub start_li {
 1716:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1717:     my $currentstring = '';
 1718:     if ($target eq 'web' || $target eq 'webgrade') {
 1719: 	$currentstring = $token->[4];     
 1720:     } elsif ($target eq 'tex') {
 1721: 	my $type=&Apache::lonxml::get_param('type',$parstack,$safeeval,undef,0);
 1722: 	my $value=&Apache::lonxml::get_param('value',$parstack,$safeeval,undef,0);
 1723: 	#FIXME need to support types i and I 
 1724: 	if ($type=~/disc/) {
 1725: 	    $currentstring .= ' \item[$\bullet$] ';
 1726: 	} elsif ($type=~/circle/) {
 1727: 	    $currentstring .= ' \item[$\circ$] ';
 1728: 	} elsif ($type=~/square/) {
 1729: 	    $currentstring .= ' \item[$\diamond$] ';
 1730: 	} elsif ($type eq '1') {
 1731: 	    $currentstring .= ' \item['.($Apache::londefdef::list_index+1).'.]';
 1732: 	} elsif ($type eq 'A') {
 1733: 	    $currentstring .= ' \item['.('A'..'Z')[$Apache::londefdef::list_index].'.]';
 1734: 	} elsif ($type eq 'a') {
 1735: 	    $currentstring .= ' \item['.('a'..'z')[$Apache::londefdef::list_index].'.]';
 1736: 	} elsif ($value ne '') {
 1737: 	    $currentstring .= ' \item['.$value.'] ';
 1738: 	} else {
 1739: 	    $currentstring .= ' \item ';
 1740: 	}  
 1741: 	$Apache::londefdef::list_index++;
 1742:     }
 1743:     return $currentstring;
 1744: }
 1745: 
 1746: sub end_li {
 1747:     my ($target,$token) = @_;
 1748:     my $currentstring = &end_p();	# In case there's a <p> in the <li>
 1749:     if ($target eq 'web' || $target eq 'webgrade') {
 1750: 	$currentstring .= $token->[2];     
 1751:     } 
 1752:     return $currentstring;
 1753: }
 1754: 
 1755: #-- <u> tag (end tag required)
 1756: sub start_u {
 1757:     my ($target,$token) = @_;
 1758:     my $currentstring = '';
 1759:     if ($target eq 'web' || $target eq 'webgrade') {
 1760: 	$currentstring .= $token->[4];
 1761:     } elsif ($target eq 'tex') {
 1762: 	&Apache::lonxml::startredirection();
 1763:     } 
 1764:     return $currentstring;
 1765: }
 1766: 
 1767: sub end_u {
 1768:     my ($target,$token) = @_;
 1769:     my $currentstring = '';
 1770:     if ($target eq 'web' || $target eq 'webgrade') {
 1771: 	$currentstring .= $token->[2];
 1772:     } elsif ($target eq 'tex') {
 1773: 	$currentstring=&Apache::lonxml::endredirection();
 1774: 	$currentstring=~s/(\S)(\s+)(\S)/$1\}$2\\underline\{$3/g;
 1775: 	$currentstring=~s/^\s*(\S)/\\underline\{$1/;
 1776: 	$currentstring=~s/(\S)\s*$/$1\}/;		
 1777:     }
 1778:     return $currentstring;
 1779: }
 1780: 
 1781: #-- <ul> tag (end tag required)
 1782: sub start_ul {
 1783:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1784:     my $currentstring = &end_p();	# Close off enclosing list.
 1785:     if ($target eq 'web' || $target eq 'webgrade') {
 1786: 	$currentstring .= $token->[4];     
 1787:     } elsif ($target eq 'tex') {
 1788: 	my $TeXtype=&Apache::lonxml::get_param('type',$parstack,$safeeval,undef,0);
 1789: 	$Apache::londefdef::list_index=0;
 1790: 	if ($TeXtype eq 'disc') {
 1791: 	    $currentstring .= '\renewcommand{\labelitemi}{$\bullet$}'.
 1792:                               '\renewcommand{\labelitemii}{$\bullet$}'. 
 1793:                               '\renewcommand{\labelitemiii}{$\bullet$}'.
 1794:                               '\renewcommand{\labelitemiv}{$\bullet$}';
 1795: 	} elsif ($TeXtype eq 'circle') {
 1796: 	    $currentstring .= '\renewcommand{\labelitemi}{$\circ$}'.
 1797:                               '\renewcommand{\labelitemii}{$\circ$}'. 
 1798:                               '\renewcommand{\labelitemiii}{$\circ$}'.
 1799:                               '\renewcommand{\labelitemiv}{$\circ$}';
 1800: 	} elsif ($TeXtype eq 'square') {
 1801: 	    $currentstring .= '\renewcommand{\labelitemi}{$\diamond$}'.
 1802:                               '\renewcommand{\labelitemii}{$\diamond$}'. 
 1803:                               '\renewcommand{\labelitemiii}{$\diamond$}'.
 1804:                               '\renewcommand{\labelitemiv}{$\diamond$}';
 1805: 	}
 1806: 	$currentstring .= '\strut \begin{itemize}';  
 1807:     } 
 1808:     return $currentstring;
 1809: }
 1810: 
 1811: sub end_ul {
 1812:     my ($target,$token) = @_;
 1813:     my $currentstring = '';
 1814:     if ($target eq 'web' || $target eq 'webgrade') {
 1815: 	$currentstring = $token->[2];     
 1816:     } elsif ($target eq 'tex') {
 1817: 	$currentstring = '\end{itemize} \renewcommand{\labelitemi}{$\bullet$}'.
 1818:                                '\renewcommand{\labelitemii}{$\bullet$}'. 
 1819:                                '\renewcommand{\labelitemiii}{$\bullet$}'.
 1820:                                '\renewcommand{\labelitemiv}{$\bullet$}\strut ';  
 1821:     } 
 1822:     return $currentstring;
 1823: }
 1824: 
 1825: #-- <menu> tag (end tag required)
 1826: sub start_menu {
 1827:     my ($target,$token) = @_;
 1828:     my $currentstring = '';
 1829:     if ($target eq 'web' || $target eq 'webgrade') {
 1830: 	$currentstring = $token->[4];     
 1831:     } elsif ($target eq 'tex') {
 1832: 	$currentstring = " \\begin{itemize} ";  
 1833:     } 
 1834:     return $currentstring;
 1835: }
 1836: 
 1837: sub end_menu {
 1838:     my ($target,$token) = @_;
 1839:     my $currentstring = '';
 1840:     if ($target eq 'web' || $target eq 'webgrade') {
 1841: 	$currentstring = $token->[2];     
 1842:     } elsif ($target eq 'tex') {
 1843: 	$currentstring = " \\end{itemize}";  
 1844:     } 
 1845:     return $currentstring;
 1846: }
 1847: 
 1848: #-- <dir> tag (end tag required)
 1849: sub start_dir {
 1850:     my ($target,$token) = @_;
 1851:     my $currentstring = &end_p();	# In case there's a <p> prior to the list.
 1852:     if ($target eq 'web' || $target eq 'webgrade') {
 1853: 	$currentstring .= $token->[4];     
 1854:     } elsif ($target eq 'tex') {
 1855: 	$currentstring .= " \\begin{itemize} ";  
 1856:     } 
 1857:     return $currentstring;
 1858: }
 1859: 
 1860: sub end_dir {
 1861:     my ($target,$token) = @_;
 1862:     my $currentstring = '';
 1863:     if ($target eq 'web' || $target eq 'webgrade') {
 1864: 	$currentstring = $token->[2];     
 1865:     } elsif ($target eq 'tex') {
 1866: 	$currentstring = " \\end{itemize}";  
 1867:     } 
 1868:     return $currentstring;
 1869: }
 1870: 
 1871: #-- <ol> tag (end tag required)
 1872: sub start_ol {
 1873:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1874:     my $currentstring = &end_p();	# In case there's a <p> prior to the list.
 1875:     if ($target eq 'web' || $target eq 'webgrade') {
 1876: 	$currentstring .= $token->[4];     
 1877:     } elsif ($target eq 'tex') {
 1878: 	$Apache::londefdef::list_index=0;
 1879: 	my $type=&Apache::lonxml::get_param('type',$parstack,$safeeval,undef,0);
 1880: 	if ($type eq '1') {
 1881: 	    $currentstring .= '\renewcommand{\labelenumi}{\arabic{enumi}.}'.
 1882:                               '\renewcommand{\labelenumii}{\arabic{enumii}.}'. 
 1883:                               '\renewcommand{\labelenumiii}{\arabic{enumiii}.}'.
 1884:                               '\renewcommand{\labelenumiv}{\arabic{enumiv}.}';
 1885: 	} elsif ($type eq 'A') {
 1886: 	    $currentstring .= '\renewcommand{\labelenumi}{\Alph{enumi}.}'.
 1887:                               '\renewcommand{\labelenumii}{\Alph{enumii}.}'. 
 1888:                               '\renewcommand{\labelenumiii}{\Alph{enumiii}.}'.
 1889:                               '\renewcommand{\labelenumiv}{\Alph{enumiv}.}';
 1890: 	} elsif ($type eq 'a') {
 1891: 	    $currentstring .= '\renewcommand{\labelenumi}{\alph{enumi}.}'.
 1892:                               '\renewcommand{\labelenumii}{\alph{enumii}.}'.
 1893:                               '\renewcommand{\labelenumiii}{\alph{enumiii}.}'.
 1894:                               '\renewcommand{\labelenumiv}{\alph{enumiv}.}';
 1895: 	} elsif ($type eq 'i') {
 1896: 	    $currentstring .= '\renewcommand{\labelenumi}{\roman{enumi}.}'.
 1897:                               '\renewcommand{\labelenumii}{\roman{enumii}.}'.
 1898:                               '\renewcommand{\labelenumiii}{\roman{enumiii}.}'.
 1899:                               '\renewcommand{\labelenumiv}{\roman{enumiv}.}';
 1900: 	} elsif ($type eq 'I') {
 1901: 	    $currentstring .= '\renewcommand{\labelenumi}{\Roman{enumi}.}'.
 1902:                               '\renewcommand{\labelenumii}{\Roman{enumii}.}'.
 1903:                               '\renewcommand{\labelenumiii}{\Roman{enumiii}.}'.
 1904:                               '\renewcommand{\labelenumiv}{\Roman{enumiv}.}';
 1905: 	}
 1906: 	$currentstring .= '\strut \begin{enumerate}';  
 1907:     } 
 1908:     return $currentstring;
 1909: }
 1910: 
 1911: sub end_ol {
 1912:     my ($target,$token) = @_;
 1913:     my $currentstring = '';
 1914:     if ($target eq 'web' || $target eq 'webgrade') {
 1915: 	$currentstring = $token->[2];     
 1916:     } elsif ($target eq 'tex') {
 1917: 	$currentstring = '\end{enumerate}\renewcommand{\labelenumi}{\arabic{enumi}.}'.
 1918:                                         '\renewcommand{\labelenumii}{\arabic{enumii}.}'.
 1919:                                         '\renewcommand{\labelenumiii}{\arabic{enumiii}.}'.
 1920:                                         '\renewcommand{\labelenumiv}{\arabic{enumiv}.}\strut ';  
 1921:     } 
 1922:     return $currentstring;
 1923: }
 1924: 
 1925: #-- <dl> tag (end tag required)
 1926: sub start_dl {
 1927:     my ($target,$token) = @_;
 1928:     my $currentstring = &end_p();	# In case there's a <p> unclosed prior to the list.
 1929:     if ($target eq 'web' || $target eq 'webgrade') {
 1930: 	$currentstring .= $token->[4];     
 1931:     } elsif ($target eq 'tex') {
 1932: 	$currentstring .= '\begin{description}';
 1933: 	$Apache::londefdef::DL++;
 1934: 	push(@Apache::londefdef::description,[]);
 1935: 	$Apache::londefdef::DD[$Apache::londefdef::DL]=0;
 1936: 	$Apache::londefdef::DT[$Apache::londefdef::DL]=0;
 1937: 	$Apache::londefdef::seenDT[$Apache::londefdef::DL]=0;
 1938:     } 
 1939:     return $currentstring;
 1940: }
 1941: 
 1942: sub end_dl {
 1943:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1944:     my $currentstring = '';
 1945:     if ($target eq 'web' || $target eq 'webgrade') {
 1946: 	$currentstring = $token->[2];     
 1947:     } elsif ($target eq 'tex') {
 1948: 	if ($Apache::londefdef::DT[-1]) { &end_dt(@_); }
 1949: 	if ($Apache::londefdef::DD[-1]) { &end_dd(@_); }
 1950: 	foreach my $element (@{$Apache::londefdef::description[-1]}) {
 1951: 	    $currentstring.=' '.$element.' ';
 1952: 	}
 1953: 	pop(@Apache::londefdef::description);
 1954: 	$currentstring.='\end{description}';  
 1955: 	delete($Apache::londefdef::DD[$Apache::londefdef::DL]);
 1956: 	delete($Apache::londefdef::DT[$Apache::londefdef::DL]);
 1957: 	delete($Apache::londefdef::seenDT[$Apache::londefdef::DL]);
 1958: 	$Apache::londefdef::DL--;
 1959:     } 
 1960:     return $currentstring;
 1961: }
 1962: 
 1963: #-- <dt> tag (end tag optional)
 1964: sub start_dt {
 1965:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1966:     my $currentstring='';
 1967:     if ($target eq 'web' || $target eq 'webgrade') {
 1968: 	$currentstring = $token->[4];     
 1969:     } elsif ($target eq 'tex') {
 1970: 	if ($Apache::londefdef::DT[-1]) { &end_dt(@_); }
 1971: 	if ($Apache::londefdef::DD[-1]) { &end_dd(@_); }
 1972: 	&Apache::lonxml::startredirection();
 1973: 	$Apache::londefdef::DT[-1]++;
 1974: 	$Apache::londefdef::seenDT[-1]=1;
 1975:     } 
 1976:     return $currentstring;
 1977: }
 1978: 
 1979: sub end_dt {
 1980:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1981:     my $currentstring = '';
 1982:     if ($target eq 'web' || $target eq 'webgrade') {
 1983: 	$currentstring = $token->[2];    
 1984:     } elsif ($target eq 'tex') {
 1985: 	if ($Apache::londefdef::DT[-1]) {
 1986: 	    my $data=&item_cleanup();
 1987: 	    push(@{$Apache::londefdef::description[-1]},'\item['.$data.'] \strut \vskip 0mm');
 1988: 	    $Apache::londefdef::DT[-1]--;
 1989: 	}
 1990:     } 
 1991:     return $currentstring;
 1992: }
 1993: 
 1994: sub item_cleanup {
 1995:     my $item=&Apache::lonxml::endredirection();
 1996:     $item=~s/\\begin{center}//g;
 1997:     $item=~s/\\end{center}//g;
 1998:     return $item;
 1999: }
 2000: 
 2001: #-- <dd> tag (end tag optional)
 2002: sub start_dd {
 2003:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2004:     my $currentstring = '';
 2005:     if ($target eq 'web' || $target eq 'webgrade') {
 2006: 	$currentstring = $token->[4];     
 2007:     } elsif ($target eq 'tex') {
 2008: 	if ($Apache::londefdef::DT[-1]) { &end_dt(@_); }
 2009: 	if ($Apache::londefdef::DD[-1]) { &end_dd(@_);}
 2010: 	if (!$Apache::londefdef::seenDT[-1]) {
 2011: 	    push(@{$Apache::londefdef::description[-1]},'\item[\strut] \strut \vskip 0mm ');
 2012: 	}
 2013: 	push(@{$Apache::londefdef::description[-1]},'');
 2014: 	$Apache::londefdef::description[-1]->[-1].=' \strut ';
 2015: 	$Apache::londefdef::DD[-1]++;
 2016: 	&Apache::lonxml::startredirection();
 2017:     } 
 2018:     return $currentstring;
 2019: }
 2020: 
 2021: sub end_dd {
 2022:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2023:     my $currentstring = '';
 2024:     if ($target eq 'web' || $target eq 'webgrade') {
 2025: 	$currentstring = $token->[2];    
 2026:     }  elsif ($target eq 'tex') {
 2027: 	$Apache::londefdef::description[-1]->[-1].=
 2028: 	    &Apache::lonxml::endredirection().' \vskip 0mm ';
 2029: 	$Apache::londefdef::DD[-1]--;
 2030:     }
 2031:     return $currentstring;
 2032: }
 2033: 
 2034: #-- <table> tag (end tag required)
 2035: #       <table> also ends any prior <p> that is not closed.
 2036: #               but, unless I allow <p>'s to nest, that's the
 2037: #               only way I could think of to allow <p> in 
 2038: #               <tr> <th> bodies
 2039: #
 2040: #list of supported attributes: border,width,TeXwidth,TeXtheme
 2041: #                              align
 2042: sub start_table {
 2043:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2044:     my $textwidth = '';
 2045:     my $currentstring = &end_p();
 2046:     if ($target eq 'web' || $target eq 'webgrade') {
 2047: 	$currentstring .= $token->[4];     
 2048:     } elsif ($target eq 'tex') {
 2049: 	&disable_para();	# Can't have paras in a table.
 2050: 
 2051: 
 2052: 	#  New table code:
 2053: 
 2054: 	#  Get the parameters that we can do something about:
 2055: 
 2056: 	my $border = &Apache::lonxml::get_param('border', $parstack, $safeeval, undef, 0);
 2057: 	my $width  = &Apache::lonxml::get_param('TeXwidth', $parstack, $safeeval, undef, 0);
 2058: 	my $theme  = &Apache::lonxml::get_param('TeXtheme', $parstack, $safeeval, undef, 0);
 2059: 	my $align  = &Apache::lonxml::get_param('align', $parstack, $safeeval, undef, 0);
 2060: 	my $cell_border = &Apache::lonxml::get_param('rules', $parstack, $safeeval, undef, 0);
 2061: 
 2062: 	# The only thing that needs any figuring out is the width.. and then only if it is
 2063: 	# a percent. If not it's assumed to be some valid TeX measurement unit e.g. 3.0cm
 2064: 	#
 2065: 
 2066: 	my $table = new Apache::lontable();
 2067: 	if ((defined $border) && ($border > 0)) {
 2068: 	#    &Apache::lonnet::logthis("Turning on table borders: $border");
 2069: 	    $table->table_border(1);
 2070: 	    if (!defined $cell_border) {
 2071: 		$table->cell_border(1); # Default for rules is all if rules not defined.
 2072: 	    }
 2073: 	}
 2074: 	# Only all or nothing for cell borders for now:
 2075: 
 2076: 	if ((defined $cell_border)) {
 2077: 	    if ($cell_border eq 'all') {
 2078: 		$table->cell_border(1);
 2079: 	    } elsif ($cell_border eq 'rows') {
 2080: 		$table->cell_border(2);
 2081: 	    } elsif ($cell_border eq 'cols') {
 2082: 		$table->cell_border(3);
 2083: 	    } elsif($cell_border eq 'groups') {
 2084: 		$table->cell_border(4);
 2085: 	    } else {
 2086: 		$table->cell_border(0);
 2087: 	    }
 2088: 	}
 2089: 	if (defined $theme) {
 2090: 	    $table->theme($theme);
 2091: 	}
 2092: 	if (defined $align) {
 2093: 	    $table->alignment($align);
 2094: 	}
 2095: 
 2096: 	# Missing width is most of page width
 2097: 
 2098: 	if (!(defined $width)) {
 2099: 	    $width = '70%';
 2100: 	}
 2101: 
 2102: 	# If a percentage, need to calculate what this means in terms of
 2103: 	# page width:
 2104: 	
 2105: 	if ($width =~ /%$/) {
 2106: 	    my $textwidth = &recalc($env{'form.textwidth'});  # Page width in mm.
 2107: 	    $width =~ s/%//;
 2108: 	    $width = $width * $textwidth / 100.0;
 2109: 	    $width .= " mm";
 2110: 	    $table->width($width);
 2111: 	}
 2112: 
 2113: 	push(@Apache::londefdef::table, $table);
 2114:         $currentstring.=' \keephidden{NEW TABLE ENTRY}';
 2115: 
 2116:     }
 2117:     return $currentstring;
 2118: }
 2119:  
 2120: sub end_table {
 2121:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2122:     my $currentstring = '';
 2123:     if ($target eq 'web' || $target eq 'webgrade') {
 2124: 	$currentstring = $token->[2];     
 2125:     } elsif ($target eq 'tex') {
 2126: 	
 2127: 	
 2128: 	my $table = pop(@Apache::londefdef::table);
 2129: 	my $t     = $table->generate();
 2130: 	# &Apache::lonnet::logthis("Generating string");
 2131: 	$currentstring = $t->generate_string();
 2132: 	# &Apache::lonnet::logthis("Generated: $currentstring");
 2133: 	&enable_para();
 2134: 	
 2135:     }
 2136:     return $currentstring;
 2137: }
 2138: 
 2139: #-- <tr> tag (end tag optional)
 2140: sub start_tr {
 2141:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2142:     my $currentstring = '';
 2143:     if ($target eq 'web' || $target eq 'webgrade') {
 2144: 	$currentstring = $token->[4];     
 2145:     } elsif ($target eq 'tex') {
 2146: 
 2147: 	my $align = &Apache::lonxml::get_param('align', $parstack, $safeeval, undef, 1);
 2148: 	$Apache::londefdef::table[-1]->start_row();
 2149: 	
 2150: 	if ($align ne '') {
 2151: 	    $Apache::londefdef::table[-1]->configure_row({default_halign => $align});
 2152: 	}
 2153: 
 2154: 	#---------------------------------------------------------------
 2155: 	# Old table code.
 2156: 	#---------------------------------------------------------------
 2157: 
 2158: 	if (0) {
 2159: 	$Apache::londefdef::table[-1]{'row_number'}++;
 2160: 	my $alignchar=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
 2161: 	if ($alignchar ne '') {
 2162: 	    push @ {$Apache::londefdef::table[-1]{'rows'} },substr($alignchar,0,1);
 2163: 	} else {
 2164: 	    push @ {$Apache::londefdef::table[-1]{'rows'} }, 'l';
 2165: 	}
 2166: 	push ( @{ $Apache::londefdef::table[-1]{'rowdata'} }, $Apache::londefdef::table[-1]{'hinc'});
 2167: 	#
 2168: 	#  Need to save the number of table columns to preserve the max # columns.
 2169: 	#
 2170: 	$Apache::londefdef::table[-1]{'prior_columns'}   = $Apache::londefdef::table[-1]{'counter_columns'};
 2171: 	$Apache::londefdef::table[-1]{'counter_columns'} = -1;
 2172: 	push @ {$Apache::londefdef::table[-1]{'TeXlen'}}, [];
 2173: 	push @ {$Apache::londefdef::table[-1]{'objectlen'}}, [];
 2174: 	push @ {$Apache::londefdef::table[-1]{'minlen'}}, [];
 2175: 	push @ {$Apache::londefdef::table[-1]{'maxlen'}}, [];
 2176: 	push @ {$Apache::londefdef::table[-1]{'content'}}, [];
 2177:     }
 2178:     } 
 2179:     return $currentstring;
 2180: }
 2181:         
 2182: sub end_tr {
 2183:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2184:     my $currentstring = &end_p();	# Close any pending <p> in the row.
 2185:     if ($target eq 'web' || $target eq 'webgrade') {
 2186: 	$currentstring .= $token->[2];     
 2187:     } elsif ($target eq 'tex') {
 2188: 
 2189: 	# In case the user is missing a </td> or </th> tag:
 2190: 
 2191: 	if ($Apache::londefdef::TD_redirection) {
 2192: 	    &end_td_tex($parstack,$parser,$safeeval);    
 2193: 	}
 2194: 	$Apache::londefdef::table[-1]->end_row();
 2195: 
 2196: 	#-----------------------------------------------
 2197: 	# Old table code
 2198: 	#-----------------------------------------------
 2199: 
 2200: 	if (0) {
 2201: 	if ($Apache::londefdef::TD_redirection) {
 2202: 	    &end_td_tex($parstack,$parser,$safeeval);    
 2203: 	}
 2204: 	# Counter columns must be the maximum number of columns seen
 2205: 	# in the table so far so:
 2206: 	if ($Apache::londefdef::table[-1]{'prior_columns'} > $Apache::londefdef::table[-1]{'counter_columns'}) {
 2207: 	    $Apache::londefdef::table[-1]{'counter_columns'} = $Apache::londefdef::table[-1]{'prior_columns'};
 2208: 	}
 2209:     }
 2210: 
 2211: 	
 2212:     }
 2213:     return $currentstring;
 2214: }
 2215: 
 2216: #-- <td> tag (end tag optional)
 2217: sub start_td {
 2218:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2219:     my $currentstring = '';
 2220:     if ($target eq 'web' || $target eq 'webgrade') {
 2221: 	$currentstring = $token->[4];     
 2222:     } elsif ($target eq 'tex') {
 2223: 	$Apache::londefdef::TD_redirection = 1;
 2224: 	&tag_check('tr','td',$tagstack,$parstack,$parser,$safeeval);
 2225:     } 
 2226:     return $currentstring;
 2227: }   
 2228:     
 2229: sub tag_check {
 2230:     my ($good_tag,$bad_tag,$tagstack,$parstack,$parser,$safeeval) = @_;
 2231:     my @ar=@$parstack; 
 2232:     for (my $i=$#ar-1;$i>=0;$i--) {
 2233: 	if (lc($$tagstack[$i]) eq $good_tag) {
 2234: 	    &start_td_tex($parstack,$parser,$safeeval);
 2235: 	    last;
 2236: 	} elsif (lc($$tagstack[$i]) eq $bad_tag) {
 2237: 	    splice @ar, $i+1;
 2238: 	    &end_td_tex(\@ar,$parser,$safeeval);
 2239: 	    &start_td_tex($parstack,$parser,$safeeval);
 2240: 	    last;
 2241: 	}
 2242:     }
 2243:     return '';
 2244: }
 2245: 
 2246: #
 2247: #  Factor out cell configuration hash generation:
 2248: #
 2249: 
 2250: sub cell_config_hash {
 2251:     my ($align, $rowspan, $colspan, $width) = @_;
 2252:     if ($rowspan ne '') {
 2253:         $rowspan =~ s/^\s+|\s+$//g; 
 2254:     }
 2255:     if ($colspan ne '') {
 2256:         $colspan =~ s/^\s+|\s+$//g;
 2257:     }
 2258:     my %config;
 2259:     if ($align ne '') {
 2260: 	$config{'halign'} = $align;
 2261:     }
 2262:     if (($colspan =~ /^\d+$/) && ($colspan > 0)) {
 2263: 	$config{'colspan'} = $colspan;
 2264:     }
 2265:     if (($rowspan =~ /^\d+$/) && ($rowspan > 0)) {
 2266: 	$config{'rowspan'} = $rowspan;
 2267:     }
 2268:     if ($width ne '') {
 2269: 	$config{'width'} = $width;
 2270:     }
 2271:     return \%config;
 2272: }
 2273:  
 2274: sub start_td_tex {
 2275:     my ($parstack,$parser,$safeeval) = @_;
 2276: 
 2277:     # At this stage, an empty cell is created with the
 2278:     # appropriate rowspan/colspan and alignment
 2279:     # attributes, but empty of text.  end_td_tex will
 2280:     # fetch the contents from the recursive parse and
 2281:     # fill the cell with them:
 2282:     my $align   = &Apache::lonxml::get_param('align', $parstack, $safeeval);
 2283:     my $rowspan = &Apache::lonxml::get_param('rowspan', $parstack, $safeeval);
 2284:     my $colspan = &Apache::lonxml::get_param('colspan', $parstack, $safeeval);
 2285:     my $width   = &Apache::lonxml::get_param('TeXwidth', $parstack, $safeeval);
 2286:     my $config = &cell_config_hash($align, $rowspan, $colspan, $width);
 2287: 
 2288:     my $table = $Apache::londefdef::table[-1];
 2289:     $table->add_cell('', $config);
 2290:     
 2291: 
 2292:     #------------------------------------------------
 2293:     #  Old table code.
 2294:     #------------------------------------------------
 2295: 
 2296:     if (0) {
 2297: 
 2298:     my $alignchar = substr(&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1),0,1);
 2299:     if ($alignchar eq '') {
 2300: 	$alignchar = $Apache::londefdef::table[-1]{'rows'}[-1];
 2301:     }
 2302:     push @{ $Apache::londefdef::table[-1]{'align'}[$Apache::londefdef::table[-1]{'row_number'}] }, $alignchar;
 2303:     $Apache::londefdef::table[-1]{'counter_columns'}++;
 2304:     my $TeXwidth=&Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval,undef,0);
 2305:     if (defined $TeXwidth) {		
 2306: 	my $current_length=&recalc($TeXwidth);
 2307: 	$current_length=~/(\d+\.?\d*)/;
 2308: 	push @ {$Apache::londefdef::table[-1]{'TeXlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$1;
 2309:     }
 2310:     }
 2311:     &Apache::lonxml::startredirection();
 2312:     return '';
 2313: }
 2314: 
 2315: sub end_td_tex {
 2316: 
 2317:     my $text = &Apache::lonxml::endredirection();
 2318:     my $table = $Apache::londefdef::table[-1];
 2319:     $table->append_cell_text($text);
 2320: 
 2321:     #-------------------------------------------------
 2322:     # Old table code
 2323:     #-------------------------------------------------
 2324: 
 2325:     if (0) {
 2326:     my ($parstack,$parser,$safeeval) = @_;
 2327:     my $current_row    = $Apache::londefdef::table[-1]{'row_number'};
 2328:     my $current_column = $Apache::londefdef::table[-1]{'counter_columns'}; 
 2329:     my $data = &Apache::lonxml::endredirection();
 2330: 
 2331:     #  The rowspan array of the table indicates which cells are part of a span.
 2332:     #  n indicates the start of a span set of n rows.
 2333:     #  ^ indicates a cell that continues a span set.
 2334:     #  _ indicates the cell is at the bottom of a span set.
 2335:     #  If this and subsequent cells are part of a rowspan, we must
 2336:     #  push along the row until we find one that is not.
 2337: 
 2338:     while ((defined $Apache::londefdef::table[-1]{'rowspan'}[$current_row] [$current_column]) 
 2339: 	   && ($Apache::londefdef::table[-1]{'rowspan'}[$current_row][$current_column] =~ /[\^\_]/)) {
 2340: 	# Part of a span.
 2341: 	push @ {$Apache::londefdef::table[-1]{'content'}[-1]}, '';
 2342: 	$current_column++;
 2343:     }
 2344:     $Apache::londefdef::table[-1]{'counter_columns'} = $current_column;
 2345:    
 2346: 
 2347:     # Get the column and row spans.
 2348:     # Colspan can be done via \multicolumn if I can figure out the data structs.
 2349: 
 2350:     my $colspan = &Apache::lonxml::get_param('colspan', $parstack, $safeeval, undef, 0);
 2351:     if (!$colspan) {
 2352: 	$colspan = 1;
 2353:     }
 2354: 
 2355:     my $rowspan = &Apache::lonxml::get_param('rowspan', $parstack, $safeeval, undef, 0);
 2356:     if (!$rowspan) {
 2357: 	$rowspan = 1;
 2358:     }
 2359: 
 2360: 
 2361: 
 2362:     for (my $c = 0; $c < $colspan; $c++) {
 2363: 	$Apache::londefdef::table[-1]{'rowspan'}[$current_row][$current_column+$c] = $rowspan;
 2364: 	for (my $i = 1; $i < $rowspan; $i++) {
 2365: 	    $Apache::londefdef::table[-1]{'rowspan'}[$current_row+$i][$current_column+$c] = '^';
 2366: 	    if ($i == ($rowspan-1)) {
 2367: 		$Apache::londefdef::table[-1]{'rowspan'}[$current_row+$i][$current_column+$c] = '_';
 2368: 	    }
 2369: 	}
 2370:     }
 2371: 
 2372:     my $TeXwidth=&Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval,undef,0);
 2373:     if (defined $TeXwidth) {		
 2374: 	for (my $c = 0; $c < $colspan; $c++) {
 2375: 	    push @ {$Apache::londefdef::table[-1]{'objectlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2376: 	    push @ {$Apache::londefdef::table[-1]{'minlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2377: 	    push @ {$Apache::londefdef::table[-1]{'maxlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2378: 	}
 2379:     } else {
 2380: 	if (($data=~m/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)/) or ($data=~m/\[(\d+\.?\d*)\s*mm\]/)) {
 2381: 	    my $garbage_data=$data;
 2382: 	    my $fwidth=0;
 2383:             while ($garbage_data=~m/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)/) {
 2384: 		my $current_length=&recalc($1);
 2385: 		$current_length=~/(\d+\.?\d*)/;
 2386: 		if ($fwidth<$1) {$fwidth=$1;}
 2387: 		$garbage_data=~s/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)//;
 2388: 	    }
 2389:             while ($garbage_data=~m/\[(\d+\.?\d*)\s*mm\]/) {
 2390: 		my $current_length=$1;
 2391: 		if ($fwidth<$current_length) {$fwidth=$current_length;}
 2392: 		$garbage_data=~s/\[(\d+\.?\d*)\s*mm\]//;
 2393: 	    }
 2394: 	    for (my $c = 0; $c < $colspan; $c++) {
 2395: 		push @ {$Apache::londefdef::table[-1]{'TeXlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2396: 		push @ {$Apache::londefdef::table[-1]{'objectlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$fwidth;
 2397: 		push @ {$Apache::londefdef::table[-1]{'minlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2398: 		push @ {$Apache::londefdef::table[-1]{'maxlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2399: 	    }
 2400: 	} elsif ($data=~/\\parbox\{\s*\d+\.?\d*\s*(mm|cm|in|pc|pt)*\s*\}/ or $data=~/\\epsfxsize\s*=\s*\d+\.?\d*\s*(mm|cm|in|pc|pt)*/) {
 2401: 	    my $garbage_data=$data;
 2402: 	    my $fwidth=0;
 2403:             while ($garbage_data=~/\\parbox\{\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)\s*\}/) {
 2404: 		my $current_length=&recalc($1);
 2405: 		$current_length=~/(\d+\.?\d*)/;
 2406: 		if ($fwidth<$1) {$fwidth=$1;}
 2407: 		$garbage_data=~s/\\parbox\{\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)//;
 2408: 	    }
 2409:             while ($garbage_data=~/\\epsfxsize\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)/) {
 2410: 		my $current_length=&recalc($1);
 2411: 		$current_length=~/(\d+\.?\d*)/;
 2412: 		if ($fwidth<$1) {$fwidth=$1;}
 2413: 		$garbage_data=~s/\\epsfxsize\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)//;
 2414: 	    }
 2415: 	    for (my $c = 0; $c < $colspan; $c++) {
 2416: 		push @ {$Apache::londefdef::table[-1]{'TeXlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2417: 		push @ {$Apache::londefdef::table[-1]{'objectlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$fwidth;
 2418: 		push @ {$Apache::londefdef::table[-1]{'minlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2419: 		push @ {$Apache::londefdef::table[-1]{'maxlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2420: 	    }
 2421: 	    $data=~s/\\\\\s*$//; 
 2422: 	} else {  
 2423: 	    $data=~s/^\s+(\S.*)/$1/; 
 2424: 	    $data=~s/(.*\S)\s+$/$1/;
 2425: 	    $data=~s/(\s)+/$1/;
 2426: 	    my ($current_length,$min_length)=(0,0);
 2427: 	    if ($data=~/\\vskip/) {
 2428:                 my $newdata=$data;
 2429: 		$newdata=~s/\\vskip \d*\.?\d*\s*mm/THISISJUSTTEMPORARYSEPARATOR/g;
 2430: 		my @newdata=split(/THISISJUSTTEMPORARYSEPARATOR/,$newdata);
 2431: 		foreach my $elementdata (@newdata) {
 2432: 		    my $lengthnewdata=2.5*&LATEX_length($elementdata);
 2433: 		    if ($lengthnewdata>$current_length) {$current_length=$lengthnewdata;}
 2434:                     my @words=split(/ /,$elementdata);
 2435: 		    foreach my $word (@words) {
 2436: 			my $lengthword=2.5*&LATEX_length($word);
 2437: 			if ($min_length<$lengthword) {$min_length=$lengthword;}
 2438: 		    }
 2439: 		}
 2440: 	    } else {
 2441: 		$current_length=2.5*&LATEX_length($data);
 2442:                     my @words=split(/ /,$data);
 2443: 		    foreach my $word (@words) {
 2444: 			my $lengthword=2*&LATEX_length($word);
 2445: 			if ($min_length<$lengthword) {$min_length=$lengthword;}
 2446: 		    }
 2447: 	    }
 2448: 	    for (my $c = 0; $c < $colspan; $c++) {
 2449: 		push @ {$Apache::londefdef::table[-1]{'TeXlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2450: 		push @ {$Apache::londefdef::table[-1]{'objectlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2451: 		push @ {$Apache::londefdef::table[-1]{'maxlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$current_length;
 2452: 		push @ {$Apache::londefdef::table[-1]{'minlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$min_length;
 2453: 	    }
 2454: 	}        
 2455:     }
 2456:     # Substitute all of the tables nested in this cell in their appropriate places.
 2457: 
 2458: 
 2459:     my $nested_count = $#{$Apache::londefdef::table[-1]{'include'}}; # This one is constant...
 2460:     for (my $in=0; $in<=$nested_count; $in++) {    
 2461: 	my $nested = shift @{$Apache::londefdef::table[-1]{'include'}};
 2462: 	$nested =~ s/\\end\{tabular\}\\strut\\\\/\\end\{tabular\}/;
 2463: 	# $data=~s/\\keephidden\{NEW TABLE ENTRY\}/$Apache::londefdef::table[-1]{'include'}[$in]/;
 2464: 	$data =~ s/\\keephidden\{NEW TABLE ENTRY\}/$nested/;
 2465: 
 2466:     }
 2467:     # Should be be killing off the 'include' elements as they're used up?
 2468: 
 2469:     push @ {$Apache::londefdef::table[-1]{'content'}[-1] },$data;
 2470: 
 2471: 
 2472: 
 2473: 
 2474:     #  the colspan array will indicate how many columns will be spanned by this
 2475:     #  cell..this requires that counter_columns also be adjusted accordingly
 2476:     #  so that the next bunch of text goes in the right cell.  Note that since
 2477:     #  counter_columns is incremented in the start_td_tex, we adjust by colspan-1.
 2478:     #
 2479: 
 2480:     $Apache::londefdef::table[-1]{'counter_columns'} += $colspan -1;
 2481:     for (my $i = 0; $i < ($colspan -1); $i++) {
 2482: 	push @ {$Apache::londefdef::table[-1]{'content'}[-1] },'';
 2483:     }
 2484:     for (my $r = 0; $r < $rowspan; $r++) {
 2485: 	$Apache::londefdef::table[-1]{'colspan'}[$current_row+$r][$current_column] = $colspan;
 2486: 	# Put empty text in spanned cols.
 2487: 	
 2488:     }
 2489: 
 2490:     }
 2491: 
 2492:     return '';
 2493: }
 2494: 
 2495: sub end_td {
 2496:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2497:     my $currentstring = '';
 2498:     if ($target eq 'web' || $target eq 'webgrade') {
 2499: 	$currentstring = $token->[2];     
 2500:     } elsif ($target eq 'tex') {
 2501:         $Apache::londefdef::TD_redirection =0;
 2502: 	&end_td_tex($parstack,$parser,$safeeval);
 2503:     }
 2504:     return $currentstring;
 2505: }
 2506: 
 2507: #-- <th> tag (end tag optional)
 2508: sub start_th {
 2509:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2510:     my $currentstring = '';
 2511:     if ($target eq 'web' || $target eq 'webgrade') {
 2512: 	$currentstring = $token->[4];     
 2513:     } elsif ($target eq 'tex') {
 2514: 	$Apache::londefdef::TD_redirection = 1;
 2515: 	&tagg_check('tr','th',$tagstack,$parstack,$parser,$safeeval);
 2516:     } 
 2517:     return $currentstring;
 2518: }   
 2519:     
 2520: sub tagg_check {
 2521:     my ($good_tag,$bad_tag,$tagstack,$parstack,$parser,$safeeval) = @_;
 2522:     my @ar=@$parstack; 
 2523:     for (my $i=$#ar-1;$i>=0;$i--) {
 2524: 	if (lc($$tagstack[$i]) eq $good_tag) {
 2525: 	    &start_th_tex($parstack,$parser,$safeeval);
 2526: 	    last;
 2527: 	} elsif (lc($$tagstack[$i]) eq $bad_tag) {
 2528: 	    splice @ar, $i+1;
 2529: 	    &end_th_tex(\@ar,$parser,$safeeval);
 2530: 	    &start_th_tex($parstack,$parser,$safeeval);
 2531: 	    last;
 2532: 	}
 2533:     }
 2534:     return '';
 2535: }
 2536:  
 2537: sub start_th_tex {
 2538:     my ($parstack,$parser,$safeeval) = @_;
 2539: 
 2540:     my $alignment = &Apache::lonxml::get_param('align', $parstack, $safeeval, undef,1);
 2541:     my $rowspan  =  &Apache::lonxml::get_param('rowspan', $parstack, $safeeval, undef, 1);
 2542:     my $colspan  =  &Apache::lonxml::get_param('colspan', $parstack, $safeeval, undef, 1);
 2543: 
 2544:     my $config   = cell_config_hash($alignment, $rowspan, $colspan);
 2545:     my $table    = $Apache::londefdef::table[-1];
 2546:     $table->add_cell('\textbf{', $config);
 2547: 
 2548:     #-------------------------------------------------------------------------------------
 2549:     #
 2550:     #  Old table code.
 2551:     #
 2552:     #--------------------------------------------------------------------------------------
 2553: 
 2554:     if (0) {
 2555: 
 2556: 
 2557:     my $alignchar = substr(&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1),0,1);
 2558:     if ($alignchar eq '') {
 2559: 	$alignchar = $Apache::londefdef::table[-1]{'rows'}[-1];
 2560:     }
 2561:     push @{ $Apache::londefdef::table[-1]{'align'}[$Apache::londefdef::table[-1]{'row_number'}] }, $alignchar;
 2562:     $Apache::londefdef::table[-1]{'counter_columns'}++;
 2563:     my $TeXwidth=&Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval,undef,0);
 2564:     if (defined $TeXwidth) {		
 2565: 	my $current_length=&recalc($TeXwidth);
 2566: 	$current_length=~/(\d+\.?\d*)/;
 2567: 	push @ {$Apache::londefdef::table[-1]{'TeXlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$1;
 2568:     }
 2569:     }
 2570: 
 2571:     # Accept xml until the </th> tag.
 2572: 
 2573:     &Apache::lonxml::startredirection();
 2574:     return '';
 2575: }
 2576: 
 2577: sub end_th_tex {
 2578:     my ($parstack,$parser,$safeeval) = @_;
 2579: 
 2580:     my $table = $Apache::londefdef::table[-1];
 2581:     my $text  = &Apache::lonxml::endredirection();
 2582:     $table->append_cell_text($text.'}');
 2583: 
 2584:     #-----------------------------------------------------------------------------
 2585:     #  Old table code:
 2586:     #-----------------------------------------------------------------------------
 2587: 
 2588:     if (0) {
 2589:     my $current_row = $Apache::londefdef::table[-1]{'row_number'};
 2590:     my $data=&Apache::lonxml::endredirection();
 2591:     my $TeXwidth=&Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval,undef,0);
 2592:     if (defined $TeXwidth) {		
 2593: 	push @ {$Apache::londefdef::table[-1]{'objectlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2594: 	push @ {$Apache::londefdef::table[-1]{'minlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2595: 	push @ {$Apache::londefdef::table[-1]{'maxlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2596:     } else {
 2597: 	if (($data=~m/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)/) or ($data=~m/\[(\d+\.?\d*)\s*mm\]/)) {
 2598: 	    my $garbage_data=$data;
 2599: 	    my $fwidth=0;
 2600:             while ($garbage_data=~m/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)/) {
 2601: 		my $current_length=&recalc($1);
 2602: 		$current_length=~/(\d+\.?\d*)/;
 2603: 		if ($fwidth<$1) {$fwidth=$1;}
 2604: 		$garbage_data=~s/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)//;
 2605: 	    }
 2606:             while ($garbage_data=~m/\[(\d+\.?\d*)\s*mm\]/) {
 2607: 		my $current_length=$1;
 2608: 		if ($fwidth<$current_length) {$fwidth=$current_length;}
 2609: 		$garbage_data=~s/\[(\d+\.?\d*)\s*mm\]//;
 2610: 	    }
 2611: 	    push @ {$Apache::londefdef::table[-1]{'TeXlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2612: 	    push @ {$Apache::londefdef::table[-1]{'objectlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$fwidth;
 2613: 	    push @ {$Apache::londefdef::table[-1]{'minlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2614: 	    push @ {$Apache::londefdef::table[-1]{'maxlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2615: 	} else {  
 2616: 	    $data=~s/^\s+(\S.*)/$1/; 
 2617: 	    $data=~s/(.*\S)\s+$/$1/;
 2618: 	    $data=~s/(\s)+/$1/;
 2619: 	    my ($current_length,$min_length)=(0,0);
 2620: 	    if ($data=~/\\vskip/) {
 2621:                 my $newdata=$data;
 2622: 		$newdata=~s/\\vskip \d*\.?\d*\s*mm/THISISJUSTTEMPORARYSEPARATOR/g;
 2623: 		my @newdata=split(/THISISJUSTTEMPORARYSEPARATOR/,$newdata);
 2624: 		foreach my $elementdata (@newdata) {
 2625: 		    my $lengthnewdata=2.5*&LATEX_length($elementdata);
 2626: 		    if ($lengthnewdata>$current_length) {$current_length=$lengthnewdata;}
 2627:                     my @words=split(/ /,$elementdata);
 2628: 		    foreach my $word (@words) {
 2629: 			my $lengthword=2.5*&LATEX_length($word);
 2630: 			if ($min_length<$lengthword) {$min_length=$lengthword;}
 2631: 		    }
 2632: 		}
 2633: 	    } else {
 2634: 		$current_length=2.5*&LATEX_length($data);
 2635:                     my @words=split(/ /,$data);
 2636: 		    foreach my $word (@words) {
 2637: 			my $lengthword=2*&LATEX_length($word);
 2638: 			if ($min_length<$lengthword) {$min_length=$lengthword;}
 2639: 		    }
 2640: 	    }
 2641: 	    push @ {$Apache::londefdef::table[-1]{'TeXlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2642: 	    push @ {$Apache::londefdef::table[-1]{'objectlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2643: 	    push @ {$Apache::londefdef::table[-1]{'maxlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$current_length;
 2644: 	    push @ {$Apache::londefdef::table[-1]{'minlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$min_length;
 2645: 	}        
 2646:     }
 2647: 	for (my $in=0; $in<=$#{$Apache::londefdef::table[-1]{'include'}};$in++) {         
 2648: 	    $data=~s/\\keephidden\{NEW TABLE ENTRY\}/$Apache::londefdef::table[-1]{'include'}[$in]/;
 2649: 	}
 2650:     #make data bold
 2651:     $data='\textbf{'.$data.'}';
 2652:     push @ {$Apache::londefdef::table[-1]{'content'}[-1] },$data;
 2653:     }
 2654:     return'';
 2655: }
 2656: 
 2657: sub end_th {
 2658:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2659:     my $currentstring = &end_p();	# Close any open <p> in the row.
 2660:     if ($target eq 'web' || $target eq 'webgrade') {
 2661: 	$currentstring .= $token->[2];     
 2662:     } elsif ($target eq 'tex') {
 2663:         $Apache::londefdef::TD_redirection =0;
 2664: 	&end_th_tex($parstack,$parser,$safeeval);
 2665:     }
 2666:     return $currentstring;
 2667: }
 2668:      
 2669: #-- <img> tag (end tag forbidden)
 2670: #
 2671: #  Render the <IMG> tag.
 2672: #     <IMG> has the following attributes (in addition to the 
 2673: #     standard HTML ones:
 2674: #      TeXwrap   - Governs how the tex target will try to wrap text around
 2675: #                  horizontally aligned images.
 2676: #      TeXwidth  - The width of the image when rendered for print (mm).
 2677: #      TeXheight - The height of the image when rendered for print (mm)
 2678: #         (Note there seems to also be support for this as a % of page size)
 2679: #      
 2680: sub start_img {
 2681:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
 2682:     my $src = &Apache::lonxml::get_param('src',$parstack,$safeeval,
 2683: 					 undef,1);
 2684:     if (! $src && 
 2685: 	($target eq 'web' || $target eq 'webgrade' || $target eq 'tex')
 2686: 	) { 
 2687: 	my $inside = &Apache::lonxml::get_all_text("/img",$parser,$style);
 2688: 	return '';
 2689:     }
 2690:     &Apache::lonxml::extlink($src);
 2691:     my $currentstring = '';
 2692:     my $scaling = .3;
 2693: 
 2694:    # Render unto browsers that which are the browser's...
 2695: 
 2696:     if ($target eq 'web' || $target eq 'webgrade') {
 2697:         my $enc = ('yes' eq 
 2698:                    lc(&Apache::lonxml::get_param('encrypturl',$parstack,
 2699:                       $safeeval)));
 2700:         $currentstring.=&Apache::lonenc::encrypt_ref($token,{'src'=>$src},
 2701:                          $enc);
 2702: 
 2703:     # and render unto TeX that which is LaTeX
 2704:     } elsif ($target eq 'tex') {
 2705: 	#
 2706: 	#  The alignment will require some superstructure to be put around
 2707: 	#  the \includegraphics stuff.  At present we can only partially
 2708: 	#  simulate the alignments offered by html.
 2709: 	#
 2710: 	#
 2711: 	my $align = lc(&Apache::lonxml::get_param('align', 
 2712: 						  $parstack,
 2713: 						  $safeeval,
 2714: 						  undef,1));
 2715: 	if(!$align) {
 2716: 		$align = "bottom";	# This is html's default so it's ours too.
 2717: 	}
 2718: 	#
 2719: 	&Apache::lonxml::debug("Alignemnt = $align");
 2720: 	#  LaTeX's image/text wrapping is really bad since it wants to
 2721: 	#  make figures float.  
 2722:         #   The user has the optional parameter (applicable only to l/r
 2723: 	# alignment to use the picins/parpic directive to get wrapped text
 2724: 	# this is also imperfect.. that's why we give them a choice...
 2725: 	# so they can't yell at us for our choice.
 2726: 	#
 2727: 	my $latex_rendering = &Apache::lonxml::get_param('TeXwrap',
 2728: 							    $parstack,
 2729: 							    $safeeval,
 2730: 							    undef,0);
 2731: 	# &Apache::lonxml::debug("LaTeX rendering = $latex_rendering");
 2732: 	if(!$latex_rendering) {
 2733: 		$latex_rendering = "texwrap";
 2734: 	}
 2735: 	# using texwrap inside a table does not work. So, if after all of this,
 2736: 	# texwrap is on, we turn it off if we detect we're in a table:
 2737: 	#
 2738: 	if (($latex_rendering eq 'texwrap') && &is_inside_of($tagstack, "table")) {
 2739: 	    $latex_rendering = 'parpic';
 2740: 	}
 2741: 
 2742: 	# &Apache::lonxml::debug("LaTeX rendering = $latex_rendering image file: $src");
 2743: 
 2744: 	#if original bmp/gif/jpg/png file exist do following:
 2745: 	my $origsrc=$src;
 2746: 	my ($path,$file) = &get_eps_image($src);
 2747: 	# &Apache::lonnet::logthis("Image source: $src result: $path $file");
 2748: 	$src=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$src);
 2749: 	&Apache::lonxml::debug("path = $path file = $file src = $src");
 2750: 	if (-e $src) {
 2751: 	    &Apache::lonxml::debug("$src exists");
 2752: 	    my ($height_param,$width_param)=
 2753: 		&image_size($origsrc,0.3,$parstack,$safeeval);
 2754: 	    my $size;
 2755: 	    if ($width_param)  { $size.='width='.$width_param.' mm,'; }
 2756: 	    if ($height_param) { $size.='height='.$height_param.' mm]'; }
 2757: 	    # Default size if not able to extract that (e.g. eps image).
 2758: 	    
 2759: 	    # &Apache::lonnet::logthis("Size = $size");
 2760: 	    
 2761: 	    $size='['.$size;
 2762: 	    $size=~s/,$/]/; 
 2763: 	    $currentstring .= '\graphicspath{{'.$path.'}}'
 2764: 		.'\includegraphics'.$size.'{'.$file.'} ';
 2765: 	    my $closure;
 2766: 	    ($currentstring, $closure) = &align_latex_image($align, 
 2767: 							    $latex_rendering, 
 2768: 							    $currentstring, 
 2769: 							    $width_param, 
 2770: 							    $height_param);
 2771: 	    $currentstring .= $closure;
 2772: 						
 2773: 	} else {
 2774: 	    &Apache::lonxml::debug("$src does not exist");
 2775: 	    #original image file doesn't exist so check the alt attribute
 2776: 	    my $alt = 
 2777: 		&Apache::lonxml::get_param('alt',$parstack,$safeeval,undef,1);
 2778: 	    unless ($alt) {
 2779: 		$alt=&Apache::lonmeta::alttag($Apache::lonxml::pwd[-1],$src);
 2780: 	    }
 2781: 
 2782: 	    if ($alt) { $currentstring .= ' '.$alt.' '; }
 2783: 	}
 2784: 
 2785: 	# And here's where the semi-quote breaks down: allow the user
 2786:         # to edit the beast as well by rendering the problem for edit:
 2787:     } elsif ($target eq 'edit') {
 2788:         my $only = join(',',&Apache::loncommon::filecategorytypes('Pictures'));
 2789: 	$currentstring .=&Apache::edit::tag_start($target,$token);
 2790: 	$currentstring .=&Apache::edit::text_arg('Image Url:','src',$token,70).
 2791: 	    &Apache::edit::browse('src',undef,'alt',$only).' '.
 2792: 	    &Apache::edit::search('src',undef,'alt').'<br />';
 2793: 	$currentstring .=&Apache::edit::text_arg('Description:','alt',$token,70).'<br />';
 2794: 	$currentstring .=&Apache::edit::text_arg('width (pixel):','width',$token,5);
 2795: 	$currentstring .=&Apache::edit::text_arg('height (pixel):','height',$token,5).'<br />';
 2796: 	$currentstring .=&Apache::edit::text_arg('TeXwidth (mm):','TeXwidth',$token,5);
 2797: 	$currentstring .=&Apache::edit::text_arg('TeXheight (mm):','TeXheight',$token,5);
 2798: 	$currentstring .=&Apache::edit::select_arg('Alignment:','align',
 2799: 						   ['','bottom','middle','top','left','right'],$token,5);
 2800: 	$currentstring .=&Apache::edit::select_arg('TeXwrap:', 'TeXwrap',
 2801: 						   ['', 'none','parbox', 'parpic', 'wrapfigure'], $token, 2);
 2802:         my $alt=    &Apache::lonxml::get_param('alt',$parstack,$safeeval);
 2803:         my $enc=    &Apache::lonxml::get_param('encrypturl',$parstack,$safeeval);
 2804:  
 2805: 	$currentstring .=&Apache::edit::select_arg('Encrypt URL:','encrypturl',
 2806: 						   ['no','yes'], $token, 2);
 2807:         if (($alt=~/\S/) && (lc($enc) eq 'yes')) {
 2808:            $currentstring.='<br /><span class="LC_warning">'.&mt('Warning: the description "[_1]" will be available, even for encrypted URL',$alt).'</span><br />';
 2809:         }
 2810: 	$currentstring .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
 2811: 	my $src=    &Apache::lonxml::get_param('src',$parstack,$safeeval);
 2812: 	my $width=  &Apache::lonxml::get_param('width',$parstack,$safeeval);
 2813: 	my $height= &Apache::lonxml::get_param('height',$parstack,$safeeval);
 2814: 
 2815:         if ($token->[2]{'src'}=~/\$/) {
 2816:            $currentstring.=&mt('Variable image source');
 2817:         } elsif ($token->[2]{'src'}=~/\S/) {
 2818: 	   $currentstring .= '<img src="'.$src.'" alt="'.$alt.'" ';
 2819: 	   if ($width) { $currentstring.=' width="'.$width.'" '; }
 2820: 	   if ($height) { $currentstring.=' height="'.$height.'" '; }
 2821: 	   $currentstring .= ' />';
 2822:         } else {
 2823:            $currentstring.=&mt("No image source specified");
 2824:         }
 2825:     } elsif ($target eq 'modified') {
 2826: 	my ($osrc,$owidth,$oheight)=
 2827: 	    ($token->[2]{'src'},$token->[2]{'width'},$token->[2]{'height'});
 2828: 	my $ctag=&Apache::edit::get_new_args($token,$parstack,
 2829: 					     $safeeval,'src','alt','align',
 2830: 					     'TeXwidth','TeXheight', 'TeXwrap',
 2831: 					     'width','height','encrypturl');
 2832: 	my ($nsrc,$nwidth,$nheight)=
 2833: 	    ($token->[2]{'src'},$token->[2]{'width'},$token->[2]{'height'});
 2834: 	my $loc=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$nsrc);
 2835: 	&image_replication($loc);
 2836: 	my ($iwidth,$iheight);
 2837: 	if (-e $loc) {
 2838: 	    my $image = Image::Magick->new;
 2839: 	    $image->Read($loc);
 2840: 	    ($iwidth, $iheight) = ($image->Get('width'),
 2841: 				   $image->Get('height'));
 2842: 	}
 2843: 	if ($osrc ne $nsrc || (!$nwidth && !$nheight)) {
 2844: 	    # changed image or no size specified,
 2845:             # if they didn't explicitly change the 
 2846:             # width or height use the ones from the image
 2847: 	    if ($iwidth && $iheight) {
 2848: 		if ($owidth == $nwidth || (!$nwidth && !$nheight)) {
 2849: 		    $token->[2]{'width'} = $iwidth;$ctag=1;
 2850: 		}
 2851: 		if ($oheight == $nheight || (!$nwidth && !$nheight)) {
 2852: 		    $token->[2]{'height'}=$iheight;$ctag=1;
 2853: 		}
 2854: 	    }
 2855: 	}
 2856: 	my ($cwidth,$cheight)=($token->[2]{'width'},$token->[2]{'height'});
 2857: 	# if we don't have a width or height
 2858: 	if ($iwidth && $cwidth && !$cheight) {
 2859: 	    $token->[2]{'height'}=int(($cwidth/$iwidth)*$iheight);$ctag=1;
 2860: 	}
 2861: 	if ($iheight && $cheight && !$cwidth) {
 2862: 	    $token->[2]{'width'}=int(($cheight/$iheight)*$iwidth);$ctag=1;
 2863: 	}
 2864: 	if ($ctag) {$currentstring=&Apache::edit::rebuild_tag($token);}
 2865:     }
 2866: 
 2867:     return $currentstring;
 2868: }
 2869: 
 2870: sub end_img {
 2871:     my ($target,$token) = @_;
 2872:     my $currentstring = '';
 2873:     if ($target eq 'web' || $target eq 'webgrade') {
 2874: 	$currentstring = $token->[2];
 2875:     } elsif ($target eq 'tex') {
 2876: 	$currentstring = '';
 2877:     }
 2878:     return $currentstring;
 2879: }
 2880: 
 2881: #-- <applet> tag (end tag required)
 2882: sub start_applet {
 2883:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2884:     
 2885:     my $code=&Apache::lonxml::get_param('code',$parstack,$safeeval,undef,1);
 2886:     &Apache::lonxml::extlink($code);
 2887:     my $archive=&Apache::lonxml::get_param('archive',$parstack,$safeeval,
 2888: 					   undef,1);
 2889:     &Apache::lonxml::extlink($archive);
 2890:     my $currentstring = '';
 2891:     if ($target eq 'web' || $target eq 'webgrade') {
 2892:         $currentstring = &Apache::lonenc::encrypt_ref($token,
 2893:                                                       {'code'=>$code,
 2894:                                                        'archive'=>$archive}
 2895:                                                      );
 2896:     } elsif ($target eq 'tex') {
 2897:         # Turn off some stuff we can't be inside thank you LaTeX
 2898: 
 2899: 	my $restart_sub = 0;
 2900: 	my $restart_sup = 0;
 2901: 
 2902: 	# Since <sub> and <sup> are simple tags it's ok to turn off/on
 2903: 	# using the start_ stop_ functions.. those tags only care about
 2904: 	# $target.
 2905: 
 2906: 	if (&is_inside_of($tagstack, "sub")) {
 2907: 	    $restart_sub = 1;
 2908: 	    $currentstring .= &end_sub($target, $token, $tagstack, 
 2909: 				       $parstack, $parser, $safeeval);
 2910: 	}
 2911: 	if (&is_inside_of($tagstack, "sup")) {
 2912: 	    $restart_sup = 1;
 2913: 	    $currentstring .= &end_sup($target, $token, $tagstack,
 2914: 				       $parstack, $parser, $safeeval);
 2915: 	}
 2916: 
 2917: 	# Now process the applet; just replace it with its alt attribute.
 2918: 
 2919: 	my $alttag= &Apache::lonxml::get_param('alt',$parstack,
 2920: 					       $safeeval,undef,1);
 2921: 	unless ($alttag) {
 2922: 	    my $code=&Apache::lonxml::get_param('code',$parstack,$safeeval,
 2923: 						undef,1);
 2924: 	    $alttag=&Apache::lonmeta::alttag($Apache::lonxml::pwd[-1],
 2925: 					     $code);
 2926: 	}
 2927: 	$currentstring.='\begin{center} \fbox{Java Applet: '.$alttag.
 2928: 	    '.}\end{center}';
 2929: 
 2930: 	# Turn stuff back on that we can't be inside of.
 2931: 
 2932: 	if ($restart_sub) {
 2933: 	    $currentstring .= &start_sub($target, $token, $tagstack,
 2934: 					$parstack, $parser, $safeeval);
 2935: 	}
 2936: 	if ($restart_sup) {
 2937: 	    $currentstring .= &start_sup($target, $token, $tagstack,
 2938: 					 $parstack, $parser, $safeeval);
 2939: 	}
 2940:     } 
 2941:     return $currentstring;
 2942: }
 2943: 
 2944: sub end_applet {
 2945:     my ($target,$token) = @_;
 2946:     my $currentstring = '';
 2947:     if ($target eq 'web' || $target eq 'webgrade') {
 2948: 	$currentstring = $token->[2];
 2949:     } elsif ($target eq 'tex') {
 2950:     } 
 2951:     return $currentstring;
 2952: }
 2953: 
 2954: #-- <embed> tag (end tag optional/required)
 2955: sub start_embed {    
 2956:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2957:     my $src=&Apache::lonxml::get_param('src',$parstack,$safeeval,undef,1);
 2958:     &Apache::lonxml::extlink($src);
 2959:     my $currentstring = '';
 2960:     if ($target eq 'web' || $target eq 'webgrade') {
 2961:     $currentstring=&Apache::lonenc::encrypt_ref($token,{'src'=>$src}); 
 2962:     } elsif ($target eq 'tex') {
 2963:     } 
 2964:     return $currentstring;
 2965: }
 2966: 
 2967: sub end_embed {
 2968:     my ($target,$token) = @_;
 2969:     my $currentstring = '';
 2970:     if ($target eq 'web' || $target eq 'webgrade') {
 2971:         $currentstring = $token->[2];
 2972:     } elsif ($target eq 'tex') {
 2973:         # ./.
 2974:     }
 2975:     return $currentstring;
 2976: }
 2977: 
 2978: #-- <param> tag (end tag forbidden)
 2979: sub start_param {
 2980:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2981:     if (&Apache::lonxml::get_param('name',$parstack,
 2982: 				   $safeeval,undef,1)=~/^cabbase$/i) {
 2983: 	my $value=&Apache::lonxml::get_param('value',$parstack,
 2984: 					     $safeeval,undef,1);
 2985: 	&Apache::lonxml::extlink($value);
 2986:     } 
 2987:   
 2988:     my $src = &Apache::lonxml::get_param('src',$parstack,$safeeval,undef,1);
 2989:     &Apache::lonxml::extlink($src);
 2990:     my $currentstring = '';
 2991:     if ($target eq 'web' || $target eq 'webgrade') {
 2992: 	my %toconvert;
 2993: 	my $src=&Apache::lonxml::get_param('src',$parstack,$safeeval,undef,1);
 2994: 	if ($src) { $toconvert{'src'}= $src; }
 2995: 	my $name=&Apache::lonxml::get_param('name',$parstack,$safeeval,
 2996: 					    undef,1);
 2997: 	if ($name=~/^cabbase$/i) {
 2998: 	    $toconvert{'value'}=&Apache::lonxml::get_param('value',$parstack,
 2999: 							   $safeeval,undef,1);
 3000: 	}
 3001: 	$currentstring = &Apache::lonenc::encrypt_ref($token,\%toconvert);
 3002:     } elsif ($target eq 'tex') {
 3003:     } 
 3004:     return $currentstring;
 3005: }
 3006: 
 3007: sub end_param {
 3008:     my ($target,$token) = @_;
 3009:     my $currentstring = '';
 3010:     if ($target eq 'web' || $target eq 'webgrade') {
 3011: 	$currentstring = $token->[2];     
 3012:     } elsif ($target eq 'tex') {
 3013:     } 
 3014:     return $currentstring;
 3015: }
 3016: 
 3017: #-- <allow> tag
 3018: sub start_allow {
 3019:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 3020:     my $src = &Apache::lonxml::get_param('src',$parstack,$safeeval,undef,1);
 3021:     &Apache::lonxml::extlink($src);
 3022: 
 3023:     if ($target eq 'tex') { &image_replication($src); }
 3024:     my $result;
 3025:     if ($target eq 'edit') {
 3026: 	$result .=&Apache::edit::tag_start($target,$token);
 3027: 	$result .=&Apache::edit::text_arg('File Spec:','src',$token,70);
 3028: 	$result .=&Apache::edit::end_row();#.&Apache::edit::start_spanning_row();
 3029:     } elsif ($target eq 'modified') {
 3030: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
 3031: 						     $safeeval,'src');
 3032: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
 3033:     }
 3034:     return $result;
 3035: }
 3036: 
 3037: sub end_allow {
 3038:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 3039:     if ( $target eq 'edit') { return (&Apache::edit::end_table()); }
 3040:     return '';
 3041: }
 3042: 
 3043: #-- Frames (end tag required)
 3044: #-- <frameset>
 3045: sub start_frameset {
 3046:     my ($target,$token) = @_;
 3047:     my $currentstring = '';	# Close any pending para.
 3048:     if ($target eq 'web' || $target eq 'webgrade') { 
 3049: 	$currentstring = 
 3050: 	    &Apache::loncommon::start_page($Apache::londefdef::title,
 3051: 					   $Apache::londefdef::head,
 3052: 					   {'add_entries'    => $token->[2],
 3053: #					    'no_title'       => 1,
 3054: 					    'force_register' => 1,
 3055: 					    'frameset'       => 1,});
 3056: 
 3057:     }
 3058:     return $currentstring;
 3059: }
 3060: 
 3061: sub end_frameset {
 3062:     my ($target,$token) = @_;
 3063:     my $currentstring = '';
 3064:     if ($target eq 'web' || $target eq 'webgrade') {
 3065: 	$currentstring = $token->[2];
 3066:     }
 3067:     return $currentstring;
 3068: }
 3069: 
 3070: #-- <xmp> (end tag required)
 3071: sub start_xmp {
 3072:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 3073:     my $currentstring = '';
 3074:     if ($target eq 'web' || $target eq 'webgrade') {
 3075: 	$currentstring .= $token->[4];
 3076:     } elsif ($target eq 'tex') {
 3077: 	$currentstring .= '\begin{verbatim}';
 3078:     } 
 3079:     return $currentstring;
 3080: }
 3081: 
 3082: sub end_xmp {
 3083:     my ($target,$token) = @_;
 3084:     my $currentstring = '';
 3085:     if ($target eq 'web' || $target eq 'webgrade') {
 3086: 	$currentstring .= $token->[2];
 3087:     } elsif ($target eq 'tex') {
 3088: 	$currentstring .= '\end{verbatim}';
 3089:     }
 3090:     return $currentstring;
 3091: }
 3092: 
 3093: #-- <pre> (end tag required)
 3094: sub start_pre {
 3095:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 3096:     my $currentstring = &end_p();	# close off pending <p>
 3097:     if ($target eq 'web' || $target eq 'webgrade') {
 3098: 	$currentstring .= $token->[4];
 3099:     } elsif ($target eq 'tex') {
 3100: 	$currentstring .= '\begin{verbatim}';
 3101: 	&Apache::lonxml::disable_LaTeX_substitutions();
 3102:     } 
 3103:     return $currentstring;
 3104: }
 3105: 
 3106: sub end_pre {
 3107:     my ($target,$token) = @_;
 3108:     my $currentstring = '';
 3109:     if ($target eq 'web' || $target eq 'webgrade') {
 3110: 	$currentstring .= $token->[2];
 3111:     } elsif ($target eq 'tex') {
 3112: 	$currentstring .= '\end{verbatim}';
 3113: 	&Apache::lonxml::enable_LaTeX_substitutions();
 3114:     }
 3115:     return $currentstring;
 3116: }
 3117: 
 3118: #-- <insert>
 3119: sub start_insert {
 3120:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 3121:     my $currentstring = '';
 3122:     if ($target eq 'web' || $target eq 'webgrade') {
 3123: 	my $display = &Apache::lonxml::get_param('display',$parstack,$safeeval,undef,1);
 3124: 	$currentstring .= '<b>'.$display.'</b>';;
 3125:     }
 3126:     return $currentstring;
 3127: }
 3128: 
 3129: sub end_insert {
 3130:     my ($target,$token) = @_;
 3131:     my $currentstring = '';
 3132:     if ($target eq 'web' || $target eq 'webgrade') {
 3133: 	$currentstring .= '';
 3134:     }
 3135:     return $currentstring;
 3136: }
 3137: 
 3138: #-- <externallink>
 3139: sub start_externallink {
 3140:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 3141:     my $currentstring = '';
 3142:     if ($target eq 'web' || $target eq 'webgrade') {
 3143: 	my $display = &Apache::lonxml::get_param('display',$parstack,$safeeval,undef,1);
 3144: 	$currentstring .= '<b>'.$display.'</b>';;
 3145:     }
 3146:     return $currentstring;
 3147: }
 3148: 
 3149: sub end_externallink {
 3150:     my ($target,$token) = @_;
 3151:     my $currentstring = '';
 3152:     if ($target eq 'web' || $target eq 'webgrade') {
 3153: 	$currentstring .= '';
 3154:     }
 3155:     return $currentstring;
 3156: }
 3157: 
 3158: #-- <blankspace heigth="">
 3159: sub start_blankspace {
 3160:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 3161:     my $currentstring = &end_p();	# closes off any unclosed <p>
 3162:     if ($target eq 'tex') {
 3163: 	my $howmuch = &Apache::lonxml::get_param('heigth',$parstack,$safeeval,undef,1);
 3164: 	$currentstring .= '\vskip '.$howmuch.' ';
 3165:     }
 3166:     return $currentstring;
 3167: }
 3168: 
 3169: sub end_blankspace {
 3170:     my ($target,$token) = @_;
 3171:     my $currentstring = '';
 3172:     if ($target eq 'tex') {
 3173: 	$currentstring .= '';
 3174:     }
 3175:     return $currentstring;
 3176: }
 3177: 
 3178: #-- <abbr> tag (end tag required)
 3179: sub start_abbr {
 3180:     my ($target,$token) = @_;
 3181:     my $currentstring = '';
 3182:     if ($target eq 'web' || $target eq 'webgrade') {
 3183: 	$currentstring = $token->[4];     
 3184:     } 
 3185:     return $currentstring;
 3186: }
 3187: 
 3188: sub end_abbr {
 3189:     my ($target,$token) = @_;
 3190:     my $currentstring = '';
 3191:     if ($target eq 'web' || $target eq 'webgrade') {
 3192: 	$currentstring = $token->[2];    
 3193:     } 
 3194:     return $currentstring;
 3195: }
 3196: 
 3197: #-- <acronym> tag (end tag required)
 3198: sub start_acronym {
 3199:     my ($target,$token) = @_;
 3200:     my $currentstring = '';
 3201:     if ($target eq 'web' || $target eq 'webgrade') {
 3202: 	$currentstring = $token->[4];     
 3203:     } 
 3204:     return $currentstring;
 3205: }
 3206: 
 3207: sub end_acronym {
 3208:     my ($target,$token) = @_;
 3209:     my $currentstring = '';
 3210:     if ($target eq 'web' || $target eq 'webgrade') {
 3211: 	$currentstring = $token->[2];    
 3212:     } 
 3213:     return $currentstring;
 3214: }
 3215: 
 3216: #-- <area> tag (end tag forbidden)
 3217: sub start_area {
 3218:     my ($target,$token) = @_;
 3219:     my $currentstring = '';
 3220:     if ($target eq 'web' || $target eq 'webgrade') {
 3221: 	$currentstring = $token->[4];     
 3222:     } 
 3223:     return $currentstring;
 3224: }
 3225: 
 3226: sub end_area {
 3227:     my ($target,$token) = @_;
 3228:     my $currentstring = '';
 3229:     if ($target eq 'web' || $target eq 'webgrade') {
 3230: 	$currentstring = $token->[2];    
 3231:     } 
 3232:     return $currentstring;
 3233: }
 3234: 
 3235: #-- <base> tag (end tag forbidden)
 3236: sub start_base {
 3237:     my ($target,$token) = @_;
 3238:     my $currentstring = '';
 3239:     if ($target eq 'web' || $target eq 'webgrade') {
 3240: 	$currentstring = $token->[4];     
 3241:     }
 3242:     return $currentstring;
 3243: }
 3244: 
 3245: sub end_base {
 3246:     my ($target,$token) = @_;
 3247:     my $currentstring = '';
 3248:     if ($target eq 'web' || $target eq 'webgrade') {
 3249: 	$currentstring = $token->[2];    
 3250:     } 
 3251:     return $currentstring;
 3252: }
 3253: 
 3254: #-- <bdo> tag (end tag required)
 3255: sub start_bdo {
 3256:     my ($target,$token) = @_;
 3257:     my $currentstring = '';
 3258:     if ($target eq 'web' || $target eq 'webgrade') {
 3259: 	$currentstring = $token->[4];     
 3260:     } 
 3261:     return $currentstring;
 3262: }
 3263: 
 3264: sub end_bdo {
 3265:     my ($target,$token) = @_;
 3266:     my $currentstring = '';
 3267:     if ($target eq 'web' || $target eq 'webgrade') {
 3268: 	$currentstring = $token->[2];    
 3269:     } 
 3270:     return $currentstring;
 3271: }
 3272: 
 3273: #-- <bgsound> tag (end tag optional)
 3274: sub start_bgsound {
 3275:     my ($target,$token) = @_;
 3276:     my $currentstring = '';
 3277:     if ($target eq 'web' || $target eq 'webgrade') {
 3278: 	$currentstring = $token->[4];     
 3279:     } 
 3280:     return $currentstring;
 3281: }
 3282: 
 3283: sub end_bgsound {
 3284:     my ($target,$token) = @_;
 3285:     my $currentstring = '';
 3286:     if ($target eq 'web' || $target eq 'webgrade') {
 3287: 	$currentstring = $token->[2];    
 3288:     } 
 3289:     return $currentstring;
 3290: }
 3291: 
 3292: #-- <blink> tag (end tag required)
 3293: sub start_blink {
 3294:     my ($target,$token) = @_;
 3295:     my $currentstring = '';
 3296:     if ($target eq 'web' || $target eq 'webgrade') {
 3297: 	$currentstring = $token->[4];     
 3298:     } 
 3299:     return $currentstring;
 3300: }
 3301: 
 3302: sub end_blink {
 3303:     my ($target,$token) = @_;
 3304:     my $currentstring = '';
 3305:     if ($target eq 'web' || $target eq 'webgrade') {
 3306: 	$currentstring = $token->[2];    
 3307:     } 
 3308:     return $currentstring;
 3309: }
 3310: 
 3311: #-- <blockquote> tag (end tag required)
 3312: sub start_blockquote {
 3313:     my ($target,$token) = @_;
 3314:     my $currentstring = &end_p();	# Close any unclosed <p>
 3315:     if ($target eq 'web' || $target eq 'webgrade') {
 3316: 	$currentstring .= $token->[4];     
 3317:     } 
 3318:     if ($target eq 'tex') {
 3319: 	$currentstring .= '\begin{quote}';
 3320:     }
 3321:     return $currentstring;
 3322: }
 3323: 
 3324: sub end_blockquote {
 3325:     my ($target,$token) = @_;
 3326:     my $currentstring = '';
 3327:     if ($target eq 'web' || $target eq 'webgrade') {
 3328: 	$currentstring = $token->[2];    
 3329:     } 
 3330:     if ($target eq 'tex') {
 3331: 	$currentstring = '\end{quote}';
 3332:     }
 3333:     return $currentstring;
 3334: }
 3335: 
 3336: #-- <button> tag (end tag required)
 3337: sub start_button {
 3338:     my ($target,$token) = @_;
 3339:     my $currentstring = '';
 3340:     if ($target eq 'web' || $target eq 'webgrade') {
 3341: 	$currentstring = $token->[4];     
 3342:     } 
 3343:     return $currentstring;
 3344: }
 3345: 
 3346: sub end_button {
 3347:     my ($target,$token) = @_;
 3348:     my $currentstring = '';
 3349:     if ($target eq 'web' || $target eq 'webgrade') {
 3350: 	$currentstring = $token->[2];    
 3351:     } 
 3352:     return $currentstring;
 3353: }
 3354: 
 3355: #-- <caption> tag (end tag required)
 3356: sub start_caption {
 3357:     my ($target,$token) = @_;
 3358:     my $currentstring = '';
 3359:     if ($target eq 'web' || $target eq 'webgrade') {
 3360: 	$currentstring = $token->[4];     
 3361:     } 
 3362:     return $currentstring;
 3363: }
 3364: 
 3365: sub end_caption {
 3366:     my ($target,$token) = @_;
 3367:     my $currentstring = '';
 3368:     if ($target eq 'web' || $target eq 'webgrade') {
 3369: 	$currentstring = $token->[2];    
 3370:     } 
 3371:     return $currentstring;
 3372: }
 3373: 
 3374: #-- <col> tag (end tag forbdden)
 3375: sub start_col {
 3376:     my ($target,$token) = @_;
 3377:     my $currentstring = '';
 3378:     if ($target eq 'web' || $target eq 'webgrade') {
 3379: 	$currentstring = $token->[4];     
 3380:     } 
 3381:     return $currentstring;
 3382: }
 3383: 
 3384: sub end_col {
 3385:     my ($target,$token) = @_;
 3386:     my $currentstring = '';
 3387:     if ($target eq 'web' || $target eq 'webgrade') {
 3388: 	$currentstring = $token->[2];    
 3389:     } 
 3390:     return $currentstring;
 3391: }
 3392: 
 3393: #-- <colgroup tag (end tag optional)
 3394: sub start_colgroup {
 3395:     my ($target,$token,$tagstack, $parstack, $parser, $safeeval, $style) = @_;
 3396:     my $currentstring = '';
 3397:     if ($target eq 'web' || $target eq 'webgrade') {
 3398: 	$currentstring = $token->[4];     
 3399:     } 
 3400:     if ($target eq 'tex') {
 3401: 	# TODO: Ensure this tag is in a table:
 3402: 
 3403: 	# Fetch the attributes and build the hash for the
 3404: 	# call to define_colgroup.
 3405: 
 3406: 	my $span    = &Apache::lonxml::get_param('span',   $parstack, $safeeval);
 3407: 	my $halign  = &Apache::lonxml::get_param('halign', $parstack, $safeeval);
 3408: 
 3409: 	my %colgroup_params;
 3410: 	if ($span ne '') {
 3411: 	    $colgroup_params{'span'} = $span;
 3412: 	}
 3413: 	if ($halign ne '') {
 3414: 	    $colgroup_params{'halign'} = $halign;
 3415: 	}
 3416: 	
 3417: 	my $table = $Apache::londefdef::table[-1];
 3418: 	$table->define_colgroup(\%colgroup_params);
 3419: 
 3420:     }
 3421:     return $currentstring;
 3422: }
 3423: 
 3424: sub end_colgroup {
 3425:     my ($target,$token) = @_;
 3426:     my $currentstring = '';
 3427:     if ($target eq 'web' || $target eq 'webgrade') {
 3428: 	$currentstring = $token->[2];    
 3429:     } 
 3430:     return $currentstring;
 3431: }
 3432: 
 3433: 
 3434: #-- <del> tag (end tag required)
 3435: sub start_del {
 3436:     my ($target,$token) = @_;
 3437:     my $currentstring = '';
 3438:     if ($target eq 'web' || $target eq 'webgrade') {
 3439: 	$currentstring = $token->[4];     
 3440:     } elsif ($target eq 'tex') {
 3441: 	&disable_para();
 3442: 	$currentstring .= '\st{';  
 3443:     } 
 3444:     return $currentstring;
 3445: }
 3446: 
 3447: sub end_del {
 3448:     my ($target,$token) = @_;
 3449:     my $currentstring = '';
 3450:     if ($target eq 'web' || $target eq 'webgrade') {
 3451: 	$currentstring = $token->[2];     
 3452:     } elsif ($target eq 'tex') {
 3453: 	&enable_para();
 3454: 	$currentstring = '}';
 3455:     } 
 3456:     return $currentstring;
 3457: }
 3458: 
 3459: #-- <fieldset> tag (end tag required)
 3460: sub start_fieldset {
 3461:     my ($target,$token) = @_;
 3462:     my $currentstring = '';
 3463:     if ($target eq 'web' || $target eq 'webgrade') {
 3464: 	$currentstring = $token->[4];     
 3465:     } 
 3466:     return $currentstring;
 3467: }
 3468: 
 3469: sub end_fieldset {
 3470:     my ($target,$token) = @_;
 3471:     my $currentstring = '';
 3472:     if ($target eq 'web' || $target eq 'webgrade') {
 3473: 	$currentstring = $token->[2];    
 3474:     } 
 3475:     return $currentstring;
 3476: }
 3477: 
 3478: #-- <frame> tag (end tag forbidden)
 3479: sub start_frame {
 3480:     my ($target,$token) = @_;
 3481:     my $currentstring = '';
 3482:     if ($target eq 'web' || $target eq 'webgrade') {
 3483: 	$currentstring = $token->[4];     
 3484:     } 
 3485:     return $currentstring;
 3486: }
 3487: 
 3488: sub end_frame {
 3489:     my ($target,$token) = @_;
 3490:     my $currentstring = '';
 3491:     if ($target eq 'web' || $target eq 'webgrade') {
 3492: 	$currentstring = $token->[2];    
 3493:     } 
 3494:     return $currentstring;
 3495: }
 3496: 
 3497: #-- <iframe> tag (end tag required)
 3498: sub start_iframe {
 3499:     my ($target,$token) = @_;
 3500:     my $currentstring = '';
 3501:     if ($target eq 'web' || $target eq 'webgrade') {
 3502: 	$currentstring = $token->[4];     
 3503:     } 
 3504:     return $currentstring;
 3505: }
 3506: 
 3507: sub end_iframe {
 3508:     my ($target,$token) = @_;
 3509:     my $currentstring = '';
 3510:     if ($target eq 'web' || $target eq 'webgrade') {
 3511: 	$currentstring = $token->[2];    
 3512:     } 
 3513:     return $currentstring;
 3514: }
 3515: 
 3516: #-- <ins> tag (end tag required)
 3517: sub start_ins {
 3518:     my ($target,$token) = @_;
 3519:     my $currentstring = '';
 3520:     if ($target eq 'web' || $target eq 'webgrade') {
 3521: 	$currentstring = $token->[4];     
 3522:     } 
 3523:     return $currentstring;
 3524: }
 3525: 
 3526: sub end_ins {
 3527:     my ($target,$token) = @_;
 3528:     my $currentstring = '';
 3529:     if ($target eq 'web' || $target eq 'webgrade') {
 3530: 	$currentstring = $token->[2];    
 3531:     } 
 3532:     return $currentstring;
 3533: }
 3534: 
 3535: #-- <isindex> tag (end tag forbidden)
 3536: sub start_isindex {
 3537:     my ($target,$token) = @_;
 3538:     my $currentstring = '';
 3539:     if ($target eq 'web' || $target eq 'webgrade') {
 3540: 	$currentstring = $token->[4];     
 3541:     } 
 3542:     return $currentstring;
 3543: }
 3544: 
 3545: sub end_isindex {
 3546:     my ($target,$token) = @_;
 3547:     my $currentstring = '';
 3548:     if ($target eq 'web' || $target eq 'webgrade') {
 3549: 	$currentstring = $token->[2];    
 3550:     } 
 3551:     return $currentstring;
 3552: }
 3553: 
 3554: #-- <keygen> tag (end tag forbidden)
 3555: sub start_keygen {
 3556:     my ($target,$token) = @_;
 3557:     my $currentstring = '';
 3558:     if ($target eq 'web' || $target eq 'webgrade') {
 3559: 	$currentstring = $token->[4];     
 3560:     } 
 3561:     return $currentstring;
 3562: }
 3563: 
 3564: sub end_keygen {
 3565:     my ($target,$token) = @_;
 3566:     my $currentstring = '';
 3567:     if ($target eq 'web' || $target eq 'webgrade') {
 3568: 	$currentstring = $token->[2];    
 3569:     } 
 3570:     return $currentstring;
 3571: }
 3572: 
 3573: #-- <label> tag
 3574: sub start_label {
 3575:     my ($target,$token) = @_;
 3576:     my $currentstring = '';
 3577:     if ($target eq 'web' || $target eq 'webgrade') {
 3578: 	$currentstring = $token->[4];     
 3579:     } 
 3580:     return $currentstring;
 3581: }
 3582: 
 3583: sub end_label {
 3584:     my ($target,$token) = @_;
 3585:     my $currentstring = '';
 3586:     if ($target eq 'web' || $target eq 'webgrade') {
 3587: 	$currentstring = $token->[2];    
 3588:     } 
 3589:     return $currentstring;
 3590: }
 3591: 
 3592: #-- <layer> tag (end tag required)
 3593: sub start_layer {
 3594:     my ($target,$token) = @_;
 3595:     my $currentstring = '';
 3596:     if ($target eq 'web' || $target eq 'webgrade') {
 3597: 	$currentstring = $token->[4];     
 3598:     } 
 3599:     return $currentstring;
 3600: }
 3601: 
 3602: sub end_layer {
 3603:     my ($target,$token) = @_;
 3604:     my $currentstring = '';
 3605:     if ($target eq 'web' || $target eq 'webgrade') {
 3606: 	$currentstring = $token->[2];    
 3607:     } 
 3608:     return $currentstring;
 3609: }
 3610: 
 3611: #-- <legend> tag (end tag required)
 3612: sub start_legend {
 3613:     my ($target,$token) = @_;
 3614:     my $currentstring = '';
 3615:     if ($target eq 'web' || $target eq 'webgrade') {
 3616: 	$currentstring = $token->[4];     
 3617:     } 
 3618:     return $currentstring;
 3619: }
 3620: 
 3621: sub end_legend {
 3622:     my ($target,$token) = @_;
 3623:     my $currentstring = '';
 3624:     if ($target eq 'web' || $target eq 'webgrade') {
 3625: 	$currentstring = $token->[2];    
 3626:     } 
 3627:     return $currentstring;
 3628: }
 3629: 
 3630: #-- <link> tag (end tag forbidden)
 3631: sub start_link {
 3632:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 3633:     my $currentstring = '';
 3634:     if ($target eq 'web' || $target eq 'webgrade') {
 3635: 	my $href=&Apache::lonxml::get_param('href',$parstack,$safeeval,
 3636: 					    undef,1);
 3637: 	&Apache::lonxml::extlink($href);
 3638: 	$currentstring = $token->[4];     
 3639:     } 
 3640:     return $currentstring;
 3641: }
 3642: 
 3643: sub end_link {
 3644:     my ($target,$token) = @_;
 3645:     my $currentstring = '';
 3646:     if ($target eq 'web' || $target eq 'webgrade') {
 3647: 	$currentstring = $token->[2];    
 3648:     } 
 3649:     return $currentstring;
 3650: }
 3651: 
 3652: #-- <marquee> tag (end tag optional)
 3653: sub start_marquee {
 3654:     my ($target,$token) = @_;
 3655:     my $currentstring = '';
 3656:     if ($target eq 'web' || $target eq 'webgrade') {
 3657: 	$currentstring = $token->[4];     
 3658:     } 
 3659:     return $currentstring;
 3660: }
 3661: 
 3662: sub end_marquee {
 3663:     my ($target,$token) = @_;
 3664:     my $currentstring = '';
 3665:     if ($target eq 'web' || $target eq 'webgrade') {
 3666: 	$currentstring = $token->[2];    
 3667:     } 
 3668:     return $currentstring;
 3669: }
 3670: 
 3671: #-- <multicol> tag (end tag required)
 3672: sub start_multicol {
 3673:     my ($target,$token) = @_;
 3674:     my $currentstring = &end_p();	# Close any pending <p>
 3675:     if ($target eq 'web' || $target eq 'webgrade') {
 3676: 	$currentstring .= $token->[4];     
 3677:     } 
 3678:     return $currentstring;
 3679: }
 3680: 
 3681: sub end_multicol {
 3682:     my ($target,$token) = @_;
 3683:     my $currentstring = '';
 3684:     if ($target eq 'web' || $target eq 'webgrade') {
 3685: 	$currentstring = $token->[2];    
 3686:     } 
 3687:     return $currentstring;
 3688: }
 3689: 
 3690: #-- <nobr> tag (end tag required)
 3691: sub start_nobr {
 3692:     my ($target,$token) = @_;
 3693:     my $currentstring = '';
 3694:     if ($target eq 'web' || $target eq 'webgrade') {
 3695: 	$currentstring = $token->[4];     
 3696:     }  elsif ($target eq 'tex') {
 3697: 	$currentstring='\mbox{';
 3698:     }
 3699:     return $currentstring;
 3700: }
 3701: 
 3702: sub end_nobr {
 3703:     my ($target,$token) = @_;
 3704:     my $currentstring = '';
 3705:     if ($target eq 'web' || $target eq 'webgrade') {
 3706: 	$currentstring = $token->[2];    
 3707:     }   elsif ($target eq 'tex') {
 3708: 	$currentstring='}';
 3709:     }
 3710:     return $currentstring;
 3711: }
 3712: 
 3713: #-- <noembed> tag (end tag required)
 3714: sub start_noembed {
 3715:     my ($target,$token) = @_;
 3716:     my $currentstring = '';
 3717:     if ($target eq 'web' || $target eq 'webgrade') {
 3718: 	$currentstring = $token->[4];     
 3719:     } 
 3720:     return $currentstring;
 3721: }
 3722: 
 3723: sub end_noembed {
 3724:     my ($target,$token) = @_;
 3725:     my $currentstring = '';
 3726:     if ($target eq 'web' || $target eq 'webgrade') {
 3727: 	$currentstring = $token->[2];    
 3728:     } 
 3729:     return $currentstring;
 3730: }
 3731: 
 3732: #-- <noframes> tag (end tag required)
 3733: sub start_noframes {
 3734:     my ($target,$token) = @_;
 3735:     my $currentstring = '';
 3736:     if ($target eq 'web' || $target eq 'webgrade') {
 3737: 	$currentstring = $token->[4];     
 3738:     } 
 3739:     return $currentstring;
 3740: }
 3741: 
 3742: sub end_noframes {
 3743:     my ($target,$token) = @_;
 3744:     my $currentstring = '';
 3745:     if ($target eq 'web' || $target eq 'webgrade') {
 3746: 	$currentstring = $token->[2];    
 3747:     } 
 3748:     return $currentstring;
 3749: }
 3750: 
 3751: #-- <nolayer> tag (end tag required)
 3752: sub start_nolayer {
 3753:     my ($target,$token) = @_;
 3754:     my $currentstring = '';
 3755:     if ($target eq 'web' || $target eq 'webgrade') {
 3756: 	$currentstring = $token->[4];     
 3757:     } 
 3758:     return $currentstring;
 3759: }
 3760: 
 3761: sub end_nolayer {
 3762:     my ($target,$token) = @_;
 3763:     my $currentstring = '';
 3764:     if ($target eq 'web' || $target eq 'webgrade') {
 3765: 	$currentstring = $token->[2];    
 3766:     } 
 3767:     return $currentstring;
 3768: }
 3769: 
 3770: #-- <noscript> tag (end tag required)
 3771: sub start_noscript {
 3772:     my ($target,$token) = @_;
 3773:     my $currentstring = '';
 3774:     if ($target eq 'web' || $target eq 'webgrade') {
 3775: 	$currentstring = $token->[4];     
 3776:     } 
 3777:     return $currentstring;
 3778: }
 3779: 
 3780: sub end_noscript {
 3781:     my ($target,$token) = @_;
 3782:     my $currentstring = '';
 3783:     if ($target eq 'web' || $target eq 'webgrade') {
 3784: 	$currentstring = $token->[2];    
 3785:     } 
 3786:     return $currentstring;
 3787: }
 3788: 
 3789: #-- <object> tag (end tag required)
 3790: sub start_object {
 3791:     my ($target,$token) = @_;
 3792:     my $currentstring = '';
 3793:     if ($target eq 'web' || $target eq 'webgrade') {
 3794: 	$currentstring = $token->[4];     
 3795:     } 
 3796:     return $currentstring;
 3797: }
 3798: 
 3799: sub end_object {
 3800:     my ($target,$token) = @_;
 3801:     my $currentstring = '';
 3802:     if ($target eq 'web' || $target eq 'webgrade') {
 3803: 	$currentstring = $token->[2];    
 3804:     } 
 3805:     return $currentstring;
 3806: }
 3807: 
 3808: #-- <optgroup> tag (end tag required)
 3809: sub start_optgroup {
 3810:     my ($target,$token) = @_;
 3811:     my $currentstring = '';
 3812:     if ($target eq 'web' || $target eq 'webgrade') {
 3813: 	$currentstring = $token->[4];     
 3814:     } 
 3815:     return $currentstring;
 3816: }
 3817: 
 3818: sub end_optgroup {
 3819:     my ($target,$token) = @_;
 3820:     my $currentstring = '';
 3821:     if ($target eq 'web' || $target eq 'webgrade') {
 3822: 	$currentstring = $token->[2];    
 3823:     } 
 3824:     return $currentstring;
 3825: }
 3826: 
 3827: #-- <samp> tag (end tag required)
 3828: sub start_samp {
 3829:     my ($target,$token) = @_;
 3830:     my $currentstring = '';
 3831:     if ($target eq 'web' || $target eq 'webgrade') {
 3832: 	$currentstring = $token->[4];     
 3833:     } elsif ($target eq 'tex') {
 3834: 	$currentstring='\texttt{';
 3835:     }
 3836:     return $currentstring;
 3837: }
 3838: 
 3839: sub end_samp {
 3840:     my ($target,$token) = @_;
 3841:     my $currentstring = '';
 3842:     if ($target eq 'web' || $target eq 'webgrade') {
 3843: 	$currentstring = $token->[2];    
 3844:     } elsif ($target eq 'tex') {
 3845: 	$currentstring='}';
 3846:     }
 3847:     return $currentstring;
 3848: }
 3849: 
 3850: #-- <server> tag
 3851: sub start_server {
 3852:     my ($target,$token) = @_;
 3853:     my $currentstring = '';
 3854:     if ($target eq 'web' || $target eq 'webgrade') {
 3855: 	$currentstring = $token->[4];     
 3856:     } 
 3857:     return $currentstring;
 3858: }
 3859: 
 3860: sub end_server {
 3861:     my ($target,$token) = @_;
 3862:     my $currentstring = '';
 3863:     if ($target eq 'web' || $target eq 'webgrade') {
 3864: 	$currentstring = $token->[2];    
 3865:     } 
 3866:     return $currentstring;
 3867: }
 3868: 
 3869: #-- <spacer> tag (end tag forbidden)
 3870: sub start_spacer {
 3871:     my ($target,$token) = @_;
 3872:     my $currentstring = &end_p();	# Close off any open <p> tag.
 3873:     if ($target eq 'web' || $target eq 'webgrade') {
 3874: 	$currentstring .= $token->[4];     
 3875:     } 
 3876:     return $currentstring;
 3877: }
 3878: 
 3879: sub end_spacer {
 3880:     my ($target,$token) = @_;
 3881:     my $currentstring = '';
 3882:     if ($target eq 'web' || $target eq 'webgrade') {
 3883: 	$currentstring = $token->[2];    
 3884:     } 
 3885:     return $currentstring;
 3886: }
 3887: 
 3888: #-- <span> tag (end tag required)
 3889: sub start_span {
 3890:     my ($target,$token) = @_;
 3891:     my $currentstring = '';
 3892:     if ($target eq 'web' || $target eq 'webgrade') {
 3893: 	$currentstring = $token->[4];     
 3894:     } 
 3895:     return $currentstring;
 3896: }
 3897: 
 3898: sub end_span {
 3899:     my ($target,$token) = @_;
 3900:     my $currentstring = '';
 3901:     if ($target eq 'web' || $target eq 'webgrade') {
 3902: 	$currentstring = $token->[2];    
 3903:     } 
 3904:     return $currentstring;
 3905: }
 3906: 
 3907: #-- <tbody> tag (end tag optional)
 3908: sub start_tbody {
 3909:     my ($target,$token) = @_;
 3910:     my $currentstring = '';
 3911:     if ($target eq 'web' || $target eq 'webgrade') {
 3912: 	$currentstring = $token->[4];     
 3913:     } 
 3914:     if ($target eq 'tex') {
 3915: 	# TODO: Ensure this tag is within a table:
 3916: 
 3917: 	my $table = $Apache::londefdef::table[-1];
 3918: 	$table->start_body();
 3919:     }
 3920:     return $currentstring;
 3921: }
 3922: 
 3923: sub end_tbody {
 3924:     my ($target,$token) = @_;
 3925:     my $currentstring = '';
 3926:     if ($target eq 'web' || $target eq 'webgrade') {
 3927: 	$currentstring = $token->[2];    
 3928:     } 
 3929:     if($target eq 'tex') {
 3930: 	# TODO: Ensure this tag is within a table:
 3931: 
 3932: 	my $table = $Apache::londefdef::table[-1];
 3933: 	$table->end_body();
 3934:     }
 3935:     return $currentstring;
 3936: }
 3937: 
 3938: #-- <tfoot> tag (end tag optional)
 3939: sub start_tfoot {
 3940:     my ($target,$token) = @_;
 3941:     my $currentstring = '';
 3942:     if ($target eq 'web' || $target eq 'webgrade') {
 3943: 	$currentstring = $token->[4];     
 3944:     } 
 3945:     if ($target eq 'tex') {
 3946:         # TODO: ensure this is within a table tag.
 3947: 	my $table = $Apache::londefdef::table[-1];
 3948: 	$table->start_foot();
 3949:     }
 3950:     return $currentstring;
 3951: }
 3952: 
 3953: sub end_tfoot {
 3954:     my ($target,$token) = @_;
 3955:     my $currentstring = '';
 3956:     if ($target eq 'web' || $target eq 'webgrade') {
 3957: 	$currentstring = $token->[2];    
 3958:     } 
 3959:     if ($target eq 'tex') {
 3960: 	#  TODO: Ensure this is in side a table 
 3961: 	my $table = $Apache::londefdef::table[-1];
 3962: 	$table->end_foot();
 3963:     }
 3964:     return $currentstring;
 3965: }
 3966: 
 3967: #-- <thead> tag (end tag optional)
 3968: sub start_thead {
 3969:     my ($target,$token) = @_;
 3970:     my $currentstring = '';
 3971:     if ($target eq 'web' || $target eq 'webgrade') {
 3972: 	$currentstring = $token->[4];     
 3973:     } 
 3974:     if ($target eq 'tex') {
 3975: 	# Assume we're in a table... TODO: Verify that and ignore tag if not.
 3976: 	my $table = $Apache::londefdef::table[-1];
 3977: 	$table->start_head();
 3978:     }
 3979:     return $currentstring;
 3980: }
 3981: 
 3982: sub end_thead {
 3983:     my ($target,$token) = @_;
 3984:     my $currentstring = '';
 3985:     if ($target eq 'web' || $target eq 'webgrade') {
 3986: 	$currentstring = $token->[2];    
 3987:     } 
 3988:     if ($target eq 'tex') {
 3989:      	# TODO: Verify we are in a table and ignore tag if not.
 3990: 
 3991: 	my $table = $Apache::londefdef::table[-1];
 3992: 	$table->end_head();
 3993:     }
 3994:     return $currentstring;
 3995: }
 3996: 
 3997: #-- <var> tag
 3998: sub start_var {
 3999:     my ($target,$token) = @_;
 4000:     my $currentstring = '';
 4001:     if ($target eq 'web' || $target eq 'webgrade') {
 4002: 	$currentstring = $token->[4];     
 4003:     } elsif ($target eq 'tex') {
 4004: 	$currentstring = '\textit{'; 
 4005:     }
 4006:     return $currentstring;
 4007: }
 4008: 
 4009: sub end_var {
 4010:     my ($target,$token) = @_;
 4011:     my $currentstring = '';
 4012:     if ($target eq 'web' || $target eq 'webgrade') {
 4013: 	$currentstring = $token->[2];
 4014:     } elsif ($target eq 'tex') {
 4015: 	$currentstring = '}'; 
 4016:     } 
 4017:     return $currentstring;
 4018: }
 4019: 
 4020: #-- <wbr> tag (end tag forbidden)
 4021: sub start_wbr {
 4022:     my ($target,$token) = @_;
 4023:     my $currentstring = '';
 4024:     if ($target eq 'web' || $target eq 'webgrade') {
 4025: 	$currentstring = $token->[4];     
 4026:     } 
 4027:     return $currentstring;
 4028: }
 4029: 
 4030: sub end_wbr {
 4031:     my ($target,$token) = @_;
 4032:     my $currentstring = '';
 4033:     if ($target eq 'web' || $target eq 'webgrade') {
 4034: 	$currentstring = $token->[2];    
 4035:     } 
 4036:     return $currentstring;
 4037: }
 4038: 
 4039: #-- <hideweboutput> tag
 4040: sub start_hideweboutput {
 4041:     my ($target,$token) = @_;
 4042:     if ($target eq 'web' || $target eq 'webgrade') {
 4043: 	&Apache::lonxml::startredirection();     
 4044:     } 
 4045:     return '';
 4046: }
 4047: 
 4048: sub end_hideweboutput {
 4049:     my ($target,$token) = @_;
 4050:     my $currentstring = '';
 4051:     if ($target eq 'web' || $target eq 'webgrade') {
 4052: 	$currentstring = &Apache::lonxml::endredirection();    
 4053:     } 
 4054:     return '';
 4055: }
 4056: 
 4057: 
 4058: sub image_replication {
 4059:     my $src = shift;
 4060:     if (not -e $src) { &Apache::lonnet::repcopy($src); }
 4061:     #replicates eps or ps 
 4062:     my $epssrc = my $pssrc = $src;
 4063:     $epssrc =~ s/\.(gif|jpg|jpeg|png)$/.eps/i;
 4064:     $pssrc  =~ s/\.(gif|jpg|jpeg|png)$/.ps/i;
 4065:     if (not -e $epssrc && not -e $pssrc) {
 4066: 	my $result=&Apache::lonnet::repcopy($epssrc);
 4067: 	if ($result ne 'ok') { &Apache::lonnet::repcopy($pssrc); }
 4068:     }
 4069:     return '';
 4070: }
 4071: 
 4072: 
 4073: 
 4074: sub resize_image {
 4075:     my ($height_param, $width_param, $scaling,
 4076: 	$parstack, $safeeval, $depth, $cis) = @_;
 4077: 
 4078:     # First apply the scaling...
 4079: 
 4080:     $height_param = $height_param * $scaling;
 4081:     $width_param  = $width_param  * $scaling;
 4082: 
 4083:     #do we have any specified LaTeX size of the picture?
 4084:     my $toget='TeXwidth'; 
 4085:     if ($cis) { 
 4086: 	$toget=lc($toget); 
 4087:     }
 4088:     my $TeXwidth = &Apache::lonxml::get_param($toget,$parstack,
 4089: 					      $safeeval,$depth,$cis);
 4090:     $toget='TeXheight'; if ($cis) { $toget=lc($toget); }
 4091:     my $TeXheight = &Apache::lonxml::get_param($toget,$parstack,
 4092: 					       $safeeval,$depth,$cis);
 4093:     #do we have any specified web size of the picture?
 4094:     my $width = &Apache::lonxml::get_param('width',$parstack,$safeeval,
 4095: 					   $depth,1);
 4096:     if ($TeXwidth) { 
 4097: 	my $old_width_param=$width_param;
 4098: 	if ($TeXwidth=~/(\d+)\s*\%/) {
 4099: 	    $width_param = $1*$env{'form.textwidth'}/100;
 4100: 	} else { 
 4101: 	    $width_param = $TeXwidth;
 4102: 	}
 4103: 	if ($TeXheight) {
 4104: 	    $height_param = $TeXheight;
 4105: 	} elsif ($old_width_param) {
 4106: 	    $height_param=$TeXwidth/$old_width_param*$height_param;
 4107: 	}
 4108:     } elsif ($TeXheight) {
 4109: 	$height_param = $TeXheight;
 4110: 	if ($height_param) {
 4111: 	    $width_param  = $TeXheight/$height_param*$width_param;
 4112: 	}
 4113:     } elsif ($width) {
 4114: 	my $old_width_param=$width_param;
 4115: 	$width_param = $width*$scaling;
 4116: 	if ($old_width_param) {
 4117: 	    $height_param=$width_param/$old_width_param*$height_param;
 4118: 	}
 4119:     }
 4120:     if ($width_param > $env{'form.textwidth'}) {
 4121:         my $old_width_param=$width_param;
 4122: 	$width_param =0.95*$env{'form.textwidth'};
 4123: 	if ($old_width_param) {
 4124: 	    $height_param=$width_param/$old_width_param*$height_param;
 4125: 	}
 4126:     }
 4127: 
 4128:     return ($height_param, $width_param);
 4129: }
 4130: 
 4131: sub image_size {
 4132:     my ($src,$scaling,$parstack,$safeeval,$depth,$cis)=@_;
 4133: 
 4134:     #size of image from gif/jpg/jpeg/png 
 4135:     my $ressrc=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$src);
 4136:     if (-e $ressrc) {
 4137: 	$src = $ressrc;
 4138:     }
 4139:     my $image = Image::Magick->new;
 4140:     my $current_figure = $image->Read($src);
 4141:     my $width_param = $image->Get('width');
 4142:     my $height_param = $image->Get('height');
 4143:     &Apache::lonxml::debug("Image magick says: $src :  Height = $height_param width = $width_param");
 4144:     undef($image);
 4145: 
 4146:     ($height_param, $width_param) = &resize_image($height_param, $width_param,
 4147: 						  $scaling, $parstack, $safeeval, 
 4148: 						  $depth, $cis);
 4149: 
 4150:     return ($height_param, $width_param);
 4151: }
 4152: 
 4153: sub image_width {
 4154:     my ($height, $width) = &image_size(@_);
 4155:     return $width;
 4156: }
 4157: #  Not yet 100% sure this is correct in all circumstances..
 4158: #  due to my uncertainty about mods to image_size.
 4159: #
 4160: sub image_height {
 4161:     my ($height, $width) = &image_size(@_);
 4162:     return $height;
 4163: }
 4164: 
 4165: sub get_eps_image {
 4166:     my ($src)=@_;
 4167:     my $orig_src=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1], $src);
 4168: 
 4169:     # In order to prevent the substitution of the alt text, we need to
 4170:     # be sure the orig_src file is on system now so:
 4171: 
 4172:     if (! -e $orig_src) {
 4173: 	&Apache::lonnet::repcopy($orig_src); # Failure is not completely fatal.
 4174:     }
 4175:     &Apache::lonxml::debug("get_eps_image: Original image: $orig_src");
 4176:     my ($spath, $sname, $sext) = &fileparse($src, qr/\.(bmp|gif|png|jpg|jpeg)/i);
 4177:     $src=~s/\.(bmp|gif|png|jpg|jpeg)$/\.eps/i;
 4178:     $src=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$src);
 4179:     &Apache::lonxml::debug("Filelocation gives: $src");
 4180:     if (! -e $src) {
 4181: 	&Apache::lonxml::debug("$src does not exist");
 4182: 	if (&Apache::lonnet::repcopy($src) ne 'ok' ) {
 4183: 	    &Apache::lonxml::debug("Repcopy of $src failed (1)");
 4184: 	    #if replication failed try to find ps file
 4185: 	    $src=~s/\.eps$/\.ps/;
 4186: 	    &Apache::lonxml::debug("Now looking for $src");
 4187: 	    #if no ps file try to replicate it.
 4188: 	    my $didrepcopy = &Apache::lonnet::repcopy($src);
 4189: 	    &Apache::lonxml::debug("repcopy of $src ... $didrepcopy");
 4190: 	    if ( (not -e $src) ||
 4191: 		($didrepcopy ne 'ok')) {
 4192: 		&Apache::lonxml::debug("Failed to find or replicate $src");
 4193: 
 4194: 		#if replication failed try to produce eps file dynamically
 4195: 		$src=~s/\.ps$/\.eps/;
 4196: 		if (open(FILE,">>/home/httpd/prtspool/$env{'user.name'}_$env{'user.domain'}_printout.dat")) {
 4197: 		    my $newsrc=$orig_src;
 4198: 		    $newsrc =~ s|(.*)/res/|/home/httpd/html/res/|;
 4199: 		    &Apache::lonxml::debug("queueing $newsrc for dynamic eps production.");
 4200: 		    print FILE ("$newsrc\n");
 4201: 		    close(FILE);
 4202:                 }
 4203: 		$src=~s|/home/httpd/html/res|/home/httpd/prtspool|;
 4204: 		$src=~s|/home/([^/]*)/public_html/|/home/httpd/prtspool/$1/|;
 4205: 		if ($sext ne "") {	 # Put the ext. back in to uniquify.
 4206: 		    $src =~ s/\.eps$/$sext.eps/;
 4207: 		}
 4208: 
 4209: 	    }
 4210: 
 4211: 	}
 4212:     } else {
 4213: 	# If the postscript file has spaces in its name,
 4214: 	# LaTeX will gratuitiously vomit.  Therefore
 4215: 	# queue such files for copy with " " replaced by "_".
 4216: 	# printout.pm will know them by their .ps  or .eps extensions.
 4217: 	my $newsrc = $orig_src;
 4218: 	$newsrc    =~  s|(.*)/res/|/home/httpd/html/res/|;
 4219: 	open(FILE,">>/home/httpd/prtspool/$env{'user.name'}_$env{'user.domain'}_printout.dat");
 4220: 	print FILE "$src\n";
 4221: 	close FILE;
 4222: 	$src=~s|/home/httpd/html/res|/home/httpd/prtspool|;
 4223: 	$src=~s|/home/([^/]*)/public_html/|/home/httpd/prtspool/$1/|;
 4224:     }
 4225:     my ($path,$file)=($src=~m|(.*)/([^/]*)$|);
 4226:     $path =~ s/ /\_/g;
 4227:     $file =~ s/ /\_/g;
 4228:     &Apache::lonxml::debug("get_eps_image returning: $path / $file<BR />");
 4229:     return ($path.'/',$file);
 4230: }
 4231: 
 4232: sub eps_generation {
 4233:     my ($src,$file,$width_param) = @_;	     
 4234:     my $filename = "/home/httpd/prtspool/$env{'user.name'}_$env{'user.domain'}_printout.dat";
 4235:     if (open(my $tmpfile,">>$filename")) { 
 4236:         print $tmpfile "$src\n";
 4237:         close($tmpfile);
 4238:     }
 4239:     my $newsrc = $src;
 4240:     $newsrc =~ s/(\.bmp|\.gif|\.jpg|\.jpeg)$/\.eps/i;
 4241:     $newsrc=~s{/home/httpd/html/res}{};
 4242:     $newsrc=~s{/home/($LONCAPA::username_re)/public_html/}{/$1/};
 4243:     $newsrc=~s{/\./}{/};
 4244:     $newsrc=~s{/([^/]+)\.(ps|eps)}{/};
 4245:     if ($newsrc=~m{/home/httpd/lonUsers/}) {
 4246: 	$newsrc=~s{/home/httpd/lonUsers}{};
 4247: 	$newsrc=~s{/($LONCAPA::domain_re)/./././}{/$1/};
 4248:     }
 4249:     if ($newsrc=~m{/userfiles/}) {
 4250: 	return ' \graphicspath{{'.$newsrc.'}}\includegraphics[width='.$width_param.' mm]{'.$file.'} ';
 4251:     } else {
 4252: 	return ' \graphicspath{{/home/httpd/prtspool'.$newsrc.'}}\includegraphics[width='.$width_param.' mm]{'.$file.'} ';
 4253:     }
 4254: }
 4255: 
 4256: sub file_path {     
 4257:     my $src=shift;
 4258:     my ($file,$path); 
 4259:     if ($src =~ m!(.*)/([^/]*)$!) {
 4260: 	$file = $2; 
 4261: 	$path = $1.'/'; 
 4262:     } 
 4263:     return $file,$path;
 4264: }
 4265: 
 4266: 
 4267: sub recalc {
 4268:     my $argument = shift;
 4269:     if (not $argument=~/(mm|cm|in|pc|pt)/) {return $argument.' mm';}
 4270:     $argument=~/\s*(\d+\.?\d*)\s*(mm|cm|in|pc|pt)/;
 4271:     my $value=$1;
 4272:     my $units=$2;
 4273:     if ($units eq 'cm') {
 4274: 	$value*=10;
 4275:     } elsif ($units eq 'in') {
 4276: 	$value*=25.4;
 4277:     } elsif ($units eq 'pc') {
 4278: 	$value*=(25.4*12/72.27);
 4279:     } elsif ($units eq 'pt') {
 4280: 	$value*=(25.4/72.27);
 4281:     }
 4282:     return $value.' mm';
 4283: }
 4284: 
 4285: sub LATEX_length {
 4286:     my $garbage=shift;
 4287:     $garbage=~s/^\s+$//;
 4288:     $garbage=~s/^\s+(\S.*)/$1/;#space before 
 4289:     $garbage=~s/(.*\S)\s+$/$1/;#space after 
 4290:     $garbage=~s/(\s)+/$1/;#only one space
 4291:     $garbage=~s/(\\begin{([^\}]+)}|\\end{([^\}]+)})//g;#remove LaTeX \begin{...} and \end{...}
 4292:     $garbage=~s/(\$\_\{|\$\_|\$\^{|\$\^|\}\$)//g;#remove $_{,$_,$^{,$^,}$
 4293:     $garbage=~s/([^\\])\$/$1/g;#$
 4294:     $garbage=~s/(\\ensuremath\{\_\{|\\ensuremath\{\_|\\ensuremath\{\^{|\\ensuremath\{\^|\})//g;#remove \ensuremath{...}
 4295:    $garbage=~s/(\\alpha|\\beta|\\gamma|\\delta|\\epsilon|\\verepsilon|\\zeta|\\eta|\\theta|\\vartheta|\\iota|\\kappa|\\lambda|\\mu|\\nu|\\xi|\\pi|\\varpi|\\rho|\\varrho|\\sigma|\\varsigma|\\tau|\\upsilon|\\phi|\\varphi|\\chi|\\psi|\\omega|\\Gamma|\\Delta|\\Theta|\\Lambda|\\Xi|\\Pi|\\Sigma|\\Upsilon|\\Phi|\\Psi|\\Omega)/1/g;
 4296:     $garbage=~s/(\\pm|\\mp|\\times|\\div|\\cdot|\\ast|\\star|\\dagger|\\ddagger|\\amalg|\\cap|\\cup|\\uplus|\\sqcap|\\sqcup|\\vee|\\wedge|\\oplus|\\ominus|\\otimes|\\circ|\\bullet|\\diamond|\\lhd|\\rhd|\\unlhd|\\unrhd|\\oslash|\\odot|\\bigcirc|\\Box|\\Diamond|\\bigtriangleup|\\bigtriangledown|\\triangleleft|\\triangleright|\\setminus|\\wr)/1/g;
 4297:     $garbage=~s/(\\le|\\ll|\\leq|\\ge|\\geq|\\gg|\\neq|\\doreq|\\sim|\\simeq|\\subset|\\subseteq|\\sqsubset|\\sqsubseteq|\\in|\\vdash|\\models|\\supset|\\supseteq|\\sqsupset|\\sqsupseteq|\\ni|\\dash|\\perp|\\approx|\\cong|\\equiv|\\propto|\\prec|\\preceq|\\parallel|\\asymp|\\smile|\\frown|\\bowtie|\\succ|\\succeq|\\mid)/1/g;
 4298:     $garbage=~s/(\\not<|\\\\not\\le|\\not\\prec|\\not\\preceq|\\not\\subset|\\not\\subseteq|\\not\\sqsubseteq|\\not\\in|\\not>|\\not\\ge|\\not\\succ|\\notsucceq|\\not\\supset|\\notsupseteq|\\not\\sqsupseteq|\\notin|\\not=|\\not\\equiv|\\not\\sim|\\not\\simeq|\\not\\approx|\\not\\cong|\\not\\asymp)/1/g;
 4299:     $garbage=~s/(\\leftarrow|\\gets|\\Leftarrow|\\rightarrow|\\to|\\Rightarrow|\\leftrightarrow|\\Leftrightarrow|\\mapsto|\\hookleftarrow|\\leftharpoonup|\\leftkarpoondown|\\rightleftharpoons|\\longleftarrow|\\Longleftarrow|\\longrightarrow|\\Longrightarrow|\\longleftrightarrow|\\Longleftrightarrow|\\longmapsto|\\hookrightarrow|\\rightharpoonup|\\rightharpoondown|\\uparrow|\\Uparrow|\\downarrow|\\Downarrow|\\updownarrow|\\Updownarrow|\\nearrow|\\searrow|\\swarrow|\\nwarrow)/11/g;
 4300:     $garbage=~s/(\\aleph|\\hbar|\\imath|\\jmath|\\ell|\\wp|\\Re|\\Im|\\mho|\\prime|\\emptyset|\\nabla|\\surd|\\partial|\\top|\\bot|\\vdash|\\dashv|\\forall|\\exists|\\neg|\\flat|\\natural|\\sharp|\\\||\\angle|\\backslash|\\Box|\\Diamond|\\triangle|\\clubsuit|\\diamondsuit|\\heartsuit|\\spadesuit|\\Join|\\infty)/11/g;
 4301:     $garbage=~s/(\\hat{([^}]+)}|\\check{([^}]+)}|\\dot{([^}]+)}|\\breve{([^}]+)}|\\acute{([^}]+)}|\\ddot{([^}]+)}|\\grave{([^}]+)}|\\tilde{([^}]+)}|\\mathring{([^}]+)}|\\bar{([^}]+)}|\\vec{([^}]+)})/$1/g;
 4302:     #remove some other LaTeX command
 4303:     $garbage=~s|\\(\w+)\\|\\|g;	 
 4304:     $garbage=~s|\\(\w+)(\s*)|$2|g;	 	 
 4305:     $garbage=~s|\+|11|g;
 4306:     my  $value=length($garbage);
 4307:     return $value;
 4308: }
 4309: 
 4310: 
 4311: sub align_latex_image {
 4312:     my ($align, $latex_rendering, $image, $width, $height) = @_;
 4313:     my $currentstring;        # The 1/2 wrapped image.
 4314:     my $closure;              # The closure of the wrappage.
 4315: 
 4316:     # if it's none just return it back
 4317:     if ($latex_rendering eq 'none') {
 4318: 	return ($image,'');
 4319:     }
 4320: 
 4321:     #    If there's an alignment specification we need to honor it here.
 4322:     #    For the horizontal alignments, we will also honor the
 4323:     #    value of the latex specfication.  The default is parbox,
 4324:     #    and that's used for illegal values too.  
 4325:     #    
 4326:     #    Even though we set a default alignment value, the user
 4327:     #    could have given us an illegal value.  In that case we
 4328:     #    just use the default alignment of bottom..
 4329:     $currentstring = '';
 4330:     if      ($align eq "top")    {
 4331: 	$currentstring .= '\raisebox{-'.$height.'mm}{'.$image;
 4332: 	$closure = '}';
 4333:     } elsif (($align eq "center") || ($align eq "middle")) { # Being kind
 4334: 	my $offset = $height/2;
 4335: 	$currentstring .= '\raisebox{-'.$offset.'mm}{'.$image;
 4336: 	$closure       = '}';
 4337:     } elsif ($align eq "left")   { 
 4338: 	if ($latex_rendering eq "parpic") { 
 4339: 	    $currentstring .= '\parpic[l]{'.$image;
 4340: 	    $closure       = '}';
 4341: 	} elsif ($latex_rendering eq "parbox") {
 4342: 	    $currentstring .= '\begin{minipage}[l]{'.$width.'mm}'
 4343: 		.$image;
 4344: 	    $closure = '\end{minipage}';
 4345: 	} elsif ($latex_rendering eq "wrapfigure"
 4346: 		 || $latex_rendering ne 'none') {  # wrapfig render
 4347: 	    $currentstring .= 
 4348: 		'\begin{wrapfigure}{l}{'.$width.'mm}'
 4349: 		.'\scalebox{1.0}{'.$image;
 4350: 	    $closure = '}\end{wrapfigure}';
 4351: 	}
 4352:     } elsif ($align eq "right")  {   
 4353: 	if ($latex_rendering eq "parpic") {
 4354: 	    $currentstring .= '\parpic[r]{'.$image;
 4355: 	    $closure = '}';
 4356: 	} elsif ($latex_rendering eq "parbox") {
 4357: 	    $currentstring .=  '\begin{minipage}[r]{'.$width.'mm}'
 4358: 		.$image;
 4359: 	    $closure = '\end{minipage}';
 4360: 	} elsif ($latex_rendering eq "wrapfigure"
 4361: 		 || $latex_rendering ne 'none') {  # wrapfig render
 4362: 	    $currentstring .= 
 4363: 		'\begin{wrapfigure}{r}{'.$width.'mm}'
 4364: 		.'\scalebox{1.0}{'.$image;
 4365: 	    $closure = '}\end{wrapfigure}';
 4366: 	}
 4367:     } else {		# Bottom is also default.
 4368: 	# $currentstring = '\raisebox{'.$height.'mm}{'.$image.'}';
 4369: 	$currentstring .= "{$image";
 4370: 	$closure       = '}';
 4371:     }
 4372:     return ($currentstring, $closure);
 4373: }
 4374: 
 4375: 
 4376: sub is_inside_of {
 4377:     my ($tagstack, $tag) = @_;
 4378:     my @stack = @$tagstack;
 4379:     for (my $i = ($#stack - 1); $i >= 0; $i--) {
 4380: 	if ($stack[$i] eq $tag) {
 4381: 	    return 1;
 4382: 	}
 4383:     }
 4384:     return 0;
 4385: }
 4386: 
 4387: 
 4388: #
 4389: #   This sub provides the typical LaTeX prefix matter for tex output:
 4390: #
 4391: sub latex_header {
 4392:     my ($mode) = @_;
 4393:     my $currentstring = '';
 4394: 
 4395:     $currentstring .= 
 4396: 	"\n% &Apache::lonxml::londefdef \n" .
 4397: 	'\documentclass[letterpaper,twoside]{article}\raggedbottom';
 4398:     if (($env{'form.latex_type'}=~'batchmode') ||
 4399: 	(!$env{'request.role.adv'}) || 
 4400: 	($mode eq 'batchmode')) {$currentstring .='\batchmode';} 
 4401:     $currentstring .= '\newcommand{\keephidden}[1]{}'.
 4402: 	'\renewcommand{\deg}{$^{\circ}$}'.
 4403: 	'\usepackage{multirow}'.
 4404: 	'\usepackage{longtable}'.
 4405: 	'\usepackage{textcomp}'.
 4406: 	'\usepackage{makeidx}'.
 4407: 	'\usepackage[dvips]{graphicx}'.
 4408: 	'\usepackage{wrapfig}'.
 4409: 	'\usepackage{picins}'.
 4410: 	'\usepackage[T1]{fontenc}'."\n".
 4411: 	'\usepackage{lmodern}'."\n".
 4412: 	'\usepackage[postscript]{ucs}'."\n".
 4413: 	'\usepackage[utf8x]{inputenc}'."\n".
 4414: 	'\usepackage{pifont}' ."\n".
 4415: 	'\usepackage{latexsym}'."\n".
 4416: 	'\usepackage{epsfig}'.
 4417: 	"\\usepackage{xtab}\n".
 4418: 	"\\usepackage{tabularx}\n".
 4419: 	"\\usepackage{booktabs}\n".
 4420: 	"\\usepackage{array}\n".
 4421: 	"\\usepackage{colortbl}\n".
 4422: 	"\\usepackage{xcolor}\n".
 4423: 	'\usepackage{calc}'.
 4424: 	'\usepackage{amsmath}'.
 4425:     '\usepackage{soul}',
 4426: 	'\usepackage{amssymb}'.
 4427: 	'\usepackage{amsfonts}'.
 4428: 	'\usepackage{amsthm}'.
 4429: 	'\usepackage{amscd}'
 4430:         .'\usepackage{picins}\usepackage{calc}'."\n". # From lonprintout.pm
 4431: 	'\usepackage[T1]{fontenc}'."\n".
 4432: 	'\usepackage{lmodern}'."\n".
 4433: 	'\usepackage[postscript]{ucs}'."\n".
 4434: 	'\usepackage[utf8x]{inputenc}'."\n".
 4435: 	'\usepackage{pifont}'  . "\n";
 4436: 	
 4437:     if($env{'form.pdfFormFields'} eq 'yes') {
 4438: 	$currentstring .= '\usepackage{hyperref}'.
 4439: 	    '\usepackage{eforms}'.
 4440: 	    '\usepackage{tabularx}';
 4441:     } 
 4442:     
 4443:         $currentstring .= '\newenvironment{choicelist}{\begin{list}{}{\setlength{\rightmargin}{0in}\setlength{\leftmargin}{0.13in}\setlength{\topsep}{0.05in}\setlength{\itemsep}{0.022in}\setlength{\parsep}{0in}\setlength{\belowdisplayskip}{0.04in}\setlength{\abovedisplayskip}{0.05in}\setlength{\abovedisplayshortskip}{-0.04in}\setlength{\belowdisplayshortskip}{0.04in}}}{\end{list}}'.
 4444:                           '\renewenvironment{theindex}{\begin{list}{}{{\vskip 1mm \noindent \large\textbf{Index}} \newline \setlength{\rightmargin}{0in}\setlength{\leftmargin}{0.13in}\setlength{\topsep}{0.01in}\setlength{\itemsep}{0.1in}\setlength{\parsep}{-0.02in}\setlength{\belowdisplayskip}{0.01in}\setlength{\abovedisplayskip}{0.01in}\setlength{\abovedisplayshortskip}{-0.04in}\setlength{\belowdisplayshortskip}{0.01in}}}{\end{list}}';
 4445:     $currentstring .= '\begin{document}';
 4446:     
 4447:     return $currentstring;
 4448: 
 4449: }
 4450: 
 4451: =pod
 4452: 
 4453: =head1 NAME
 4454: 
 4455: Apache::londefdef.pm
 4456: 
 4457: =head1 SYNOPSIS
 4458: 
 4459: Tags Default Definition Module
 4460: 
 4461: This is part of the LearningOnline Network with CAPA project
 4462: described at http://www.lon-capa.org.
 4463: 
 4464: 
 4465: =head1 NOTABLE SUBROUTINES
 4466: 
 4467: =over
 4468: 
 4469: =item start_hideweboutput()
 4470: 
 4471: =item end_hideweboutput()
 4472: 
 4473: =item image_replication()
 4474: 
 4475: =item resize_image()
 4476: 
 4477: 	Get correct sizing parameter for an image given
 4478: 	it's initial ht. and wid.  This allows sizing of
 4479: 	images that are generated on-the-fly (e.g. gnuplot)
 4480: 	as well as serving as a utility for image_size.
 4481:  
 4482: 	Parameter:
 4483:         height_param
 4484:         width_param    - Initial picture dimensions.
 4485:         scaling        - A scale factor.
 4486:         parstack,      - the current stack of tag attributes 
 4487:                          from the xml parser
 4488:         safeeval,      - pointer to the safespace
 4489:         depth,         - from what level in the stack to look for attributes
 4490:                          (assumes -1 if unspecified)
 4491:         cis            - look for attrubutes case insensitively
 4492:                          (assumes false)
 4493: 
 4494: 	Returns:
 4495: 		height, width   - new dimensions.
 4496: 
 4497: =item image_size()
 4498: 
 4499: =item image_width()
 4500: 
 4501: =item image_height()
 4502: 
 4503: =item get_eps_image()
 4504: 
 4505: =item eps_generation()
 4506: 
 4507: =item file_path()
 4508: 
 4509: =item recalc()
 4510: 
 4511: 	Converts a measurement in to mm from any of 
 4512: 	the other valid LaTeX units of measure.
 4513: 	If the units of measure are missing from the 
 4514: 	parameter, it is assumed to be in and returned
 4515: 	with mm units of measure
 4516: 
 4517: =item LATEX_length()
 4518: 
 4519: =item align_latex_image()
 4520: 
 4521:   	Wrap image 'stuff' inside of the LaTeX required to implement 
 4522:    	alignment:
 4523:      	align_tex_image(align, latex_rendering, image)
 4524:    	Where:
 4525:      	align   - The HTML alignment specification.
 4526:      	latex_rendering - rendering hint for latex.
 4527:      	image   - The LaTeX needed to insert the image itsef.
 4528:      	width,height - dimensions of the image.
 4529:  	Returns:
 4530:     	The 1/2 wrapped image and the stuff required to close the
 4531:     	wrappage.  This allows e.g. randomlabel to insert more stuff
 4532:     	into the closure.
 4533: 
 4534: 
 4535: =item is_inside_of($tagstack, $tag)
 4536:    	This sub returns true if the current state of Xml processing is inside of the tag.   
 4537: 	Parameters:
 4538:     	tagstack   - The tagstack from the parser.
 4539:     	tag        - The tag (without the <>'s.).
 4540: 	Sample usage:
 4541:     	if (is_inside_of($tagstack "table")) {
 4542:      	   I'm in a table....
 4543:      	}
 4544: 
 4545: 
 4546: 
 4547: =back
 4548: 
 4549: =cut
 4550: 
 4551: 
 4552: 1;
 4553: __END__

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>