File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.456.2.3: download - view: text, annotated - select for diffs
Mon Jan 30 20:14:31 2017 UTC (7 years, 3 months ago) by raeburn
Branches: version_2_11_X
CVS tags: version_2_11_2_uiuc, version_2_11_2_msu, version_2_11_2_educog, version_2_11_2
Diff to branchpoint 1.456: preferred, unified
- For 2.11
  - Backport 1.463

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

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