File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.358: download - view: text, annotated - select for diffs
Tue Mar 13 13:35:39 2007 UTC (17 years, 2 months ago) by foxr
Branches: MAIN
CVS tags: HEAD
Correct table widths.  The original frame correction was not sufficient.

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

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