File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.440: download - view: text, annotated - select for diffs
Fri Jul 6 22:46:12 2012 UTC (11 years, 10 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Managing dependencies and uploading archive files to a course.
  - Support decompression of archive files(e.g., zip files) uploaded to
    Supplemental Content.
  - Support upload of multiple (linked) web pages to a course, where only
    one (containing links to others) is included in a folder (i.e., sequence
    file) -- access allowed to others in course context based on httpref.
  - When listing unused files in a directory in /docs/ or /supplemental/
    exclude files included in httprefs.

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

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