File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.277: download - view: text, annotated - select for diffs
Thu Jul 7 10:09:50 2005 UTC (18 years, 10 months ago) by foxr
Branches: MAIN
CVS tags: HEAD
Bug 3948 - Re-do how <p> ends... force </p> at several begin tags that
do new para like things.  This prevents the crashes described in this bug
both for the case described and for other similar cases.  Note that

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

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