File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.448: download - view: text, annotated - select for diffs
Mon Sep 30 13:44:16 2013 UTC (10 years, 8 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Access to dependencies/links in HTML files uploaded directly to a course,
  where dependency or linked item was also uploaded directly to the same
  course.
  - Fix regexp used to identify items referenced in javascript object in text
   within <script></script>.
  - .js files in src attribute of <script> tag still retrieved, even if
   (type ="text/javascript" is missing) i.e., not valid xhtml.

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

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