File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.253: download - view: text, annotated - select for diffs
Wed Feb 9 15:47:06 2005 UTC (19 years, 4 months ago) by www
Branches: MAIN
CVS tags: HEAD
Works better if it does not produce LaTeX errors *and* follows web layout.

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

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