File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.390.2.2: download - view: text, annotated - select for diffs
Thu Oct 2 18:09:38 2008 UTC (15 years, 7 months ago) by raeburn
Branches: version_2_7_X
Diff to branchpoint 1.390: preferred, unified
- Backport 1.393

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

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