File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.415.4.6: download - view: text, annotated - select for diffs
Tue Nov 15 17:34:19 2011 UTC (12 years, 5 months ago) by raeburn
Branches: version_2_10_X
CVS tags: version_2_10_1, loncapaMITrelate_1
Diff to branchpoint 1.415: preferred, unified
- Backport 1.430. (bug 6317).

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

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