File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.441: download - view: text, annotated - select for diffs
Thu Nov 29 20:37:13 2012 UTC (11 years, 6 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Consistent interface.
- HTML files uploaded to course's Supplemental content area can be edited,
  and any dependencies can be replaced (or uploaded if missing).
- "Course View" button provided when "editing" HTML files uploaded
  directly to a course removed; use "Exit Editing" item in Functions instead.

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

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