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

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

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