File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.302.2.1: download - view: text, annotated - select for diffs
Wed Jan 4 23:15:52 2006 UTC (18 years, 4 months ago) by albertel
Branches: version_2_1_X
Diff to branchpoint 1.302: preferred, unified
- backport 1.308 1.309 1.310

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

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