File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.437: download - view: text, annotated - select for diffs
Sun Feb 5 22:53:42 2012 UTC (12 years, 3 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Include dependencies for Camtasia files uploaded directly to a course
  in httpref.

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

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