File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.350: download - view: text, annotated - select for diffs
Mon Jan 8 19:09:23 2007 UTC (17 years, 5 months ago) by albertel
Branches: MAIN
CVS tags: version_2_3_X, version_2_3_2, version_2_3_1, HEAD
- workaround for latex not liking \par or \vskip inside of \multirow

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

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