File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.286: download - view: text, annotated - select for diffs
Mon Sep 19 10:59:08 2005 UTC (18 years, 8 months ago) by foxr
Branches: MAIN
CVS tags: HEAD
Try using wrapfigure env. to get better text wrapping on figures.

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

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