File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.456.2.6.2.4: download - view: text, annotated - select for diffs
Wed Feb 28 18:15:50 2024 UTC (2 months, 2 weeks ago) by raeburn
Branches: version_2_11_4_msu
Diff to branchpoint 1.456.2.6: preferred, unified
- For 2.11.4 (modified)
  Include remainder of changes in 1.474 omitted from earlier 1.456.2.6.2.3

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

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