File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.275: download - view: text, annotated - select for diffs
Mon May 23 09:49:36 2005 UTC (18 years, 11 months ago) by foxr
Branches: MAIN
CVS tags: version_1_99_1_tmcc, version_1_99_1, version_1_99_0, HEAD
Undo the undo of the commit of separation of image sizing and image getting.
Too early in the morning for the first commit

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

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