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

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

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