File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.337: download - view: text, annotated - select for diffs
Tue Sep 19 10:57:11 2006 UTC (17 years, 8 months ago) by foxr
Branches: MAIN
CVS tags: HEAD
- Do table cell alignments using the \multicolumn{1}{|alignspec}{  method.
- Add a correction for spacing at the end of a \center in a table cell.
 this looks like it fixes all the nasty issues with space after centering.
 See bug 3058.

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

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