File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.302.2.5: download - view: text, annotated - select for diffs
Wed Mar 29 23:00:51 2006 UTC (18 years, 2 months ago) by albertel
Branches: version_2_1_X
CVS tags: version_2_1_3
Diff to branchpoint 1.302: preferred, unified
- backport 1.324

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

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