File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.300: download - view: text, annotated - select for diffs
Mon Dec 5 23:22:49 2005 UTC (18 years, 6 months ago) by foxr
Branches: MAIN
CVS tags: HEAD
When tables had subtables with some rows not having all columns,
widths for printing were not calculated correctly because the
code implicitly assumed that that number of columns in the last row was
was the correct max number of columns.   Now explicitly keep track of
the maximum number of columns in the counter_columns element of the
table array so that sizing can include all rows.

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

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