File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.325: download - view: text, annotated - select for diffs
Wed Mar 29 22:41:39 2006 UTC (18 years, 2 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- adding webgrade to most tags

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

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