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

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

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