File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.255: download - view: text, annotated - select for diffs
Wed Feb 9 19:51:43 2005 UTC (19 years, 3 months ago) by albertel
Branches: MAIN
CVS tags: version_1_3_X, version_1_3_3, HEAD
- I think this is more correctish

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

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