File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.323: download - view: text, annotated - select for diffs
Thu Mar 23 22:34:53 2006 UTC (18 years, 2 months ago) by foxr
Branches: MAIN
CVS tags: HEAD
Defect 4701 - use \flushleft \flushright for paragraph
positioning since that appears to interact with wrapfig much
better than generating boxes or parboxes.

    1: # The LearningOnline Network with CAPA
    2: # Tags Default Definition Module 
    3: #
    4: # $Id: londefdef.pm,v 1.323 2006/03/23 22:34:53 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.="\n".'{\flushright ';
 1208: #	    $currentstring.='\makebox['.$env{'form.textwidth'}.']{\hfill\llap{';
 1209: 	    $closing_string= "}\n";
 1210: 	} elsif ($align eq 'left') {
 1211: 	    $currentstring.= "\n".'{\flushleft ';
 1212: #	    $currentstring.='\noindent\makebox['.$env{'form.textwidth'}.']{{';
 1213: 	    $closing_string = "}\n";
 1214: 	} else {
 1215:             $currentstring.='\par ';
 1216: 	    if (&is_inside_of($tagstack, 'table')) {
 1217: 		$closing_string = '\vskip 0pt'; # Seems to be consistent with <p> in tables.
 1218: 	    } else {
 1219: 		$closing_string = '\strut\\\\\strut ';
 1220: 	    }
 1221:         }
 1222: 
 1223:     }
 1224:     return $currentstring;
 1225: }
 1226: #
 1227: #  End paragraph processing just requires that we output the
 1228: #  closing string that was saved and blank it.
 1229: sub end_p {
 1230:     #  Note only 'tex' mode uses disable_para and enable_para
 1231:     #  so we don't need to know the target in the check below:
 1232: 
 1233:     if (!$para_disabled) {
 1234: 	my $current_string = $closing_string;
 1235: 	$closing_string = '';	# Not in a para anymore.
 1236: 	return $current_string;
 1237:     } else {
 1238: 	return '';
 1239:     }
 1240: 
 1241: }
 1242: }
 1243: #-- <br> tag (end tag forbidden)
 1244: sub start_br {
 1245:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
 1246:     my $currentstring = '';
 1247:     if ($target eq 'web') {
 1248: 	$currentstring .= $token->[4];
 1249:     } elsif ($target eq 'tex') {
 1250: 	my @tempo=@$tagstack;
 1251: 	my $signal=0;
 1252: 	#  Not going to factor this to is_inside_of since that would require
 1253:         #  multiple stack traversals.
 1254: 	#
 1255: 	for (my $i=$#tempo;$i>=0;$i--) {
 1256: 	    if (($tempo[$i] eq 'b') || ($tempo[$i] eq 'strong') ||
 1257:                 ($tempo[$i] eq 'ol') || ($tempo[$i] eq 'ul') ||
 1258:                 ($tempo[$i] eq 'td') || ($tempo[$i] eq 'th'))  {
 1259: 		$signal=1;
 1260: 		last;
 1261: 	    }
 1262: 	}
 1263: 	if ($signal) {
 1264: 	    $currentstring .= ' \vskip 0 mm ';
 1265: 	} elsif ($$tagstack[-2] ne 'sub' && $$tagstack[-2] ne 'sup') {
 1266: 	    $currentstring .= '\strut \\\\ \strut ';
 1267: 	}
 1268:     }
 1269:     return $currentstring;
 1270: }
 1271: 
 1272: sub end_br {
 1273:     my ($target,$token) = @_;
 1274:     my $currentstring = '';
 1275:     if ($target eq 'web') {
 1276: 	$currentstring .= $token->[2];
 1277:     }
 1278:     return $currentstring;
 1279: }
 1280: 
 1281: #-- <big> tag (end tag required)
 1282: sub start_big {
 1283:     my ($target,$token) = @_;
 1284:     my $currentstring = '';
 1285:     if ($target eq 'web') {
 1286: 	$currentstring .= $token->[4];
 1287:     } elsif ($target eq 'tex') {
 1288: 	$currentstring .= '{\large ';
 1289:     } 
 1290:     return $currentstring;
 1291: }
 1292: 
 1293: sub end_big {
 1294:     my ($target,$token) = @_;
 1295:     my $currentstring = '';
 1296:     if ($target eq 'web') {
 1297: 	$currentstring .= $token->[2];
 1298:     } elsif ($target eq 'tex') {
 1299: 	$currentstring .= '}';
 1300:     }
 1301:     return $currentstring;
 1302: }
 1303: 
 1304: #-- <small> tag (end tag required)
 1305: sub start_small {
 1306:     my ($target,$token) = @_;
 1307:     my $currentstring = '';
 1308:     if ($target eq 'web') {
 1309: 	$currentstring .= $token->[4];
 1310:     } elsif ($target eq 'tex') {
 1311: 	$currentstring .= '{\footnotesize ';
 1312:     }
 1313:     return $currentstring;
 1314: }
 1315: 
 1316: sub end_small {
 1317:     my ($target,$token) = @_;
 1318:     my $currentstring = '';
 1319:     if ($target eq 'web') {
 1320: 	$currentstring .= $token->[2];
 1321:     } elsif ($target eq 'tex') {
 1322: 	$currentstring .= '}';
 1323:     }
 1324:     return $currentstring;
 1325: }
 1326: 
 1327: #-- <basefont> tag (end tag forbidden)
 1328: sub start_basefont {
 1329:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
 1330:     my $currentstring = '';
 1331:     if ($target eq 'web') {
 1332: 	$currentstring = $token->[4];     
 1333:     } elsif ($target eq 'tex') {
 1334: 	my $basesize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval);
 1335: 	if (defined $basesize) {
 1336: 	    $currentstring = '{\\'.$basesize.' ';
 1337: 	}
 1338:     }
 1339:     return $currentstring;
 1340: }
 1341: 
 1342: sub end_basefont {
 1343:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1344:     my $currentstring = '';
 1345:     if ($target eq 'web') {
 1346: 	$currentstring = $token->[4];     
 1347:     } elsif ($target eq 'tex') {
 1348: 	my $basesize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval);
 1349: 	if (defined $basesize) {
 1350: 	    $currentstring = '}';
 1351: 	}
 1352:     }
 1353:     return $currentstring;
 1354: }
 1355: 
 1356: #-- <font> tag (end tag required)
 1357: sub start_font {
 1358:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
 1359:     my $currentstring = '';
 1360:     if ($target eq 'web') {
 1361: 	my $face=&Apache::lonxml::get_param('face',$parstack,$safeeval);
 1362: 	if ($face!~/symbol/i) {
 1363: 	    if (($env{'browser.fontenhance'} eq 'on') || 
 1364: 		($env{'browser.blackwhite'} eq 'on')) { return ''; }
 1365: 	}
 1366: 	$currentstring = $token->[4];     
 1367:     }  elsif ($target eq 'tex') {
 1368: 	my $fontsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval);
 1369: 	if (defined $fontsize) {
 1370: 	    $currentstring = '{\\'.$fontsize.' ';
 1371: 	}
 1372:     }
 1373:     return $currentstring;
 1374: }
 1375: 
 1376: sub end_font {
 1377:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
 1378:     my $currentstring = '';
 1379:     if ($target eq 'web') {
 1380: 	$currentstring = $token->[2];    
 1381:     }  elsif ($target eq 'tex') {
 1382: 	my $fontsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval);
 1383: 	if (defined $fontsize) {
 1384: 	    $currentstring = '}';
 1385: 	}
 1386:     }
 1387:     return $currentstring;
 1388: }
 1389:  
 1390: #-- <strike> tag (end tag required)
 1391: sub start_strike {
 1392:     my ($target,$token) = @_;
 1393:     my $currentstring = '';
 1394:     if ($target eq 'web') {
 1395: 	$currentstring .= $token->[4];
 1396:     } elsif ($target eq 'tex') {
 1397: 	&Apache::lonxml::startredirection();
 1398:     } 
 1399:     return $currentstring;
 1400: }
 1401: 
 1402: sub end_strike {
 1403:     my ($target,$token) = @_;
 1404:     my $currentstring = '';
 1405:     if ($target eq 'web') {
 1406: 	$currentstring .= $token->[2];
 1407:     } elsif ($target eq 'tex') {
 1408: 	$currentstring=&Apache::lonxml::endredirection();
 1409: 	$currentstring=~s/(\S)(\s+)(\S)/$1\}$2\\underline\{$3/g; 
 1410: 	$currentstring=~s/^\s*(\S)/\\underline\{$1/; 
 1411: 	$currentstring=~s/(\S)\s*$/$1\}/;
 1412:     }
 1413:     return $currentstring;
 1414: }
 1415: 
 1416: #-- <s> tag (end tag required)
 1417: sub start_s {
 1418:     my ($target,$token) = @_;
 1419:     my $currentstring = '';
 1420:     if ($target eq 'web') {
 1421: 	$currentstring .= $token->[4];
 1422:     } elsif ($target eq 'tex') {
 1423: 	&Apache::lonxml::startredirection();
 1424:     } 
 1425:     return $currentstring;
 1426: }
 1427: 
 1428: sub end_s {
 1429:     my ($target,$token) = @_;
 1430:     my $currentstring = '';
 1431:     if ($target eq 'web') {
 1432: 	$currentstring .= $token->[2];
 1433:     } elsif ($target eq 'tex') {
 1434: 	$currentstring=&Apache::lonxml::endredirection();
 1435: 	$currentstring=~s/(\S)(\s+)(\S)/$1\}$2\\underline\{$3/g;
 1436: 	$currentstring=~s/^\s*(\S)/\\underline\{$1/;
 1437: 	$currentstring=~s/(\S)\s*$/$1\}/;	
 1438:     }
 1439:     return $currentstring;
 1440: }
 1441: 
 1442: #-- <sub> tag (end tag required)
 1443: sub start_sub {
 1444:     my ($target,$token) = @_;
 1445:     my $currentstring = '';
 1446:     if ($target eq 'web') {
 1447: 	$currentstring .= $token->[4];
 1448:     } elsif ($target eq 'tex') {
 1449: 	$currentstring .= '\ensuremath{_{';
 1450:     } 
 1451:     return $currentstring;
 1452: }
 1453: 
 1454: sub end_sub {
 1455:     my ($target,$token) = @_;
 1456:     my $currentstring = '';
 1457:     if ($target eq 'web') {
 1458: 	$currentstring .= $token->[2];
 1459:     } elsif ($target eq 'tex') {
 1460: 	$currentstring .= '}}';
 1461:     }
 1462:     return $currentstring;
 1463: }
 1464: 
 1465: #-- <sup> tag (end tag required)
 1466: sub start_sup {
 1467:     my ($target,$token) = @_;
 1468:     my $currentstring = '';
 1469:     if ($target eq 'web') {
 1470: 	$currentstring .= $token->[4];
 1471:     } elsif ($target eq 'tex') {
 1472: 	$currentstring .= '\ensuremath{^{';
 1473:     } 
 1474:     return $currentstring;
 1475: }
 1476: 
 1477: sub end_sup {
 1478:     my ($target,$token) = @_;
 1479:     my $currentstring = '';
 1480:     if ($target eq 'web') {
 1481: 	$currentstring .= $token->[2];
 1482:     } elsif ($target eq 'tex') {
 1483: 	$currentstring .= '}}';
 1484:     }
 1485:     return $currentstring;
 1486: }
 1487: 
 1488: #-- <hr> tag (end tag forbidden)
 1489: sub start_hr {
 1490:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1491:     my $currentstring = &end_p();	# End enclosing para.
 1492:     if ($target eq 'web') {
 1493: 	$currentstring .= $token->[4];
 1494:     } elsif ($target eq 'tex') {
 1495: 	my $LaTeXwidth = &Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval,undef,0);
 1496: 	if (defined $LaTeXwidth) {
 1497: 	    if ($LaTeXwidth=~/^%/) {
 1498: 		substr($LaTeXwidth,0,1)='';
 1499: 		$LaTeXwidth=($LaTeXwidth/100).'\textwidth';
 1500: 	    }
 1501: 	} else {
 1502: 	    $LaTeXwidth ='0.9\textwidth';
 1503: 	}
 1504: 	my ($pre,$post);
 1505: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
 1506: 	if (($align eq 'center') || (not defined $align)) {
 1507: 	    $pre=''; $post='';
 1508: 	} elsif ($align eq 'left') {
 1509: 	    $pre='\rlap{'; $post='} \hfill';
 1510: 	} elsif ($align eq 'right') {
 1511: 	    $pre=' \hfill \llap{'; $post='}';
 1512: 	}
 1513: 	$currentstring .= ' \vskip 0 mm \noindent\makebox['.$LaTeXwidth.']{'.$pre.'\makebox['.
 1514:                                     $LaTeXwidth.'][b]{\hrulefill}'.$post.'}\vskip 0 mm ';
 1515:     } 
 1516:     return $currentstring;
 1517: }
 1518: 
 1519: sub end_hr {
 1520:     my ($target,$token) = @_;
 1521:     my $currentstring = '';
 1522:     if ($target eq 'web') {
 1523: 	$currentstring .= $token->[2];
 1524:     }
 1525:     return $currentstring;
 1526: }
 1527: 
 1528: #-- <div> tag (end tag required)
 1529: {
 1530: 
 1531: #  Since div can be nested, the stack below is used
 1532: #  in 'tex' mode to store the ending strings
 1533: #  for the div stack.
 1534: 
 1535:     my @div_end_stack;
 1536: 
 1537: sub start_div {
 1538:     my ($target,$token, $tagstack, $parstack, $parser, $safeeval) = @_;
 1539:     my $currentstring = &end_p();	# Close enclosing para.
 1540:     if ($target eq 'web') {
 1541: 	$currentstring .= $token->[4];
 1542:     } 
 1543:     if ($target eq 'tex') {
 1544: 	# 4 possible alignments: left, right, center, and -missing-.
 1545: 
 1546: 	my $endstring = '';
 1547: 
 1548: 	my $align = lc(&Apache::lonxml::get_param('align', $parstack,
 1549: 						  $safeeval, undef, 1));
 1550: 	if ($align eq 'center') {
 1551: 	    $currentstring .= '\begin{center}';
 1552: 	    $endstring      = '\end{center}';
 1553: 	    if (&is_inside_of($tagstack, "table")) {
 1554: 		$currentstring = &center_correction().$currentstring;
 1555: 	    }
 1556: 	}
 1557: 	elsif ($align eq 'right') {
 1558: 	    $currentstring .= '\begin{flushright}';
 1559: 	    $endstring     .= '\end{flushright}';
 1560: 	} elsif ($align eq 'left') {
 1561: 	    $currentstring .= '\begin{flushleft}';
 1562: 	    $endstring     = '\end{flushleft}';
 1563: 	} else {
 1564: 	
 1565: 	}
 1566: 	$currentstring .= "\n";   # For human readability.
 1567: 	$endstring       = "\n$endstring\n"; # For human readability
 1568: 	push(@div_end_stack, $endstring);
 1569:     }
 1570:     return $currentstring;
 1571: }
 1572: 
 1573: sub end_div {
 1574:     my ($target,$token) = @_;
 1575:     my $currentstring = '';
 1576:     if ($target eq 'web') {
 1577: 	$currentstring .= $token->[2];
 1578:     }
 1579:     if ($target eq 'tex') {
 1580: 	my $endstring = pop @div_end_stack;
 1581: 	$currentstring .= $endstring;
 1582:     }
 1583:     return $currentstring;
 1584: }
 1585: }
 1586: 
 1587: #-- <a> tag (end tag required)
 1588: sub start_a {
 1589:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1590:     my $currentstring = '';
 1591:     if ($target eq 'web') {
 1592: 	my $href=&Apache::lonxml::get_param('href',$parstack,$safeeval,
 1593: 					    undef,1);
 1594: 	$currentstring=&Apache::lonenc::encrypt_ref($token,{'href'=>$href});
 1595:     } elsif ($target eq 'tex') {
 1596: 	my $a=&Apache::lonxml::get_param('href',$parstack,$safeeval,undef,1);
 1597: 	my $b=&Apache::lonxml::get_param('name',$parstack,$safeeval,undef,1);
 1598: 	if ($a=~/\S/) {
 1599: 	    $a=~s/([^\\])%/$1\\\%/g;
 1600: 	    $currentstring .= '\ref{URI: '.$a.'}';
 1601: 	} elsif ($b=~/\S/) {
 1602: 	    $currentstring .= '\ref{Anchor: '.$b.'}';
 1603: 	} else {
 1604: 	    $currentstring.='';
 1605: 	}
 1606:     }
 1607:     return $currentstring;
 1608: }
 1609: 
 1610: sub end_a {
 1611:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1612:     my $currentstring = '';
 1613:     if ($target eq 'web') {
 1614: 	$currentstring .= $token->[2];
 1615:     }
 1616:     return $currentstring;
 1617: }
 1618: 
 1619: #-- <li> tag (end tag optional)
 1620: sub start_li {
 1621:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1622:     my $currentstring = '';
 1623:     if ($target eq 'web') {
 1624: 	$currentstring = $token->[4];     
 1625:     } elsif ($target eq 'tex') {
 1626: 	my $type=&Apache::lonxml::get_param('type',$parstack,$safeeval,undef,0);
 1627: 	my $value=&Apache::lonxml::get_param('value',$parstack,$safeeval,undef,0);
 1628: 	#FIXME need to support types i and I 
 1629: 	if ($type=~/disc/) {
 1630: 	    $currentstring .= ' \item[$\bullet$] ';
 1631: 	} elsif ($type=~/circle/) {
 1632: 	    $currentstring .= ' \item[$\circ$] ';
 1633: 	} elsif ($type=~/square/) {
 1634: 	    $currentstring .= ' \item[$\diamond$] ';
 1635: 	} elsif ($type eq '1') {
 1636: 	    $currentstring .= ' \item['.($Apache::londefdef::list_index+1).'.]';
 1637: 	} elsif ($type eq 'A') {
 1638: 	    $currentstring .= ' \item['.('A'..'Z')[$Apache::londefdef::list_index].'.]';
 1639: 	} elsif ($type eq 'a') {
 1640: 	    $currentstring .= ' \item['.('a'..'z')[$Apache::londefdef::list_index].'.]';
 1641: 	} elsif ($value ne '') {
 1642: 	    $currentstring .= ' \item['.$value.'] ';
 1643: 	} else {
 1644: 	    $currentstring .= ' \item ';
 1645: 	}  
 1646: 	$Apache::londefdef::list_index++;
 1647:     }
 1648:     return $currentstring;
 1649: }
 1650: 
 1651: sub end_li {
 1652:     my ($target,$token) = @_;
 1653:     my $currentstring = &end_p();	# In case there's a <p> in the <li>
 1654:     if ($target eq 'web') {
 1655: 	$currentstring .= $token->[2];     
 1656:     } 
 1657:     return $currentstring;
 1658: }
 1659: 
 1660: #-- <u> tag (end tag required)
 1661: sub start_u {
 1662:     my ($target,$token) = @_;
 1663:     my $currentstring = '';
 1664:     if ($target eq 'web') {
 1665: 	$currentstring .= $token->[4];
 1666:     } elsif ($target eq 'tex') {
 1667: 	&Apache::lonxml::startredirection();
 1668:     } 
 1669:     return $currentstring;
 1670: }
 1671: 
 1672: sub end_u {
 1673:     my ($target,$token) = @_;
 1674:     my $currentstring = '';
 1675:     if ($target eq 'web') {
 1676: 	$currentstring .= $token->[2];
 1677:     } elsif ($target eq 'tex') {
 1678: 	$currentstring=&Apache::lonxml::endredirection();
 1679: 	$currentstring=~s/(\S)(\s+)(\S)/$1\}$2\\underline\{$3/g;
 1680: 	$currentstring=~s/^\s*(\S)/\\underline\{$1/;
 1681: 	$currentstring=~s/(\S)\s*$/$1\}/;		
 1682:     }
 1683:     return $currentstring;
 1684: }
 1685: 
 1686: #-- <ul> tag (end tag required)
 1687: sub start_ul {
 1688:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1689:     my $currentstring = &end_p();	# Close off enclosing list.
 1690:     if ($target eq 'web') {
 1691: 	$currentstring .= $token->[4];     
 1692:     } elsif ($target eq 'tex') {
 1693: 	my $TeXtype=&Apache::lonxml::get_param('type',$parstack,$safeeval,undef,0);
 1694: 	$Apache::londefdef::list_index=0;
 1695: 	if ($TeXtype eq 'disc') {
 1696: 	    $currentstring .= '\renewcommand{\labelitemi}{$\bullet$}'.
 1697:                               '\renewcommand{\labelitemii}{$\bullet$}'. 
 1698:                               '\renewcommand{\labelitemiii}{$\bullet$}'.
 1699:                               '\renewcommand{\labelitemiv}{$\bullet$}';
 1700: 	} elsif ($TeXtype eq 'circle') {
 1701: 	    $currentstring .= '\renewcommand{\labelitemi}{$\circ$}'.
 1702:                               '\renewcommand{\labelitemii}{$\circ$}'. 
 1703:                               '\renewcommand{\labelitemiii}{$\circ$}'.
 1704:                               '\renewcommand{\labelitemiv}{$\circ$}';
 1705: 	} elsif ($TeXtype eq 'square') {
 1706: 	    $currentstring .= '\renewcommand{\labelitemi}{$\diamond$}'.
 1707:                               '\renewcommand{\labelitemii}{$\diamond$}'. 
 1708:                               '\renewcommand{\labelitemiii}{$\diamond$}'.
 1709:                               '\renewcommand{\labelitemiv}{$\diamond$}';
 1710: 	}
 1711: 	$currentstring .= '\strut \begin{itemize}';  
 1712:     } 
 1713:     return $currentstring;
 1714: }
 1715: 
 1716: sub end_ul {
 1717:     my ($target,$token) = @_;
 1718:     my $currentstring = '';
 1719:     if ($target eq 'web') {
 1720: 	$currentstring = $token->[2];     
 1721:     } elsif ($target eq 'tex') {
 1722: 	$currentstring = '\end{itemize} \renewcommand{\labelitemi}{$\bullet$}'.
 1723:                                '\renewcommand{\labelitemii}{$\bullet$}'. 
 1724:                                '\renewcommand{\labelitemiii}{$\bullet$}'.
 1725:                                '\renewcommand{\labelitemiv}{$\bullet$}\strut ';  
 1726:     } 
 1727:     return $currentstring;
 1728: }
 1729: 
 1730: #-- <menu> tag (end tag required)
 1731: sub start_menu {
 1732:     my ($target,$token) = @_;
 1733:     my $currentstring = '';
 1734:     if ($target eq 'web') {
 1735: 	$currentstring = $token->[4];     
 1736:     } elsif ($target eq 'tex') {
 1737: 	$currentstring = " \\begin{itemize} ";  
 1738:     } 
 1739:     return $currentstring;
 1740: }
 1741: 
 1742: sub end_menu {
 1743:     my ($target,$token) = @_;
 1744:     my $currentstring = '';
 1745:     if ($target eq 'web') {
 1746: 	$currentstring = $token->[2];     
 1747:     } elsif ($target eq 'tex') {
 1748: 	$currentstring = " \\end{itemize}";  
 1749:     } 
 1750:     return $currentstring;
 1751: }
 1752: 
 1753: #-- <dir> tag (end tag required)
 1754: sub start_dir {
 1755:     my ($target,$token) = @_;
 1756:     my $currentstring = &end_p();	# In case there's a <p> prior to the list.
 1757:     if ($target eq 'web') {
 1758: 	$currentstring .= $token->[4];     
 1759:     } elsif ($target eq 'tex') {
 1760: 	$currentstring .= " \\begin{itemize} ";  
 1761:     } 
 1762:     return $currentstring;
 1763: }
 1764: 
 1765: sub end_dir {
 1766:     my ($target,$token) = @_;
 1767:     my $currentstring = '';
 1768:     if ($target eq 'web') {
 1769: 	$currentstring = $token->[2];     
 1770:     } elsif ($target eq 'tex') {
 1771: 	$currentstring = " \\end{itemize}";  
 1772:     } 
 1773:     return $currentstring;
 1774: }
 1775: 
 1776: #-- <ol> tag (end tag required)
 1777: sub start_ol {
 1778:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1779:     my $currentstring = &end_p();	# In case there's a <p> prior to the list.
 1780:     if ($target eq 'web') {
 1781: 	$currentstring .= $token->[4];     
 1782:     } elsif ($target eq 'tex') {
 1783: 	$Apache::londefdef::list_index=0;
 1784: 	my $type=&Apache::lonxml::get_param('type',$parstack,$safeeval,undef,0);
 1785: 	if ($type eq '1') {
 1786: 	    $currentstring .= '\renewcommand{\labelenumi}{\arabic{enumi}.}'.
 1787:                               '\renewcommand{\labelenumii}{\arabic{enumii}.}'. 
 1788:                               '\renewcommand{\labelenumiii}{\arabic{enumiii}.}'.
 1789:                               '\renewcommand{\labelenumiv}{\arabic{enumiv}.}';
 1790: 	} elsif ($type eq 'A') {
 1791: 	    $currentstring .= '\renewcommand{\labelenumi}{\Alph{enumi}.}'.
 1792:                               '\renewcommand{\labelenumii}{\Alph{enumii}.}'. 
 1793:                               '\renewcommand{\labelenumiii}{\Alph{enumiii}.}'.
 1794:                               '\renewcommand{\labelenumiv}{\Alph{enumiv}.}';
 1795: 	} elsif ($type eq 'a') {
 1796: 	    $currentstring .= '\renewcommand{\labelenumi}{\alph{enumi}.}'.
 1797:                               '\renewcommand{\labelenumii}{\alph{enumii}.}'.
 1798:                               '\renewcommand{\labelenumiii}{\alph{enumiii}.}'.
 1799:                               '\renewcommand{\labelenumiv}{\alph{enumiv}.}';
 1800: 	} elsif ($type eq 'i') {
 1801: 	    $currentstring .= '\renewcommand{\labelenumi}{\roman{enumi}.}'.
 1802:                               '\renewcommand{\labelenumii}{\roman{enumii}.}'.
 1803:                               '\renewcommand{\labelenumiii}{\roman{enumiii}.}'.
 1804:                               '\renewcommand{\labelenumiv}{\roman{enumiv}.}';
 1805: 	} elsif ($type eq 'I') {
 1806: 	    $currentstring .= '\renewcommand{\labelenumi}{\Roman{enumi}.}'.
 1807:                               '\renewcommand{\labelenumii}{\Roman{enumii}.}'.
 1808:                               '\renewcommand{\labelenumiii}{\Roman{enumiii}.}'.
 1809:                               '\renewcommand{\labelenumiv}{\Roman{enumiv}.}';
 1810: 	}
 1811: 	$currentstring .= '\strut \begin{enumerate}';  
 1812:     } 
 1813:     return $currentstring;
 1814: }
 1815: 
 1816: sub end_ol {
 1817:     my ($target,$token) = @_;
 1818:     my $currentstring = '';
 1819:     if ($target eq 'web') {
 1820: 	$currentstring = $token->[2];     
 1821:     } elsif ($target eq 'tex') {
 1822: 	$currentstring = '\end{enumerate}\renewcommand{\labelenumi}{\arabic{enumi}.}'.
 1823:                                         '\renewcommand{\labelenumii}{\arabic{enumii}.}'.
 1824:                                         '\renewcommand{\labelenumiii}{\arabic{enumiii}.}'.
 1825:                                         '\renewcommand{\labelenumiv}{\arabic{enumiv}.}\strut ';  
 1826:     } 
 1827:     return $currentstring;
 1828: }
 1829: 
 1830: #-- <dl> tag (end tag required)
 1831: sub start_dl {
 1832:     my ($target,$token) = @_;
 1833:     my $currentstring = &end_p();	# In case there's a <p> unclosed prior to the list.
 1834:     if ($target eq 'web') {
 1835: 	$currentstring .= $token->[4];     
 1836:     } elsif ($target eq 'tex') {
 1837: 	$currentstring .= '\begin{description}';
 1838: 	$Apache::londefdef::DL++;
 1839: 	push(@Apache::londefdef::description,[]);
 1840: 	$Apache::londefdef::DD[$Apache::londefdef::DL]=0;
 1841: 	$Apache::londefdef::DT[$Apache::londefdef::DL]=0;
 1842: 	$Apache::londefdef::seenDT[$Apache::londefdef::DL]=0;
 1843:     } 
 1844:     return $currentstring;
 1845: }
 1846: 
 1847: sub end_dl {
 1848:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1849:     my $currentstring = '';
 1850:     if ($target eq 'web') {
 1851: 	$currentstring = $token->[2];     
 1852:     } elsif ($target eq 'tex') {
 1853: 	if ($Apache::londefdef::DT[-1]) { &end_dt(@_); }
 1854: 	if ($Apache::londefdef::DD[-1]) { &end_dd(@_); }
 1855: 	foreach my $element (@{$Apache::londefdef::description[-1]}) {
 1856: 	    $currentstring.=' '.$element.' ';
 1857: 	}
 1858: 	pop(@Apache::londefdef::description);
 1859: 	$currentstring.='\end{description}';  
 1860: 	delete($Apache::londefdef::DD[$Apache::londefdef::DL]);
 1861: 	delete($Apache::londefdef::DT[$Apache::londefdef::DL]);
 1862: 	delete($Apache::londefdef::seenDT[$Apache::londefdef::DL]);
 1863: 	$Apache::londefdef::DL--;
 1864:     } 
 1865:     return $currentstring;
 1866: }
 1867: 
 1868: #-- <dt> tag (end tag optional)
 1869: sub start_dt {
 1870:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1871:     my $currentstring='';
 1872:     if ($target eq 'web') {
 1873: 	$currentstring = $token->[4];     
 1874:     } elsif ($target eq 'tex') {
 1875: 	if ($Apache::londefdef::DT[-1]) { &end_dt(@_); }
 1876: 	if ($Apache::londefdef::DD[-1]) { &end_dd(@_); }
 1877: 	&Apache::lonxml::startredirection();
 1878: 	$Apache::londefdef::DT[-1]++;
 1879: 	$Apache::londefdef::seenDT[-1]=1;
 1880:     } 
 1881:     return $currentstring;
 1882: }
 1883: 
 1884: sub end_dt {
 1885:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1886:     my $currentstring = '';
 1887:     if ($target eq 'web') {
 1888: 	$currentstring = $token->[2];    
 1889:     } elsif ($target eq 'tex') {
 1890: 	if ($Apache::londefdef::DT[-1]) {
 1891: 	    my $data=&item_cleanup();
 1892: 	    push(@{$Apache::londefdef::description[-1]},'\item['.$data.'] \strut \vskip 0mm');
 1893: 	    $Apache::londefdef::DT[-1]--;
 1894: 	}
 1895:     } 
 1896:     return $currentstring;
 1897: }
 1898: 
 1899: sub item_cleanup {
 1900:     my $item=&Apache::lonxml::endredirection();
 1901:     $item=~s/\\begin{center}//g;
 1902:     $item=~s/\\end{center}//g;
 1903:     return $item;
 1904: }
 1905: 
 1906: #-- <dd> tag (end tag optional)
 1907: sub start_dd {
 1908:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1909:     my $currentstring = '';
 1910:     if ($target eq 'web') {
 1911: 	$currentstring = $token->[4];     
 1912:     } elsif ($target eq 'tex') {
 1913: 	if ($Apache::londefdef::DT[-1]) { &end_dt(@_); }
 1914: 	if ($Apache::londefdef::DD[-1]) { &end_dd(@_);}
 1915: 	if (!$Apache::londefdef::seenDT[-1]) {
 1916: 	    push(@{$Apache::londefdef::description[-1]},'\item[\strut] \strut \vskip 0mm ');
 1917: 	}
 1918: 	push(@{$Apache::londefdef::description[-1]},'');
 1919: 	$Apache::londefdef::description[-1]->[-1].=' \strut ';
 1920: 	$Apache::londefdef::DD[-1]++;
 1921: 	&Apache::lonxml::startredirection();
 1922:     } 
 1923:     return $currentstring;
 1924: }
 1925: 
 1926: sub end_dd {
 1927:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1928:     my $currentstring = '';
 1929:     if ($target eq 'web') {
 1930: 	$currentstring = $token->[2];    
 1931:     }  elsif ($target eq 'tex') {
 1932: 	$Apache::londefdef::description[-1]->[-1].=
 1933: 	    &Apache::lonxml::endredirection().' \vskip 0mm ';
 1934: 	$Apache::londefdef::DD[-1]--;
 1935:     }
 1936:     return $currentstring;
 1937: }
 1938: 
 1939: #-- <table> tag (end tag required)
 1940: #       <table> also ends any prior <p> that is not closed.
 1941: #               but, unless I allow <p>'s to nest, that's the
 1942: #               only way I could think of to allow <p> in 
 1943: #               <tr> <th> bodies
 1944: #
 1945: #list of supported attributes: border,width,TeXwidth
 1946: sub start_table {
 1947:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1948:     my $textwidth = '';
 1949:     my $currentstring = &end_p();
 1950:     if ($target eq 'web') {
 1951: 	$currentstring .= $token->[4];     
 1952:     } elsif ($target eq 'tex') {
 1953: 	my $aa = {};
 1954: 	push @Apache::londefdef::table, $aa; 
 1955: 	$Apache::londefdef::table[-1]{'row_number'} = -1;
 1956:         #maximum table's width (default coincides with text line length)
 1957: 	if ($#Apache::londefdef::table==0) {
 1958: 	    $textwidth=&recalc($env{'form.textwidth'}); #result is always in mm
 1959: 	    $textwidth=~/(\d+\.?\d*)/;
 1960: 	    $textwidth=0.95*$1; #accounts "internal" LaTeX space for table frame
 1961: 	} else {
 1962: 	    if ($Apache::londefdef::table[-2]{'TeXlen'}[$Apache::londefdef::table[-2]{'row_number'}][$Apache::londefdef::table[-2]{'counter_columns'}]=~/\d/) {
 1963: 		#the maximum width of nested table is determined by LATeX width of parent cell
 1964: 		$textwidth=$Apache::londefdef::table[-2]{'TeXlen'}[$Apache::londefdef::table[-2]{'row_number'}][$Apache::londefdef::table[-2]{'counter_columns'}]; 
 1965: 	    } else {
 1966:               #try to use all space not used before (minus 5% for LaTeX table internal) - rather silly
 1967: 		$textwidth=$Apache::londefdef::table[-2]{'width'};
 1968: 		for (my $i=0;$i<$Apache::londefdef::table[-2]{'counter_columns'};$i++) {
 1969: 		    $textwidth=$textwidth-$Apache::londefdef::table[-2]{'TeXlen'}[0][$i];
 1970: 		}
 1971: 	    }
 1972: 	}
 1973: 
 1974: 	# width either comes forced from the TeXwidth or the width parameters.
 1975: 	# in either case it can be a percentage or absolute width.
 1976: 	# in the width case we ignore absolute width 
 1977: 	my $TeXwidth = &Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval,undef,0);
 1978: 	if (!defined($TeXwidth)) {
 1979: 	    my $htmlwidth = &Apache::lonxml::get_param('width',$parstack,
 1980: 						       $safeeval,undef,1);
 1981: 	    if ($htmlwidth =~ /%/) {
 1982: 		$TeXwidth = $htmlwidth;
 1983: 	    } else { 
 1984: 		$TeXwidth = $textwidth;
 1985: 	    }
 1986: 	} else {
 1987: 	    $Apache::londefdef::table[-1]{'forcedtablewidth'} = 1;
 1988: 	}
 1989: 	if ($TeXwidth=~/%/) {
 1990: 	    $Apache::londefdef::table[-1]{'percent'}=1;
 1991: 	    $TeXwidth=~/(\d+)/;
 1992:             $Apache::londefdef::table[-1]{'width'}=$1*$textwidth/100;
 1993: 	} else {
 1994: 	    $Apache::londefdef::table[-1]{'width'}=$TeXwidth;
 1995: 	}
 1996:         #  In the end, however the table width cannot be wider than $textwidth...
 1997: 	
 1998: 	if ($Apache::londefdef::table[-1]{'width'} > $textwidth) {
 1999: 	    $Apache::londefdef::table[-1]{'width'} = $textwidth;
 2000: 	}
 2001: 
 2002:         #table's border
 2003: 	my $border = &Apache::lonxml::get_param('border',$parstack,$safeeval); 
 2004:         my $permission=&Apache::lonxml::get_param('TeXDropEmptyColumns',$parstack,$safeeval,undef,0);
 2005: 	unless (defined $border) { $border = 0; }
 2006: 	if ($border) { 
 2007: 	    $Apache::londefdef::table[-1]{'hinc'} = '\hline '; 
 2008: 	    $Apache::londefdef::table[-1]{'vinc'} = '&'; 
 2009: 	    $Apache::londefdef::table[-1]{'vvinc'} = '|';
 2010: 	} else {
 2011: 	    $Apache::londefdef::table[-1]{'hinc'} = ''; 
 2012: 	    $Apache::londefdef::table[-1]{'vinc'} = '&'; 
 2013: 	    $Apache::londefdef::table[-1]{'vvinc'} = '';
 2014: 	}
 2015: 	if ($#Apache::londefdef::table==0) {
 2016: 	    #    Note that \newline seems to destroy the alignment envs.
 2017: 	    # $Apache::londefdef::table[-1]{'output'}='\strut\newline\strut\setlength{\tabcolsep}{1 mm}';
 2018: 	    $Apache::londefdef::table[-1]{'output'}='\strut'.'\\\\'."\n".'\strut\setlength{\tabcolsep}{1 mm}';
 2019: 	}
 2020: 	$Apache::londefdef::table[-1]{'output'}.=' \noindent \begin{tabular} ';
 2021:         $Apache::londefdef::table[-1]{'TeXlen'}=[];
 2022:         $Apache::londefdef::table[-1]{'objectlen'}=[];
 2023:         $Apache::londefdef::table[-1]{'objectsignal'}=[];
 2024:         $Apache::londefdef::table[-1]{'maxlen'}=[];
 2025:         $Apache::londefdef::table[-1]{'minlen'}=[];
 2026:         $Apache::londefdef::table[-1]{'content'}=[];
 2027:         $Apache::londefdef::table[-1]{'align'}=[];
 2028:         $currentstring.='\keephidden{NEW TABLE ENTRY}';
 2029: 
 2030: 
 2031:     }
 2032:     return $currentstring;
 2033: }
 2034:  
 2035: sub end_table {
 2036:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2037:     my $currentstring = '';
 2038:     if ($target eq 'web') {
 2039: 	$currentstring = $token->[2];     
 2040:     } elsif ($target eq 'tex') {
 2041: 	my $inmemory = '';
 2042: 	my $output = '';
 2043: 	my $WARNING='';
 2044:         #width of columns from TeXwidth attributes
 2045: 
 2046: 	for (my $in=0;$in<=$Apache::londefdef::table[-1]{'row_number'};$in++) {
 2047: 	    for (my $jn=0;$jn<=$Apache::londefdef::table[-1]{'counter_columns'};$jn++) {
 2048: 		if ($Apache::londefdef::table[-1]{'TeXlen'}[0][$jn]<$Apache::londefdef::table[-1]{'TeXlen'}[$in][$jn]) {
 2049: 		    $Apache::londefdef::table[-1]{'TeXlen'}[0][$jn]=$Apache::londefdef::table[-1]{'TeXlen'}[$in][$jn];
 2050: 		}	
 2051: 	    }
 2052: 	}
 2053:         #free space and number of empty columns
 2054: 	my ($available_space,$empty_columns)=($Apache::londefdef::table[-1]{'width'},0);
 2055: 	if ($#Apache::londefdef::table ne 0) {$available_space=0.9*$available_space;} 
 2056: 	for (my $jn=0;$jn<=$Apache::londefdef::table[-1]{'counter_columns'};$jn++) {
 2057: 	    if ($Apache::londefdef::table[-1]{'TeXlen'}[0][$jn]==0) {
 2058: 		$empty_columns++;
 2059: 	    } else {
 2060: 		$available_space=$available_space-$Apache::londefdef::table[-1]{'TeXlen'}[0][$jn];
 2061: 	    }
 2062: 	}
 2063:         #boundaries for contents columns
 2064: 	my @min_len=();#columns can not be narrower 
 2065: 	my @max_len=();#maximum length of column
 2066: 	for (my $jn=0;$jn<=$Apache::londefdef::table[-1]{'counter_columns'};$jn++) {
 2067: 		my ($localmin,$localmax)=(0,0);
 2068: 		for (my $in=0;$in<=$Apache::londefdef::table[-1]{'row_number'};$in++) {
 2069: 		    if ($localmin<$Apache::londefdef::table[-1]{'minlen'}[$in][$jn]) {
 2070: 			$localmin=$Apache::londefdef::table[-1]{'minlen'}[$in][$jn];
 2071: 		    }
 2072: 		    if ($localmax<$Apache::londefdef::table[-1]{'maxlen'}[$in][$jn]) {
 2073: 			$localmax=$Apache::londefdef::table[-1]{'maxlen'}[$in][$jn];
 2074: 		    }
 2075: 		}
 2076: 		push @min_len, $localmin;
 2077: 		push @max_len, $localmax;
 2078: 	}
 2079: 	for (my $jn=0;$jn<=$Apache::londefdef::table[-1]{'counter_columns'};$jn++) {
 2080: 	    my $localmin=0,;
 2081: 	    for (my $in=0;$in<=$Apache::londefdef::table[-1]{'row_number'};$in++) {
 2082: 		if ($localmin<$Apache::londefdef::table[-1]{'objectlen'}[$in][$jn]) {
 2083: 		    $localmin=$Apache::londefdef::table[-1]{'objectlen'}[$in][$jn];
 2084: 		}
 2085: 	    }
 2086: 	    if ($max_len[$jn]<$localmin) {
 2087: 		$max_len[$jn]=$localmin;
 2088: 	    	$Apache::londefdef::table[-1]{'objectsignal'}[$jn]=1;
 2089: 	    }#object size is bigger
 2090: 	    if ($min_len[$jn]<$localmin) {
 2091: 		$min_len[$jn]=$localmin;
 2092: 		$Apache::londefdef::table[-1]{'objectsignal'}[$jn]=1;
 2093: 	    }#object size is bigger
 2094: 	    if ($Apache::londefdef::table[-1]{'TeXlen'}[0][$jn]!=0) {
 2095: 		$min_len[$jn]=0;
 2096: 		$max_len[$jn]=0;
 2097: 	    }
 2098: 	}
 2099:        #final adjustment of column width
 2100: 	my @fwidth=@{$Apache::londefdef::table[-1]{'TeXlen'}[0]};#final width array
 2101: 	my @adjust=();
 2102:         #step 1. adjustment by maximum value
 2103: 	my $space_neeeded=0;
 2104: 	for (my $jn=0;$jn<=$#max_len;$jn++) {
 2105: 	    $space_neeeded=$space_neeeded+$max_len[$jn];
 2106: 	}
 2107: 	if ($space_neeeded<=$available_space) {
 2108: 	    for (my $jn=0;$jn<=$#max_len;$jn++) {
 2109: 		if ($fwidth[$jn]==0) {
 2110: 		    $fwidth[$jn]=$max_len[$jn];
 2111: 		}
 2112: 	    }
 2113: 	} else {
 2114:         #step 2. adjustment by minimum value (estimation)
 2115: 	    $space_neeeded=0;
 2116: 	    for (my $jn=0;$jn<=$#min_len;$jn++) {
 2117: 		$space_neeeded+=$min_len[$jn];
 2118: 	    }
 2119: 	    if ($space_neeeded>$available_space) {
 2120: 		$WARNING=' \textbf{NOT ENOUGH SPACE FOR TABLE} ';
 2121: 		for (my $jn=0;$jn<=$#max_len;$jn++) {
 2122: 		    if ($fwidth[$jn]==0) {
 2123: 			$fwidth[$jn]=$min_len[$jn];
 2124: 		    }
 2125: 		}
 2126: 		#check if we have objects which can be scaled
 2127: 		my $how_many_to_scale=0;
 2128: 		my @to_scale=();
 2129: 		for (my $jn=0;$jn<=$#max_len;$jn++) {
 2130: 		    if ($Apache::londefdef::table[-1]{'objectsignal'}[$jn] eq '1') {
 2131: 			$how_many_to_scale++;
 2132: 			push @to_scale, $jn;
 2133: 		    }
 2134: 		}
 2135: 		if ($how_many_to_scale>0) {
 2136: 		    my $space_to_adjust=($space_neeeded-$available_space)/$how_many_to_scale;
 2137: 		    foreach my $jn (@to_scale) {
 2138: 			for (my $in=0;$in<=$Apache::londefdef::table[-1]{'row_number'};$in++) {
 2139: 			    $Apache::londefdef::table[-1]{'content'}[$in][$jn]=~m/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)/;
 2140: 			    if ($1 ne '') {
 2141: 				my $current_length=&recalc($1);
 2142: 				$current_length=~/(\d+\.?\d*)/;
 2143: 				$current_length=$current_length-$space_to_adjust;
 2144: 				$Apache::londefdef::table[-1]{'content'}[$in][$jn]=~s/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)/width=$current_length mm/;
 2145: 			    }
 2146: 			    $Apache::londefdef::table[-1]{'content'}[$in][$jn]=~m/\[(\d+\.?\d*)\s*mm\]/;
 2147: 			    if ($1 ne '') {
 2148: 				my $current_length=$1;
 2149: 				$current_length=$current_length-$space_to_adjust;
 2150: 				$Apache::londefdef::table[-1]{'content'}[$in][$jn]=~s/\[(\d+\.?\d*)\s*mm\]/\[$current_length mm\]/;
 2151: 			    }				
 2152: 			}
 2153: 			$fwidth[$jn]=$fwidth[$jn]-$space_to_adjust;
 2154: 		    }
 2155: 		}
 2156: 	    } else {
 2157: 	      #step 3. adjustment over minimal + corrections
 2158: 		my $enlarge_coef=$available_space/$space_neeeded;
 2159: 		my $acsessive=0;
 2160: 		for (my $jn=0;$jn<=$#min_len;$jn++) {
 2161: 		    $adjust[$jn]=$min_len[$jn]*$enlarge_coef;
 2162: 		    if ($adjust[$jn]>$max_len[$jn]) {
 2163: 			$fwidth[$jn]=$max_len[$jn];
 2164: 			$acsessive=$acsessive+$adjust[$jn]-$max_len[$jn];
 2165: 			$adjust[$jn]=0;
 2166: 		    }
 2167: 		}
 2168: 		if ($acsessive>0) {
 2169: 		#we have an excess of space and can redistribute it
 2170: 		    my $notempty_columns=0;
 2171: 		    for (my $jn=0;$jn<=$#min_len;$jn++) {
 2172: 			if ($adjust[$jn]!=0) {
 2173: 			    $notempty_columns++;
 2174: 			}
 2175: 		    }
 2176: 		    my $per_column=$acsessive/$notempty_columns;
 2177: 		    for (my $jn=0;$jn<=$#min_len;$jn++) {
 2178: 			if ($adjust[$jn]!=0) {
 2179: 			    $adjust[$jn]+=$per_column;
 2180: 			    $fwidth[$jn]=$adjust[$jn];
 2181: 			}
 2182: 		    }
 2183: 		} else {
 2184: 		    for (my $jn=0;$jn<=$#min_len;$jn++) {
 2185: 			$fwidth[$jn]=$adjust[$jn];
 2186: 		    }
 2187: 		}
 2188: 	    }
 2189: 	}
 2190:         #use all available width if it is defined in % or as TeXwidth
 2191:         if (($Apache::londefdef::table[-1]{'percent'}==1) || ($Apache::londefdef::table[-1]{'forcetablewidth'}==1)) {
 2192: 	    my $current=0; 
 2193: 	    for (my $i=0;$i<=$#fwidth;$i++) {  
 2194: 		$current+=$fwidth[$i];
 2195: 	    }
 2196: 	    my $coef=$Apache::londefdef::table[-1]{'width'}/$current;
 2197: 	    for (my $i=0;$i<=$#fwidth;$i++) {  
 2198: 		$fwidth[$i]*=$coef;
 2199: 	    }
 2200: 	}
 2201:         #removing of empty columns if allowed
 2202:         my $permission=&Apache::lonxml::get_param('TeXDropEmptyColumns',$parstack,$safeeval,undef,0);
 2203: 	if ($permission eq 'yes') {
 2204: 	    my @cleaned_table=();
 2205:             my @cleaned_header=();
 2206: 	    my $colind=0;
 2207: 	    for (my $jn=0;$jn<=$Apache::londefdef::table[-1]{'counter_columns'};$jn++) {
 2208: 		if ($fwidth[$jn]!=0) {
 2209: 		    #we need to copy column
 2210: 		    for (my $in=0;$in<=$Apache::londefdef::table[-1]{'row_number'};$in++) {
 2211: 			$cleaned_table[$in][$colind]=$Apache::londefdef::table[-1]{'content'}[$in][$jn];
 2212: 			$cleaned_header[$colind]=$fwidth[$jn];
 2213: 		    }
 2214: 		    $colind++;
 2215: 		}
 2216: 	    }
 2217: 	    $Apache::londefdef::table[-1]{'content'}=\@cleaned_table;
 2218: 	    @fwidth=@cleaned_header;
 2219: 	}
 2220: 	#construct header of the table
 2221: 	my $header_of_table = '{'.$Apache::londefdef::table[-1]{'vvinc'};
 2222: 	for (my $in=0;$in<=$#fwidth;$in++) {
 2223: 	    $header_of_table.='p{'.$fwidth[$in].' mm}'.$Apache::londefdef::table[-1]{'vvinc'};
 2224: 	}
 2225: 	$header_of_table .= '}';
 2226: 	#fill the table
 2227: 	for (my $in=0;$in<=$Apache::londefdef::table[-1]{'row_number'};$in++) {
 2228: 	    my $have_rowspan = 0;
 2229: 	    for (my $jn=0;$jn<=$#fwidth;$jn++) {
 2230: 
 2231: 		#-----------------------------------------------------------
 2232:                 #   I think this order of doing things will ensure that
 2233: 		#   single rowspan, columspan and combined row/colspans will
 2234:                 #   work correctly.  LaTeX is delicate here.
 2235: 		#    RF.
 2236: 
 2237: 		# Start a rowspan if necessary:
 2238: 
 2239: 		my $rowspan = $Apache::londefdef::table[-1]{'rowspan'}[$in][$jn];
 2240: 		my $colspan = $Apache::londefdef::table[-1]{'colspan'}[$in][$jn];
 2241: 		#
 2242: 		#  Do the appropriate magic if this has a colspan
 2243: 		# 
 2244: 
 2245: 		if ($colspan > 1) {
 2246: 		    $output .= '\multicolumn{'.
 2247: 			$colspan
 2248: 			.'}{|l|}{';
 2249: 		}
 2250: 
 2251: 		if ($rowspan > 1) {
 2252: 		    $have_rowspan++;
 2253: 		    $output .= '\multirow{'.$rowspan.'}[0]{'.$fwidth[$jn].'mm}{';
 2254: 		}
 2255: 		if (($rowspan eq '^') || ($rowspan eq '_')) {
 2256: 		    $have_rowspan++;
 2257: 		}
 2258: 		#--------------------------------------------------------------
 2259: 
 2260: 		if ($Apache::londefdef::table[-1]{'align'}[$in][$jn] eq 'c') {
 2261: 		    $output.=&center_correction().'\begin{center}';
 2262: 		} elsif ($Apache::londefdef::table[-1]{'align'}[$in][$jn] eq 'r') {
 2263: 		    $output.=' \hfill \llap{'
 2264: 		}
 2265: 		$output.=$Apache::londefdef::table[-1]{'content'}[$in][$jn];
 2266: 		if ($Apache::londefdef::table[-1]{'align'}[$in][$jn] eq 'c') {
 2267: 		    $output.='\end{center}';
 2268: 		} elsif ($Apache::londefdef::table[-1]{'align'}[$in][$jn] eq 'r') {
 2269: 		    $output.='} ';
 2270: 		}
 2271: 		# Close off any open multirow:
 2272: 		
 2273: 		if ($rowspan > 1) {
 2274: 		    $output .= '}';
 2275: 		}
 2276: 		#  Close off the colspan...
 2277: 		#
 2278: 		if ($colspan > 1)  {
 2279: 		    $output .= '}';
 2280: 		    $jn += $colspan-1; # Adjust for number of rows really left.
 2281: 		}
 2282:                 if ($jn!=$#fwidth) {$output.=' '.$Apache::londefdef::table[-1]{'vinc'};}
 2283: 	    }
 2284: 	    #  If have_rowspan > 0, and borders are on, then 
 2285: 	    #  we need to do more than put an \hline at the bottom of row.
 2286: 	    #  we need to do the appropriate \cline to ensure that
 2287: 	    #  the spanned rows don't have \hlines through them.
 2288: 
 2289: 	    if (($Apache::londefdef::table[-1]{'hinc'} =~ /\\hline/) && $have_rowspan) {
 2290: 		$output .= ' \\\\ ';
 2291: 		for (my $jn=0; $jn<=$#fwidth;$jn++) {
 2292: 		    my $rowspan = $Apache::londefdef::table[-1]{'rowspan'}[$in][$jn];
 2293: 		    if ($rowspan ne "^") {
 2294: 			if (($rowspan <= 1) || ($rowspan eq '_')) {
 2295: 			    my $column = $jn+1;
 2296: 			    $output .= '\cline{'.$column.'-'.$column.'} ';
 2297: 			}
 2298: 		    }
 2299: 		}
 2300: 
 2301: 	    } else {
 2302: 		$output.=' \\\\ '.$Apache::londefdef::table[-1]{'hinc'}.' ';
 2303: 	    }
 2304: 	}
 2305: 	# Note that \newline destroys alignment env's produced  by e.g. <div>
 2306: 	# $Apache::londefdef::table[-1]{'output'} .= $header_of_table.$Apache::londefdef::table[-1]{'hinc'}.$output.'\end{tabular}\strut\newline\strut ';
 2307: 	$Apache::londefdef::table[-1]{'output'} .= $header_of_table.$Apache::londefdef::table[-1]{'hinc'}.$output.'\end{tabular}\strut'.'\\\\'."\n".'\strut ';
 2308: 	if ($#Apache::londefdef::table > 0) {	    
 2309: 	    my $inmemory = $Apache::londefdef::table[-1]{'output'};
 2310: 	    # Figure out max/and min width  by summing us and then
 2311: 	    # apply that to the current column of the table we nest in
 2312: 	    # if it's larger than the current width or the current width
 2313: 	    # is undefined.
 2314: 	    #
 2315: 	    my $min_nested_width = 0;
 2316: 	    my $max_nested_width = 0;
 2317: 	    for (my $col = 0; $col <= $Apache::londefdef::table[-1]{'counter_columns'}; $col++) {
 2318: 		$min_nested_width +=  $min_len[$col];
 2319: 		$max_nested_width +=  $max_len[$col];
 2320: 		
 2321: 	    }
 2322: 	    # Fudge in an extra 5 mm for borders etc:
 2323: 	    
 2324: 	    $min_nested_width += 5;
 2325: 	    $max_nested_width += 5;
 2326: 
 2327: 	    my $outer_column = $Apache::londefdef::table[-2]{'counter_columns'};
 2328: 	    my $outer_row    = $Apache::londefdef::table[-2]{'row_number'};
 2329: 	    if ($min_nested_width > $Apache::londefdef::table[-2]{'minlen'}[$outer_row][$outer_column]) {
 2330: 		$Apache::londefdef::table[-2]{'minlen'}[$outer_row][$outer_column] = $min_nested_width;
 2331: 	    }
 2332: 	    if ($max_nested_width > $Apache::londefdef::table[-2]{'maxlen'}[$outer_row][$outer_column]) {
 2333: 		$Apache::londefdef::table[-2]{'maxlen'}[$outer_row][$outer_column] = $max_nested_width;
 2334: 	    }
 2335: 
 2336: 	    pop @Apache::londefdef::table;
 2337: 	    push @{$Apache::londefdef::table[-1]{'include'}}, $inmemory;
 2338: 	} else {
 2339: 	    $currentstring .= $Apache::londefdef::table[-1]{'output'};
 2340: 	    pop @Apache::londefdef::table;
 2341: 	    undef @Apache::londefdef::table;
 2342: 	}
 2343:     }
 2344:     return $currentstring;
 2345: }
 2346: 
 2347: #-- <tr> tag (end tag optional)
 2348: sub start_tr {
 2349:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2350:     my $currentstring = '';
 2351:     if ($target eq 'web') {
 2352: 	$currentstring = $token->[4];     
 2353:     } elsif ($target eq 'tex') {
 2354: 	$Apache::londefdef::table[-1]{'row_number'}++;
 2355: 	my $alignchar=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
 2356: 	if ($alignchar ne '') {
 2357: 	    push @ {$Apache::londefdef::table[-1]{'rows'} },substr($alignchar,0,1);
 2358: 	} else {
 2359: 	    push @ {$Apache::londefdef::table[-1]{'rows'} }, 'l';
 2360: 	}
 2361: 	push ( @{ $Apache::londefdef::table[-1]{'rowdata'} }, $Apache::londefdef::table[-1]{'hinc'});
 2362: 	#
 2363: 	#  Need to save the number of table columns to preserve the max # columns.
 2364: 	#
 2365: 	$Apache::londefdef::table[-1]{'prior_columns'}   = $Apache::londefdef::table[-1]{'counter_columns'};
 2366: 	$Apache::londefdef::table[-1]{'counter_columns'} = -1;
 2367: 	push @ {$Apache::londefdef::table[-1]{'TeXlen'}}, [];
 2368: 	push @ {$Apache::londefdef::table[-1]{'objectlen'}}, [];
 2369: 	push @ {$Apache::londefdef::table[-1]{'minlen'}}, [];
 2370: 	push @ {$Apache::londefdef::table[-1]{'maxlen'}}, [];
 2371: 	push @ {$Apache::londefdef::table[-1]{'content'}}, [];
 2372:     } 
 2373:     return $currentstring;
 2374: }
 2375:         
 2376: sub end_tr {
 2377:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2378:     my $currentstring = &end_p();	# Close any pending <p> in the row.
 2379:     if ($target eq 'web') {
 2380: 	$currentstring .= $token->[2];     
 2381:     } elsif ($target eq 'tex') {
 2382: 	if ($Apache::londefdef::TD_redirection) {
 2383: 	    &end_td_tex($parstack,$parser,$safeeval);    
 2384: 	}
 2385: 	# Counter columns must be the maximum number of columns seen
 2386: 	# in the table so far so:
 2387: 	if ($Apache::londefdef::table[-1]{'prior_columns'} > $Apache::londefdef::table[-1]{'counter_columns'}) {
 2388: 	    $Apache::londefdef::table[-1]{'counter_columns'} = $Apache::londefdef::table[-1]{'prior_columns'};
 2389: 	}
 2390: 
 2391: 
 2392: 	
 2393:     }
 2394:     return $currentstring;
 2395: }
 2396: 
 2397: #-- <td> tag (end tag optional)
 2398: sub start_td {
 2399:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2400:     my $currentstring = '';
 2401:     if ($target eq 'web') {
 2402: 	$currentstring = $token->[4];     
 2403:     } elsif ($target eq 'tex') {
 2404: 	$Apache::londefdef::TD_redirection = 1;
 2405: 	&tag_check('tr','td',$tagstack,$parstack,$parser,$safeeval);
 2406:     } 
 2407:     return $currentstring;
 2408: }   
 2409:     
 2410: sub tag_check {
 2411:     my ($good_tag,$bad_tag,$tagstack,$parstack,$parser,$safeeval) = @_;
 2412:     my @ar=@$parstack; 
 2413:     for (my $i=$#ar-1;$i>=0;$i--) {
 2414: 	if (lc($$tagstack[$i]) eq $good_tag) {
 2415: 	    &start_td_tex($parstack,$parser,$safeeval);
 2416: 	    last;
 2417: 	} elsif (lc($$tagstack[$i]) eq $bad_tag) {
 2418: 	    splice @ar, $i+1;
 2419: 	    &end_td_tex(\@ar,$parser,$safeeval);
 2420: 	    &start_td_tex($parstack,$parser,$safeeval);
 2421: 	    last;
 2422: 	}
 2423:     }
 2424:     return '';
 2425: }
 2426:  
 2427: sub start_td_tex {
 2428:     my ($parstack,$parser,$safeeval) = @_;
 2429:     my $alignchar = substr(&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1),0,1);
 2430:     if ($alignchar eq '') {
 2431: 	$alignchar = $Apache::londefdef::table[-1]{'rows'}[-1];
 2432:     }
 2433:     push @{ $Apache::londefdef::table[-1]{'align'}[$Apache::londefdef::table[-1]{'row_number'}] }, $alignchar;
 2434:     $Apache::londefdef::table[-1]{'counter_columns'}++;
 2435:     my $TeXwidth=&Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval,undef,0);
 2436:     if (defined $TeXwidth) {		
 2437: 	my $current_length=&recalc($TeXwidth);
 2438: 	$current_length=~/(\d+\.?\d*)/;
 2439: 	push @ {$Apache::londefdef::table[-1]{'TeXlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$1;
 2440:     }
 2441:     &Apache::lonxml::startredirection();
 2442:     return '';
 2443: }
 2444: 
 2445: sub end_td_tex {
 2446:     my ($parstack,$parser,$safeeval) = @_;
 2447:     my $current_row    = $Apache::londefdef::table[-1]{'row_number'};
 2448:     my $current_column = $Apache::londefdef::table[-1]{'counter_columns'}; 
 2449:     my $data = &Apache::lonxml::endredirection();
 2450: 
 2451:     #  The rowspan array of the table indicates which cells are part of a span.
 2452:     #  n indicates the start of a span set of n rows.
 2453:     #  ^ indicates a cell that continues a span set.
 2454:     #  _ indicates the cell is at the bottom of a span set.
 2455:     #  If this and subsequent cells are part of a rowspan, we must
 2456:     #  push along the row until we find one that is not.
 2457: 
 2458:     while ((defined $Apache::londefdef::table[-1]{'rowspan'}[$current_row] [$current_column]) 
 2459: 	   && ($Apache::londefdef::table[-1]{'rowspan'}[$current_row][$current_column] =~ /[\^\_]/)) {
 2460: 	# Part of a span.
 2461: 	push @ {$Apache::londefdef::table[-1]{'content'}[-1]}, '';
 2462: 	$current_column++;
 2463:     }
 2464:     $Apache::londefdef::table[-1]{'counter_columns'} = $current_column;
 2465:    
 2466: 
 2467:     # Get the column and row spans.
 2468:     # Colspan can be done via \multicolumn if I can figure out the data structs.
 2469: 
 2470:     my $colspan = &Apache::lonxml::get_param('colspan', $parstack, $safeeval, undef, 0);
 2471:     if (!$colspan) {
 2472: 	$colspan = 1;
 2473:     }
 2474: 
 2475:     my $rowspan = &Apache::lonxml::get_param('rowspan', $parstack, $safeeval, undef, 0);
 2476:     if (!$rowspan) {
 2477: 	$rowspan = 1;
 2478:     }
 2479: 
 2480: 
 2481: 
 2482:     for (my $c = 0; $c < $colspan; $c++) {
 2483: 	$Apache::londefdef::table[-1]{'rowspan'}[$current_row][$current_column+$c] = $rowspan;
 2484: 	for (my $i = 1; $i < $rowspan; $i++) {
 2485: 	    $Apache::londefdef::table[-1]{'rowspan'}[$current_row+$i][$current_column+$c] = '^';
 2486: 	    if ($i == ($rowspan-1)) {
 2487: 		$Apache::londefdef::table[-1]{'rowspan'}[$current_row+$i][$current_column+$c] = '_';
 2488: 	    }
 2489: 	}
 2490:     }
 2491: 
 2492:     my $TeXwidth=&Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval,undef,0);
 2493:     if (defined $TeXwidth) {		
 2494: 	push @ {$Apache::londefdef::table[-1]{'objectlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2495: 	push @ {$Apache::londefdef::table[-1]{'minlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2496: 	push @ {$Apache::londefdef::table[-1]{'maxlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2497:     } else {
 2498: 	if (($data=~m/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)/) or ($data=~m/\[(\d+\.?\d*)\s*mm\]/)) {
 2499: 	    my $garbage_data=$data;
 2500: 	    my $fwidth=0;
 2501:             while ($garbage_data=~m/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)/) {
 2502: 		my $current_length=&recalc($1);
 2503: 		$current_length=~/(\d+\.?\d*)/;
 2504: 		if ($fwidth<$1) {$fwidth=$1;}
 2505: 		$garbage_data=~s/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)//;
 2506: 	    }
 2507:             while ($garbage_data=~m/\[(\d+\.?\d*)\s*mm\]/) {
 2508: 		my $current_length=$1;
 2509: 		if ($fwidth<$current_length) {$fwidth=$current_length;}
 2510: 		$garbage_data=~s/\[(\d+\.?\d*)\s*mm\]//;
 2511: 	    }
 2512: 	    push @ {$Apache::londefdef::table[-1]{'TeXlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2513: 	    push @ {$Apache::londefdef::table[-1]{'objectlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$fwidth;
 2514: 	    push @ {$Apache::londefdef::table[-1]{'minlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2515: 	    push @ {$Apache::londefdef::table[-1]{'maxlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2516: 	} 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)*/) {
 2517: 	    my $garbage_data=$data;
 2518: 	    my $fwidth=0;
 2519:             while ($garbage_data=~/\\parbox\{\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)\s*\}/) {
 2520: 		my $current_length=&recalc($1);
 2521: 		$current_length=~/(\d+\.?\d*)/;
 2522: 		if ($fwidth<$1) {$fwidth=$1;}
 2523: 		$garbage_data=~s/\\parbox\{\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)//;
 2524: 	    }
 2525:             while ($garbage_data=~/\\epsfxsize\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)/) {
 2526: 		my $current_length=&recalc($1);
 2527: 		$current_length=~/(\d+\.?\d*)/;
 2528: 		if ($fwidth<$1) {$fwidth=$1;}
 2529: 		$garbage_data=~s/\\epsfxsize\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)//;
 2530: 	    }
 2531: 	    push @ {$Apache::londefdef::table[-1]{'TeXlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2532: 	    push @ {$Apache::londefdef::table[-1]{'objectlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$fwidth;
 2533: 	    push @ {$Apache::londefdef::table[-1]{'minlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2534: 	    push @ {$Apache::londefdef::table[-1]{'maxlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2535: 	    $data=~s/\\\\\s*$//; 
 2536: 	} else {  
 2537: 	    $data=~s/^\s+(\S.*)/$1/; 
 2538: 	    $data=~s/(.*\S)\s+$/$1/;
 2539: 	    $data=~s/(\s)+/$1/;
 2540: 	    my ($current_length,$min_length)=(0,0);
 2541: 	    if ($data=~/\\vskip/) {
 2542:                 my $newdata=$data;
 2543: 		$newdata=~s/\\vskip \d*\.?\d*\s*mm/THISISJUSTTEMPORARYSEPARATOR/g;
 2544: 		my @newdata=split(/THISISJUSTTEMPORARYSEPARATOR/,$newdata);
 2545: 		foreach my $elementdata (@newdata) {
 2546: 		    my $lengthnewdata=2.5*&LATEX_length($elementdata);
 2547: 		    if ($lengthnewdata>$current_length) {$current_length=$lengthnewdata;}
 2548:                     my @words=split(/ /,$elementdata);
 2549: 		    foreach my $word (@words) {
 2550: 			my $lengthword=2.5*&LATEX_length($word);
 2551: 			if ($min_length<$lengthword) {$min_length=$lengthword;}
 2552: 		    }
 2553: 		}
 2554: 	    } else {
 2555: 		$current_length=2.5*&LATEX_length($data);
 2556:                     my @words=split(/ /,$data);
 2557: 		    foreach my $word (@words) {
 2558: 			my $lengthword=2*&LATEX_length($word);
 2559: 			if ($min_length<$lengthword) {$min_length=$lengthword;}
 2560: 		    }
 2561: 	    }
 2562: 	    push @ {$Apache::londefdef::table[-1]{'TeXlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2563: 	    push @ {$Apache::londefdef::table[-1]{'objectlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2564: 	    push @ {$Apache::londefdef::table[-1]{'maxlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$current_length;
 2565: 	    push @ {$Apache::londefdef::table[-1]{'minlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$min_length;
 2566: 	}        
 2567:     }
 2568:     # Substitute all of the tables nested in this cell in their appropriate places.
 2569: 
 2570: 
 2571:     my $nested_count = $#{$Apache::londefdef::table[-1]{'include'}}; # This one is constant...
 2572:     for (my $in=0; $in<=$nested_count; $in++) {    
 2573: 	my $nested = shift @{$Apache::londefdef::table[-1]{'include'}};
 2574: 	$nested =~ s/\\end\{tabular\}\\strut\\\\/\\end\{tabular\}/;
 2575: 	# $data=~s/\\keephidden\{NEW TABLE ENTRY\}/$Apache::londefdef::table[-1]{'include'}[$in]/;
 2576: 	$data =~ s/\\keephidden\{NEW TABLE ENTRY\}/$nested/;
 2577: 
 2578:     }
 2579:     # Should be be killing off the 'include' elements as they're used up?
 2580: 
 2581:     push @ {$Apache::londefdef::table[-1]{'content'}[-1] },$data;
 2582: 
 2583: 
 2584: 
 2585: 
 2586:     #  the colspan array will indicate how many columns will be spanned by this
 2587:     #  cell..this requires that counter_columns also be adjusted accordingly
 2588:     #  so that the next bunch of text goes in the right cell.  Note that since
 2589:     #  counter_columns is incremented in the start_td_tex, we adjust by colspan-1.
 2590:     #
 2591: 
 2592:     $Apache::londefdef::table[-1]{'counter_columns'} += $colspan -1;
 2593:     for (my $i = 0; $i < ($colspan -1); $i++) {
 2594: 	push @ {$Apache::londefdef::table[-1]{'content'}[-1] },'';
 2595:     }
 2596:     for (my $r = 0; $r < $rowspan; $r++) {
 2597: 	$Apache::londefdef::table[-1]{'colspan'}[$current_row+$r][$current_column] = $colspan;
 2598: 	# Put empty text in spanned cols.
 2599: 	
 2600:     }
 2601: 
 2602: 
 2603: 
 2604:     return '';
 2605: }
 2606: 
 2607: sub end_td {
 2608:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2609:     my $currentstring = '';
 2610:     if ($target eq 'web') {
 2611: 	$currentstring = $token->[2];     
 2612:     } elsif ($target eq 'tex') {
 2613:         $Apache::londefdef::TD_redirection =0;
 2614: 	&end_td_tex($parstack,$parser,$safeeval);
 2615:     }
 2616:     return $currentstring;
 2617: }
 2618: 
 2619: #-- <th> tag (end tag optional)
 2620: sub start_th {
 2621:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2622:     my $currentstring = '';
 2623:     if ($target eq 'web') {
 2624: 	$currentstring = $token->[4];     
 2625:     } elsif ($target eq 'tex') {
 2626: 	$Apache::londefdef::TD_redirection = 1;
 2627: 	&tagg_check('tr','th',$tagstack,$parstack,$parser,$safeeval);
 2628:     } 
 2629:     return $currentstring;
 2630: }   
 2631:     
 2632: sub tagg_check {
 2633:     my ($good_tag,$bad_tag,$tagstack,$parstack,$parser,$safeeval) = @_;
 2634:     my @ar=@$parstack; 
 2635:     for (my $i=$#ar-1;$i>=0;$i--) {
 2636: 	if (lc($$tagstack[$i]) eq $good_tag) {
 2637: 	    &start_th_tex($parstack,$parser,$safeeval);
 2638: 	    last;
 2639: 	} elsif (lc($$tagstack[$i]) eq $bad_tag) {
 2640: 	    splice @ar, $i+1;
 2641: 	    &end_th_tex(\@ar,$parser,$safeeval);
 2642: 	    &start_th_tex($parstack,$parser,$safeeval);
 2643: 	    last;
 2644: 	}
 2645:     }
 2646:     return '';
 2647: }
 2648:  
 2649: sub start_th_tex {
 2650:     my ($parstack,$parser,$safeeval) = @_;
 2651:     my $alignchar = substr(&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1),0,1);
 2652:     if ($alignchar eq '') {
 2653: 	$alignchar = $Apache::londefdef::table[-1]{'rows'}[-1];
 2654:     }
 2655:     push @{ $Apache::londefdef::table[-1]{'align'}[$Apache::londefdef::table[-1]{'row_number'}] }, $alignchar;
 2656:     $Apache::londefdef::table[-1]{'counter_columns'}++;
 2657:     my $TeXwidth=&Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval,undef,0);
 2658:     if (defined $TeXwidth) {		
 2659: 	my $current_length=&recalc($TeXwidth);
 2660: 	$current_length=~/(\d+\.?\d*)/;
 2661: 	push @ {$Apache::londefdef::table[-1]{'TeXlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$1;
 2662:     }
 2663:     &Apache::lonxml::startredirection();
 2664:     return '';
 2665: }
 2666: 
 2667: sub end_th_tex {
 2668:     my ($parstack,$parser,$safeeval) = @_;
 2669:     my $current_row = $Apache::londefdef::table[-1]{'row_number'};
 2670:     my $data=&Apache::lonxml::endredirection();
 2671:     my $TeXwidth=&Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval,undef,0);
 2672:     if (defined $TeXwidth) {		
 2673: 	push @ {$Apache::londefdef::table[-1]{'objectlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2674: 	push @ {$Apache::londefdef::table[-1]{'minlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2675: 	push @ {$Apache::londefdef::table[-1]{'maxlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2676:     } else {
 2677: 	if (($data=~m/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)/) or ($data=~m/\[(\d+\.?\d*)\s*mm\]/)) {
 2678: 	    my $garbage_data=$data;
 2679: 	    my $fwidth=0;
 2680:             while ($garbage_data=~m/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)/) {
 2681: 		my $current_length=&recalc($1);
 2682: 		$current_length=~/(\d+\.?\d*)/;
 2683: 		if ($fwidth<$1) {$fwidth=$1;}
 2684: 		$garbage_data=~s/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)//;
 2685: 	    }
 2686:             while ($garbage_data=~m/\[(\d+\.?\d*)\s*mm\]/) {
 2687: 		my $current_length=$1;
 2688: 		if ($fwidth<$current_length) {$fwidth=$current_length;}
 2689: 		$garbage_data=~s/\[(\d+\.?\d*)\s*mm\]//;
 2690: 	    }
 2691: 	    push @ {$Apache::londefdef::table[-1]{'TeXlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2692: 	    push @ {$Apache::londefdef::table[-1]{'objectlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$fwidth;
 2693: 	    push @ {$Apache::londefdef::table[-1]{'minlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2694: 	    push @ {$Apache::londefdef::table[-1]{'maxlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2695: 	} else {  
 2696: 	    $data=~s/^\s+(\S.*)/$1/; 
 2697: 	    $data=~s/(.*\S)\s+$/$1/;
 2698: 	    $data=~s/(\s)+/$1/;
 2699: 	    my ($current_length,$min_length)=(0,0);
 2700: 	    if ($data=~/\\vskip/) {
 2701:                 my $newdata=$data;
 2702: 		$newdata=~s/\\vskip \d*\.?\d*\s*mm/THISISJUSTTEMPORARYSEPARATOR/g;
 2703: 		my @newdata=split(/THISISJUSTTEMPORARYSEPARATOR/,$newdata);
 2704: 		foreach my $elementdata (@newdata) {
 2705: 		    my $lengthnewdata=2.5*&LATEX_length($elementdata);
 2706: 		    if ($lengthnewdata>$current_length) {$current_length=$lengthnewdata;}
 2707:                     my @words=split(/ /,$elementdata);
 2708: 		    foreach my $word (@words) {
 2709: 			my $lengthword=2.5*&LATEX_length($word);
 2710: 			if ($min_length<$lengthword) {$min_length=$lengthword;}
 2711: 		    }
 2712: 		}
 2713: 	    } else {
 2714: 		$current_length=2.5*&LATEX_length($data);
 2715:                     my @words=split(/ /,$data);
 2716: 		    foreach my $word (@words) {
 2717: 			my $lengthword=2*&LATEX_length($word);
 2718: 			if ($min_length<$lengthword) {$min_length=$lengthword;}
 2719: 		    }
 2720: 	    }
 2721: 	    push @ {$Apache::londefdef::table[-1]{'TeXlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2722: 	    push @ {$Apache::londefdef::table[-1]{'objectlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2723: 	    push @ {$Apache::londefdef::table[-1]{'maxlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$current_length;
 2724: 	    push @ {$Apache::londefdef::table[-1]{'minlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$min_length;
 2725: 	}        
 2726:     }
 2727: 	for (my $in=0; $in<=$#{$Apache::londefdef::table[-1]{'include'}};$in++) {         
 2728: 	    $data=~s/\\keephidden\{NEW TABLE ENTRY\}/$Apache::londefdef::table[-1]{'include'}[$in]/;
 2729: 	}
 2730:     #make data bold
 2731:     $data='\textbf{'.$data.'}';
 2732:     push @ {$Apache::londefdef::table[-1]{'content'}[-1] },$data;
 2733:     return'';
 2734: }
 2735: 
 2736: sub end_th {
 2737:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2738:     my $currentstring = &end_p();	# Close any open <p> in the row.
 2739:     if ($target eq 'web') {
 2740: 	$currentstring .= $token->[2];     
 2741:     } elsif ($target eq 'tex') {
 2742:         $Apache::londefdef::TD_redirection =0;
 2743: 	&end_th_tex($parstack,$parser,$safeeval);
 2744:     }
 2745:     return $currentstring;
 2746: }
 2747:      
 2748: #-- <img> tag (end tag forbidden)
 2749: #
 2750: #  Render the <IMG> tag.
 2751: #     <IMG> has the following attributes (in addition to the 
 2752: #     standard HTML ones:
 2753: #      TeXwrap   - Governs how the tex target will try to wrap text around
 2754: #                  horizontally aligned images.
 2755: #      TeXwidth  - The width of the image when rendered for print (mm).
 2756: #      TeXheight - The height of the image when rendered for print (mm)
 2757: #         (Note there seems to also be support for this as a % of page size)
 2758: #      
 2759: sub start_img {
 2760:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
 2761:     my $src = &Apache::lonxml::get_param('src',$parstack,$safeeval,
 2762: 					 undef,1);
 2763:     if (not $src and ($target eq 'web' or $target eq 'tex')) { 
 2764: 	my $inside = &Apache::lonxml::get_all_text("/img",$parser,$style);
 2765: 	return '';
 2766:     }
 2767:     &Apache::lonxml::extlink($src);
 2768:     my $currentstring = '';
 2769:     my $scaling = .3;
 2770: 
 2771:    # Render unto browsers that which are the browser's...
 2772: 
 2773:     if ($target eq 'web') {
 2774: 	if ($env{'browser.imagesuppress'} ne 'on') {
 2775: 	    my $enc = ('yes' eq 
 2776: 		       lc(&Apache::lonxml::get_param('encrypturl',$parstack,
 2777: 						     $safeeval)));
 2778: 	    $currentstring.=&Apache::lonenc::encrypt_ref($token,{'src'=>$src},
 2779: 							 $enc);
 2780: 	} else {
 2781: 	    my $alttag = &Apache::lonxml::get_param('alt',$parstack,$safeeval,
 2782: 						    undef,1);
 2783: 	    if (!$alttag) {
 2784: 		$alttag = &Apache::lonmeta::alttag($Apache::lonxml::pwd[-1],
 2785: 						   $src);
 2786: 	    }
 2787: 	    $currentstring.='[IMAGE: '.$alttag.']';
 2788: 	}
 2789: 
 2790: 	# and render unto TeX that which is LaTeX
 2791: 
 2792:     } elsif ($target eq 'tex') {
 2793: 	#
 2794: 	#  The alignment will require some superstructure to be put around
 2795: 	#  the \includegraphics stuff.  At present we can only partially
 2796: 	#  simulate the alignments offered by html.
 2797: 	#
 2798: 	#
 2799: 	my $align = lc(&Apache::lonxml::get_param('align', 
 2800: 						  $parstack,
 2801: 						  $safeeval,
 2802: 						  undef,1));
 2803: 	if(!$align) {
 2804: 		$align = "bottom";	# This is html's default so it's ours too.
 2805: 	}
 2806: 	#
 2807: 	&Apache::lonxml::debug("Alignemnt = $align");
 2808: 	#  LaTeX's image/text wrapping is really bad since it wants to
 2809: 	#  make figures float.  
 2810:         #   The user has the optional parameter (applicable only to l/r
 2811: 	# alignment to use the picins/parpic directive to get wrapped text
 2812: 	# this is also imperfect.. that's why we give them a choice...
 2813: 	# so they can't yell at us for our choice.
 2814: 	#
 2815: 	my $latex_rendering = &Apache::lonxml::get_param('TeXwrap',
 2816: 							    $parstack,
 2817: 							    $safeeval,
 2818: 							    undef,0);
 2819: 	# &Apache::lonxml::debug("LaTeX rendering = $latex_rendering");
 2820: 	if(!$latex_rendering) {
 2821: 		$latex_rendering = "texwrap";
 2822: 	}
 2823: 	# using texwrap inside a table does not work. So, if after all of this,
 2824: 	# texwrap is on, we turn it off if we detect we're in a table:
 2825: 	#
 2826: 	if (($latex_rendering eq 'texwrap') && &is_inside_of($tagstack, "table")) {
 2827: 	    $latex_rendering = 'parpic';
 2828: 	}
 2829: 
 2830: 	# &Apache::lonxml::debug("LaTeX rendering = $latex_rendering image file: $src");
 2831: 
 2832: 	#if original gif/jpg/png file exist do following:
 2833: 	my $origsrc=$src;
 2834: 	my ($path,$file) = &get_eps_image($src);
 2835: 	$src=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$src);
 2836: 	&Apache::lonxml::debug("path = $path file = $file src = $src");
 2837: 	if (-e $src) {
 2838: 	    &Apache::lonxml::debug("$src exists");
 2839: 	    my ($height_param,$width_param)=
 2840: 		&image_size($origsrc,0.3,$parstack,$safeeval);
 2841: 	    my $destpath = $path;
 2842: 	    $destpath    =~ s/ /\_/g; # Spaces in path cause LaTex to vomit.
 2843: 	    my $destfile = $file;
 2844: 	    $destfile    =~ s/ /\_/g;
 2845: 	    my $size;
 2846: 	    if ($width_param)  { $size.='width='.$width_param.' mm,'; }
 2847: 	    if ($height_param) { $size.='height='.$height_param.' mm]'; }
 2848: 	    $size='['.$size;
 2849: 	    $size=~s/,$/]/; 
 2850: 	    $currentstring .= '\graphicspath{{'.$destpath.'}}'
 2851: 		.'\includegraphics'.$size.'{'.$destfile.'} ';
 2852: 
 2853: 	    #    If there's an alignment specification we need to honor it here.
 2854: 	    #    For the horizontal alignments, we will also honor the
 2855: 	    #    value of the latex specfication.  The default is parbox,
 2856: 	    #    and that's used for illegal values too.  
 2857: 	    #    
 2858: 	    #    Even though we set a default alignment value, the user
 2859: 	    #    could have given us an illegal value.  In that case we
 2860: 	    #    just use the default alignment of bottom..
 2861: 	    if      ($align eq "top")    {
 2862: 		$currentstring = '\raisebox{-'.$height_param.'mm}{'.$currentstring.'}';
 2863: 	    } elsif (($align eq "center") || ($align eq "middle")) { # Being kind
 2864: 		my $offset = $height_param/2;
 2865: 		$currentstring = '\raisebox{-'.$offset.'mm}{'.$currentstring.'}';
 2866: 	    } elsif ($align eq "left")   { 
 2867: 		if ($latex_rendering eq "parpic") { 
 2868: 		    $currentstring = '\parpic[l]{'.$currentstring.'}';
 2869: 		} else {    	                                 # wrapfig render
 2870: 		    $currentstring = '\begin{wrapfigure}{l}{'.$width_param.'mm}'
 2871: 			.'\scalebox{1.0}{'.$currentstring.'}\end{wrapfigure}';
 2872: 		}
 2873: 	    } elsif ($align eq "right")  {   
 2874: 		if ($latex_rendering eq "parpic") {
 2875: 		    $currentstring = '\parpic[r]{'.$currentstring.'}';
 2876: 		} else {	                                 # wrapfig rendering
 2877: 		    $currentstring = '\begin{wrapfigure}{r}{'.$width_param.'mm}'
 2878: 			.'\scalebox{1.0}{'.$currentstring.'}\end{wrapfigure}';
 2879: 
 2880: 		}
 2881: 	    } else {		# Bottom is also default.
 2882: 		# $currentstring = '\raisebox{'.$height_param.'mm}{'.$currentstring.'}';
 2883: 	    }
 2884: 	} else {
 2885: 	    &Apache::lonxml::debug("$src does not exist");
 2886: 	    #original image file doesn't exist so check the alt attribute
 2887: 	    my $alt = 
 2888: 		&Apache::lonxml::get_param('alt',$parstack,$safeeval,undef,1);
 2889: 	    unless ($alt) {
 2890: 		$alt=&Apache::lonmeta::alttag($Apache::lonxml::pwd[-1],$src);
 2891: 	    }
 2892: 
 2893: 	    if ($alt) { $currentstring .= ' '.$alt.' '; }
 2894: 	}
 2895: 
 2896: 	# And here's where the semi-quote breaks down: allow the user
 2897:         # to edit the beast as well by rendering the problem for edit:
 2898:     } elsif ($target eq 'edit') {
 2899: 	$currentstring .=&Apache::edit::tag_start($target,$token);
 2900: 	$currentstring .=&Apache::edit::text_arg('Image Url:','src',$token,70).
 2901: 	    &Apache::edit::browse('src',undef,'alt').' '.
 2902: 	    &Apache::edit::search('src',undef,'alt').'<br />';
 2903: 	$currentstring .=&Apache::edit::text_arg('Description:','alt',$token,70).'<br />';
 2904: 	$currentstring .=&Apache::edit::text_arg('width (pixel):','width',$token,5);
 2905: 	$currentstring .=&Apache::edit::text_arg('height (pixel):','height',$token,5).'<br />';
 2906: 	$currentstring .=&Apache::edit::text_arg('TeXwidth (mm):','TeXwidth',$token,5);
 2907: 	$currentstring .=&Apache::edit::text_arg('TeXheight (mm):','TeXheight',$token,5);
 2908: 	$currentstring .=&Apache::edit::select_arg('Alignment:','align',
 2909: 						   ['','bottom','middle','top','left','right'],$token,5);
 2910: 	$currentstring .=&Apache::edit::select_arg('TeXwrap:', 'TeXwrap',
 2911: 						   ['', 'parbox', 'parpic'], $token, 2);
 2912: 	$currentstring .=&Apache::edit::select_arg('Encyrpt URL:','encrypturl',
 2913: 						   ['no','yes'], $token, 2);
 2914: 	$currentstring .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
 2915: 	my $src=    &Apache::lonxml::get_param('src',$parstack,$safeeval);
 2916: 	my $alt=    &Apache::lonxml::get_param('alt',$parstack,$safeeval);
 2917: 	my $width=  &Apache::lonxml::get_param('width',$parstack,$safeeval);
 2918: 	my $height= &Apache::lonxml::get_param('height',$parstack,$safeeval);
 2919: 
 2920: 
 2921: 	$currentstring .= '<img src="'.$src.'" alt="'.$alt.'" ';
 2922: 	if ($width) { $currentstring.=' width="'.$width.'" '; }
 2923: 	if ($height) { $currentstring.=' height="'.$height.'" '; }
 2924: 	$currentstring .= ' />';
 2925:     } elsif ($target eq 'modified') {
 2926: 	my ($osrc,$owidth,$oheight)=
 2927: 	    ($token->[2]{'src'},$token->[2]{'width'},$token->[2]{'height'});
 2928: 	my $ctag=&Apache::edit::get_new_args($token,$parstack,
 2929: 					     $safeeval,'src','alt','align',
 2930: 					     'TeXwidth','TeXheight', 'TeXwrap',
 2931: 					     'width','height','encrypturl');
 2932: 	my ($nsrc,$nwidth,$nheight)=
 2933: 	    ($token->[2]{'src'},$token->[2]{'width'},$token->[2]{'height'});
 2934: 	my $loc=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$nsrc);
 2935: 	&image_replication($loc);
 2936: 	my ($iwidth,$iheight);
 2937: 	if (-e $loc) {
 2938: 	    my $image = Image::Magick->new;
 2939: 	    $image->Read($loc);
 2940: 	    ($iwidth, $iheight) = ($image->Get('width'),
 2941: 				   $image->Get('height'));
 2942: 	}
 2943: 	if ($osrc ne $nsrc || (!$nwidth && !$nheight)) {
 2944: 	    # changed image or no size specified,
 2945:             # if they didn't explicitly change the 
 2946:             # width or height use the ones from the image
 2947: 	    if ($iwidth && $iheight) {
 2948: 		if ($owidth == $nwidth || (!$nwidth && !$nheight)) {
 2949: 		    $token->[2]{'width'} = $iwidth;$ctag=1;
 2950: 		}
 2951: 		if ($oheight == $nheight || (!$nwidth && !$nheight)) {
 2952: 		    $token->[2]{'height'}=$iheight;$ctag=1;
 2953: 		}
 2954: 	    }
 2955: 	}
 2956: 	my ($cwidth,$cheight)=($token->[2]{'width'},$token->[2]{'height'});
 2957: 	# if we don't have a width or height
 2958: 	if ($iwidth && $cwidth && !$cheight) {
 2959: 	    $token->[2]{'height'}=int(($cwidth/$iwidth)*$iheight);$ctag=1;
 2960: 	}
 2961: 	if ($iheight && $cheight && !$cwidth) {
 2962: 	    $token->[2]{'width'}=int(($cheight/$iheight)*$iwidth);$ctag=1;
 2963: 	}
 2964: 	if ($ctag) {$currentstring=&Apache::edit::rebuild_tag($token);}
 2965:     }
 2966: 
 2967:     return $currentstring;
 2968: }
 2969: 
 2970: sub end_img {
 2971:     my ($target,$token) = @_;
 2972:     my $currentstring = '';
 2973:     if ($target eq 'web') {
 2974: 	$currentstring = $token->[2];
 2975:     } elsif ($target eq 'tex') {
 2976: 	$currentstring = '';
 2977:     }
 2978:     return $currentstring;
 2979: }
 2980: 
 2981: #-- <applet> tag (end tag required)
 2982: sub start_applet {
 2983:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2984:     
 2985:     my $code=&Apache::lonxml::get_param('code',$parstack,$safeeval,undef,1);
 2986:     &Apache::lonxml::extlink($code);
 2987:     my $archive=&Apache::lonxml::get_param('archive',$parstack,$safeeval,
 2988: 					   undef,1);
 2989:     &Apache::lonxml::extlink($archive);
 2990:     my $currentstring = '';
 2991:     if ($target eq 'web') {
 2992: 	if ($env{'browser.appletsuppress'} ne 'on') {
 2993: 	    $currentstring = &Apache::lonenc::encrypt_ref($token,
 2994: 							  {'code'=>$code,
 2995: 							   'archive'=>$archive}
 2996: 							  );
 2997: 	} else {
 2998: 	    my $alttag= &Apache::lonxml::get_param('alt',$parstack,
 2999: 						   $safeeval,undef,1);
 3000: 	    unless ($alttag) {
 3001: 		$alttag=&Apache::lonmeta::alttag($Apache::lonxml::pwd[-1],
 3002: 						 $code);
 3003: 	    }
 3004: 	    $currentstring='[APPLET: '.$alttag.']';
 3005: 	}
 3006:     } elsif ($target eq 'tex') {
 3007: 	my $alttag= &Apache::lonxml::get_param('alt',$parstack,
 3008: 					       $safeeval,undef,1);
 3009: 	unless ($alttag) {
 3010: 	    my $code=&Apache::lonxml::get_param('code',$parstack,$safeeval,
 3011: 						undef,1);
 3012: 	    $alttag=&Apache::lonmeta::alttag($Apache::lonxml::pwd[-1],
 3013: 					     $code);
 3014: 	}
 3015: 	$currentstring.='\begin{center} \fbox{Java Applet: '.$alttag.
 3016: 	    '.}\end{center}';
 3017:     } 
 3018:     return $currentstring;
 3019: }
 3020: 
 3021: sub end_applet {
 3022:     my ($target,$token) = @_;
 3023:     my $currentstring = '';
 3024:     if ($target eq 'web') {
 3025: 	$currentstring = $token->[2];
 3026:     } elsif ($target eq 'tex') {
 3027:     } 
 3028:     return $currentstring;
 3029: }
 3030: 
 3031: #-- <embed> tag (end tag optional/required)
 3032: sub start_embed {    
 3033:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 3034:     my $src=&Apache::lonxml::get_param('src',$parstack,$safeeval,undef,1);
 3035:     &Apache::lonxml::extlink($src);
 3036:     my $currentstring = '';
 3037:     if ($target eq 'web') {
 3038: 	if ($env{'browser.embedsuppress'} ne 'on') {
 3039: 	    $currentstring=&Apache::lonenc::encrypt_ref($token,{'src'=>$src});
 3040: 	} else {
 3041: 	    my $alttag=&Apache::lonxml::get_param
 3042: 		('alt',$parstack,$safeeval,undef,1);
 3043: 	    unless ($alttag) {
 3044: 		$alttag=&Apache::lonmeta::alttag($Apache::lonxml::pwd[-1],$src);
 3045: 	    }
 3046: 	    $currentstring='[EMBED: '.$alttag.']';
 3047: 	}
 3048:     } elsif ($target eq 'tex') {
 3049:     } 
 3050:     return $currentstring;
 3051: }
 3052: 
 3053: sub end_embed {
 3054:     my ($target,$token) = @_;
 3055:     my $currentstring = '';
 3056:     if ($target eq 'web') {
 3057: 	$currentstring = $token->[2];     
 3058:     } elsif ($target eq 'tex') {  
 3059:     } 
 3060:     return $currentstring;
 3061: }
 3062: 
 3063: #-- <param> tag (end tag forbidden)
 3064: sub start_param {
 3065:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 3066:     if (&Apache::lonxml::get_param('name',$parstack,
 3067: 				   $safeeval,undef,1)=~/^cabbase$/i) {
 3068: 	my $value=&Apache::lonxml::get_param('value',$parstack,
 3069: 					     $safeeval,undef,1);
 3070: 	&Apache::lonxml::extlink($value);
 3071:     } 
 3072:   
 3073:     my $src = &Apache::lonxml::get_param('src',$parstack,$safeeval,undef,1);
 3074:     &Apache::lonxml::extlink($src);
 3075:     my $currentstring = '';
 3076:     if ($target eq 'web') {
 3077: 	my %toconvert;
 3078: 	my $src=&Apache::lonxml::get_param('src',$parstack,$safeeval,undef,1);
 3079: 	if ($src) { $toconvert{'src'}= $src; }
 3080: 	my $name=&Apache::lonxml::get_param('name',$parstack,$safeeval,
 3081: 					    undef,1);
 3082: 	if ($name=~/^cabbase$/i) {
 3083: 	    $toconvert{'value'}=&Apache::lonxml::get_param('value',$parstack,
 3084: 							   $safeeval,undef,1);
 3085: 	}
 3086: 	$currentstring = &Apache::lonenc::encrypt_ref($token,\%toconvert);
 3087:     } elsif ($target eq 'tex') {
 3088:     } 
 3089:     return $currentstring;
 3090: }
 3091: 
 3092: sub end_param {
 3093:     my ($target,$token) = @_;
 3094:     my $currentstring = '';
 3095:     if ($target eq 'web') {
 3096: 	$currentstring = $token->[2];     
 3097:     } elsif ($target eq 'tex') {
 3098:     } 
 3099:     return $currentstring;
 3100: }
 3101: 
 3102: #-- <allow> tag
 3103: sub start_allow {
 3104:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 3105:     my $src = &Apache::lonxml::get_param('src',$parstack,$safeeval,undef,1);
 3106:     &Apache::lonxml::extlink($src);
 3107: 
 3108:     if ($target eq 'tex') { &image_replication($src); }
 3109:     my $result;
 3110:     if ($target eq 'edit') {
 3111: 	$result .=&Apache::edit::tag_start($target,$token);
 3112: 	$result .=&Apache::edit::text_arg('File Spec:','src',$token,70);
 3113: 	$result .=&Apache::edit::end_row();#.&Apache::edit::start_spanning_row();
 3114:     } elsif ($target eq 'modified') {
 3115: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
 3116: 						     $safeeval,'src');
 3117: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
 3118:     }
 3119:     return $result;
 3120: }
 3121: 
 3122: sub end_allow {
 3123:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 3124:     if ( $target eq 'edit') { return (&Apache::edit::end_table()); }
 3125:     return '';
 3126: }
 3127: 
 3128: #-- Frames (end tag required)
 3129: #-- <frameset>
 3130: sub start_frameset {
 3131:     my ($target,$token) = @_;
 3132:     my $currentstring = '';	# Close any pending para.
 3133:     if ($target eq 'web') { 
 3134: 	if (!$Apache::lonxml::registered &&
 3135: 	    $env{'request.state'} eq 'published') {
 3136: 	    $currentstring.='<head>'.
 3137: 		&Apache::lonmenu::registerurl(undef,$target).'</head>';
 3138: 	}
 3139: 	my $onLoad='';
 3140: 	foreach my $key (keys(%{$token->[2]})) {
 3141: 	    if ($key =~ /^onload$/i) {
 3142: 		$onLoad.=$token->[2]->{$key}.';';
 3143: 		delete($token->[2]->{$key});
 3144: 	    }
 3145: 	}
 3146: 	$token->[2]->{'onload'}=&Apache::lonmenu::loadevents().';'.$onLoad;
 3147: 	my $onUnload='';
 3148: 	foreach my $key (keys(%{$token->[2]})) {
 3149: 	    if ($key =~ /^onunload$/i) {
 3150: 		$onUnload.=$token->[2]->{$key}.';';
 3151: 		delete($token->[2]->{$key});
 3152: 	    }
 3153: 	}
 3154: 	$token->[2]->{'onunload'}=&Apache::lonmenu::unloadevents().
 3155: 	    ';'.$onUnload;
 3156: 	
 3157: 	$currentstring .= '<'.$token->[1];
 3158: 	foreach (keys %{$token->[2]}) {
 3159: 	    $currentstring.=' '.$_.'="'.$token->[2]->{$_}.'"';
 3160: 	}
 3161: 	$currentstring.='>';
 3162:     }
 3163:     return $currentstring;
 3164: }
 3165: 
 3166: sub end_frameset {
 3167:     my ($target,$token) = @_;
 3168:     my $currentstring = '';
 3169:     if ($target eq 'web') {
 3170: 	$currentstring = $token->[2];
 3171:     }
 3172:     return $currentstring;
 3173: }
 3174: 
 3175: #-- <xmp> (end tag required)
 3176: sub start_xmp {
 3177:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 3178:     my $currentstring = '';
 3179:     if ($target eq 'web') {
 3180: 	$currentstring .= $token->[4];
 3181:     } elsif ($target eq 'tex') {
 3182: 	$currentstring .= '\begin{verbatim}';
 3183:     } 
 3184:     return $currentstring;
 3185: }
 3186: 
 3187: sub end_xmp {
 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:     }
 3195:     return $currentstring;
 3196: }
 3197: 
 3198: #-- <pre> (end tag required)
 3199: sub start_pre {
 3200:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 3201:     my $currentstring = &end_p();	# close off pending <p>
 3202:     if ($target eq 'web') {
 3203: 	$currentstring .= $token->[4];
 3204:     } elsif ($target eq 'tex') {
 3205: 	$currentstring .= '\begin{verbatim}';
 3206: 	&Apache::lonxml::disable_LaTeX_substitutions();
 3207:     } 
 3208:     return $currentstring;
 3209: }
 3210: 
 3211: sub end_pre {
 3212:     my ($target,$token) = @_;
 3213:     my $currentstring = '';
 3214:     if ($target eq 'web') {
 3215: 	$currentstring .= $token->[2];
 3216:     } elsif ($target eq 'tex') {
 3217: 	$currentstring .= '\end{verbatim}';
 3218: 	&Apache::lonxml::enable_LaTeX_substitutions();
 3219:     }
 3220:     return $currentstring;
 3221: }
 3222: 
 3223: #-- <insert>
 3224: sub start_insert {
 3225:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 3226:     my $currentstring = '';
 3227:     if ($target eq 'web') {
 3228: 	my $display = &Apache::lonxml::get_param('display',$parstack,$safeeval,undef,1);
 3229: 	$currentstring .= '<b>'.$display.'</b>';;
 3230:     }
 3231:     return $currentstring;
 3232: }
 3233: 
 3234: sub end_insert {
 3235:     my ($target,$token) = @_;
 3236:     my $currentstring = '';
 3237:     if ($target eq 'web') {
 3238: 	$currentstring .= '';
 3239:     }
 3240:     return $currentstring;
 3241: }
 3242: 
 3243: #-- <externallink>
 3244: sub start_externallink {
 3245:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 3246:     my $currentstring = '';
 3247:     if ($target eq 'web') {
 3248: 	my $display = &Apache::lonxml::get_param('display',$parstack,$safeeval,undef,1);
 3249: 	$currentstring .= '<b>'.$display.'</b>';;
 3250:     }
 3251:     return $currentstring;
 3252: }
 3253: 
 3254: sub end_externallink {
 3255:     my ($target,$token) = @_;
 3256:     my $currentstring = '';
 3257:     if ($target eq 'web') {
 3258: 	$currentstring .= '';
 3259:     }
 3260:     return $currentstring;
 3261: }
 3262: 
 3263: #-- <blankspace heigth="">
 3264: sub start_blankspace {
 3265:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 3266:     my $currentstring = &end_p();	# closes off any unclosed <p>
 3267:     if ($target eq 'tex') {
 3268: 	my $howmuch = &Apache::lonxml::get_param('heigth',$parstack,$safeeval,undef,1);
 3269: 	$currentstring .= '\vskip '.$howmuch.' ';
 3270:     }
 3271:     return $currentstring;
 3272: }
 3273: 
 3274: sub end_blankspace {
 3275:     my ($target,$token) = @_;
 3276:     my $currentstring = '';
 3277:     if ($target eq 'tex') {
 3278: 	$currentstring .= '';
 3279:     }
 3280:     return $currentstring;
 3281: }
 3282: 
 3283: #-- <abbr> tag (end tag required)
 3284: sub start_abbr {
 3285:     my ($target,$token) = @_;
 3286:     my $currentstring = '';
 3287:     if ($target eq 'web') {
 3288: 	$currentstring = $token->[4];     
 3289:     } 
 3290:     return $currentstring;
 3291: }
 3292: 
 3293: sub end_abbr {
 3294:     my ($target,$token) = @_;
 3295:     my $currentstring = '';
 3296:     if ($target eq 'web') {
 3297: 	$currentstring = $token->[2];    
 3298:     } 
 3299:     return $currentstring;
 3300: }
 3301: 
 3302: #-- <acronym> tag (end tag required)
 3303: sub start_acronym {
 3304:     my ($target,$token) = @_;
 3305:     my $currentstring = '';
 3306:     if ($target eq 'web') {
 3307: 	$currentstring = $token->[4];     
 3308:     } 
 3309:     return $currentstring;
 3310: }
 3311: 
 3312: sub end_acronym {
 3313:     my ($target,$token) = @_;
 3314:     my $currentstring = '';
 3315:     if ($target eq 'web') {
 3316: 	$currentstring = $token->[2];    
 3317:     } 
 3318:     return $currentstring;
 3319: }
 3320: 
 3321: #-- <area> tag (end tag forbidden)
 3322: sub start_area {
 3323:     my ($target,$token) = @_;
 3324:     my $currentstring = '';
 3325:     if ($target eq 'web') {
 3326: 	$currentstring = $token->[4];     
 3327:     } 
 3328:     return $currentstring;
 3329: }
 3330: 
 3331: sub end_area {
 3332:     my ($target,$token) = @_;
 3333:     my $currentstring = '';
 3334:     if ($target eq 'web') {
 3335: 	$currentstring = $token->[2];    
 3336:     } 
 3337:     return $currentstring;
 3338: }
 3339: 
 3340: #-- <base> tag (end tag forbidden)
 3341: sub start_base {
 3342:     my ($target,$token) = @_;
 3343:     my $currentstring = '';
 3344:     if ($target eq 'web') {
 3345: 	$currentstring = $token->[4];     
 3346:     }
 3347:     return $currentstring;
 3348: }
 3349: 
 3350: sub end_base {
 3351:     my ($target,$token) = @_;
 3352:     my $currentstring = '';
 3353:     if ($target eq 'web') {
 3354: 	$currentstring = $token->[2];    
 3355:     } 
 3356:     return $currentstring;
 3357: }
 3358: 
 3359: #-- <bdo> tag (end tag required)
 3360: sub start_bdo {
 3361:     my ($target,$token) = @_;
 3362:     my $currentstring = '';
 3363:     if ($target eq 'web') {
 3364: 	$currentstring = $token->[4];     
 3365:     } 
 3366:     return $currentstring;
 3367: }
 3368: 
 3369: sub end_bdo {
 3370:     my ($target,$token) = @_;
 3371:     my $currentstring = '';
 3372:     if ($target eq 'web') {
 3373: 	$currentstring = $token->[2];    
 3374:     } 
 3375:     return $currentstring;
 3376: }
 3377: 
 3378: #-- <bgsound> tag (end tag optional)
 3379: sub start_bgsound {
 3380:     my ($target,$token) = @_;
 3381:     my $currentstring = '';
 3382:     if ($target eq 'web') {
 3383: 	$currentstring = $token->[4];     
 3384:     } 
 3385:     return $currentstring;
 3386: }
 3387: 
 3388: sub end_bgsound {
 3389:     my ($target,$token) = @_;
 3390:     my $currentstring = '';
 3391:     if ($target eq 'web') {
 3392: 	$currentstring = $token->[2];    
 3393:     } 
 3394:     return $currentstring;
 3395: }
 3396: 
 3397: #-- <blink> tag (end tag required)
 3398: sub start_blink {
 3399:     my ($target,$token) = @_;
 3400:     my $currentstring = '';
 3401:     if ($target eq 'web') {
 3402: 	$currentstring = $token->[4];     
 3403:     } 
 3404:     return $currentstring;
 3405: }
 3406: 
 3407: sub end_blink {
 3408:     my ($target,$token) = @_;
 3409:     my $currentstring = '';
 3410:     if ($target eq 'web') {
 3411: 	$currentstring = $token->[2];    
 3412:     } 
 3413:     return $currentstring;
 3414: }
 3415: 
 3416: #-- <blockquote> tag (end tag required)
 3417: sub start_blockquote {
 3418:     my ($target,$token) = @_;
 3419:     my $currentstring = &end_p();	# Close any unclosed <p>
 3420:     if ($target eq 'web') {
 3421: 	$currentstring .= $token->[4];     
 3422:     } 
 3423:     return $currentstring;
 3424: }
 3425: 
 3426: sub end_blockquote {
 3427:     my ($target,$token) = @_;
 3428:     my $currentstring = '';
 3429:     if ($target eq 'web') {
 3430: 	$currentstring = $token->[2];    
 3431:     } 
 3432:     return $currentstring;
 3433: }
 3434: 
 3435: #-- <button> tag (end tag required)
 3436: sub start_button {
 3437:     my ($target,$token) = @_;
 3438:     my $currentstring = '';
 3439:     if ($target eq 'web') {
 3440: 	$currentstring = $token->[4];     
 3441:     } 
 3442:     return $currentstring;
 3443: }
 3444: 
 3445: sub end_button {
 3446:     my ($target,$token) = @_;
 3447:     my $currentstring = '';
 3448:     if ($target eq 'web') {
 3449: 	$currentstring = $token->[2];    
 3450:     } 
 3451:     return $currentstring;
 3452: }
 3453: 
 3454: #-- <caption> tag (end tag required)
 3455: sub start_caption {
 3456:     my ($target,$token) = @_;
 3457:     my $currentstring = '';
 3458:     if ($target eq 'web') {
 3459: 	$currentstring = $token->[4];     
 3460:     } 
 3461:     return $currentstring;
 3462: }
 3463: 
 3464: sub end_caption {
 3465:     my ($target,$token) = @_;
 3466:     my $currentstring = '';
 3467:     if ($target eq 'web') {
 3468: 	$currentstring = $token->[2];    
 3469:     } 
 3470:     return $currentstring;
 3471: }
 3472: 
 3473: #-- <col> tag (end tag forbdden)
 3474: sub start_col {
 3475:     my ($target,$token) = @_;
 3476:     my $currentstring = '';
 3477:     if ($target eq 'web') {
 3478: 	$currentstring = $token->[4];     
 3479:     } 
 3480:     return $currentstring;
 3481: }
 3482: 
 3483: sub end_col {
 3484:     my ($target,$token) = @_;
 3485:     my $currentstring = '';
 3486:     if ($target eq 'web') {
 3487: 	$currentstring = $token->[2];    
 3488:     } 
 3489:     return $currentstring;
 3490: }
 3491: 
 3492: #-- <colgroup> tag (end tag optional)
 3493: sub start_colgroup {
 3494:     my ($target,$token) = @_;
 3495:     my $currentstring = '';
 3496:     if ($target eq 'web') {
 3497: 	$currentstring = $token->[4];     
 3498:     } 
 3499:     return $currentstring;
 3500: }
 3501: 
 3502: sub end_colgroup {
 3503:     my ($target,$token) = @_;
 3504:     my $currentstring = '';
 3505:     if ($target eq 'web') {
 3506: 	$currentstring = $token->[2];    
 3507:     } 
 3508:     return $currentstring;
 3509: }
 3510: 
 3511: #-- <del> tag (end tag required)
 3512: sub start_del {
 3513:     my ($target,$token) = @_;
 3514:     my $currentstring = '';
 3515:     if ($target eq 'web') {
 3516: 	$currentstring = $token->[4];     
 3517:     } 
 3518:     return $currentstring;
 3519: }
 3520: 
 3521: sub end_del {
 3522:     my ($target,$token) = @_;
 3523:     my $currentstring = '';
 3524:     if ($target eq 'web') {
 3525: 	$currentstring = $token->[2];    
 3526:     } 
 3527:     return $currentstring;
 3528: }
 3529: 
 3530: #-- <fieldset> tag (end tag required)
 3531: sub start_fieldset {
 3532:     my ($target,$token) = @_;
 3533:     my $currentstring = '';
 3534:     if ($target eq 'web') {
 3535: 	$currentstring = $token->[4];     
 3536:     } 
 3537:     return $currentstring;
 3538: }
 3539: 
 3540: sub end_fieldset {
 3541:     my ($target,$token) = @_;
 3542:     my $currentstring = '';
 3543:     if ($target eq 'web') {
 3544: 	$currentstring = $token->[2];    
 3545:     } 
 3546:     return $currentstring;
 3547: }
 3548: 
 3549: #-- <frame> tag (end tag forbidden)
 3550: sub start_frame {
 3551:     my ($target,$token) = @_;
 3552:     my $currentstring = '';
 3553:     if ($target eq 'web') {
 3554: 	$currentstring = $token->[4];     
 3555:     } 
 3556:     return $currentstring;
 3557: }
 3558: 
 3559: sub end_frame {
 3560:     my ($target,$token) = @_;
 3561:     my $currentstring = '';
 3562:     if ($target eq 'web') {
 3563: 	$currentstring = $token->[2];    
 3564:     } 
 3565:     return $currentstring;
 3566: }
 3567: 
 3568: #-- <iframe> tag (end tag required)
 3569: sub start_iframe {
 3570:     my ($target,$token) = @_;
 3571:     my $currentstring = '';
 3572:     if ($target eq 'web') {
 3573: 	$currentstring = $token->[4];     
 3574:     } 
 3575:     return $currentstring;
 3576: }
 3577: 
 3578: sub end_iframe {
 3579:     my ($target,$token) = @_;
 3580:     my $currentstring = '';
 3581:     if ($target eq 'web') {
 3582: 	$currentstring = $token->[2];    
 3583:     } 
 3584:     return $currentstring;
 3585: }
 3586: 
 3587: #-- <ins> tag (end tag required)
 3588: sub start_ins {
 3589:     my ($target,$token) = @_;
 3590:     my $currentstring = '';
 3591:     if ($target eq 'web') {
 3592: 	$currentstring = $token->[4];     
 3593:     } 
 3594:     return $currentstring;
 3595: }
 3596: 
 3597: sub end_ins {
 3598:     my ($target,$token) = @_;
 3599:     my $currentstring = '';
 3600:     if ($target eq 'web') {
 3601: 	$currentstring = $token->[2];    
 3602:     } 
 3603:     return $currentstring;
 3604: }
 3605: 
 3606: #-- <isindex> tag (end tag forbidden)
 3607: sub start_isindex {
 3608:     my ($target,$token) = @_;
 3609:     my $currentstring = '';
 3610:     if ($target eq 'web') {
 3611: 	$currentstring = $token->[4];     
 3612:     } 
 3613:     return $currentstring;
 3614: }
 3615: 
 3616: sub end_isindex {
 3617:     my ($target,$token) = @_;
 3618:     my $currentstring = '';
 3619:     if ($target eq 'web') {
 3620: 	$currentstring = $token->[2];    
 3621:     } 
 3622:     return $currentstring;
 3623: }
 3624: 
 3625: #-- <keygen> tag (end tag forbidden)
 3626: sub start_keygen {
 3627:     my ($target,$token) = @_;
 3628:     my $currentstring = '';
 3629:     if ($target eq 'web') {
 3630: 	$currentstring = $token->[4];     
 3631:     } 
 3632:     return $currentstring;
 3633: }
 3634: 
 3635: sub end_keygen {
 3636:     my ($target,$token) = @_;
 3637:     my $currentstring = '';
 3638:     if ($target eq 'web') {
 3639: 	$currentstring = $token->[2];    
 3640:     } 
 3641:     return $currentstring;
 3642: }
 3643: 
 3644: #-- <label> tag
 3645: sub start_label {
 3646:     my ($target,$token) = @_;
 3647:     my $currentstring = '';
 3648:     if ($target eq 'web') {
 3649: 	$currentstring = $token->[4];     
 3650:     } 
 3651:     return $currentstring;
 3652: }
 3653: 
 3654: sub end_label {
 3655:     my ($target,$token) = @_;
 3656:     my $currentstring = '';
 3657:     if ($target eq 'web') {
 3658: 	$currentstring = $token->[2];    
 3659:     } 
 3660:     return $currentstring;
 3661: }
 3662: 
 3663: #-- <layer> tag (end tag required)
 3664: sub start_layer {
 3665:     my ($target,$token) = @_;
 3666:     my $currentstring = '';
 3667:     if ($target eq 'web') {
 3668: 	$currentstring = $token->[4];     
 3669:     } 
 3670:     return $currentstring;
 3671: }
 3672: 
 3673: sub end_layer {
 3674:     my ($target,$token) = @_;
 3675:     my $currentstring = '';
 3676:     if ($target eq 'web') {
 3677: 	$currentstring = $token->[2];    
 3678:     } 
 3679:     return $currentstring;
 3680: }
 3681: 
 3682: #-- <legend> tag (end tag required)
 3683: sub start_legend {
 3684:     my ($target,$token) = @_;
 3685:     my $currentstring = '';
 3686:     if ($target eq 'web') {
 3687: 	$currentstring = $token->[4];     
 3688:     } 
 3689:     return $currentstring;
 3690: }
 3691: 
 3692: sub end_legend {
 3693:     my ($target,$token) = @_;
 3694:     my $currentstring = '';
 3695:     if ($target eq 'web') {
 3696: 	$currentstring = $token->[2];    
 3697:     } 
 3698:     return $currentstring;
 3699: }
 3700: 
 3701: #-- <link> tag (end tag forbidden)
 3702: sub start_link {
 3703:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 3704:     my $currentstring = '';
 3705:     if ($target eq 'web') {
 3706: 	my $href=&Apache::lonxml::get_param('href',$parstack,$safeeval,
 3707: 					    undef,1);
 3708: 	&Apache::lonxml::extlink($href);
 3709: 	$currentstring = $token->[4];     
 3710:     } 
 3711:     return $currentstring;
 3712: }
 3713: 
 3714: sub end_link {
 3715:     my ($target,$token) = @_;
 3716:     my $currentstring = '';
 3717:     if ($target eq 'web') {
 3718: 	$currentstring = $token->[2];    
 3719:     } 
 3720:     return $currentstring;
 3721: }
 3722: 
 3723: #-- <marquee> tag (end tag optional)
 3724: sub start_marquee {
 3725:     my ($target,$token) = @_;
 3726:     my $currentstring = '';
 3727:     if ($target eq 'web') {
 3728: 	$currentstring = $token->[4];     
 3729:     } 
 3730:     return $currentstring;
 3731: }
 3732: 
 3733: sub end_marquee {
 3734:     my ($target,$token) = @_;
 3735:     my $currentstring = '';
 3736:     if ($target eq 'web') {
 3737: 	$currentstring = $token->[2];    
 3738:     } 
 3739:     return $currentstring;
 3740: }
 3741: 
 3742: #-- <multicol> tag (end tag required)
 3743: sub start_multicol {
 3744:     my ($target,$token) = @_;
 3745:     my $currentstring = &end_p();	# Close any pending <p>
 3746:     if ($target eq 'web') {
 3747: 	$currentstring .= $token->[4];     
 3748:     } 
 3749:     return $currentstring;
 3750: }
 3751: 
 3752: sub end_multicol {
 3753:     my ($target,$token) = @_;
 3754:     my $currentstring = '';
 3755:     if ($target eq 'web') {
 3756: 	$currentstring = $token->[2];    
 3757:     } 
 3758:     return $currentstring;
 3759: }
 3760: 
 3761: #-- <nobr> tag (end tag required)
 3762: sub start_nobr {
 3763:     my ($target,$token) = @_;
 3764:     my $currentstring = '';
 3765:     if ($target eq 'web') {
 3766: 	$currentstring = $token->[4];     
 3767:     }  elsif ($target eq 'tex') {
 3768: 	$currentstring='\mbox{';
 3769:     }
 3770:     return $currentstring;
 3771: }
 3772: 
 3773: sub end_nobr {
 3774:     my ($target,$token) = @_;
 3775:     my $currentstring = '';
 3776:     if ($target eq 'web') {
 3777: 	$currentstring = $token->[2];    
 3778:     }   elsif ($target eq 'tex') {
 3779: 	$currentstring='}';
 3780:     }
 3781:     return $currentstring;
 3782: }
 3783: 
 3784: #-- <noembed> tag (end tag required)
 3785: sub start_noembed {
 3786:     my ($target,$token) = @_;
 3787:     my $currentstring = '';
 3788:     if ($target eq 'web') {
 3789: 	$currentstring = $token->[4];     
 3790:     } 
 3791:     return $currentstring;
 3792: }
 3793: 
 3794: sub end_noembed {
 3795:     my ($target,$token) = @_;
 3796:     my $currentstring = '';
 3797:     if ($target eq 'web') {
 3798: 	$currentstring = $token->[2];    
 3799:     } 
 3800:     return $currentstring;
 3801: }
 3802: 
 3803: #-- <noframes> tag (end tag required)
 3804: sub start_noframes {
 3805:     my ($target,$token) = @_;
 3806:     my $currentstring = '';
 3807:     if ($target eq 'web') {
 3808: 	$currentstring = $token->[4];     
 3809:     } 
 3810:     return $currentstring;
 3811: }
 3812: 
 3813: sub end_noframes {
 3814:     my ($target,$token) = @_;
 3815:     my $currentstring = '';
 3816:     if ($target eq 'web') {
 3817: 	$currentstring = $token->[2];    
 3818:     } 
 3819:     return $currentstring;
 3820: }
 3821: 
 3822: #-- <nolayer> tag (end tag required)
 3823: sub start_nolayer {
 3824:     my ($target,$token) = @_;
 3825:     my $currentstring = '';
 3826:     if ($target eq 'web') {
 3827: 	$currentstring = $token->[4];     
 3828:     } 
 3829:     return $currentstring;
 3830: }
 3831: 
 3832: sub end_nolayer {
 3833:     my ($target,$token) = @_;
 3834:     my $currentstring = '';
 3835:     if ($target eq 'web') {
 3836: 	$currentstring = $token->[2];    
 3837:     } 
 3838:     return $currentstring;
 3839: }
 3840: 
 3841: #-- <noscript> tag (end tag required)
 3842: sub start_noscript {
 3843:     my ($target,$token) = @_;
 3844:     my $currentstring = '';
 3845:     if ($target eq 'web') {
 3846: 	$currentstring = $token->[4];     
 3847:     } 
 3848:     return $currentstring;
 3849: }
 3850: 
 3851: sub end_noscript {
 3852:     my ($target,$token) = @_;
 3853:     my $currentstring = '';
 3854:     if ($target eq 'web') {
 3855: 	$currentstring = $token->[2];    
 3856:     } 
 3857:     return $currentstring;
 3858: }
 3859: 
 3860: #-- <object> tag (end tag required)
 3861: sub start_object {
 3862:     my ($target,$token) = @_;
 3863:     my $currentstring = '';
 3864:     if ($target eq 'web') {
 3865: 	$currentstring = $token->[4];     
 3866:     } 
 3867:     return $currentstring;
 3868: }
 3869: 
 3870: sub end_object {
 3871:     my ($target,$token) = @_;
 3872:     my $currentstring = '';
 3873:     if ($target eq 'web') {
 3874: 	$currentstring = $token->[2];    
 3875:     } 
 3876:     return $currentstring;
 3877: }
 3878: 
 3879: #-- <optgroup> tag (end tag required)
 3880: sub start_optgroup {
 3881:     my ($target,$token) = @_;
 3882:     my $currentstring = '';
 3883:     if ($target eq 'web') {
 3884: 	$currentstring = $token->[4];     
 3885:     } 
 3886:     return $currentstring;
 3887: }
 3888: 
 3889: sub end_optgroup {
 3890:     my ($target,$token) = @_;
 3891:     my $currentstring = '';
 3892:     if ($target eq 'web') {
 3893: 	$currentstring = $token->[2];    
 3894:     } 
 3895:     return $currentstring;
 3896: }
 3897: 
 3898: #-- <samp> tag (end tag required)
 3899: sub start_samp {
 3900:     my ($target,$token) = @_;
 3901:     my $currentstring = '';
 3902:     if ($target eq 'web') {
 3903: 	$currentstring = $token->[4];     
 3904:     } elsif ($target eq 'tex') {
 3905: 	$currentstring='\texttt{';
 3906:     }
 3907:     return $currentstring;
 3908: }
 3909: 
 3910: sub end_samp {
 3911:     my ($target,$token) = @_;
 3912:     my $currentstring = '';
 3913:     if ($target eq 'web') {
 3914: 	$currentstring = $token->[2];    
 3915:     } elsif ($target eq 'tex') {
 3916: 	$currentstring='}';
 3917:     }
 3918:     return $currentstring;
 3919: }
 3920: 
 3921: #-- <server> tag
 3922: sub start_server {
 3923:     my ($target,$token) = @_;
 3924:     my $currentstring = '';
 3925:     if ($target eq 'web') {
 3926: 	$currentstring = $token->[4];     
 3927:     } 
 3928:     return $currentstring;
 3929: }
 3930: 
 3931: sub end_server {
 3932:     my ($target,$token) = @_;
 3933:     my $currentstring = '';
 3934:     if ($target eq 'web') {
 3935: 	$currentstring = $token->[2];    
 3936:     } 
 3937:     return $currentstring;
 3938: }
 3939: 
 3940: #-- <spacer> tag (end tag forbidden)
 3941: sub start_spacer {
 3942:     my ($target,$token) = @_;
 3943:     my $currentstring = &end_p();	# Close off any open <p> tag.
 3944:     if ($target eq 'web') {
 3945: 	$currentstring .= $token->[4];     
 3946:     } 
 3947:     return $currentstring;
 3948: }
 3949: 
 3950: sub end_spacer {
 3951:     my ($target,$token) = @_;
 3952:     my $currentstring = '';
 3953:     if ($target eq 'web') {
 3954: 	$currentstring = $token->[2];    
 3955:     } 
 3956:     return $currentstring;
 3957: }
 3958: 
 3959: #-- <span> tag (end tag required)
 3960: sub start_span {
 3961:     my ($target,$token) = @_;
 3962:     my $currentstring = '';
 3963:     if ($target eq 'web') {
 3964: 	$currentstring = $token->[4];     
 3965:     } 
 3966:     return $currentstring;
 3967: }
 3968: 
 3969: sub end_span {
 3970:     my ($target,$token) = @_;
 3971:     my $currentstring = '';
 3972:     if ($target eq 'web') {
 3973: 	$currentstring = $token->[2];    
 3974:     } 
 3975:     return $currentstring;
 3976: }
 3977: 
 3978: #-- <tbody> tag (end tag optional)
 3979: sub start_tbody {
 3980:     my ($target,$token) = @_;
 3981:     my $currentstring = '';
 3982:     if ($target eq 'web') {
 3983: 	$currentstring = $token->[4];     
 3984:     } 
 3985:     return $currentstring;
 3986: }
 3987: 
 3988: sub end_tbody {
 3989:     my ($target,$token) = @_;
 3990:     my $currentstring = '';
 3991:     if ($target eq 'web') {
 3992: 	$currentstring = $token->[2];    
 3993:     } 
 3994:     return $currentstring;
 3995: }
 3996: 
 3997: #-- <tfoot> tag (end tag optional)
 3998: sub start_tfoot {
 3999:     my ($target,$token) = @_;
 4000:     my $currentstring = '';
 4001:     if ($target eq 'web') {
 4002: 	$currentstring = $token->[4];     
 4003:     } 
 4004:     return $currentstring;
 4005: }
 4006: 
 4007: sub end_tfoot {
 4008:     my ($target,$token) = @_;
 4009:     my $currentstring = '';
 4010:     if ($target eq 'web') {
 4011: 	$currentstring = $token->[2];    
 4012:     } 
 4013:     return $currentstring;
 4014: }
 4015: 
 4016: #-- <thead> tag (end tag optional)
 4017: sub start_thead {
 4018:     my ($target,$token) = @_;
 4019:     my $currentstring = '';
 4020:     if ($target eq 'web') {
 4021: 	$currentstring = $token->[4];     
 4022:     } 
 4023:     return $currentstring;
 4024: }
 4025: 
 4026: sub end_thead {
 4027:     my ($target,$token) = @_;
 4028:     my $currentstring = '';
 4029:     if ($target eq 'web') {
 4030: 	$currentstring = $token->[2];    
 4031:     } 
 4032:     return $currentstring;
 4033: }
 4034: 
 4035: #-- <var> tag
 4036: sub start_var {
 4037:     my ($target,$token) = @_;
 4038:     my $currentstring = '';
 4039:     if ($target eq 'web') {
 4040: 	$currentstring = $token->[4];     
 4041:     } elsif ($target eq 'tex') {
 4042: 	$currentstring = '\textit{'; 
 4043:     }
 4044:     return $currentstring;
 4045: }
 4046: 
 4047: sub end_var {
 4048:     my ($target,$token) = @_;
 4049:     my $currentstring = '';
 4050:     if ($target eq 'web') {
 4051: 	$currentstring = $token->[2];
 4052:     } elsif ($target eq 'tex') {
 4053: 	$currentstring = '}'; 
 4054:     } 
 4055:     return $currentstring;
 4056: }
 4057: 
 4058: #-- <wbr> tag (end tag forbidden)
 4059: sub start_wbr {
 4060:     my ($target,$token) = @_;
 4061:     my $currentstring = '';
 4062:     if ($target eq 'web') {
 4063: 	$currentstring = $token->[4];     
 4064:     } 
 4065:     return $currentstring;
 4066: }
 4067: 
 4068: sub end_wbr {
 4069:     my ($target,$token) = @_;
 4070:     my $currentstring = '';
 4071:     if ($target eq 'web') {
 4072: 	$currentstring = $token->[2];    
 4073:     } 
 4074:     return $currentstring;
 4075: }
 4076: 
 4077: #-- <hideweboutput> tag
 4078: sub start_hideweboutput {
 4079:     my ($target,$token) = @_;
 4080:     if ($target eq 'web') {
 4081: 	&Apache::lonxml::startredirection();     
 4082:     } 
 4083:     return '';
 4084: }
 4085: 
 4086: sub end_hideweboutput {
 4087:     my ($target,$token) = @_;
 4088:     my $currentstring = '';
 4089:     if ($target eq 'web') {
 4090: 	$currentstring = &Apache::lonxml::endredirection();    
 4091:     } 
 4092:     return '';
 4093: }
 4094: 
 4095: 
 4096: sub image_replication {
 4097:     my $src = shift;
 4098:     if (not -e $src) { &Apache::lonnet::repcopy($src); }
 4099:     #replicates eps or ps 
 4100:     my $epssrc = my $pssrc = $src;
 4101:     $epssrc =~ s/\.(gif|jpg|jpeg|png)$/.eps/i;
 4102:     $pssrc  =~ s/\.(gif|jpg|jpeg|png)$/.ps/i;
 4103:     if (not -e $epssrc && not -e $pssrc) {
 4104: 	my $result=&Apache::lonnet::repcopy($epssrc);
 4105: 	if ($result ne 'ok') { &Apache::lonnet::repcopy($pssrc); }
 4106:     }
 4107:     return '';
 4108: }
 4109: #
 4110: #   Get correct sizing parameter for an image given
 4111: #   it's initial ht. and wid.  This allows sizing of
 4112: #   images that are generated on-the-fly (e.g. gnuplot)
 4113: #   as well as serving as a utility for image_size.
 4114: # 
 4115: #  Parameter:
 4116: #        height_param
 4117: #        width_param    - Initial picture dimensions.
 4118: #        scaling        - A scale factor.
 4119: #        parstack,      - the current stack of tag attributes 
 4120: #                         from the xml parser
 4121: #        safeeval,      - pointer to the safespace
 4122: #        depth,         - from what level in the stack to look for attributes
 4123: #                         (assumes -1 if unspecified)
 4124: #        cis            - look for attrubutes case insensitively
 4125: #                         (assumes false)
 4126: #
 4127: # Returns:
 4128: #   height, width   - new dimensions.
 4129: #
 4130: sub resize_image {
 4131:     my ($height_param, $width_param, $scaling,
 4132: 	$parstack, $safeeval, $depth, $cis) = @_;
 4133: 
 4134:     # First apply the scaling...
 4135: 
 4136:     $height_param = $height_param * $scaling;
 4137:     $width_param  = $width_param  * $scaling;
 4138: 
 4139:     #do we have any specified LaTeX size of the picture?
 4140:     my $toget='TeXwidth'; 
 4141:     if ($cis) { 
 4142: 	$toget=lc($toget); 
 4143:     }
 4144:     my $TeXwidth = &Apache::lonxml::get_param($toget,$parstack,
 4145: 					      $safeeval,$depth,$cis);
 4146:     $toget='TeXheight'; if ($cis) { $toget=lc($toget); }
 4147:     my $TeXheight = &Apache::lonxml::get_param($toget,$parstack,
 4148: 					       $safeeval,$depth,$cis);
 4149:     #do we have any specified web size of the picture?
 4150:     my $width = &Apache::lonxml::get_param('width',$parstack,$safeeval,
 4151: 					   $depth,1);
 4152:     if ($TeXwidth) { 
 4153: 	my $old_width_param=$width_param;
 4154: 	if ($TeXwidth=~/(\d+)\s*\%/) {
 4155: 	    $width_param = $1*$env{'form.textwidth'}/100;
 4156: 	} else { 
 4157: 	    $width_param = $TeXwidth;
 4158: 	}
 4159: 	if ($TeXheight) {
 4160: 	    $height_param = $TeXheight;
 4161: 	} elsif ($old_width_param) {
 4162: 	    $height_param=$TeXwidth/$old_width_param*$height_param;
 4163: 	}
 4164:     } elsif ($TeXheight) {
 4165: 	$height_param = $TeXheight;
 4166: 	if ($height_param) {
 4167: 	    $width_param  = $TeXheight/$height_param*$width_param;
 4168: 	}
 4169:     } elsif ($width) {
 4170: 	my $old_width_param=$width_param;
 4171: 	$width_param = $width*$scaling;
 4172: 	if ($old_width_param) {
 4173: 	    $height_param=$width_param/$old_width_param*$height_param;
 4174: 	}
 4175:     }
 4176:     if ($width_param > $env{'form.textwidth'}) {
 4177:         my $old_width_param=$width_param;
 4178: 	$width_param =0.95*$env{'form.textwidth'};
 4179: 	if ($old_width_param) {
 4180: 	    $height_param=$width_param/$old_width_param*$height_param;
 4181: 	}
 4182:     }
 4183: 
 4184:     return ($height_param, $width_param);
 4185: }
 4186: 
 4187: sub image_size {
 4188:     my ($src,$scaling,$parstack,$safeeval,$depth,$cis)=@_;
 4189: 
 4190:     #size of image from gif/jpg/jpeg/png 
 4191:     my $ressrc=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$src);
 4192:     if (-e $ressrc) {
 4193: 	$src = $ressrc;
 4194:     }
 4195:     my $image = Image::Magick->new;
 4196:     my $current_figure = $image->Read($src);
 4197:     my $width_param = $image->Get('width');
 4198:     my $height_param = $image->Get('height');
 4199:     &Apache::lonxml::debug("Image magick says: $src :  Height = $height_param width = $width_param");
 4200:     undef($image);
 4201: 
 4202:     ($height_param, $width_param) = &resize_image($height_param, $width_param,
 4203: 						  $scaling, $parstack, $safeeval, 
 4204: 						  $depth, $cis);
 4205: 
 4206:     return ($height_param, $width_param);
 4207: }
 4208: 
 4209: sub image_width {
 4210:     my ($height, $width) = &image_size(@_);
 4211:     return $width;
 4212: }
 4213: #  Not yet 100% sure this is correct in all circumstances..
 4214: #  due to my uncertainty about mods to image_size.
 4215: #
 4216: sub image_height {
 4217:     my ($height, $width) = &image_size(@_);
 4218:     return $height;
 4219: }
 4220: 
 4221: sub get_eps_image {
 4222:     my ($src)=@_;
 4223:     my $orig_src=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1], $src);
 4224: 
 4225:     # In order to prevent the substitution of the alt text, we need to
 4226:     # be sure the orig_src file is on system now so:
 4227: 
 4228:     if (! -e $orig_src) {
 4229: 	&Apache::lonnet::repcopy($orig_src); # Failure is not completely fatal.
 4230:     }
 4231:     &Apache::lonxml::debug("get_eps_image: Original image: $orig_src");
 4232:     my ($spath, $sname, $sext) = fileparse($src, qr/\.(gif|png|jpg|jpeg)/i);
 4233:     $src=~s/\.(gif|png|jpg|jpeg)$/\.eps/i;
 4234:     $src=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$src);
 4235:     &Apache::lonxml::debug("Filelocation gives: $src");
 4236:     if (! -e $src) {
 4237: 	&Apache::lonxml::debug("$src does not exist");
 4238: 	if (&Apache::lonnet::repcopy($src) ne 'ok' ) {
 4239: 	    &Apache::lonxml::debug("Repcopy of $src failed (1)");
 4240: 	    #if replication failed try to find ps file
 4241: 	    $src=~s/\.eps$/\.ps/;
 4242: 	    &Apache::lonxml::debug("Now looking for $src");
 4243: 	    #if no ps file try to replicate it.
 4244: 	    my $didrepcopy = &Apache::lonnet::repcopy($src);
 4245: 	    &Apache::lonxml::debug("repcopy of $src ... $didrepcopy");
 4246: 	    if ( (not -e $src) ||
 4247: 		($didrepcopy ne 'ok')) {
 4248: 		&Apache::lonxml::debug("Failed to find or replicate $src");
 4249: 
 4250: 		#if replication failed try to produce eps file dynamically
 4251: 		$src=~s/\.ps$/\.eps/;
 4252: 		my $temp_file;
 4253: 		open(FILE,">>/home/httpd/prtspool/$env{'user.name'}_$env{'user.domain'}_printout.dat");
 4254: 		my $newsrc=$orig_src;
 4255: 		$newsrc =~ s|(.*)/res/|/home/httpd/html/res/|;
 4256: 		&Apache::lonxml::debug("queueing $newsrc for dynamic eps production.");
 4257: 		print FILE "$newsrc\n";
 4258: 		close FILE;
 4259: 		$src=~s|/home/httpd/html/res|/home/httpd/prtspool|;
 4260: 		$src=~s|/home/([^/]*)/public_html/|/home/httpd/prtspool/$1/|;
 4261: 		if ($sext ne "") {	 # Put the ext. back in to uniquify.
 4262: 		    $src =~ s/\.eps$/$sext.eps/;
 4263: 		}
 4264: 	    }
 4265: 	}
 4266:     }
 4267:     my ($path,$file)=($src=~m|(.*)/([^/]*)$|);
 4268:     &Apache::lonxml::debug("get_eps_image returning: $path / $file<BR />");
 4269:     return ($path.'/',$file);
 4270: }
 4271: 
 4272: sub eps_generation {
 4273:     my ($src,$file,$width_param) = @_;	     
 4274:     my $filename = "/home/httpd/prtspool/$env{'user.name'}_$env{'user.domain'}_printout.dat";
 4275:     my $temp_file = Apache::File->new('>>'.$filename); 
 4276:     print $temp_file "$src\n";
 4277:     my $newsrc = $src;
 4278:     $newsrc =~ s/(\.gif|\.jpg|\.jpeg)$/\.eps/i;
 4279:     $newsrc=~s/\/home\/httpd\/html\/res//;
 4280:     $newsrc=~s/\/home\/([^\/]*)\/public_html\//\/$1\//;
 4281:     $newsrc=~s/\/\.\//\//;
 4282:     $newsrc=~s/\/([^\/]+)\.(ps|eps)/\//;
 4283:     if ($newsrc=~/\/home\/httpd\/lonUsers\//) {
 4284: 	$newsrc=~s/\/home\/httpd\/lonUsers//;
 4285: 	$newsrc=~s/\/([^\/]+)\/(\w)\/(\w)\/(\w)\//\/$1\//;
 4286:     }
 4287:     if ($newsrc=~/\/userfiles\//) {
 4288: 	return ' \graphicspath{{'.$newsrc.'}}\includegraphics[width='.$width_param.' mm]{'.$file.'} ';
 4289:     } else {
 4290: 	return ' \graphicspath{{/home/httpd/prtspool'.$newsrc.'}}\includegraphics[width='.$width_param.' mm]{'.$file.'} ';
 4291:     }
 4292: }
 4293: 
 4294: sub file_path {     
 4295:     my $src=shift;
 4296:     my ($file,$path); 
 4297:     if ($src =~ m!(.*)/([^/]*)$!) {
 4298: 	$file = $2; 
 4299: 	$path = $1.'/'; 
 4300:     } 
 4301:     return $file,$path;
 4302: }
 4303: #  Converts a measurement in to mm from any of 
 4304: #  the other valid LaTeX units of measure.
 4305: #  If the units of measure are missing from the 
 4306: #  parameter, it is assumed to be in and returned
 4307: #  with mm units of measure
 4308: sub recalc {
 4309:     my $argument = shift;
 4310:     if (not $argument=~/(mm|cm|in|pc|pt)/) {return $argument.' mm';}
 4311:     $argument=~/\s*(\d+\.?\d*)\s*(mm|cm|in|pc|pt)/;
 4312:     my $value=$1;
 4313:     my $units=$2;
 4314:     if ($units eq 'cm') {
 4315: 	$value*=10;
 4316:     } elsif ($units eq 'in') {
 4317: 	$value*=25.4;
 4318:     } elsif ($units eq 'pc') {
 4319: 	$value*=(25.4*12/72.27);
 4320:     } elsif ($units eq 'pt') {
 4321: 	$value*=(25.4/72.27);
 4322:     }
 4323:     return $value.' mm';
 4324: }
 4325: 
 4326: sub LATEX_length {
 4327:     my $garbage=shift;
 4328:     $garbage=~s/^\s+$//;
 4329:     $garbage=~s/^\s+(\S.*)/$1/;#space before 
 4330:     $garbage=~s/(.*\S)\s+$/$1/;#space after 
 4331:     $garbage=~s/(\s)+/$1/;#only one space
 4332:     $garbage=~s/(\\begin{([^\}]+)}|\\end{([^\}]+)})//g;#remove LaTeX \begin{...} and \end{...}
 4333:     $garbage=~s/(\$\_\{|\$\_|\$\^{|\$\^|\}\$)//g;#remove $_{,$_,$^{,$^,}$
 4334:     $garbage=~s/([^\\])\$/$1/g;#$
 4335:     $garbage=~s/(\\ensuremath\{\_\{|\\ensuremath\{\_|\\ensuremath\{\^{|\\ensuremath\{\^|\})//g;#remove \ensuremath{...}
 4336:    $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;
 4337:     $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;
 4338:     $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;
 4339:     $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;
 4340:     $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;
 4341:     $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;
 4342:     $garbage=~s/(\\hat{([^}]+)}|\\check{([^}]+)}|\\dot{([^}]+)}|\\breve{([^}]+)}|\\acute{([^}]+)}|\\ddot{([^}]+)}|\\grave{([^}]+)}|\\tilde{([^}]+)}|\\mathring{([^}]+)}|\\bar{([^}]+)}|\\vec{([^}]+)})/$1/g;
 4343:     #remove some other LaTeX command
 4344:     $garbage=~s|\\(\w+)\\|\\|g;	 
 4345:     $garbage=~s|\\(\w+)(\s*)|$2|g;	 	 
 4346:     $garbage=~s|\+|11|g;
 4347:     my  $value=length($garbage);
 4348:     return $value;
 4349: }
 4350: 
 4351: 
 4352: # is_inside_of $tagstack $tag
 4353: #    This sub returns true if the current state of Xml processing
 4354: #    is inside of the tag.   
 4355: # Parameters:
 4356: #     tagstack   - The tagstack from the parser.
 4357: #     tag        - The tag (without the <>'s.).
 4358: # Sample usage:
 4359: #     if (is_inside_of($tagstack "table")) {
 4360: #          # I'm in a table....
 4361: #      }
 4362: sub is_inside_of {
 4363:     my ($tagstack, $tag) = @_;
 4364:     my @stack = @$tagstack;
 4365:     for (my $i = ($#stack - 1); $i >= 0; $i--) {
 4366: 	if ($stack[$i] eq $tag) {
 4367: 	    return 1;
 4368: 	}
 4369:     }
 4370:     return 0;
 4371: }
 4372: 
 4373: 
 4374: 1;
 4375: __END__

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