File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.446: download - view: text, annotated - select for diffs
Sun Sep 22 15:35:45 2013 UTC (10 years, 8 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Suppress display of header items when displaying iframe in a file in
  Authoring Space, where src attribute in /res or a relative path
  (inhibitmenu=yes will have been appended to query string in src).

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

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