File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.353: download - view: text, annotated - select for diffs
Mon Jan 22 11:28:08 2007 UTC (17 years, 4 months ago) by foxr
Branches: MAIN
CVS tags: HEAD
Factor out image alignment wrapping so that enhancement request
4952 can be done without duplicating code between londefdef.pm
and randomlabel.pm.

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

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