File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.473: download - view: text, annotated - select for diffs
Wed Sep 27 14:52:27 2023 UTC (7 months, 2 weeks ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- No breadcrumbs trail in modal pop-up when previewing aboutme, syllabus,
  roster, and pdf documents from links in Course Editor.
- Breadcrumbs trails for "Supplemental" and "Main" content more consistent:
  - No Main Menu link as first item if symb or folderpath available
  - No Return to Last location link/icon if symb or folderpath available
- Don't set supplementalflag to 1 for documents uploaded to Main Content
  area which require "showdoc", so breadcrumb trail is available to students.

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

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