File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.368: download - view: text, annotated - select for diffs
Tue Jun 19 20:25:45 2007 UTC (16 years, 11 months ago) by banghart
Branches: MAIN
CVS tags: HEAD
	Use filecategories.tab to read Pictures extensions

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

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