--- loncom/xml/londefdef.pm 2005/05/20 21:52:27 1.273
+++ loncom/xml/londefdef.pm 2006/01/12 23:48:00 1.313
@@ -1,7 +1,7 @@
# The LearningOnline Network with CAPA
# Tags Default Definition Module
#
-# $Id: londefdef.pm,v 1.273 2005/05/20 21:52:27 albertel Exp $
+# $Id: londefdef.pm,v 1.313 2006/01/12 23:48:00 foxr Exp $
#
#
# Copyright Michigan State University Board of Trustees
@@ -47,7 +47,8 @@ use Image::Magick;
use Apache::lonmenu();
use Apache::lonmeta();
use Apache::Constants qw(:common);
-
+use File::Basename;
+# use Data::Dumper;
BEGIN {
@@ -55,6 +56,20 @@ BEGIN {
}
+#
+# Dumps all elements of the table structure.
+# Need this 'cause evidently when given an array, Data::Dumper only seems
+# to dump element 0.
+#
+#sub debug_dump_table {
+# my $lastrow = $#Apache::londefdef::table;
+# &Apache::lonnet::logthis("Dumping table: Last row index: $lastrow");
+# my $row;
+# for ($row =0; $row <= $lastrow; $row++ ) {
+# my $text = Dumper($Apache::londefdef::table[$row]);
+# &Apache::lonnet::logthis("table [ $row ]".$text);
+# }
+#}
sub initialize_londefdef {
$Apache::londefdef::TD_redirection=0;
@Apache::londefdef::table = ();
@@ -90,7 +105,8 @@ sub start_m {
$inside=&Apache::run::evaluate($inside,$safeeval,$$parstack[-1]);
#&Apache::lonxml::debug("M is evaulated to:$inside:");
}
- $currentstring = &Apache::lontexconvert::converted(\$inside);
+ my $display=&Apache::lonxml::get_param('display',$parstack,$safeeval);
+ $currentstring = &Apache::lontexconvert::converted(\$inside,$display);
if ($Apache::lontexconvert::errorstring) {
&Apache::lonxml::warning("tth error: ".
$Apache::lontexconvert::errorstring);
@@ -108,8 +124,8 @@ sub start_m {
# detect simple math mode entry exits, and convert them
# to use \ensuremath
if ($currentstring=~/^\s*\$[^\$].*[^\$]\$\s*$/) {
- $currentstring=~s/^\$//;
- $currentstring=~s/\$$//;
+ $currentstring=~s/^(\s*)\$/$1/;
+ $currentstring=~s/\$(\s*)$/$1/;
$currentstring='\ensuremath{'.$currentstring.'}';
}
$Apache::lonxml::post_evaluate=0;
@@ -127,10 +143,11 @@ sub end_m {
}
sub start_tthoption {
- my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
+ my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
my $result;
if ($target eq 'web') {
- my $inside = &Apache::lonxml::get_all_text("/tthoption",$parser);
+ my $inside = &Apache::lonxml::get_all_text("/tthoption",$parser,
+ $style);
$inside=~s/^\s*//;
if ($env{'browser.mathml'}) {
&tth::ttmoptions($inside);
@@ -156,15 +173,17 @@ sub start_html {
if ($target eq 'web' || $target eq 'edit' || $target eq 'webgrade' ) {
$currentstring = &Apache::lonxml::xmlbegin();
} elsif ($target eq 'tex') {
- $currentstring .= '\documentclass[letterpaper]{article}';
+ $currentstring .= '\documentclass[letterpaper,twoside]{article}';
if (($env{'form.latex_type'}=~'batchmode') ||
(!$env{'request.role.adv'})) {$currentstring .='\batchmode';}
$currentstring .= '\newcommand{\keephidden}[1]{}'.
'\renewcommand{\deg}{$^{\circ}$}'.
+ '\usepackage{multirow}'.
'\usepackage{longtable}'.
'\usepackage{textcomp}'.
'\usepackage{makeidx}'.
'\usepackage[dvips]{graphicx}'.
+ '\usepackage{wrapfig}'.
'\usepackage{picins}'.
'\usepackage{epsfig}'.
'\usepackage{calc}'.
@@ -366,13 +385,13 @@ sub end_title {
#-- tag (end tag forbidden)
sub start_meta {
- my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
+ my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
my $currentstring = '';
if ($target eq 'web') {
my $args='';
if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
if ($args eq '') {
- &Apache::lonxml::get_all_text("/meta",$parser);
+ &Apache::lonxml::get_all_text("/meta",$parser,$style);
} else {
$currentstring = $token->[4];
}
@@ -435,7 +454,7 @@ sub end_meta {
# accessrule
sub start_accessrule {
- my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
+ my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
my $currentstring = '';
my $eff=&Apache::lonxml::get_param
('effect',$parstack,$safeeval,undef,1);
@@ -454,7 +473,7 @@ sub start_accessrule {
my $args='';
if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
if ($args eq '') {
- &Apache::lonxml::get_all_text("/accessrule",$parser);
+ &Apache::lonxml::get_all_text("/accessrule",$parser,$style);
} else {
$currentstring = $token->[4];
}
@@ -534,23 +553,25 @@ sub start_body {
$token->[2]->{'onunload'}=&Apache::lonmenu::unloadevents().
';'.$onUnload;
- if ($env{'request.state'} ne 'construct') {
- $currentstring .= '<'.$token->[1];
- }
+ $currentstring .= '<'.$token->[1];
foreach (keys %{$token->[2]}) {
$currentstring.=' '.$_.'="'.$token->[2]->{$_}.'"';
}
- if ($env{'request.state'} ne 'construct') {
- $currentstring.='>';
+ $currentstring.='>';
+ &Apache::lontexconvert::jsMath_reset();
+ if ($env{'environment.texengine'} eq 'jsMath') {
+ $currentstring.=&Apache::lontexconvert::jsMath_header();
}
if ($env{'request.state'} ne 'published') {
- my $remote=($env{'environment.remote'} ne 'off');
- $currentstring=&Apache::loncommon::bodytag(undef,undef,
- $currentstring,$remote);
+ if ($env{'environment.remote'} eq 'off') {
+ $currentstring.=
+ &Apache::lonmenu::constspaceform().
+ &Apache::lonmenu::menubuttons(1,'web',1);
+ }
$currentstring.=(<
-
-
+
EDITBUTTON
} else {
$currentstring.=&Apache::lonmenu::menubuttons(undef,$target,1);
@@ -564,46 +585,57 @@ EDITBUTTON
sub end_body {
my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
- my $currentstring = '';
+ my $currentstring = &end_p(); # Close off unclosed
if ($target eq 'web') {
- $currentstring = &Apache::lonxml::xmlend($target,$parser);
+ $currentstring .= &Apache::lonxml::xmlend($target,$parser);
} elsif ($target eq 'tex') {
- $currentstring = '\strut\newline\noindent\makebox[\textwidth/$number_of_columns][b]{\hrulefill}\newline\noindent \end{document}';
+ $currentstring .= '\strut\newline\noindent\makebox[\textwidth/$number_of_columns][b]{\hrulefill}\newline\noindent \end{document}';
}
return $currentstring;
}
+# \begin{center} causes a new paragprah spacing that looks odd inside
+# of a table cell
+sub center_correction { return '\vspace*{-6 mm}'; }
#--
tag (end tag required)
sub start_center {
- my ($target,$token) = @_;
- my $currentstring = '';
+ my ($target,$token,$tagstack) = @_;
+ my $currentstring = &end_p(); # Close off any prior para.
if ($target eq 'web') {
- $currentstring = $token->[4];
+ $currentstring .= $token->[4];
} elsif ($target eq 'tex') {
- $currentstring = '\begin{center}';
+ if (&is_inside_of($tagstack, "table")) {
+ $currentstring .= ¢er_correction();
+ }
+ $currentstring .= '\begin{center}';
}
return $currentstring;
}
sub end_center {
- my ($target,$token) = @_;
+ my ($target,$token,$tagstack) = @_;
my $currentstring = '';
if ($target eq 'web') {
$currentstring = $token->[2];
} elsif ($target eq 'tex') {
$currentstring = '\end{center}';
+ if (&is_inside_of($tagstack, "table")) {
+ $currentstring .= ¢er_correction();
+ }
}
return $currentstring;
}
#-- tag (end tag required)
+# NOTE: In TeX mode disables internal
sub start_b {
my ($target,$token) = @_;
my $currentstring = '';
if ($target eq 'web') {
$currentstring = $token->[4];
} elsif ($target eq 'tex') {
- $currentstring = '\textbf{';
+ &disable_para();
+ $currentstring .= '\textbf{';
}
return $currentstring;
}
@@ -614,18 +646,21 @@ sub end_b {
if ($target eq 'web') {
$currentstring = $token->[2];
} elsif ($target eq 'tex') {
- $currentstring = '}';
+ &enable_para();
+ $currentstring = '}';
}
return $currentstring;
}
#-- tag (end tag required)
+# NOTE: in TeX mode disables internal
sub start_strong {
my ($target,$token) = @_;
my $currentstring = '';
if ($target eq 'web') {
$currentstring = $token->[4];
} elsif ($target eq 'tex') {
+ &disable_para();
$currentstring = '\textbf{';
}
return $currentstring;
@@ -637,6 +672,7 @@ sub end_strong {
if ($target eq 'web') {
$currentstring = $token->[2];
} elsif ($target eq 'tex') {
+ &enable_para();
$currentstring = '}';
}
return $currentstring;
@@ -645,7 +681,7 @@ sub end_strong {
#--
tag (end tag required)
sub start_h1 {
my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
- my $currentstring = '';
+ my $currentstring = &end_p(); # Close off any prior para.
if ($target eq 'web') {
$currentstring .= $token->[4];
} elsif ($target eq 'tex') {
@@ -662,7 +698,7 @@ sub start_h1 {
if (not defined $TeXsize) {$TeXsize="large";}
$currentstring .= '\strut\newline '.$pre.'{\\'.$TeXsize.' \textbf{';
} elsif ($target eq 'meta') {
- $currentstring='';
+ $currentstring.='';
&start_output($target);
}
return $currentstring;
@@ -694,7 +730,7 @@ sub end_h1 {
#--
tag
sub start_h2 {
my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
- my $currentstring = '';
+ my $currentstring = &end_p(); # Close off any prior para.
if ($target eq 'web') {
$currentstring .= $token->[4];
} elsif ($target eq 'tex') {
@@ -737,7 +773,7 @@ sub end_h2 {
#--
tag
sub start_h3 {
my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
- my $currentstring = '';
+ my $currentstring = &end_p(); # Close off any prior para.
if ($target eq 'web') {
$currentstring .= $token->[4];
} elsif ($target eq 'tex') {
@@ -780,7 +816,7 @@ sub end_h3 {
#--
tag
sub start_h4 {
my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
- my $currentstring = '';
+ my $currentstring = &end_p(); # Close off any prior para.
if ($target eq 'web') {
$currentstring .= $token->[4];
} elsif ($target eq 'tex') {
@@ -823,7 +859,7 @@ sub end_h4 {
#--
tag
sub start_h5 {
my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
- my $currentstring = '';
+ my $currentstring = &end_p(); # Close off any prior paras.
if ($target eq 'web') {
$currentstring .= $token->[4];
} elsif ($target eq 'tex') {
@@ -866,7 +902,7 @@ sub end_h5 {
#--
tag
sub start_h6 {
my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
- my $currentstring = '';
+ my $currentstring = &end_p(); # Close off any prior paras.
if ($target eq 'web') {
$currentstring .= $token->[4];
} elsif ($target eq 'tex') {
@@ -1113,52 +1149,89 @@ sub end_q {
return $currentstring;
}
+#
is a bit strange since it does not require a closing
+# However in latex, we must often output closing stuff to end
+# environments and {}'s etc. Therefore we do all the work
+# of figuring out the ending strings in the start tag processing,
+# and provide a mechanism to output the stop text external
+# to tag processing.
+#
+{
+
+ my $closing_string = ''; # String required to close
+
+# Some tags are
fragile meaning that
inside of them
+# does not work within TeX mode. This is managed via the
+# counter below:
+#
+
+ my $para_disabled = 0;
+
+sub disable_para {
+ $para_disabled++;
+}
+sub enable_para {
+ $para_disabled--;
+}
+
+
#--
tag (end tag optional)
#optional attribute - align="center|left|right"
sub start_p {
my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
my $currentstring = '';
if ($target eq 'web') {
+ $currentstring .= &end_p(); # close off prior para if in progress.
$currentstring .= $token->[4];
- } elsif ($target eq 'tex') {
+ if (! ($currentstring =~ /\//)) {
+ $closing_string = '
'; # Deal correctly with e.g.
+ }
+ } elsif ($target eq 'tex' && !$para_disabled) {
+
+ $currentstring .= &end_p(); # close off prior para if in progress.
my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
if ($align eq 'center') {
- $currentstring='\begin{center}\par';
+ $currentstring .='\begin{center}\par';
+ $closing_string = '\end{center}';
+ if (&is_inside_of($tagstack, "table")) {
+ $currentstring = ¢er_correction().$currentstring;
+ $closing_string .= ¢er_correction();
+ }
} elsif ($align eq 'right') {
- $currentstring='\makebox['.$env{'form.textwidth'}.']{\hfill\llap{';
+ $currentstring.='\makebox['.$env{'form.textwidth'}.']{\hfill\llap{';
+ $closing_string= '}}';
} elsif ($align eq 'left') {
- $currentstring='\noindent\makebox['.$env{'form.textwidth'}.']{\rlap{';
+ $currentstring.='\noindent\makebox['.$env{'form.textwidth'}.']{\rlap{';
+ $closing_string = '}\hfill}';
} else {
- $currentstring='\par ';
+ $currentstring.='\par ';
+ if (&is_inside_of($tagstack, 'table')) {
+ $closing_string = '\vskip 12pt'; # Safer in tables.
+ } else {
+ $closing_string = '\strut\\\\\strut ';
+ }
}
- my $signal=1;#
does not work inside ...
- foreach my $tag (@$tagstack) {if (lc($tag) eq 'b') {$signal=0;}
- if (!$signal) {$currentstring = '';}
- }
+
}
return $currentstring;
}
-
+#
+# End paragraph processing just requires that we output the
+# closing string that was saved and blank it.
sub end_p {
- my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
- my $currentstring = '';
- if ($target eq 'web') {
- $currentstring .= $token->[2];
- } elsif ($target eq 'tex') {
- my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
- if (not defined $align) {
- $currentstring.='\strut\\\\\strut ';
- } elsif ($align eq 'center') {
- $currentstring .= '\end{center}';
- } elsif ($align eq 'right') {
- $currentstring .= '}}';
- } elsif ($align eq 'left') {
- $currentstring .= '}\hfill}';
- }
+ # Note only 'tex' mode uses disable_para and enable_para
+ # so we don't need to know the target in the check below:
+
+ if (!$para_disabled) {
+ my $current_string = $closing_string;
+ $closing_string = ''; # Not in a para anymore.
+ return $current_string;
+ } else {
+ return '';
}
- return $currentstring;
-}
+}
+}
#-- tag (end tag forbidden)
sub start_br {
my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
@@ -1168,6 +1241,9 @@ sub start_br {
} elsif ($target eq 'tex') {
my @tempo=@$tagstack;
my $signal=0;
+ # Not going to factor this to is_inside_of since that would require
+ # multiple stack traversals.
+ #
for (my $i=$#tempo;$i>=0;$i--) {
if (($tempo[$i] eq 'b') || ($tempo[$i] eq 'strong') ||
($tempo[$i] eq 'ol') || ($tempo[$i] eq 'ul') ||
@@ -1404,7 +1480,7 @@ sub end_sup {
#--
tag (end tag forbidden)
sub start_hr {
my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
- my $currentstring = '';
+ my $currentstring = &end_p(); # End enclosing para.
if ($target eq 'web') {
$currentstring .= $token->[4];
} elsif ($target eq 'tex') {
@@ -1442,12 +1518,48 @@ sub end_hr {
}
#--
tag (end tag required)
+{
+
+# Since div can be nested, the stack below is used
+# in 'tex' mode to store the ending strings
+# for the div stack.
+
+ my @div_end_stack;
+
sub start_div {
- my ($target,$token) = @_;
- my $currentstring = '';
+ my ($target,$token, $tagstack, $parstack, $parser, $safeeval) = @_;
+ my $currentstring = &end_p(); # Close enclosing para.
if ($target eq 'web') {
$currentstring .= $token->[4];
}
+ if ($target eq 'tex') {
+ # 4 possible alignments: left, right, center, and -missing-.
+
+ my $endstring = '';
+
+ my $align = lc(&Apache::lonxml::get_param('align', $parstack,
+ $safeeval, undef, 1));
+ if ($align eq 'center') {
+ $currentstring .= '\begin{center}';
+ $endstring = '\end{center}';
+ if (&is_inside_of($tagstack, "table")) {
+ $currentstring = ¢er_correction().$currentstring;
+ $endstring .= ¢er_correction();
+ }
+ }
+ elsif ($align eq 'right') {
+ $currentstring .= '\begin{flushright}';
+ $endstring .= '\end{flushright}';
+ } elsif ($align eq 'left') {
+ $currentstring .= '\begin{flushleft}';
+ $endstring = '\end{flushleft}';
+ } else {
+
+ }
+ $currentstring .= "\n"; # For human readability.
+ $endstring = "\n$endstring\n"; # For human readability
+ push(@div_end_stack, $endstring);
+ }
return $currentstring;
}
@@ -1456,9 +1568,14 @@ sub end_div {
my $currentstring = '';
if ($target eq 'web') {
$currentstring .= $token->[2];
- }
+ }
+ if ($target eq 'tex') {
+ my $endstring = pop @div_end_stack;
+ $currentstring .= $endstring;
+ }
return $currentstring;
}
+}
#-- tag (end tag required)
sub start_a {
@@ -1526,9 +1643,9 @@ sub start_li {
sub end_li {
my ($target,$token) = @_;
- my $currentstring = '';
+ my $currentstring = &end_p(); # In case there's a