File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.445: download - view: text, annotated - select for diffs
Sat Sep 14 02:04:47 2013 UTC (10 years, 8 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Bug 6668
  - Append inhibitmenu=yes in a query string within an iframe's src attribute
    where src points at an HTML resource (/uploaded/, /res, or relative path).
  - Example use case: <iframe src="title_player.html"> in title.html file
    created in Camtasia 8.1.

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

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