File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.278: download - view: text, annotated - select for diffs
Sun Jul 10 21:43:35 2005 UTC (18 years, 10 months ago) by www
Branches: MAIN
CVS tags: HEAD
Bug #3750:
View HTML/XML/... ignored bodytag settings, forced style.
Double DTD entry, because lonxml now produces one itself.

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

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