Annotation of loncom/xml/londefdef.pm, revision 1.396.2.2

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

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