File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.386: download - view: text, annotated - select for diffs
Tue Apr 29 10:16:37 2008 UTC (16 years ago) by foxr
Branches: MAIN
CVS tags: HEAD
1. Fix UTF-8 re-coding in entities.pm
2. add \usepackage{ucs} to latex header in
   lonprintout.pm, londefdef.pm.

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

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