File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.334: download - view: text, annotated - select for diffs
Thu Aug 31 18:45:42 2006 UTC (17 years, 9 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- latex goes into an infite loop when one does:
\begin{tabular}{c}
\multicolumn{1}{|l|}{\vskip 0 mm B \vskip 0 mm}\\
\end{tabular}

as a workaround, <br /> will have no effect when inside of a <td colspan="..">

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

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