File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.397: download - view: text, annotated - select for diffs
Mon Nov 24 18:55:01 2008 UTC (15 years, 6 months ago) by jms
Branches: MAIN
CVS tags: HEAD
Added/modified POD comments

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

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