File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.364: download - view: text, annotated - select for diffs
Fri Apr 6 10:41:45 2007 UTC (17 years, 1 month ago) by foxr
Branches: MAIN
CVS tags: HEAD
Clean up table width calculations now that I'm a bit less
confused about what's going on here. I think this will allow
me to mark bz 4967 fixed with a clean conscience.

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

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