File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.443: download - view: text, annotated - select for diffs
Sun May 5 00:10:16 2013 UTC (11 years, 1 month ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Provide "Manage dependencies" button when editing an HTML file uploaded as
  the syllabus, i.e., user supplied file, instead of the templated syllabus.

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

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