File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.299: download - view: text, annotated - select for diffs
Thu Dec 1 18:46:17 2005 UTC (18 years, 5 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- finish off the conversion to allowing style tags get_all_text across tags
- BUG#4428

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

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