File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.371: download - view: text, annotated - select for diffs
Tue Jun 26 01:36:48 2007 UTC (16 years, 11 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- BUG#5301, current ends up being 0 when a user does
  <table>stuff</table> with no <tr> or <td>

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

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