File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.451: download - view: text, annotated - select for diffs
Thu May 15 20:19:06 2014 UTC (10 years ago) by raeburn
Branches: MAIN
CVS tags: version_2_11_0_RC3, HEAD
- Efficiency.
  Only call &printsuppression() from loncommon::headtag() to check if
  printing is disabled, for cases where that might apply.

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

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