File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.456.2.6.2.3: download - view: text, annotated - select for diffs
Fri Dec 29 23:33:43 2023 UTC (4 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 changes in 1.474

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

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