File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.415.4.6.2.1: download - view: text, annotated - select for diffs
Mon Mar 5 14:41:15 2012 UTC (12 years, 2 months ago) by raeburn
Branches: loncapaMITrelate_1
Diff to branchpoint 1.415.4.6: preferred, unified
- Customization for MITrelate
  - Backport 1.438.

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

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