File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.279: download - view: text, annotated - select for diffs
Mon Jul 11 09:49:59 2005 UTC (18 years, 11 months ago) by foxr
Branches: MAIN
CVS tags: HEAD
Fix various issues with <p> that don't have end tags in tex mode.

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

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