File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.318: download - view: text, annotated - select for diffs
Sat Feb 4 22:11:42 2006 UTC (18 years, 3 months ago) by foxr
Branches: MAIN
CVS tags: HEAD
defect 4520: When printing a <pre></pre> section latex symbol substitution
must be turned off or else output can look funny e.g.
%  -> \%  <  -> \ensuremath{<} etc.
 ----------------------------------------------------------------------

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

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