File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.382: download - view: text, annotated - select for diffs
Thu Mar 6 20:31:52 2008 UTC (16 years, 3 months ago) by www
Branches: MAIN
CVS tags: HEAD
Bug #5652: control printing of URLs and Anchors

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

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