File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.409: download - view: text, annotated - select for diffs
Thu May 28 17:08:43 2009 UTC (15 years ago) by bisitz
Branches: MAIN
CVS tags: HEAD
Changes related to modification of LON-CAPA screen header.
Added dynamic link to Construction Space to breadcrumbs

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

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